From c7e3aff70d2882086058c8be8cd5b72809a9676b Mon Sep 17 00:00:00 2001 From: Cosimo Lupo Date: Sat, 24 Mar 2018 18:59:52 +0000 Subject: [PATCH 1/2] [py2.py3] add function to coerce strings to six.binary_type python-autopxd already requires six, but the new `ensure_binary` function is only in the git repo and hasn't been released to PyPI yet, so I temporarily copied it here. The source comes from: https://github.com/benjaminp/six/pull/204 It's a trivial function (every py2.py3 project has its own way to do the same thing). Fixes #12 Supersedes #3 --- autopxd/__init__.py | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/autopxd/__init__.py b/autopxd/__init__.py index 81d0351..0ecc642 100644 --- a/autopxd/__init__.py +++ b/autopxd/__init__.py @@ -43,6 +43,24 @@ )) +# TODO use upstream six.ensure_binary once next version of six is released +def ensure_binary(s, encoding='utf-8', errors='strict'): + """Coerce **s** to six.binary_type. + For Python 2: + - `unicode` -> encoded to `str` + - `str` -> `str` + For Python 3: + - `str` -> encoded to `bytes` + - `bytes` -> `bytes` + """ + if isinstance(s, six.text_type): + return s.encode(encoding, errors) + elif isinstance(s, six.binary_type): + return s + else: + raise TypeError("not expecting type '%s'" % type(s)) + + class PxdNode(object): indent = ' ' @@ -311,7 +329,8 @@ def preprocess(code, extra_cpp_args=[]): 'cpp', '-nostdinc', '-D__attribute__(x)=', '-I', BUILTIN_HEADERS_DIR, ] + extra_cpp_args + ['-'], stdin=subprocess.PIPE, stdout=subprocess.PIPE) result = [] - result.append(proc.communicate(input=code.encode('utf-8'))[0]) + code = ensure_binary(code) + result.append(proc.communicate(input=code)[0]) while proc.poll() is None: result.append(proc.communicate()[0]) if proc.returncode: @@ -355,4 +374,5 @@ def translate(code, hdrname, extra_cpp_args=[], whitelist=None): @click.argument('infile', type=click.File('rb'), default=sys.stdin) @click.argument('outfile', type=click.File('wb'), default=sys.stdout) def cli(infile, outfile): - outfile.write(translate(infile.read(), infile.name)) + output = translate(infile.read(), infile.name) + outfile.write(ensure_binary(output)) From cd201a9d462970ebd23f2b3085577dde1848030b Mon Sep 17 00:00:00 2001 From: Cosimo Lupo Date: Sat, 24 Mar 2018 19:57:37 +0000 Subject: [PATCH 2/2] leave output as native str when printing to stdout on py2, stdout is a bytes stream, on py3 it is a unicode text stream --- autopxd/__init__.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/autopxd/__init__.py b/autopxd/__init__.py index 0ecc642..e59fcfb 100644 --- a/autopxd/__init__.py +++ b/autopxd/__init__.py @@ -375,4 +375,6 @@ def translate(code, hdrname, extra_cpp_args=[], whitelist=None): @click.argument('outfile', type=click.File('wb'), default=sys.stdout) def cli(infile, outfile): output = translate(infile.read(), infile.name) - outfile.write(ensure_binary(output)) + if outfile is not sys.stdout: + output = ensure_binary(output) + outfile.write(output)