Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
if isinstance(m, typedesc.DispMethod):
for a in m.arguments:
self.generate(a[0])
self.generate(m.returns)
elif isinstance(m, typedesc.DispProperty):
self.generate(m.typ)
else:
raise TypeError(m)
self.need_dispid()
self.need_DISPMETHOD()
print >> self.stream, "%s._disp_methods_ = [" % body.itf.name
for m in body.itf.members:
if isinstance(m, typedesc.DispMethod):
self.make_DispMethod(m)
elif isinstance(m, typedesc.DispProperty):
self.make_DispProperty(m)
else:
raise TypeError(m)
print >> self.stream, "]"
def ParseEnum(self, tinfo, ta):
ta = tinfo.GetTypeAttr()
enum_name = tinfo.GetDocumentation(-1)[0]
enum = typedesc.Enumeration(enum_name, 32, 32)
self._register(enum_name, enum)
for i in range(ta.cVars):
vd = tinfo.GetVarDesc(i)
name = tinfo.GetDocumentation(vd.memid)[0]
assert vd.varkind == typeinfo.VAR_CONST
num_val = vd._.lpvarValue[0].value
v = typedesc.EnumValue(name, num_val, enum)
enum.add_value(v)
return enum
def StructureBody(self, body):
fields = []
methods = []
for m in body.struct.members:
if type(m) is typedesc.Field:
fields.append(m)
if type(m.typ) is typedesc.Typedef:
self.generate(get_real_type(m.typ))
self.generate(m.typ)
elif type(m) is typedesc.Method:
methods.append(m)
self.generate(m.returns)
self.generate_all(m.iterArgTypes())
elif type(m) is typedesc.Constructor:
pass
# we don't need _pack_ on Unions (I hope, at least), and not
# on COM interfaces:
if not methods:
try:
pack = calc_packing(body.struct, fields)
def get_real_type(tp):
if type(tp) is typedesc.Typedef:
return get_real_type(tp.typ)
elif isinstance(tp, typedesc.CvQualifiedType):
return get_real_type(tp.typ)
return tp
def make_type(self, tdesc, tinfo):
try:
return COMTYPES[tdesc.vt]
except KeyError:
pass
if tdesc.vt == automation.VT_CARRAY:
typ = self.make_type(tdesc._.lpadesc[0].tdescElem, tinfo)
for i in range(tdesc._.lpadesc[0].cDims):
typ = typedesc.ArrayType(typ,
tdesc._.lpadesc[0].rgbounds[i].lLbound,
tdesc._.lpadesc[0].rgbounds[i].cElements-1)
return typ
elif tdesc.vt == automation.VT_PTR:
typ = self.make_type(tdesc._.lptdesc[0], tinfo)
return PTR(typ)
elif tdesc.vt == automation.VT_USERDEFINED:
try:
ti = tinfo.GetRefTypeInfo(tdesc._.hreftype)
except COMError, details:
type_name = "__error_hreftype_%d__" % tdesc._.hreftype
tlib_name = get_tlib_filename(self.tlib)
if tlib_name is None:
tlib_name = "unknown typelib"
def get_real_type(tp):
if type(tp) is typedesc.Typedef:
return get_real_type(tp.typ)
elif isinstance(tp, typedesc.CvQualifiedType):
return get_real_type(tp.typ)
return tp
def ParseCoClass(self, tinfo, ta):
# possible ta.wTypeFlags: helpstring, helpcontext, licensed,
# version, control, hidden, and appobject
coclass_name, doc = tinfo.GetDocumentation(-1)[0:2]
tlibattr = tinfo.GetContainingTypeLib()[0].GetLibAttr()
coclass = typedesc.CoClass(coclass_name,
str(ta.guid),
self.coclass_type_flags(ta.wTypeFlags),
tlibattr)
if doc is not None:
coclass.doc = doc
self._register(coclass_name, coclass)
for i in range(ta.cImplTypes):
hr = tinfo.GetRefTypeOfImplType(i)
ti = tinfo.GetRefTypeInfo(hr)
itf = self.parse_typeinfo(ti)
flags = tinfo.GetImplTypeFlags(i)
coclass.add_interface(itf, flags)
return coclass
def midlSAFEARRAY(typ):
return typedesc.SAFEARRAYType(typ)
# problems with these, and we can, for comtypes, ignore them.
continue
fd = tinfo.GetFuncDesc(i)
dllname, func_name, ordinal = tinfo.GetDllEntry(fd.memid, fd.invkind)
func_doc = tinfo.GetDocumentation(fd.memid)[1]
assert 0 == fd.cParamsOpt # XXX
returns = self.make_type(fd.elemdescFunc.tdesc, tinfo)
if fd.callconv == typeinfo.CC_CDECL:
attributes = "__cdecl__"
elif fd.callconv == typeinfo.CC_STDCALL:
attributes = "__stdcall__"
else:
raise ValueError("calling convention %d" % fd.callconv)
func = typedesc.Function(func_name, returns, attributes, extern=1)
if func_doc is not None:
func.doc = func_doc.encode("mbcs")
func.dllname = dllname
self._register(func_name, func)
for i in range(fd.cParams):
argtype = self.make_type(fd.lprgelemdescParam[i].tdesc, tinfo)
func.add_argument(argtype)
# constants
for i in range(ta.cVars):
vd = tinfo.GetVarDesc(i)
name, var_doc = tinfo.GetDocumentation(vd.memid)[0:2]
assert vd.varkind == typeinfo.VAR_CONST
typ = self.make_type(vd.elemdescVar.tdesc, tinfo)
var_value = vd._.lpvarValue[0].value
v = typedesc.Constant(name, typ, var_value)
return "c_void_p"
return result
elif isinstance(t, typedesc.ArrayType):
return "%s * %s" % (self.type_name(t.typ, generate), int(t.max)+1)
elif isinstance(t, typedesc.FunctionType):
args = [self.type_name(x, generate) for x in [t.returns] + list(t.iterArgTypes())]
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
return "%s" % self.type_name(t.typ, generate)
elif isinstance(t, typedesc.FundamentalType):
return ctypes_names[t.name]
elif isinstance(t, typedesc.Structure):
return t.name
elif isinstance(t, typedesc.Enumeration):
if t.name:
return t.name
return "c_int" # enums are integers
return t.name