Skip to content

Commit 047ea3b

Browse files
author
Per Rosengren
committed
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.
1 parent 064b143 commit 047ea3b

File tree

1 file changed

+10
-11
lines changed

1 file changed

+10
-11
lines changed

autopxd/__init__.py

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -303,16 +303,15 @@ def lines(self):
303303

304304

305305
def preprocess(code, extra_cpp_args=[]):
306-
proc = subprocess.Popen([
307-
'cpp', '-nostdinc', '-D__attribute__(x)=', '-I', BUILTIN_HEADERS_DIR,
308-
] + extra_cpp_args + ['-'], stdin=subprocess.PIPE, stdout=subprocess.PIPE)
309-
result = []
310-
result.append(proc.communicate(input=code.encode('utf-8'))[0])
311-
while proc.poll() is None:
312-
result.append(proc.communicate()[0])
306+
proc = subprocess.Popen(
307+
['cpp', '-nostdinc', '-D__attribute__(x)=',
308+
'-I', BUILTIN_HEADERS_DIR] +
309+
extra_cpp_args + ['-'],
310+
stdin=subprocess.PIPE, stdout=subprocess.PIPE, universal_newlines=True)
311+
result = proc.communicate(input=code)[0]
313312
if proc.returncode:
314313
raise Exception('Invoking C preprocessor failed')
315-
return b''.join(result).decode('utf-8')
314+
return result
316315

317316

318317
def parse(code, extra_cpp_args=[]):
@@ -328,14 +327,14 @@ def parse(code, extra_cpp_args=[]):
328327

329328

330329
def translate(code, hdrname, extra_cpp_args=[]):
331-
extra_incdir = os.path.dirname(hdrname)
330+
extra_incdir = os.path.normpath(os.path.dirname(hdrname))
332331
p = AutoPxd(hdrname)
333332
p.visit(parse(code, extra_cpp_args=['-I', extra_incdir]))
334333
return str(p)
335334

336335

337336
@click.command()
338-
@click.argument('infile', type=click.File('rb'), default=sys.stdin)
339-
@click.argument('outfile', type=click.File('wb'), default=sys.stdout)
337+
@click.argument('infile', type=click.File(), default=sys.stdin)
338+
@click.argument('outfile', type=click.File('wt'), default=sys.stdout)
340339
def cli(infile, outfile):
341340
outfile.write(translate(infile.read(), infile.name))

0 commit comments

Comments
 (0)