Skip to content

Commit e0bfb45

Browse files
committed
Merge branch 'py3/2'
2 parents 59a0165 + 4bc3115 commit e0bfb45

File tree

2 files changed

+35
-12
lines changed

2 files changed

+35
-12
lines changed

test/runjsontests.py

Lines changed: 27 additions & 10 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')
@@ -34,7 +52,7 @@ def safeGetLine( lines, index ):
3452

3553
def safeReadFile( path ):
3654
try:
37-
return file( path, 'rt' ).read()
55+
return open( path, 'rt', encoding = 'utf-8' ).read()
3856
except IOError as e:
3957
return '<File "%s" is missing: %s>' % (path,e)
4058

@@ -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:
@@ -77,13 +94,13 @@ def runAllTests( jsontest_executable_path, input_dir = None,
7794
base_path = os.path.splitext(input_path)[0]
7895
actual_output = safeReadFile( base_path + '.actual' )
7996
actual_rewrite_output = safeReadFile( base_path + '.actual-rewrite' )
80-
file(base_path + '.process-output','wt').write( process_output )
97+
open(base_path + '.process-output', 'wt', encoding = 'utf-8').write( process_output )
8198
if status:
8299
print('parsing failed')
83100
failed_tests.append( (input_path, 'Parsing failed:\n' + process_output) )
84101
else:
85102
expected_output_path = os.path.splitext(input_path)[0] + '.expected'
86-
expected_output = file( expected_output_path, 'rt' ).read()
103+
expected_output = open( expected_output_path, 'rt', encoding = 'utf-8' ).read()
87104
detail = ( compareOutputs( expected_output, actual_output, 'input' )
88105
or compareOutputs( expected_output, actual_rewrite_output, 'rewrite' ) )
89106
if detail:

test/rununittests.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
from __future__ import print_function
2+
from __future__ import unicode_literals
3+
from io import open
24
from glob import glob
35
import sys
46
import os
@@ -19,7 +21,11 @@ def run( self, options ):
1921
else:
2022
cmd = []
2123
cmd.extend( [self.test_exe_path, '--test-auto'] + options )
22-
process = subprocess.Popen( cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT )
24+
try:
25+
process = subprocess.Popen( cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT )
26+
except:
27+
print(cmd)
28+
raise
2329
stdout = process.communicate()[0]
2430
if process.returncode:
2531
return False, stdout
@@ -31,7 +37,7 @@ def runAllTests( exe_path, use_valgrind=False ):
3137
if not status:
3238
print("Failed to obtain unit tests list:\n" + test_names, file=sys.stderr)
3339
return 1
34-
test_names = [name.strip() for name in test_names.strip().split('\n')]
40+
test_names = [name.strip() for name in test_names.decode('utf-8').strip().split('\n')]
3541
failures = []
3642
for name in test_names:
3743
print('TESTING %s:' % name, end=' ')

0 commit comments

Comments
 (0)