[ctypes-commit] ctypes/codegen/ctypes_codegen codegenerator.py, 1.3, 1.4
Brought to you by:
theller
From: Thomas H. <th...@us...> - 2006-06-09 17:35:10
|
Update of /cvsroot/ctypes/ctypes/codegen/ctypes_codegen In directory sc8-pr-cvs4.sourceforge.net:/tmp/cvs-serv6996 Modified Files: codegenerator.py Log Message: Added a USE_COMMENTS variable which allows to turn comment generation off. Generated '_anonymous_' declarations for unnamed structure fields. Changed the way libraries are loaded, the cdecl and stdcall decorators are no longer used. Functions are generated with assignments of .restype and .argtypes. Index: codegenerator.py =================================================================== RCS file: /cvsroot/ctypes/ctypes/codegen/ctypes_codegen/codegenerator.py,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** codegenerator.py 8 Jun 2006 20:04:14 -0000 1.3 --- codegenerator.py 9 Jun 2006 17:35:01 -0000 1.4 *************** *** 2,8 **** # Type descriptions are collections of typedesc instances. ! import typedesc, sys import textwrap ASSUME_STRINGS = True --- 2,10 ---- # Type descriptions are collections of typedesc instances. ! import typedesc, sys, os import textwrap + # This should be configurable + USE_COMMENTS = True ASSUME_STRINGS = True *************** *** 288,292 **** self.generate(struct.get_head()) self.more.add(struct) ! if head.struct.location: print >> self.stream, "# %s %s" % head.struct.location basenames = [self.type_name(b) for b in head.struct.bases] --- 290,294 ---- self.generate(struct.get_head()) self.more.add(struct) ! if USE_COMMENTS and head.struct.location: print >> self.stream, "# %s %s" % head.struct.location basenames = [self.type_name(b) for b in head.struct.bases] *************** *** 319,323 **** else: self.generate(tp.typ) ! if self.type_name(tp.typ) in self.known_symbols: stream = self.imports else: --- 321,325 ---- else: self.generate(tp.typ) ! if 0 and self.type_name(tp.typ) in self.known_symbols: stream = self.imports else: *************** *** 450,472 **** for f in fields: self.type_name(f.typ) print >> self.stream, "%s._fields_ = [" % body.struct.name ! if body.struct.location: print >> self.stream, " # %s %s" % body.struct.location ! # unnamed fields will get autogenerated names "_", "_1". "_2", "_3", ... ! unnamed_index = 0 for f in fields: ! if not f.name: ! if unnamed_index: ! fieldname = "_%d" % unnamed_index ! else: ! fieldname = "_" ! unnamed_index += 1 ! print >> self.stream, " # Unnamed field renamed to '%s'" % fieldname ! else: ! fieldname = f.name if f.bits is None: ! print >> self.stream, " ('%s', %s)," % (fieldname, self.type_name(f.typ)) else: ! print >> self.stream, " ('%s', %s, %s)," % (fieldname, self.type_name(f.typ), f.bits) print >> self.stream, "]" # generate assert statements for size and alignment --- 452,477 ---- for f in fields: self.type_name(f.typ) + + # unnamed fields get autogenerated names "_0", "_1". "_2", "_3", ... + unnamed_fields = {} + for f in fields: + if not f.name: + unnamed_fields[f] = "_%d" % len(unnamed_fields) + if unnamed_fields: + print >> self.stream, "%s._anonymous_ = %r" % \ + (body.struct.name, unnamed_fields.values()) print >> self.stream, "%s._fields_ = [" % body.struct.name ! ! if USE_COMMENTS and body.struct.location: print >> self.stream, " # %s %s" % body.struct.location ! index = 0 for f in fields: ! fieldname = unnamed_fields.get(f, f.name) if f.bits is None: ! print >> self.stream, " ('%s', %s)," % \ ! (fieldname, self.type_name(f.typ)) else: ! print >> self.stream, " ('%s', %s, %s)," % \ ! (fieldname, self.type_name(f.typ), f.bits) print >> self.stream, "]" # generate assert statements for size and alignment *************** *** 495,513 **** return None ! _loadedlibs = None ! def get_sharedlib(self, dllname): ! if self._loadedlibs is None: ! self._loadedlibs = {} ! try: ! return self._loadedlibs[dllname] ! except KeyError: ! pass ! import os ! basename = os.path.basename(dllname) ! name, ext = os.path.splitext(basename) ! self._loadedlibs[dllname] = name ! # This should be handled in another way! ! ## print >> self.stream, "%s = CDLL(%r)" % (name, dllname) ! return name _STRING_defined = False --- 500,531 ---- return None ! _c_libraries = None ! def need_CLibraries(self): ! # Create a '_libraries' doctionary in the generated code, if ! # it not yet exists. Will map library pathnames to loaded libs. ! if self._c_libraries is None: ! self._c_libraries = {} ! print >> self.imports, "_libraries = {}" ! ! _stdcall_libraries = None ! def need_WinLibraries(self): ! # Create a '_stdcall_libraries' doctionary in the generated code, if ! # it not yet exists. Will map library pathnames to loaded libs. ! if self._stdcall_libraries is None: ! self._stdcall_libraries = {} ! print >> self.imports, "_stdcall_libraries = {}" ! ! def get_sharedlib(self, dllname, cc): ! if cc == "stdcall": ! self.need_WinLibraries() ! if not dllname in self._stdcall_libraries: ! print >> self.imports, "_stdcall_libraries[%r] = WinDLL(%r)" % (dllname, dllname) ! self._stdcall_libraries[dllname] = None ! return "_stdcall_libraries[%r]" % dllname ! self.need_CLibraries() ! if not dllname in self._c_libraries: ! print >> self.imports, "_libraries[%r] = CDLL(%r)" % (dllname, dllname) ! self._c_libraries[dllname] = None ! return "_libraries[%r]" % dllname _STRING_defined = False *************** *** 537,555 **** else: cc = "cdecl" ! libname = self.get_sharedlib(dllname) ! print >> self.stream ! if self.use_decorators: ! print >> self.stream, "@ %s(%s, '%s', [%s])" % \ ! (cc, self.type_name(func.returns), libname, ", ".join(args)) argnames = [a or "p%d" % (i+1) for i, a in enumerate(func.iterArgNames())] ! # function definition ! print >> self.stream, "def %s(%s):" % (func.name, ", ".join(argnames)) ! if func.location: ! print >> self.stream, " # %s %s" % func.location ! print >> self.stream, " return %s._api_(%s)" % (func.name, ", ".join(argnames)) ! if not self.use_decorators: ! print >> self.stream, "%s = %s(%s, '%s', [%s]) (%s)" % \ ! (func.name, cc, self.type_name(func.returns), libname, ", ".join(args), func.name) ! print >> self.stream self.names.add(func.name) self._functiontypes += 1 --- 555,570 ---- else: cc = "cdecl" ! ! libname = self.get_sharedlib(dllname, cc) ! argnames = [a or "p%d" % (i+1) for i, a in enumerate(func.iterArgNames())] ! ! if USE_COMMENTS and func.location: ! print >> self.stream, "# %s %s" % func.location ! print >> self.stream, "%s = %s.%s" % (func.name, libname, func.name) ! print >> self.stream, "%s.restypes = %s" % (func.name, self.type_name(func.returns)) ! print >> self.stream, "# %s(%s)" % (func.name, ", ".join(argnames)) ! print >> self.stream, "%s.argtypes = [%s]" % (func.name, ", ".join(args)) ! self.names.add(func.name) self._functiontypes += 1 |