[ctypes-commit] ctypes/sandbox/tools/structures genapi.py,1.1,1.2
Brought to you by:
theller
|
From: Thomas H. <th...@us...> - 2004-09-10 21:51:30
|
Update of /cvsroot/ctypes/ctypes/sandbox/tools/structures In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv13971 Modified Files: genapi.py Log Message: Making progress. Index: genapi.py =================================================================== RCS file: /cvsroot/ctypes/ctypes/sandbox/tools/structures/genapi.py,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** genapi.py 10 Sep 2004 07:45:20 -0000 1.1 --- genapi.py 10 Sep 2004 08:47:18 -0000 1.2 *************** *** 3,9 **** --- 3,27 ---- ctypes_names = { + "char": "c_char", + "unsigned char": "c_ubyte", + + "wchar_t": "c_wchar", + + "short int": "c_short", + "short unsigned int": "c_ushort", + "int": "c_int", "unsigned int": "c_uint", + + "long int": "c_long", + "long unsigned int": "c_ulong", + "long long int": "c_longlong", + "long long unsigned int": "c_ulonglong", + + "void": "void", + + "double": "c_double", + "float": "c_float", } *************** *** 13,16 **** --- 31,38 ---- def run(self): + # returns a tuple of two lists. The first one contains + # objects for which to generate code, the second contains + # objects with unresolved dependencies. + result = [] done = Set() remaining = Set() *************** *** 39,62 **** if not deps - done: resolved.add(o) ! print o ! print print "# resolved %d of %d -> %d" % (len(resolved), len(remaining), len(remaining) - len(resolved)) - print if not resolved: ! print "%d unresolved deps" % len(remaining) ! return remaining done = done | resolved remaining = remaining - resolved ! ## raw_input("Weiter?") if __name__ == "__main__": import sys if len(sys.argv) == 1: ! ## sys.argv.extend("-D NONAMELESSUNION -D _WIN32_WINNT=0x500 -c msvc6 -o- windef.h".split()) ! sys.argv.extend("-D NONAMELESSUNION -D _WIN32_WINNT=0x500 -c msvc6 -o- windows.h".split()) result = gccxmltools.main() p = DependencyResolver(result) ! p.run() --- 61,153 ---- if not deps - done: resolved.add(o) ! result.append(o) print "# resolved %d of %d -> %d" % (len(resolved), len(remaining), len(remaining) - len(resolved)) if not resolved: ! print "# %d unresolved deps" % len(remaining) ! return result, remaining done = done | resolved remaining = remaining - resolved ! return result, list(remaining) ! ! ################################################################ ! ! class CodeGenerator(gccxmltools.Visitor): + def __init__(self, *args, **kw): + super(CodeGenerator, self).__init__(*args, **kw) + self._env = {} + exec "from ctypes import *" in self._env + + def Enumeration(self, enum): + # generate the ctypes code for an enumeration. + code = ["class %s(c_int):" % enum.name] + code += [" %s = %s" % pair for pair in enum.values] + code = "\n".join(code) + exec code in self._env + print code + + def Typedef(self, td): + if type(td.typ) is gccxmltools.Structure and td.typ.isClass(): + return + code = "%s = %s" % (td.name, self.ctypes_name(td.typ)) + try: + exec code in self._env + except Exception: + print "#", code + else: + print code + + def Structure(self, struct): + if struct.name.startswith("$_"): + return + code = ["class %s(Structure):" % struct.name] + code += [" _fields_ = []"] + try: + exec "\n".join(code) in self._env + except Exception: + print "#", code[0] + else: + print "\n".join(code) + + Union = Structure + + def ctypes_name(self, obj): + if type(obj) is gccxmltools.FundamentalType: + return ctypes_names[obj.name] + elif type(obj) is gccxmltools.FunctionType: + return "c_void_p" # fixme + elif type(obj) is gccxmltools.PointerType: + name = self.ctypes_name(obj.typ) + if name == "void": + return "c_void_p" + return "POINTER(%s)" % name + elif type(obj) is gccxmltools.CvQualifiedType: + return self.ctypes_name(obj.typ) + elif type(obj) is gccxmltools.ArrayType: + assert obj.min == 0 + return "%s * %d" % (self.ctypes_name(obj.typ), obj.max + 1) + elif type(obj) in (gccxmltools.Typedef, gccxmltools.Enumeration, gccxmltools.Structure, gccxmltools.Union): + return obj.name + raise TypeError, type(obj) + + ################################################################ if __name__ == "__main__": import sys if len(sys.argv) == 1: ! sys.argv.extend("-D NONAMELESSUNION -D _WIN32_WINNT=0x500 -c msvc6 -o- windef.h".split()) ! ## sys.argv.extend("-D NONAMELESSUNION -D _WIN32_WINNT=0x500 -c msvc6 -o- windows.h".split()) result = gccxmltools.main() p = DependencyResolver(result) ! result, remaining = p.run() ! ! print "from ctypes import *" ! print ! ! cg = CodeGenerator(result) ! cg.go() ! ! for o in remaining: ! print "#", o |