forked from pythonnet/pythonnet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgeninterop.py
More file actions
293 lines (237 loc) · 8.57 KB
/
geninterop.py
File metadata and controls
293 lines (237 loc) · 8.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
TypeOffset is a C# class that mirrors the in-memory layout of heap
allocated Python objects.
This script parses the Python C headers and outputs the TypeOffset
C# class.
Requirements:
- pycparser
- clang
"""
from __future__ import print_function
import logging
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 = {
"ht_name": "name",
"ht_qualname": "qualname"
}
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"""
def __init__(self):
self.__typedefs = {}
self.__typedecls = {}
self.__structs = {}
self.__struct_stack = []
self.__struct_members_stack = []
self.__ptr_decl_depth = 0
self.__struct_members = {}
def get_struct_members(self, name):
"""return a list of (name, type) of struct members"""
if name in self.__typedefs:
node = self.__get_leaf_node(self.__typedefs[name])
name = node.name
if name not in self.__struct_members:
raise Exception("Unknown struct '%s'" % name)
return self.__struct_members[name]
def visit(self, node):
if isinstance(node, c_ast.FileAST):
self.visit_ast(node)
elif isinstance(node, c_ast.Typedef):
self.visit_typedef(node)
elif isinstance(node, c_ast.TypeDecl):
self.visit_typedecl(node)
elif isinstance(node, c_ast.Struct):
self.visit_struct(node)
elif isinstance(node, c_ast.Decl):
self.visit_decl(node)
elif isinstance(node, c_ast.PtrDecl):
self.visit_ptrdecl(node)
elif isinstance(node, c_ast.IdentifierType):
self.visit_identifier(node)
def visit_ast(self, ast):
for name, node in ast.children():
self.visit(node)
def visit_typedef(self, typedef):
self.__typedefs[typedef.name] = typedef.type
self.visit(typedef.type)
def visit_typedecl(self, typedecl):
self.visit(typedecl.type)
def visit_struct(self, struct):
self.__structs[self.__get_struct_name(struct)] = struct
if struct.decls:
# recurse into the struct
self.__struct_stack.insert(0, struct)
for decl in struct.decls:
self.__struct_members_stack.insert(0, decl.name)
self.visit(decl)
self.__struct_members_stack.pop(0)
self.__struct_stack.pop(0)
elif self.__ptr_decl_depth:
# the struct is empty, but add it as a member to the current
# struct as the current member maybe a pointer to it.
self.__add_struct_member(struct.name)
def visit_decl(self, decl):
self.visit(decl.type)
def visit_ptrdecl(self, ptrdecl):
self.__ptr_decl_depth += 1
self.visit(ptrdecl.type)
self.__ptr_decl_depth -= 1
def visit_identifier(self, identifier):
type_name = " ".join(identifier.names)
self.__add_struct_member(type_name)
def __add_struct_member(self, type_name):
if not (self.__struct_stack and self.__struct_members_stack):
return
# add member to current struct
current_struct = self.__struct_stack[0]
member_name = self.__struct_members_stack[0]
struct_members = self.__struct_members.setdefault(
self.__get_struct_name(current_struct), [])
# get the node associated with this type
node = None
if type_name in self.__typedefs:
node = self.__get_leaf_node(self.__typedefs[type_name])
elif type_name in self.__structs:
node = self.__structs[type_name]
# If it's a struct (and not a pointer to a struct) expand
# it into the current struct definition
if not self.__ptr_decl_depth and isinstance(node, c_ast.Struct):
for decl in node.decls or []:
self.__struct_members_stack.insert(0, decl.name)
self.visit(decl)
self.__struct_members_stack.pop(0)
else:
# otherwise add it as a single member
struct_members.append((member_name, type_name))
def __get_leaf_node(self, node):
if isinstance(node, c_ast.Typedef):
return self.__get_leaf_node(node.type)
if isinstance(node, c_ast.TypeDecl):
return self.__get_leaf_node(node.type)
return node
def __get_struct_name(self, node):
return node.name or "_struct_%d" % id(node)
def preprocess_python_headers():
"""Return Python.h pre-processed, ready for parsing.
Requires clang.
"""
fake_libc_include = os.path.join(os.path.dirname(__file__),
"fake_libc_include")
include_dirs = [fake_libc_include]
include_py = sysconfig.get_config_var("INCLUDEPY")
include_dirs.append(include_py)
defines = [
"-D", "__attribute__(x)=",
"-D", "__inline__=inline",
"-D", "__asm__=;#pragma asm",
"-D", "__int64=long long",
"-D", "_POSIX_THREADS"
]
if hasattr(sys, "abiflags"):
if "d" in sys.abiflags:
defines.extend(("-D", "PYTHON_WITH_PYDEBUG"))
if "m" in sys.abiflags:
defines.extend(("-D", "PYTHON_WITH_PYMALLOC"))
if "u" in sys.abiflags:
defines.extend(("-D", "PYTHON_WITH_WIDE_UNICODE"))
python_h = os.path.join(include_py, "Python.h")
cmd = ["clang", "-pthread", "-I"] + include_dirs + defines + ["-E", python_h]
# normalize as the parser doesn't like windows line endings.
lines = []
for line in _check_output(cmd).splitlines():
if line.startswith("#"):
line = line.replace("\\", "/")
lines.append(line)
return "\n".join(lines)
def gen_interop_code(members):
"""Generate the TypeOffset C# class"""
defines = [
"PYTHON{0}{1}".format(PY_MAJOR, PY_MINOR)
]
if hasattr(sys, "abiflags"):
if "d" in sys.abiflags:
defines.append("PYTHON_WITH_PYDEBUG")
if "m" in sys.abiflags:
defines.append("PYTHON_WITH_PYMALLOC")
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.
#if %s
using System;
using System.Collections;
using System.Collections.Specialized;
using System.Runtime.InteropServices;
using System.Reflection;
using System.Text;
namespace Python.Runtime
{
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
internal class TypeOffset
{
static TypeOffset()
{
Type type = typeof(TypeOffset);
FieldInfo[] fi = type.GetFields();
int size = IntPtr.Size;
for (int i = 0; i < fi.Length; i++)
{
fi[i].SetValue(null, i * size);
}
}
public static int magic()
{
return ob_size;
}
// Auto-generated from PyHeapTypeObject in Python.h
""" % (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.
for name, tpy in members:
name = _typeoffset_member_renames.get(name, name)
class_definition += " public static int %s = 0;\n" % name
class_definition += """
/* here are optional user slots, followed by the members. */
public static int members = 0;
}
}
#endif
"""
return class_definition
def main():
# preprocess Python.h and build the AST
python_h = preprocess_python_headers()
parser = c_parser.CParser()
ast = parser.parse(python_h)
# extract struct members from the AST
ast_parser = AstParser()
ast_parser.visit(ast)
# generate the C# code
members = ast_parser.get_struct_members("PyHeapTypeObject")
interop_cs = gen_interop_code(members)
if len(sys.argv) > 1:
with open(sys.argv[1], "w") as fh:
fh.write(interop_cs)
else:
print(interop_cs)
if __name__ == "__main__":
sys.exit(main())