From 047ea3b24536e9208c780b6ece559ee25bfcb554 Mon Sep 17 00:00:00 2001 From: Per Rosengren Date: Wed, 14 Sep 2016 22:12:23 +0200 Subject: [PATCH] Windows fix: normalizes extra_incdir, universal_newlines on preprocessor output. When input is a filename without directory, the dirname is empty, which inserts `-I `, instead of ` -I .`, which breaks the command. Remedied with os.path.normpath On Windows with MinGW gcc, preprocessor output has Windows newlines, which pycparser can't handle. Remedied by universal_newlines=True for subprocess. Py3: universal_newlines=True changes `subprocess.communicate` to take and return strings instead of bytes. Adjusted for this. Current master reads input headers as bytes. This causes `code.encode` to fail. Changed `cli` to open files as text fixes this. Tested with `test.py` on Python 2.7 and 3.5 on Windows 10. --- autopxd/__init__.py | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/autopxd/__init__.py b/autopxd/__init__.py index 20709bf..b018621 100644 --- a/autopxd/__init__.py +++ b/autopxd/__init__.py @@ -303,16 +303,15 @@ def lines(self): def preprocess(code, extra_cpp_args=[]): - proc = subprocess.Popen([ - '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]) - while proc.poll() is None: - result.append(proc.communicate()[0]) + proc = subprocess.Popen( + ['cpp', '-nostdinc', '-D__attribute__(x)=', + '-I', BUILTIN_HEADERS_DIR] + + extra_cpp_args + ['-'], + stdin=subprocess.PIPE, stdout=subprocess.PIPE, universal_newlines=True) + result = proc.communicate(input=code)[0] if proc.returncode: raise Exception('Invoking C preprocessor failed') - return b''.join(result).decode('utf-8') + return result def parse(code, extra_cpp_args=[]): @@ -328,14 +327,14 @@ def parse(code, extra_cpp_args=[]): def translate(code, hdrname, extra_cpp_args=[]): - extra_incdir = os.path.dirname(hdrname) + extra_incdir = os.path.normpath(os.path.dirname(hdrname)) p = AutoPxd(hdrname) p.visit(parse(code, extra_cpp_args=['-I', extra_incdir])) return str(p) @click.command() -@click.argument('infile', type=click.File('rb'), default=sys.stdin) -@click.argument('outfile', type=click.File('wb'), default=sys.stdout) +@click.argument('infile', type=click.File(), default=sys.stdin) +@click.argument('outfile', type=click.File('wt'), default=sys.stdout) def cli(infile, outfile): outfile.write(translate(infile.read(), infile.name))