Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion profiling/p21_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
17 changes: 13 additions & 4 deletions src/steputils/p21.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "\\"
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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