Skip to content

Commit

Permalink
Determine whether a disp interface is one of the known symbols not on…
Browse files Browse the repository at this point in the history
…ly by its name but also by using its iid (#534)
  • Loading branch information
junkmd authored Apr 30, 2024
1 parent b046b46 commit efd900d
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 14 deletions.
2 changes: 2 additions & 0 deletions comtypes/hints.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ else:
from typing_extensions import Protocol
if sys.version_info >= (3, 10):
from typing import Concatenate, ParamSpec, TypeAlias
from typing import TypeGuard as TypeGuard
else:
from typing_extensions import Concatenate, ParamSpec, TypeAlias
from typing_extensions import TypeGuard as TypeGuard
if sys.version_info >= (3, 11):
from typing import Self
else:
Expand Down
72 changes: 59 additions & 13 deletions comtypes/tools/codegenerator.py
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,16 @@ def _get_common_elms(self) -> Tuple[List[_IdlFlagType], str, str]:
return (idlflags, type_name, self._m.name)


_InterfaceTypeDesc = _UnionT[
typedesc.ComInterface,
typedesc.ComInterfaceHead,
typedesc.ComInterfaceBody,
typedesc.DispInterface,
typedesc.DispInterfaceHead,
typedesc.DispInterfaceBody,
]


class CodeGenerator(object):
def __init__(self, known_symbols=None, known_interfaces=None) -> None:
self.stream = io.StringIO()
Expand All @@ -441,13 +451,8 @@ def __init__(self, known_symbols=None, known_interfaces=None) -> None:
def generate(self, item):
if item in self.done:
return
if isinstance(item, typedesc.ComInterface):
if self._is_known_interface(item):
self.imports.add(item.name, symbols=self.known_symbols)
self.done.add(item)
return
self.done.add(item) # to avoid infinite recursion.
self.ComInterface(item)
if self._is_interface_typedesc(item):
self._define_interface(item)
return
if isinstance(item, typedesc.StructureHead):
name = getattr(item.struct, "name", None)
Expand Down Expand Up @@ -1080,12 +1085,56 @@ def CoClass(self, coclass: typedesc.CoClass) -> None:

self.names.add(coclass.name)

def _is_interface_typedesc(
self, item: Any
) -> "comtypes.hints.TypeGuard[_InterfaceTypeDesc]":
return isinstance(
item,
(
typedesc.ComInterface,
typedesc.ComInterfaceHead,
typedesc.ComInterfaceBody,
typedesc.DispInterface,
typedesc.DispInterfaceHead,
typedesc.DispInterfaceBody,
),
)

def _define_interface(self, item: _InterfaceTypeDesc) -> None:
if isinstance(
item,
(
typedesc.ComInterfaceHead,
typedesc.ComInterfaceBody,
typedesc.DispInterfaceHead,
typedesc.DispInterfaceBody,
),
):
if self._is_known_interface(item.itf):
self.imports.add(item.itf.name, symbols=self.known_symbols)
self.done.add(item)
return
elif isinstance(item, (typedesc.ComInterface, typedesc.DispInterface)):
if self._is_known_interface(item):
self.imports.add(item.name, symbols=self.known_symbols)
self.done.add(item)
self.done.add(item.get_head())
self.done.add(item.get_body())
return
else:
raise TypeError
self.done.add(item) # to avoid infinite recursion.
mth = getattr(self, type(item).__name__)
mth(item)

def ComInterface(self, itf: typedesc.ComInterface) -> None:
self.generate(itf.get_head())
self.generate(itf.get_body())
self.names.add(itf.name)

def _is_known_interface(self, item: typedesc.ComInterface) -> bool:
def _is_known_interface(
self, item: _UnionT[typedesc.ComInterface, typedesc.DispInterface]
) -> bool:
"""Returns whether an interface is statically defined in `comtypes`,
based on its name and iid.
"""
Expand All @@ -1104,14 +1153,11 @@ def _is_enuminterface(self, itf: typedesc.ComInterface) -> bool:
return True

def ComInterfaceHead(self, head: typedesc.ComInterfaceHead) -> None:
if self._is_known_interface(head.itf):
return
base = head.itf.base
if head.itf.base is None:
# we don't beed to generate IUnknown
return
self.generate(base.get_head())
self.more.add(base)
self.generate(head.itf.base.get_head())
self.more.add(head.itf.base)
basename = self._to_type_name(head.itf.base)

self.imports.add("comtypes", "GUID")
Expand Down
2 changes: 1 addition & 1 deletion comtypes/tools/typedesc.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ def __init__(
self,
name: str,
members: List[ComMethod],
base: Any,
base: "Optional[ComInterface]",
iid: str,
idlflags: List[str],
) -> None:
Expand Down

0 comments on commit efd900d

Please sign in to comment.