Skip to content

Commit 1a81284

Browse files
Use str.splitlines consistently when splitting testcases.
1 parent b7d18a4 commit 1a81284

File tree

1 file changed

+44
-31
lines changed

1 file changed

+44
-31
lines changed

src/lithium/testcases.py

Lines changed: 44 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -112,38 +112,46 @@ def load(self, path):
112112
self.filename = str(path)
113113
self.extension = os.path.splitext(self.filename)[1]
114114

115-
with open(self.filename, "rb") as fileobj:
116-
before = []
117-
for line in fileobj:
118-
before.append(line)
119-
if line.find(b"DDBEGIN") != -1:
120-
self.before = b"".join(before)
121-
del before
122-
break
123-
if line.find(b"DDEND") != -1:
124-
raise LithiumError(
125-
"The testcase (%s) has a line containing 'DDEND' "
126-
"without a line containing 'DDBEGIN' before it."
127-
% (self.filename,)
128-
)
129-
else:
130-
# no DDBEGIN/END, `before` contains the whole testcase
131-
self.split_parts(b"".join(before))
132-
return
133-
134-
between = []
135-
for line in fileobj:
136-
if line.find(b"DDEND") != -1:
137-
self.after = line + fileobj.read()
138-
break
139-
140-
between.append(line)
141-
else:
115+
with open(
116+
self.filename, "r", encoding="utf-8", errors="surrogateescape"
117+
) as fileobj:
118+
lines = [
119+
line.encode("utf-8", errors="surrogateescape")
120+
for line in fileobj.read().splitlines(keepends=True)
121+
]
122+
123+
before = []
124+
while lines:
125+
line = lines.pop(0)
126+
before.append(line)
127+
if line.find(b"DDBEGIN") != -1:
128+
self.before = b"".join(before)
129+
del before
130+
break
131+
if line.find(b"DDEND") != -1:
142132
raise LithiumError(
143-
"The testcase (%s) has a line containing 'DDBEGIN' "
144-
"but no line containing 'DDEND'." % (self.filename,)
133+
"The testcase (%s) has a line containing 'DDEND' "
134+
"without a line containing 'DDBEGIN' before it." % (self.filename,)
145135
)
146-
self.split_parts(b"".join(between))
136+
else:
137+
# no DDBEGIN/END, `before` contains the whole testcase
138+
self.split_parts(b"".join(before))
139+
return
140+
141+
between = []
142+
while lines:
143+
line = lines.pop(0)
144+
if line.find(b"DDEND") != -1:
145+
self.after = line + b"".join(lines)
146+
break
147+
148+
between.append(line)
149+
else:
150+
raise LithiumError(
151+
"The testcase (%s) has a line containing 'DDBEGIN' "
152+
"but no line containing 'DDEND'." % (self.filename,)
153+
)
154+
self.split_parts(b"".join(between))
147155

148156
@staticmethod
149157
def add_arguments(parser):
@@ -199,7 +207,12 @@ def split_parts(self, data):
199207
data (bytes): Input data read from the testcase file.
200208
"""
201209
orig = len(self.parts)
202-
self.parts.extend(data.splitlines(keepends=True))
210+
self.parts.extend(
211+
line.encode("utf-8", errors="surrogateescape")
212+
for line in data.decode("utf-8", errors="surrogateescape").splitlines(
213+
keepends=True
214+
)
215+
)
203216
added = len(self.parts) - orig
204217
self.reducible.extend([True] * added)
205218

0 commit comments

Comments
 (0)