Skip to content

Commit 1e7818f

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 7488853 commit 1e7818f

File tree

1 file changed

+8
-12
lines changed

1 file changed

+8
-12
lines changed

lib/test.py

+8-12
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,6 @@
1111
from functools import partial
1212
from hashlib import md5
1313

14-
try:
15-
# Python 2
16-
from StringIO import StringIO
17-
except ImportError:
18-
# Python 3
19-
from io import StringIO
20-
2114
from . import Options
2215
from .colorer import color_stdout
2316
from .utils import non_empty_valgrind_logs
@@ -59,19 +52,18 @@ def __init__(self, filename):
5952
# always open the output stream in line-buffered mode,
6053
# to see partial results of a failed test
6154
#
62-
self.stream = open(filename, "w+", 1)
55+
self.stream = open(filename, "wb+")
6356
self.filters = []
6457
self.inspector = None
6558

6659
def write(self, fragment):
6760
"""Apply all filters, then write result to the undelrying stream.
6861
Do line-oriented filtering: the fragment doesn't have to represent
6962
just one line."""
70-
if isinstance(fragment, (bytes, bytearray)):
71-
fragment = fragment.decode('utf-8')
72-
fragment_stream = StringIO(fragment)
63+
if sys.version[0] == '3' and isinstance(fragment, str):
64+
fragment = fragment.encode('utf-8')
7365
skipped = False
74-
for line in fragment_stream:
66+
for line in fragment.splitlines(True):
7567
original_len = len(line.strip())
7668
for pattern, replacement in self.filters:
7769
line = re.sub(pattern, replacement, line)
@@ -83,6 +75,10 @@ def write(self, fragment):
8375
self.stream.write(line)
8476

8577
def push_filter(self, pattern, replacement):
78+
if sys.version[0] == '3' and isinstance(pattern, str):
79+
pattern = pattern.encode('utf-8')
80+
if sys.version[0] == '3' and isinstance(replacement, str):
81+
replacement = replacement.encode('utf-8')
8682
self.filters.append([pattern, replacement])
8783

8884
def pop_filter(self):

0 commit comments

Comments
 (0)