Skip to content

Commit 328d994

Browse files
authored
Merge branch 'master' into refactor_nev_header_reading
2 parents 01091b8 + b404802 commit 328d994

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

neo/rawio/spikegadgetsrawio.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -109,16 +109,13 @@ def _produce_ephys_channel_ids(self, n_total_channels, n_channels_per_chip, miss
109109

110110
def _parse_header(self):
111111
# parse file until "</Configuration>"
112-
header_size = None
113112
with open(self.filename, mode="rb") as f:
114-
while True:
115-
line = f.readline()
113+
for line in f:
116114
if b"</Configuration>" in line:
117115
header_size = f.tell()
118116
break
119-
120-
if header_size is None:
121-
ValueError("SpikeGadgets: the xml header does not contain '</Configuration>'")
117+
else:
118+
raise ValueError("SpikeGadgets: the xml header does not contain '</Configuration>'")
122119

123120
f.seek(0)
124121
header_txt = f.read(header_size).decode("utf8")

neo/test/rawiotest/test_spikegadgetsrawio.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import unittest
22
from pathlib import Path
3+
import tempfile
34

45
from neo.rawio.spikegadgetsrawio import SpikeGadgetsRawIO
56
from neo.test.rawiotest.common_rawio_test import BaseTestRawIO
@@ -51,3 +52,17 @@ def test_parse_header_missing_channels(self):
5152
]
5253
# fmt: on
5354
)
55+
56+
def test_opening_gibberish_file(self):
57+
"""Test that parsing a file without </Configuration> raises ValueError instead of infinite loop."""
58+
# Create a temporary file with gibberish content that doesn't have the required tag
59+
with tempfile.NamedTemporaryFile(mode='wb', suffix='.rec') as temp_file:
60+
# Write simple gibberish content without the required </Configuration> tag
61+
temp_file.write(b"gibberish\n")
62+
temp_file.flush()
63+
64+
reader = SpikeGadgetsRawIO(filename=temp_file.name)
65+
with self.assertRaises(ValueError) as cm:
66+
reader.parse_header()
67+
68+
self.assertIn("xml header does not contain '</Configuration>'", str(cm.exception))

0 commit comments

Comments
 (0)