Skip to content

Commit

Permalink
Extract the methods for parsing externals and dual interfaces from `P…
Browse files Browse the repository at this point in the history
…arser.parse_typeinfo`. (#620)

* Add `Parser._parse_External`
and replace the previous internal process with it.

* Add `Parser._parse_DualInterface`
and replace the previous internal process with it.

* To f-string.
  • Loading branch information
junkmd authored Sep 21, 2024
1 parent 8dd456d commit 0ef1d74
Showing 1 changed file with 21 additions and 13 deletions.
34 changes: 21 additions & 13 deletions comtypes/tools/tlbparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -605,20 +605,13 @@ def parse_typeinfo(self, tinfo: typeinfo.ITypeInfo) -> Any:
name = tinfo.GetDocumentation(-1)[0]
modname = self._typelib_module()
try:
return self.items["%s.%s" % (modname, name)]
return self.items[f"{modname}.{name}"]
except KeyError:
pass

tlib = tinfo.GetContainingTypeLib()[0]
if tlib != self.tlib:
ta = tinfo.GetTypeAttr()
size = ta.cbSizeInstance * 8
align = ta.cbAlignment * 8
typ = typedesc.External(
tlib, name, size, align, tlib.GetDocumentation(-1)[:2]
)
self._register(name, typ, tlib)
return typ
return self._parse_External(name, tlib, tinfo)

ta = tinfo.GetTypeAttr()
tkind = ta.typekind
Expand All @@ -639,10 +632,7 @@ def parse_typeinfo(self, tinfo: typeinfo.ITypeInfo) -> Any:
except COMError:
# no dual interface
return self.ParseDispatch(tinfo, ta)
tinfo = tinfo.GetRefTypeInfo(href)
ta = tinfo.GetTypeAttr()
assert ta.typekind == typeinfo.TKIND_INTERFACE
return self.ParseInterface(tinfo, ta)
return self._parse_DualInterface(tinfo.GetRefTypeInfo(href))
elif tkind == typeinfo.TKIND_COCLASS: # 5
return self.ParseCoClass(tinfo, ta)
elif tkind == typeinfo.TKIND_ALIAS: # 6
Expand All @@ -653,6 +643,24 @@ def parse_typeinfo(self, tinfo: typeinfo.ITypeInfo) -> Any:
print("NYI", tkind)
# raise "NYI", tkind

def _parse_DualInterface(
self, tinfo: typeinfo.ITypeInfo
) -> Optional[typedesc.ComInterface]:
ta = tinfo.GetTypeAttr()
assert ta.typekind == typeinfo.TKIND_INTERFACE
return self.ParseInterface(tinfo, ta)

def _parse_External(
self, name: str, tlib: typeinfo.ITypeLib, tinfo: typeinfo.ITypeInfo
) -> typedesc.External:
ta = tinfo.GetTypeAttr()
size = ta.cbSizeInstance * 8
align = ta.cbAlignment * 8
docs = tlib.GetDocumentation(-1)[:2]
typ = typedesc.External(tlib, name, size, align, docs)
self._register(name, typ, tlib)
return typ

def parse_LibraryDescription(self):
la = self.tlib.GetLibAttr()
name, doc = self.tlib.GetDocumentation(-1)[:2]
Expand Down

0 comments on commit 0ef1d74

Please sign in to comment.