From cd140b5141c457818c86bf84a8ced696374896af Mon Sep 17 00:00:00 2001 From: Christopher Dunn Date: Fri, 16 Jan 2015 13:44:27 -0600 Subject: [PATCH] py2 and py3 --- test/runjsontests.py | 31 ++++++++++++++++++++++++------- 1 file changed, 24 insertions(+), 7 deletions(-) diff --git a/test/runjsontests.py b/test/runjsontests.py index 62e5600..7243063 100644 --- a/test/runjsontests.py +++ b/test/runjsontests.py @@ -1,12 +1,30 @@ from __future__ import print_function +from __future__ import unicode_literals +from io import open +from glob import glob import sys import os import os.path -from glob import glob import optparse VALGRIND_CMD = 'valgrind --tool=memcheck --leak-check=yes --undef-value-errors=yes ' +def getStatusOutput(cmd): + """ + Return int, unicode (for both Python 2 and 3). + Note: os.popen().close() would return None for 0. + """ + pipe = os.popen(cmd) + process_output = pipe.read() + try: + # We have been using os.popen(). When we read() the result + # we get 'str' (bytes) in py2, and 'str' (unicode) in py3. + # Ugh! There must be a better way to handle this. + process_output = process_output.decode('utf-8') + except AttributeError: + pass # python3 + status = pipe.close() + return status, process_output def compareOutputs( expected, actual, message ): expected = expected.strip().replace('\r','').split('\n') actual = actual.strip().replace('\r','').split('\n') @@ -54,21 +72,20 @@ def runAllTests( jsontest_executable_path, input_dir = None, is_json_checker_test = (input_path in test_jsonchecker) or expect_failure print('TESTING:', input_path, end=' ') options = is_json_checker_test and '--json-checker' or '' - pipe = os.popen( '%s%s %s "%s"' % ( + cmd = '%s%s %s "%s"' % ( valgrind_path, jsontest_executable_path, options, - input_path) ) - process_output = pipe.read() - status = pipe.close() + input_path) + status, process_output = getStatusOutput(cmd) if is_json_checker_test: if expect_failure: - if status is None: + if not status: print('FAILED') failed_tests.append( (input_path, 'Parsing should have failed:\n%s' % safeReadFile(input_path)) ) else: print('OK') else: - if status is not None: + if status: print('FAILED') failed_tests.append( (input_path, 'Parsing failed:\n' + process_output) ) else: