Skip to content

Commit cd140b5

Browse files
committed
py2 and py3
1 parent 01aee4a commit cd140b5

File tree

1 file changed

+24
-7
lines changed

1 file changed

+24
-7
lines changed

test/runjsontests.py

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,30 @@
11
from __future__ import print_function
2+
from __future__ import unicode_literals
3+
from io import open
4+
from glob import glob
25
import sys
36
import os
47
import os.path
5-
from glob import glob
68
import optparse
79

810
VALGRIND_CMD = 'valgrind --tool=memcheck --leak-check=yes --undef-value-errors=yes '
911

12+
def getStatusOutput(cmd):
13+
"""
14+
Return int, unicode (for both Python 2 and 3).
15+
Note: os.popen().close() would return None for 0.
16+
"""
17+
pipe = os.popen(cmd)
18+
process_output = pipe.read()
19+
try:
20+
# We have been using os.popen(). When we read() the result
21+
# we get 'str' (bytes) in py2, and 'str' (unicode) in py3.
22+
# Ugh! There must be a better way to handle this.
23+
process_output = process_output.decode('utf-8')
24+
except AttributeError:
25+
pass # python3
26+
status = pipe.close()
27+
return status, process_output
1028
def compareOutputs( expected, actual, message ):
1129
expected = expected.strip().replace('\r','').split('\n')
1230
actual = actual.strip().replace('\r','').split('\n')
@@ -54,21 +72,20 @@ def runAllTests( jsontest_executable_path, input_dir = None,
5472
is_json_checker_test = (input_path in test_jsonchecker) or expect_failure
5573
print('TESTING:', input_path, end=' ')
5674
options = is_json_checker_test and '--json-checker' or ''
57-
pipe = os.popen( '%s%s %s "%s"' % (
75+
cmd = '%s%s %s "%s"' % (
5876
valgrind_path, jsontest_executable_path, options,
59-
input_path) )
60-
process_output = pipe.read()
61-
status = pipe.close()
77+
input_path)
78+
status, process_output = getStatusOutput(cmd)
6279
if is_json_checker_test:
6380
if expect_failure:
64-
if status is None:
81+
if not status:
6582
print('FAILED')
6683
failed_tests.append( (input_path, 'Parsing should have failed:\n%s' %
6784
safeReadFile(input_path)) )
6885
else:
6986
print('OK')
7087
else:
71-
if status is not None:
88+
if status:
7289
print('FAILED')
7390
failed_tests.append( (input_path, 'Parsing failed:\n' + process_output) )
7491
else:

0 commit comments

Comments
 (0)