From bd661a3ae8fa54c21166d3601e83d5a0c15d7967 Mon Sep 17 00:00:00 2001 From: Bob Tolbert Date: Sun, 22 Jun 2014 18:08:04 -0600 Subject: [PATCH] Fix hy2py output on Windows/Python 3 If there are Unicode symbols in the hy2py output (as in tests/native_test/language.hy) and the user is on Windows, and the user is using Python3 and the user doesn't have an appropriate codepage in the terminal for Unicode, then hy2py will fail at encoding the output. This fix makes sure that if encoding fails, the bytes output is shown instead of throwing an exception and failing completely. This also allows the hy2py tests to pass on Windows. If the user does activate an appropriate codepage, for example, chcp 65001 then the Unicode output will show correctly. Converted printing code to small function to eliminate some duplicate code --- hy/cmdline.py | 34 ++++++++++++++++++++++++++++++---- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/hy/cmdline.py b/hy/cmdline.py index a29d901..51417b2 100644 --- a/hy/cmdline.py +++ b/hy/cmdline.py @@ -47,7 +47,7 @@ from hy.models.expression import HyExpression from hy.models.string import HyString from hy.models.symbol import HySymbol -from hy._compat import builtins +from hy._compat import builtins, PY3 class HyQuitter(object): @@ -327,6 +327,7 @@ def hyc_main(): # entry point for cmd line script "hy2py" def hy2py_main(): + import platform module_name = "" options = dict(prog="hy2py", usage="%(prog)s [options] FILE", @@ -349,17 +350,42 @@ def hy2py_main(): if options.with_source: hst = import_file_to_hst(options.args[0]) - print(hst) + # need special printing on Windows in case the + # codepage doesn't support utf-8 characters + if PY3 and platform.system() == "Windows": + for h in hst: + try: + print(h) + except: + print(str(h).encode('utf-8')) + else: + print(hst) print() print() _ast = import_file_to_ast(options.args[0], module_name) if options.with_ast: - print(astor.dump(_ast)) + if PY3 and platform.system() == "Windows": + _print_for_windows(astor.dump(_ast)) + else: + print(astor.dump(_ast)) print() print() if not options.without_python: - print(astor.codegen.to_source(_ast)) + if PY3 and platform.system() == "Windows": + _print_for_windows(astor.codegen.to_source(_ast)) + else: + print(astor.codegen.to_source(_ast)) parser.exit(0) + + +# need special printing on Windows in case the +# codepage doesn't support utf-8 characters +def _print_for_windows(src): + for line in src.split("\n"): + try: + print(line) + except: + print(line.encode('utf-8'))