Skip to content

Commit 77fc2f6

Browse files
committed
feat(bruker): handle encoding/decode errors resiliently during JCAMP reading
1 parent da64362 commit 77fc2f6

2 files changed

Lines changed: 24 additions & 3 deletions

File tree

nmrglue/fileio/bruker.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2174,10 +2174,13 @@ def read_jcamp(filename, encoding=locale.getpreferredencoding()):
21742174
"""
21752175
dic = {"_coreheader": [], "_comments": []} # create empty dictionary
21762176

2177-
with open(filename, 'r', encoding=encoding) as f:
2177+
with open(filename, 'r', encoding=encoding, errors='replace') as f:
21782178
while True: # loop until end of file is found
2179-
2180-
line = f.readline().rstrip() # read a line
2179+
try:
2180+
line = f.readline().rstrip() # read a line
2181+
except Exception as e:
2182+
warn(f"Unable to read line in file {filename}: {e}. Treating it as comment.")
2183+
line = "$$"
21812184
if line == '': # end of file found
21822185
break
21832186

nmrglue/fileio/tests/test_bruker.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,3 +80,21 @@ def test_write_pdata():
8080
assert np.all(data == rdata)
8181
assert rdic['procs'].keys() == dic['procs'].keys()
8282
shutil.rmtree(td)
83+
84+
85+
def test_read_jcamp_decoding_error():
86+
"""Testing read_jcamp handling of decoding errors"""
87+
fd, temp_path = tempfile.mkstemp()
88+
try:
89+
with os.fdopen(fd, 'wb') as f:
90+
f.write(b"##$KEY=value\n")
91+
f.write(b"##$BAD=\xff\xfe\xfd\n") # Invalid UTF-8 sequence
92+
f.write(b"##END=\n")
93+
94+
# This should read it and treat the bad characters by replacing them, not crash
95+
dic = ng.bruker.read_jcamp(temp_path, encoding='utf-8')
96+
assert dic["KEY"] == "value"
97+
assert "\ufffd" in dic["BAD"]
98+
finally:
99+
os.remove(temp_path)
100+

0 commit comments

Comments
 (0)