[ctypes-commit] ctypes/sandbox/tools/structures genapi.py,1.2,1.3
Brought to you by:
theller
|
From: Thomas H. <th...@us...> - 2004-09-10 09:49:03
|
Update of /cvsroot/ctypes/ctypes/sandbox/tools/structures In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv24677 Modified Files: genapi.py Log Message: Works - although there are still bugs and limitations. Index: genapi.py =================================================================== RCS file: /cvsroot/ctypes/ctypes/sandbox/tools/structures/genapi.py,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** genapi.py 10 Sep 2004 08:47:18 -0000 1.2 --- genapi.py 10 Sep 2004 09:48:53 -0000 1.3 *************** *** 1,2 **** --- 1,17 ---- + # Create ctypes python wrapper for everything in windows.h. + # '#define' preprocessor statements are *not* handled - this will be a + # separate effort. + # + # Bugs: + # Structure packing is wrong (gccxml doesn't handle the #pragma statements) + # + # COM interfaces are not included, so structures and unions containing + # pointers to COM interfaces will not be generated (VARIANT, for example) + # + # enums are generated as subclasses of c_int, with the enum values as + # class variables. This should probably change. + # + # Unnamed structure fields will probably not work correctly. + # import gccxmltools from sets import Set *************** *** 79,82 **** --- 94,100 ---- exec "from ctypes import *" in self._env + def PointerType(self, ptr): + pass + def Enumeration(self, enum): # generate the ctypes code for an enumeration. *************** *** 91,115 **** 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: --- 109,161 ---- return code = "%s = %s" % (td.name, self.ctypes_name(td.typ)) ! if self.try_code(code): print code + else: + print "# failed", code def Structure(self, struct): + code = self.gen_structure(struct) if struct.name.startswith("$_"): return ! if self.try_code("\n".join(code)): print "\n".join(code) + else: + print "# failed " + "\n# ".join(code) Union = Structure + def gen_structure(self, struct, indent=""): + # create inner classes + base = struct.__class__.__name__ + inner = [] + name = struct.name + if name.startswith("$_"): + name = "_inner_" + name[2:] + for field in struct.members: + if type(field.typ) in (gccxmltools.Structure, gccxmltools.Union) \ + and field.typ.name.startswith("$_"): + inner.append(field.typ) + code = [indent + "class %s(%s):" % (name, base)] + for i in inner: + code += self.gen_structure(i, indent = indent + " ") + code += [indent + " _fields_ = ["] + code += [indent + " ('%s', %s)," % self.gen_member(f) for f in struct.members] + code += [indent + " ]"] + return code + + def gen_member(self, field): + name = field.name + if name.startswith("$_"): + name = "_inner_" + name[2:] + return name, self.ctypes_name(field.typ) + + def try_code(self, code): + # code it either a string or a sequence of strings + try: + exec code in self._env + except Exception: + return False + return True + def ctypes_name(self, obj): if type(obj) is gccxmltools.FundamentalType: *************** *** 128,132 **** 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) --- 174,181 ---- return "%s * %d" % (self.ctypes_name(obj.typ), obj.max + 1) elif type(obj) in (gccxmltools.Typedef, gccxmltools.Enumeration, gccxmltools.Structure, gccxmltools.Union): ! name = obj.name ! if obj.name.startswith("$_"): ! name = "_inner_" + name[2:] ! return name raise TypeError, type(obj) *************** *** 136,141 **** 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() --- 185,190 ---- 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() |