Skip to content

Commit d1db2d3

Browse files
ligurioTotktonada
andcommitted
python3: use byte representation of test output
Output of test unit/json.test contains incorrect Unicode codes and we cannot decode output to UTF-8. To avoid a problem we use byte representation of test output. Part of #20 Co-authored-by: Alexander Turenko <[email protected]>
1 parent c6c2764 commit d1db2d3

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

lib/test.py

+8-5
Original file line numberDiff line numberDiff line change
@@ -58,19 +58,18 @@ def __init__(self, filename):
5858
# always open the output stream in line-buffered mode,
5959
# to see partial results of a failed test
6060
#
61-
self.stream = open(filename, "w+", 1)
61+
self.stream = open(filename, "wb+")
6262
self.filters = []
6363
self.inspector = None
6464

6565
def write(self, fragment):
6666
"""Apply all filters, then write result to the undelrying stream.
6767
Do line-oriented filtering: the fragment doesn't have to represent
6868
just one line."""
69-
if isinstance(fragment, (bytes, bytearray)):
70-
fragment = fragment.decode('utf-8')
71-
fragment_stream = StringIO(fragment)
69+
if sys.version[0] == '3' and isinstance(fragment, str):
70+
fragment = fragment.encode('utf-8')
7271
skipped = False
73-
for line in fragment_stream:
72+
for line in fragment.splitlines(True):
7473
original_len = len(line.strip())
7574
for pattern, replacement in self.filters:
7675
line = re.sub(pattern, replacement, line)
@@ -82,6 +81,10 @@ def write(self, fragment):
8281
self.stream.write(line)
8382

8483
def push_filter(self, pattern, replacement):
84+
if sys.version[0] == '3' and isinstance(pattern, str):
85+
pattern = pattern.encode('utf-8')
86+
if sys.version[0] == '3' and isinstance(replacement, str):
87+
replacement = replacement.encode('utf-8')
8588
self.filters.append([pattern, replacement])
8689

8790
def pop_filter(self):

0 commit comments

Comments
 (0)