diff --git a/profiling/p21_loader.py b/profiling/p21_loader.py index e0c789f..05605a0 100644 --- a/profiling/p21_loader.py +++ b/profiling/p21_loader.py @@ -52,7 +52,7 @@ def load_p21(fn: Path) -> LoadResult: start_time = time.perf_counter() loaded = time.perf_counter() try: - data = open(str(fn), mode='rt', encoding=p21.STEP_FILE_ENCODING).read() + data = open(str(fn), mode='rt', encoding=p21.STEP_FILE_ENCODINGS).read() loaded = time.perf_counter() content = p21.loads(data) except IOError as e: diff --git a/src/steputils/p21.py b/src/steputils/p21.py index 3d6d2ee..b13a7e5 100644 --- a/src/steputils/p21.py +++ b/src/steputils/p21.py @@ -46,7 +46,7 @@ # ISO 10303-21-2016 allows UTF-8 encoding, prior versions of the standard used used IOS-8859-1 encoding, but # lower code points (<127) are the same for both encodings. -STEP_FILE_ENCODING = "utf-8" +STEP_FILE_ENCODINGS = ("utf-8", "latin-1", "cp1252") END_OF_INSTANCE = ";\n" BACKSLASH = "\\" @@ -513,7 +513,7 @@ def _set_schemas(self): def save(self, name: str) -> None: """Export STEP-file to the file system.""" - with open(name, mode="wt", encoding=STEP_FILE_ENCODING) as fp: + with open(name, mode="wt", encoding=STEP_FILE_ENCODINGS) as fp: self.write(fp) def __str__(self) -> str: @@ -1161,5 +1161,14 @@ def load(fp: TextIO) -> StepFile: def readfile(filename: str) -> StepFile: """Read STEP-file (ISO 10303-21:2002) `filename` from file system.""" - with open(filename, "rt", encoding=STEP_FILE_ENCODING) as fp: - return load(fp) + + last_error = None + + for encoding in STEP_FILE_ENCODINGS: + try: + with open(filename, "rt", encoding=encoding) as fp: + return load(fp) + except UnicodeDecodeError as exc: + last_error = exc + + raise last_error