[ctypes-commit] ctypes/sandbox/tools/codegen codegenerator.py,1.43,1.44
Brought to you by:
theller
From: Thomas H. <th...@us...> - 2005-01-31 09:47:49
|
Update of /cvsroot/ctypes/ctypes/sandbox/tools/codegen In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv19101 Modified Files: codegenerator.py Log Message: Using 'from ctypes import *' in the generated code again. Index: codegenerator.py =================================================================== RCS file: /cvsroot/ctypes/ctypes/sandbox/tools/codegen/codegenerator.py,v retrieving revision 1.43 retrieving revision 1.44 diff -C2 -d -r1.43 -r1.44 *** codegenerator.py 28 Jan 2005 09:48:20 -0000 1.43 --- codegenerator.py 31 Jan 2005 09:47:39 -0000 1.44 *************** *** 13,37 **** # XXX Should this be in ctypes itself? ctypes_names = { ! "unsigned char": "C.c_ubyte", ! "signed char": "C.c_byte", ! "char": "C.c_char", ! "wchar_t": "C.c_wchar", ! "short unsigned int": "C.c_ushort", ! "short int": "C.c_short", ! "long unsigned int": "C.c_ulong", ! "long int": "C.c_long", ! "long signed int": "C.c_long", ! "unsigned int": "C.c_uint", ! "int": "C.c_int", ! "long long unsigned int": "C.c_ulonglong", ! "long long int": "C.c_longlong", ! "double": "C.c_double", ! "float": "C.c_float", # Hm... --- 13,37 ---- # XXX Should this be in ctypes itself? ctypes_names = { ! "unsigned char": "c_ubyte", ! "signed char": "c_byte", ! "char": "c_char", ! "wchar_t": "c_wchar", ! "short unsigned int": "c_ushort", ! "short int": "c_short", ! "long unsigned int": "c_ulong", ! "long int": "c_long", ! "long signed int": "c_long", ! "unsigned int": "c_uint", ! "int": "c_int", ! "long long unsigned int": "c_ulonglong", ! "long long int": "c_longlong", ! "double": "c_double", ! "float": "c_float", # Hm... *************** *** 118,123 **** def get_real_type(tp): # why was this? ! if type(tp) is typedesc.Typedef: ! return get_real_type(tp.typ) return tp --- 118,123 ---- def get_real_type(tp): # why was this? ! ## if type(tp) is typedesc.Typedef: ! ## return get_real_type(tp.typ) return tp *************** *** 139,149 **** def init_value(self, t, init): tn = self.type_name(t, False) ! if tn in ["C.c_ulonglong", "C.c_ulong", "C.c_uint", "C.c_ushort", "C.c_ubyte"]: return decode_value(init) ! elif tn in ["C.c_longlong", "C.c_long", "C.c_int", "C.c_short", "C.c_byte"]: return decode_value(init) ! elif tn in ["C.c_float", "C.c_double"]: return float(init) ! elif tn == "C.POINTER(C.c_char)": if init[0] == '"': value = eval(init) --- 139,149 ---- def init_value(self, t, init): tn = self.type_name(t, False) ! if tn in ["c_ulonglong", "c_ulong", "c_uint", "c_ushort", "c_ubyte"]: return decode_value(init) ! elif tn in ["c_longlong", "c_long", "c_int", "c_short", "c_byte"]: return decode_value(init) ! elif tn in ["c_float", "c_double"]: return float(init) ! elif tn == "POINTER(c_char)": if init[0] == '"': value = eval(init) *************** *** 151,155 **** value = int(init, 16) return value ! elif tn == "C.POINTER(C.c_wchar)": if init[0] == '"': value = eval(init) --- 151,155 ---- value = int(init, 16) return value ! elif tn == "POINTER(c_wchar)": if init[0] == '"': value = eval(init) *************** *** 160,164 **** value = value.decode("utf-16") # XXX Is this correct? return value ! elif tn == "C.c_void_p": if init[0] == "0": value = int(init, 16) --- 160,164 ---- value = value.decode("utf-16") # XXX Is this correct? return value ! elif tn == "c_void_p": if init[0] == "0": value = int(init, 16) *************** *** 167,178 **** # Hm, ctypes represents them as SIGNED int return value ! elif tn == "C.c_char": return decode_value(init) ! elif tn == "C.c_wchar": value = decode_value(init) if isinstance(value, int): return unichr(value) return value ! elif tn.startswith("C.POINTER("): # Hm, POINTER(HBITMAP__) for example return decode_value(init) --- 167,178 ---- # Hm, ctypes represents them as SIGNED int return value ! elif tn == "c_char": return decode_value(init) ! elif tn == "c_wchar": value = decode_value(init) if isinstance(value, int): return unichr(value) return value ! elif tn.startswith("POINTER("): # Hm, POINTER(HBITMAP__) for example return decode_value(init) *************** *** 182,195 **** def type_name(self, t, generate=True): # Return a string, containing an expression which can be used to ! # refer to the type. Assumes the C.* namespace is available. if isinstance(t, typedesc.PointerType): ! result = "C.POINTER(%s)" % self.type_name(t.typ, generate) # XXX Better to inspect t.typ! ! if result.startswith("C.POINTER(C.WINFUNCTYPE"): ! return result[len("C.POINTER("):-1] ! if result.startswith("C.POINTER(C.CFUNCTYPE"): ! return result[len("C.POINTER("):-1] ! elif result == "C.POINTER(None)": ! return "C.c_void_p" return result elif isinstance(t, typedesc.ArrayType): --- 182,195 ---- def type_name(self, t, generate=True): # Return a string, containing an expression which can be used to ! # refer to the type. Assumes the * namespace is available. if isinstance(t, typedesc.PointerType): ! result = "POINTER(%s)" % self.type_name(t.typ, generate) # XXX Better to inspect t.typ! ! if result.startswith("POINTER(WINFUNCTYPE"): ! return result[len("POINTER("):-1] ! if result.startswith("POINTER(CFUNCTYPE"): ! return result[len("POINTER("):-1] ! elif result == "POINTER(None)": ! return "c_void_p" return result elif isinstance(t, typedesc.ArrayType): *************** *** 198,204 **** args = [self.type_name(x, generate) for x in [t.returns] + t.arguments] if "__stdcall__" in t.attributes: ! return "C.WINFUNCTYPE(%s)" % ", ".join(args) else: ! return "C.CFUNCTYPE(%s)" % ", ".join(args) elif isinstance(t, typedesc.CvQualifiedType): # const and volatile are ignored --- 198,204 ---- args = [self.type_name(x, generate) for x in [t.returns] + t.arguments] if "__stdcall__" in t.attributes: ! return "WINFUNCTYPE(%s)" % ", ".join(args) else: ! return "CFUNCTYPE(%s)" % ", ".join(args) elif isinstance(t, typedesc.CvQualifiedType): # const and volatile are ignored *************** *** 211,215 **** if t.name: return t.name ! return "C.c_int" # enums are integers elif isinstance(t, typedesc.Typedef): return t.name --- 211,215 ---- if t.name: return t.name ! return "c_int" # enums are integers elif isinstance(t, typedesc.Typedef): return t.name *************** *** 257,263 **** print >> self.stream, "class %s(_com_interface):" % head.struct.name elif type(head.struct) == typedesc.Structure: ! print >> self.stream, "class %s(C.Structure):" % head.struct.name elif type(head.struct) == typedesc.Union: ! print >> self.stream, "class %s(C.Union):" % head.struct.name if head.struct.location: print >> self.stream, " # %s %s" % head.struct.location --- 257,263 ---- print >> self.stream, "class %s(_com_interface):" % head.struct.name elif type(head.struct) == typedesc.Structure: ! print >> self.stream, "class %s(Structure):" % head.struct.name elif type(head.struct) == typedesc.Union: ! print >> self.stream, "class %s(Union):" % head.struct.name if head.struct.location: print >> self.stream, " # %s %s" % head.struct.location *************** *** 348,352 **** if tp.name: print >> self.stream ! print >> self.stream, "%s = C.c_int # enum" % tp.name for item in tp.values: self.generate(item) --- 348,352 ---- if tp.name: print >> self.stream ! print >> self.stream, "%s = c_int # enum" % tp.name for item in tp.values: self.generate(item) *************** *** 410,417 **** if body.struct.size and body.struct.name not in dont_assert_size: size = body.struct.size // 8 ! print >> self.stream, "assert C.sizeof(%s) == %s, C.sizeof(%s)" % \ (body.struct.name, size, body.struct.name) align = body.struct.align // 8 ! print >> self.stream, "assert C.alignment(%s) == %s, C.alignment(%s)" % \ (body.struct.name, align, body.struct.name) --- 410,417 ---- if body.struct.size and body.struct.name not in dont_assert_size: size = body.struct.size // 8 ! print >> self.stream, "assert sizeof(%s) == %s, sizeof(%s)" % \ (body.struct.name, size, body.struct.name) align = body.struct.align // 8 ! print >> self.stream, "assert alignment(%s) == %s, alignment(%s)" % \ (body.struct.name, align, body.struct.name) *************** *** 429,433 **** for m in methods: args = [self.type_name(a) for a in m.arguments] ! print >> self.stream, " C.STDMETHOD(%s, '%s', [%s])," % ( self.type_name(m.returns), m.name, --- 429,433 ---- for m in methods: args = [self.type_name(a) for a in m.arguments] ! print >> self.stream, " STDMETHOD(%s, '%s', [%s])," % ( self.type_name(m.returns), m.name, *************** *** 463,467 **** name, ext = os.path.splitext(basename) self._loadedlibs[dllname] = name ! print >> self.stream, "%s = C.CDLL(%r)" % (name, dllname) return name --- 463,467 ---- name, ext = os.path.splitext(basename) self._loadedlibs[dllname] = name ! print >> self.stream, "%s = CDLL(%r)" % (name, dllname) return name *************** *** 540,544 **** def generate_code(self, items, known_symbols, searched_dlls): ! print >> self.stream, "import ctypes as C" items = set(items) if known_symbols: --- 540,544 ---- def generate_code(self, items, known_symbols, searched_dlls): ! print >> self.stream, "from ctypes import *" items = set(items) if known_symbols: |