Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Windows fix: normalizes extra_incdir, universal_newlines on preproces… #4

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 10 additions & 11 deletions autopxd/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)=',
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please avoid making unnecessary code style/spacing changes.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I exceeded pep8 max line length with the extra argument. Please suggest the style you prefer.

'-I', BUILTIN_HEADERS_DIR] +
extra_cpp_args + ['-'],
stdin=subprocess.PIPE, stdout=subprocess.PIPE, universal_newlines=True)
result = proc.communicate(input=code)[0]
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

communicate should be called only once. doc:

Read data from stdout and stderr, until end-of-file is reached. Wait for process to terminate.

Use universal_newlines to get platform independent source code strings. Note doc:

The type of input must be bytes or, if universal_newlines was True, a string.
communicate() returns a tuple (stdout_data, stderr_data). The data will be bytes or, if universal_newlines was True, strings.

if proc.returncode:
raise Exception('Invoking C preprocessor failed')
return b''.join(result).decode('utf-8')
return result


def parse(code, extra_cpp_args=[]):
Expand All @@ -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)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'rt' is default

@click.argument('outfile', type=click.File('wt'), default=sys.stdout)
def cli(infile, outfile):
outfile.write(translate(infile.read(), infile.name))