Skip to content


Subversion checkout URL

You can clone with
Download ZIP
tree: e5e0523c53
Fetching contributors…

Cannot retrieve contributors at this time

92 lines (80 sloc) 3.117 kb
import sys
import re
from ctypeslib.codegen import typedesc
from tp_puller import TypePuller
from misc import classify, query_items
from cytypes import generic_decl, generic_named_decl
from funcs import generic_as_arg, named_pointer_decl
def generate_cython(output, genitems, enumvals):
# Generate the cython code
cython_code = [cy_generate(i) for i in genitems]
if enumvals:
output.write("\tcdef enum:\n")
for i in enumvals:
output.write("\t\t%s = %d\n" % (, int(i.value)))
for i in cython_code:
if not i:
if len(i) > 1:
output.write("\t%s\n" % i[0])
for j in i[1:]:
output.write("\t%s\n" % j)
output.write("\t%s\n" % i[0])
def cy_generate_typedef(item):
if not isinstance(item.typ, typedesc.PointerType):
return ["ctypedef %s %s" % (,]
return ["ctypedef %s" % (named_pointer_decl(item.typ) %]
def cy_generate_structure(tp, union=False):
if union:
output = ['cdef union %s:' %]
output = ['cdef struct %s:' %]
for m in tp.members:
if isinstance(m, typedesc.Field):
output.append("\t" + (generic_named_decl(m.typ) %
elif isinstance(m, typedesc.Structure):
output.append("\t%s" % generic_decl(m))
elif isinstance(m, typedesc.Ignored):
print "Struct member not handled:", m
if not tp.members:
return output
def cy_generate_enumeration(tp):
output = ['cdef enum %s:' %]
for v in tp.values:
output.append("\t%s = %s" % (, v.value))
return output
def cy_generate_function(func):
args = [generic_as_arg(a) for a in func.iterArgTypes()]
return ["%s %s(%s)" % (generic_as_arg(func.returns),, ", ".join(args))]
def cy_generate_enum_value(tp):
output = ['cdef enum:']
output.append("\t%s = %d" % (, int(tp.value)))
return output
def cy_generate(item):
if isinstance(item, typedesc.Typedef):
#print "Typedef Generating", item,
return cy_generate_typedef(item)
elif isinstance(item, typedesc.Structure):
#print "Struct Generating", item,
return cy_generate_structure(item)
elif isinstance(item, typedesc.Union):
#print "Union Generating", item,
return cy_generate_structure(item, union=True)
elif isinstance(item, typedesc.Function):
#print "FunctionType Generating", item,
return cy_generate_function(item)
elif isinstance(item, typedesc.EnumValue):
#print "FunctionType Generating", item
return cy_generate_enum_value(item)
elif isinstance(item, typedesc.Enumeration):
#print "FunctionType Generating", item
return cy_generate_enumeration(item)
print "Item not handled for cy_generate", item
# raise ValueError, ("item not handled:", item)
Jump to Line
Something went wrong with that request. Please try again.