Skip to content
Prev Previous commit
Next Next commit
Clean-up getinterop
  • Loading branch information
vmuriart committed Jan 30, 2017
commit ccf96cbe8edcbb73a0f45269d5349e7431828bd3
46 changes: 21 additions & 25 deletions tools/geninterop/geninterop.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,19 @@

from __future__ import print_function

from distutils.sysconfig import get_config_var
from subprocess import Popen, CalledProcessError, PIPE
from pycparser import c_parser, c_ast
import logging
import sys
import os
import sys
import sysconfig
import subprocess

from pycparser import c_ast, c_parser

_log = logging.getLogger()
logging.basicConfig(level=logging.DEBUG)

PY_MAJOR = sys.version_info[0]
PY_MINOR = sys.version_info[1]

# rename some members from their C name when generating the C#
_typeoffset_member_renames = {
Expand All @@ -33,6 +36,14 @@
}


def _check_output(*args, **kwargs):
"""Check output wrapper for py2/py3 compatibility"""
output = subprocess.check_output(*args, **kwargs)
if PY_MAJOR == 2:
return output
return output.decode("ascii")


class AstParser(object):
"""Walk an AST and determine the members of all structs"""

Expand Down Expand Up @@ -147,23 +158,6 @@ def __get_struct_name(self, node):
return node.name or "_struct_%d" % id(node)


def check_output(*popenargs, **kwargs):
"""subprocess.check_output from python 2.7.
Added here to support building for earlier versions of Python.
"""
process = Popen(stdout=PIPE, *popenargs, **kwargs)
output, unused_err = process.communicate()
retcode = process.poll()
if retcode:
cmd = kwargs.get("args")
if cmd is None:
cmd = popenargs[0]
raise CalledProcessError(retcode, cmd)
if sys.version_info[0] > 2:
return output.decode("ascii")
return output


def preprocess_python_headers():
"""Return Python.h pre-processed, ready for parsing.
Requires clang.
Expand All @@ -172,7 +166,7 @@ def preprocess_python_headers():
"fake_libc_include")
include_dirs = [fake_libc_include]

include_py = get_config_var("INCLUDEPY")
include_py = sysconfig.get_config_var("INCLUDEPY")
include_dirs.append(include_py)

defines = [
Expand All @@ -195,7 +189,7 @@ def preprocess_python_headers():

# normalize as the parser doesn't like windows line endings.
lines = []
for line in check_output(cmd).splitlines():
for line in _check_output(cmd).splitlines():
if line.startswith("#"):
line = line.replace("\\", "/")
lines.append(line)
Expand All @@ -206,7 +200,7 @@ def gen_interop_code(members):
"""Generate the TypeOffset C# class"""

defines = [
"PYTHON%d%d" % (sys.version_info[:2])
"PYTHON{0}{1}".format(PY_MAJOR, PY_MINOR)
]

if hasattr(sys, "abiflags"):
Expand All @@ -217,6 +211,8 @@ def gen_interop_code(members):
if "u" in sys.abiflags:
defines.append("PYTHON_WITH_WIDE_UNICODE")

filename = os.path.basename(__file__)
defines_str = " && ".join(defines)
class_definition = """
// Auto-generated by %s.
// DO NOT MODIFIY BY HAND.
Expand Down Expand Up @@ -252,7 +248,7 @@ def gen_interop_code(members):
}

// Auto-generated from PyHeapTypeObject in Python.h
""" % (os.path.basename(__file__), " && ".join(defines))
""" % (filename, defines_str)

# All the members are sizeof(void*) so we don't need to do any
# extra work to determine the size based on the type.
Expand Down