| 1 |
|
| 2 |
from cparser import * |
| 3 |
|
| 4 |
ctypes_type_map = { |
| 5 |
|
| 6 |
('void', True, 0): 'None', |
| 7 |
('int', True, 0): 'c_int', |
| 8 |
('int', False, 0): 'c_uint', |
| 9 |
('int', True, 1): 'c_long', |
| 10 |
('int', False, 1): 'c_ulong', |
| 11 |
('int', True, 2): 'c_longlong', |
| 12 |
('int', False, 2): 'c_ulonglong', |
| 13 |
('char', True, 0): 'c_char', |
| 14 |
('char', False, 0): 'c_ubyte', |
| 15 |
('short', True, 0): 'c_short', |
| 16 |
('short', False, 0): 'c_ushort', |
| 17 |
('float', True, 0): 'c_float', |
| 18 |
('double', True, 0): 'c_double', |
| 19 |
('size_t', True, 0): 'c_size_t', |
| 20 |
('int8_t', True, 0): 'c_int8', |
| 21 |
('int16_t', True, 0): 'c_int16', |
| 22 |
('int32_t', True, 0): 'c_int32', |
| 23 |
('int64_t', True, 0): 'c_int64', |
| 24 |
('uint8_t', True, 0): 'c_uint8', |
| 25 |
('uint16_t',True, 0): 'c_uint16', |
| 26 |
('uint32_t',True, 0): 'c_uint32', |
| 27 |
('uint64_t',True, 0): 'c_uint64', |
| 28 |
('wchar_t', True, 0): 'c_wchar', |
| 29 |
('ptrdiff_t',True, 0): 'c_ptrdiff_t', |
| 30 |
} |
| 31 |
|
| 32 |
reserved_names = ['None', 'True', 'False'] |
| 33 |
|
| 34 |
def get_ctypes_type(typ, declarator): |
| 35 |
signed = True |
| 36 |
typename = 'int' |
| 37 |
longs = 0 |
| 38 |
t = None |
| 39 |
for specifier in typ.specifiers: |
| 40 |
if isinstance(specifier, StructTypeSpecifier): |
| 41 |
t = CtypesStruct(specifier) |
| 42 |
elif isinstance(specifier, EnumSpecifier): |
| 43 |
t = CtypesEnum(specifier) |
| 44 |
elif specifier == 'signed': |
| 45 |
signed = True |
| 46 |
elif specifier == 'unsigned': |
| 47 |
signed = False |
| 48 |
elif specifier == 'long': |
| 49 |
longs += 1 |
| 50 |
else: |
| 51 |
typename = str(specifier) |
| 52 |
if not t: |
| 53 |
ctypes_name = ctypes_type_map.get((typename, signed, longs), typename) |
| 54 |
t = CtypesType(ctypes_name) |
| 55 |
|
| 56 |
while declarator and declarator.pointer: |
| 57 |
if declarator.parameters is not None: |
| 58 |
t = CtypesFunction(t, declarator.parameters) |
| 59 |
a = declarator.array |
| 60 |
while a: |
| 61 |
t = CtypesArray(t, a.size) |
| 62 |
a = a.array |
| 63 |
|
| 64 |
if type(t) == CtypesType and t.name == 'c_char': |
| 65 |
t = CtypesType('c_char_p') |
| 66 |
elif type(t) == CtypesType and t.name == 'c_wchar': |
| 67 |
t = CtypesType('c_wchar_p') |
| 68 |
else: |
| 69 |
t = CtypesPointer(t, declarator.qualifiers) |
| 70 |
declarator = declarator.pointer |
| 71 |
if declarator and declarator.parameters is not None: |
| 72 |
t = CtypesFunction(t, declarator.parameters) |
| 73 |
if declarator: |
| 74 |
a = declarator.array |
| 75 |
while a: |
| 76 |
t = CtypesArray(t, a.size) |
| 77 |
a = a.array |
| 78 |
return t |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
def remove_function_pointer(t): |
| 83 |
if type(t) == CtypesPointer and type(t.destination) == CtypesFunction: |
| 84 |
return t.destination |
| 85 |
elif type(t) == CtypesPointer: |
| 86 |
t.destination = remove_function_pointer(t.destination) |
| 87 |
return t |
| 88 |
else: |
| 89 |
return t |
| 90 |
|
| 91 |
class CtypesTypeVisitor(object): |
| 92 |
def visit_struct(self, struct): |
| 93 |
pass |
| 94 |
|
| 95 |
def visit_enum(self, enum): |
| 96 |
pass |
| 97 |
|
| 98 |
class CtypesType(object): |
| 99 |
def __init__(self, name): |
| 100 |
self.name = name |
| 101 |
|
| 102 |
def get_required_type_names(self): |
| 103 |
'''Return all type names defined or needed by this type''' |
| 104 |
return (self.name,) |
| 105 |
|
| 106 |
def visit(self, visitor): |
| 107 |
pass |
| 108 |
|
| 109 |
def __str__(self): |
| 110 |
return self.name |
| 111 |
|
| 112 |
class CtypesPointer(CtypesType): |
| 113 |
def __init__(self, destination, qualifiers): |
| 114 |
self.destination = destination |
| 115 |
|
| 116 |
|
| 117 |
def get_required_type_names(self): |
| 118 |
if self.destination: |
| 119 |
return self.destination.get_required_type_names() |
| 120 |
else: |
| 121 |
return () |
| 122 |
|
| 123 |
def visit(self, visitor): |
| 124 |
if self.destination: |
| 125 |
self.destination.visit(visitor) |
| 126 |
|
| 127 |
def __str__(self): |
| 128 |
return 'POINTER(%s)' % str(self.destination) |
| 129 |
|
| 130 |
class CtypesArray(CtypesType): |
| 131 |
def __init__(self, base, count): |
| 132 |
self.base = base |
| 133 |
self.count = count |
| 134 |
|
| 135 |
def get_required_type_names(self): |
| 136 |
|
| 137 |
return self.base.get_required_type_names() |
| 138 |
|
| 139 |
def visit(self, visitor): |
| 140 |
|
| 141 |
self.base.visit(visitor) |
| 142 |
|
| 143 |
def __str__(self): |
| 144 |
if self.count is None: |
| 145 |
return 'POINTER(%s)' % str(self.base) |
| 146 |
if type(self.base) == CtypesArray: |
| 147 |
return '(%s) * %s' % (str(self.base), str(self.count)) |
| 148 |
else: |
| 149 |
return '%s * %s' % (str(self.base), str(self.count)) |
| 150 |
|
| 151 |
class CtypesFunction(CtypesType): |
| 152 |
def __init__(self, restype, parameters): |
| 153 |
if parameters and parameters[-1] == '...': |
| 154 |
|
| 155 |
|
| 156 |
parameters = parameters[:-1] |
| 157 |
|
| 158 |
self.restype = restype |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
if type(self.restype) == CtypesPointer and \ |
| 165 |
type(self.restype.destination) == CtypesType and \ |
| 166 |
self.restype.destination.name == 'None': |
| 167 |
self.restype = CtypesPointer(CtypesType('c_void'), ()) |
| 168 |
|
| 169 |
self.argtypes = [remove_function_pointer( |
| 170 |
get_ctypes_type(p.type, p.declarator)) \ |
| 171 |
for p in parameters] |
| 172 |
|
| 173 |
def get_required_type_names(self): |
| 174 |
lst = list(self.restype.get_required_type_names()) |
| 175 |
for a in self.argtypes: |
| 176 |
lst += list(a.get_required_type_names()) |
| 177 |
return lst |
| 178 |
|
| 179 |
def visit(self, visitor): |
| 180 |
self.restype.visit(visitor) |
| 181 |
for a in self.argtypes: |
| 182 |
a.visit(visitor) |
| 183 |
|
| 184 |
def __str__(self): |
| 185 |
return 'CFUNCTYPE(%s)' % ', '.join([str(self.restype)] + \ |
| 186 |
[str(a) for a in self.argtypes]) |
| 187 |
|
| 188 |
last_tagnum = 0 |
| 189 |
def anonymous_struct_tag(): |
| 190 |
global last_tagnum |
| 191 |
last_tagnum += 1 |
| 192 |
return 'anon_%d' % last_tagnum |
| 193 |
|
| 194 |
class CtypesStruct(CtypesType): |
| 195 |
def __init__(self, specifier): |
| 196 |
self.is_union = specifier.is_union |
| 197 |
self.tag = specifier.tag |
| 198 |
if not self.tag: |
| 199 |
self.tag = anonymous_struct_tag() |
| 200 |
|
| 201 |
if specifier.declarations: |
| 202 |
self.opaque = False |
| 203 |
self.members = [] |
| 204 |
for declaration in specifier.declarations: |
| 205 |
t = get_ctypes_type(declaration.type, declaration.declarator) |
| 206 |
declarator = declaration.declarator |
| 207 |
if declarator is None: |
| 208 |
|
| 209 |
return |
| 210 |
while declarator.pointer: |
| 211 |
declarator = declarator.pointer |
| 212 |
name = declarator.identifier |
| 213 |
self.members.append((name, t)) |
| 214 |
else: |
| 215 |
self.opaque = True |
| 216 |
self.members = [] |
| 217 |
|
| 218 |
def get_required_type_names(self): |
| 219 |
lst = ['struct_%s' % self.tag] |
| 220 |
for m in self.members: |
| 221 |
lst += m[1].get_required_type_names() |
| 222 |
return lst |
| 223 |
|
| 224 |
def visit(self, visitor): |
| 225 |
visitor.visit_struct(self) |
| 226 |
|
| 227 |
def __str__(self): |
| 228 |
return 'struct_%s' % self.tag |
| 229 |
|
| 230 |
last_tagnum = 0 |
| 231 |
def anonymous_enum_tag(): |
| 232 |
global last_tagnum |
| 233 |
last_tagnum += 1 |
| 234 |
return 'anon_%d' % last_tagnum |
| 235 |
|
| 236 |
class CtypesEnum(CtypesType): |
| 237 |
def __init__(self, specifier): |
| 238 |
self.tag = specifier.tag |
| 239 |
if not self.tag: |
| 240 |
self.tag = anonymous_enum_tag() |
| 241 |
|
| 242 |
value = 0 |
| 243 |
context = EvaluationContext() |
| 244 |
self.enumerators = [] |
| 245 |
for e in specifier.enumerators: |
| 246 |
if e.expression: |
| 247 |
try: |
| 248 |
value = int(e.expression.evaluate(context)) |
| 249 |
except: |
| 250 |
pass |
| 251 |
self.enumerators.append((e.name, value)) |
| 252 |
value += 1 |
| 253 |
|
| 254 |
def get_required_type_names(self): |
| 255 |
return [] |
| 256 |
|
| 257 |
def visit(self, visitor): |
| 258 |
visitor.visit_enum(self) |
| 259 |
|
| 260 |
def __str__(self): |
| 261 |
return 'enum_%s' % self.tag |
| 262 |
|
| 263 |
class CtypesParser(CParser): |
| 264 |
'''Parse a C file for declarations that can be used by ctypes. |
| 265 |
|
| 266 |
Subclass and override the handle_ctypes_* methods. |
| 267 |
''' |
| 268 |
def handle_define(self, name, value, filename, lineno): |
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
if value == 'int': |
| 274 |
t = CtypesType('c_int') |
| 275 |
self.handle_ctypes_type_definition( |
| 276 |
name, t, filename, lineno) |
| 277 |
|
| 278 |
def handle_define_constant(self, name, value, filename, lineno): |
| 279 |
if name in reserved_names: |
| 280 |
name += '_' |
| 281 |
self.handle_ctypes_constant(name, value, filename, lineno) |
| 282 |
|
| 283 |
def handle_declaration(self, declaration, filename, lineno): |
| 284 |
t = get_ctypes_type(declaration.type, declaration.declarator) |
| 285 |
declarator = declaration.declarator |
| 286 |
if declarator is None: |
| 287 |
|
| 288 |
return |
| 289 |
while declarator.pointer: |
| 290 |
declarator = declarator.pointer |
| 291 |
name = declarator.identifier |
| 292 |
if declaration.storage == 'typedef': |
| 293 |
self.handle_ctypes_type_definition( |
| 294 |
name, remove_function_pointer(t), filename, lineno) |
| 295 |
elif type(t) == CtypesFunction: |
| 296 |
self.handle_ctypes_function( |
| 297 |
name, t.restype, t.argtypes, filename, lineno) |
| 298 |
elif declaration.storage != 'static': |
| 299 |
self.handle_ctypes_variable(name, t, filename, lineno) |
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
def handle_ctypes_constant(self, name, value, filename, lineno): |
| 304 |
pass |
| 305 |
|
| 306 |
def handle_ctypes_type_definition(self, name, ctype, filename, lineno): |
| 307 |
pass |
| 308 |
|
| 309 |
def handle_ctypes_function(self, name, restype, argtypes, filename, lineno): |
| 310 |
pass |
| 311 |
|
| 312 |
def handle_ctypes_variable(self, name, ctype, filename, lineno): |
| 313 |
pass |
| 314 |
|