Skip to content
This repository has been archived by the owner on May 31, 2020. It is now read-only.

Work around Unicode encoding error on Windows #703

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 11 additions & 7 deletions tests/utils.py
Expand Up @@ -116,7 +116,7 @@ def adjust(text, run_in_function=False):
return '\n'.join(final_lines)


def runAsPython(test_dir, main_code, extra_code=None, args=None):
def runAsPython(test_dir, main_code, extra_code=None, args=None, env=None):
"""Run a block of Python code with the Python interpreter."""
# Output source code into test directory
with open(os.path.join(test_dir, 'test.py'), 'w', encoding='utf-8') as py_source:
Expand All @@ -143,6 +143,7 @@ def runAsPython(test_dir, main_code, extra_code=None, args=None):
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
cwd=test_dir,
env=env,
)
out = proc.communicate()

Expand Down Expand Up @@ -319,6 +320,7 @@ def setUpClass(cls):
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
cwd=_output_dir,
env=dict(os.environ, PYTHONIOENCODING='utf-8'),
)

@classmethod
Expand Down Expand Up @@ -368,6 +370,7 @@ def assertCodeExecution(
args=None, substitutions=None):
"Run code as native python, and under Java and check the output is identical"
self.maxDiff = None
env = dict(os.environ, PYTHONIOENCODING='utf-8')
# ==================================================
# Pass 1 - run the code in the global context
# ==================================================
Expand All @@ -377,8 +380,8 @@ def assertCodeExecution(
# Run the code as Python and as Java.
adj_code = adjust(code, run_in_function=False)
adj_code += '\nprint("%s")\n' % END_OF_CODE_STRING
py_out = runAsPython(self.temp_dir, adj_code, extra_code, args=args)
java_out = self.runAsJava(adj_code, extra_code, args=args)
py_out = runAsPython(self.temp_dir, adj_code, extra_code, args=args, env=env)
java_out = self.runAsJava(adj_code, extra_code, args=args, env=env)
except Exception as e:
self.fail(e)
finally:
Expand Down Expand Up @@ -416,8 +419,8 @@ def assertCodeExecution(
# Run the code as Python and as Java.
adj_code = adjust(code, run_in_function=True)
adj_code += '\nprint("%s")\n' % END_OF_CODE_STRING
py_out = runAsPython(self.temp_dir, adj_code, extra_code, args=args)
java_out = self.runAsJava(adj_code, extra_code, args=args)
py_out = runAsPython(self.temp_dir, adj_code, extra_code, args=args, env=env)
java_out = self.runAsJava(adj_code, extra_code, args=args, env=env)
except Exception as e:
self.fail(e)
finally:
Expand Down Expand Up @@ -534,7 +537,7 @@ def makeTempDir(self):
except FileExistsError:
pass

def runAsJava(self, main_code, extra_code=None, args=None):
def runAsJava(self, main_code, extra_code=None, args=None, env=None):
"""Run a block of Python code as a Java program."""
# Output source code into test directory
transpiler = Transpiler(verbosity=0)
Expand Down Expand Up @@ -579,7 +582,8 @@ def runAsJava(self, main_code, extra_code=None, args=None):
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
cwd=self.temp_dir
cwd=self.temp_dir,
env=env,
)
out = proc.communicate()[0].decode('utf8')

Expand Down