15
15
16
16
from __future__ import print_function
17
17
18
- from distutils .sysconfig import get_config_var
19
- from subprocess import Popen , CalledProcessError , PIPE
20
- from pycparser import c_parser , c_ast
21
18
import logging
22
- import sys
23
19
import os
20
+ import sys
21
+ import sysconfig
22
+ import subprocess
23
+
24
+ from pycparser import c_ast , c_parser
24
25
25
26
_log = logging .getLogger ()
26
27
logging .basicConfig (level = logging .DEBUG )
27
28
29
+ PY_MAJOR = sys .version_info [0 ]
30
+ PY_MINOR = sys .version_info [1 ]
28
31
29
32
# rename some members from their C name when generating the C#
30
33
_typeoffset_member_renames = {
33
36
}
34
37
35
38
39
+ def _check_output (* args , ** kwargs ):
40
+ """Check output wrapper for py2/py3 compatibility"""
41
+ output = subprocess .check_output (* args , ** kwargs )
42
+ if PY_MAJOR == 2 :
43
+ return output
44
+ return output .decode ("ascii" )
45
+
46
+
36
47
class AstParser (object ):
37
48
"""Walk an AST and determine the members of all structs"""
38
49
@@ -147,23 +158,6 @@ def __get_struct_name(self, node):
147
158
return node .name or "_struct_%d" % id (node )
148
159
149
160
150
- def check_output (* popenargs , ** kwargs ):
151
- """subprocess.check_output from python 2.7.
152
- Added here to support building for earlier versions of Python.
153
- """
154
- process = Popen (stdout = PIPE , * popenargs , ** kwargs )
155
- output , unused_err = process .communicate ()
156
- retcode = process .poll ()
157
- if retcode :
158
- cmd = kwargs .get ("args" )
159
- if cmd is None :
160
- cmd = popenargs [0 ]
161
- raise CalledProcessError (retcode , cmd )
162
- if sys .version_info [0 ] > 2 :
163
- return output .decode ("ascii" )
164
- return output
165
-
166
-
167
161
def preprocess_python_headers ():
168
162
"""Return Python.h pre-processed, ready for parsing.
169
163
Requires clang.
@@ -172,7 +166,7 @@ def preprocess_python_headers():
172
166
"fake_libc_include" )
173
167
include_dirs = [fake_libc_include ]
174
168
175
- include_py = get_config_var ("INCLUDEPY" )
169
+ include_py = sysconfig . get_config_var ("INCLUDEPY" )
176
170
include_dirs .append (include_py )
177
171
178
172
defines = [
@@ -195,7 +189,7 @@ def preprocess_python_headers():
195
189
196
190
# normalize as the parser doesn't like windows line endings.
197
191
lines = []
198
- for line in check_output (cmd ).splitlines ():
192
+ for line in _check_output (cmd ).splitlines ():
199
193
if line .startswith ("#" ):
200
194
line = line .replace ("\\ " , "/" )
201
195
lines .append (line )
@@ -206,7 +200,7 @@ def gen_interop_code(members):
206
200
"""Generate the TypeOffset C# class"""
207
201
208
202
defines = [
209
- "PYTHON%d%d" % ( sys . version_info [: 2 ] )
203
+ "PYTHON{0}{1}" . format ( PY_MAJOR , PY_MINOR )
210
204
]
211
205
212
206
if hasattr (sys , "abiflags" ):
@@ -217,6 +211,8 @@ def gen_interop_code(members):
217
211
if "u" in sys .abiflags :
218
212
defines .append ("PYTHON_WITH_WIDE_UNICODE" )
219
213
214
+ filename = os .path .basename (__file__ )
215
+ defines_str = " && " .join (defines )
220
216
class_definition = """
221
217
// Auto-generated by %s.
222
218
// DO NOT MODIFIY BY HAND.
@@ -252,7 +248,7 @@ def gen_interop_code(members):
252
248
}
253
249
254
250
// Auto-generated from PyHeapTypeObject in Python.h
255
- """ % (os . path . basename ( __file__ ), " && " . join ( defines ) )
251
+ """ % (filename , defines_str )
256
252
257
253
# All the members are sizeof(void*) so we don't need to do any
258
254
# extra work to determine the size based on the type.
0 commit comments