Skip to content

Commit

Permalink
Merge branch 'main' into fix_525_attempted_to_reuse_key_and_more
Browse files Browse the repository at this point in the history
  • Loading branch information
junkmd committed Oct 5, 2024
2 parents 3795c56 + fa08187 commit e00b945
Show file tree
Hide file tree
Showing 28 changed files with 199 additions and 188 deletions.
28 changes: 6 additions & 22 deletions .github/workflows/autofmt.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,9 @@ jobs:
uses: actions/setup-python@v5
with:
python-version: 3.8
- name: Install black
run: pip install black==22.12.0
# PRs from the forked repo will trigger format-checking only.
# Auto-commit to the branch from a forked repo will fail without
# any access tokens or permissions.
# So formatting and auto-commit will be triggered by PRs from the
# base repo only.
- if: github.repository != github.event.pull_request.head.repo.full_name
name: Check style
run: python -m black comtypes/. --check --diff --color
- if: github.repository == github.event.pull_request.head.repo.full_name
name: Format
run: python -m black comtypes/.
- if: github.repository == github.event.pull_request.head.repo.full_name
name: Auto-commit
uses: stefanzweifel/git-auto-commit-action@v4
with:
branch: ${{ github.head_ref }}
commit_message: apply automatic formatter
commit_user_name: github-actions[bot]
commit_user_email: 41898282+github-actions[bot]@users.noreply.github.com
commit_author: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
- name: Install ruff
run: pip install ruff==0.6.9
- name: Check format
run: python -m ruff format comtypes/. --check --diff
- name: Check lint
run: python -m ruff check --output-format=github comtypes/.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

![Works on Windows only](https://img.shields.io/badge/-Windows-0078D6.svg?logo=windows&style=flat)
[![PyPI version](https://badge.fury.io/py/comtypes.svg)](https://pypi.org/project/comtypes/) [![PyPI - Python Version](https://img.shields.io/pypi/pyversions/comtypes)](https://pypi.org/project/comtypes/) [![PyPI - License](https://img.shields.io/pypi/l/comtypes)](https://pypi.org/project/comtypes/) [![PyPI - Downloads](https://img.shields.io/pypi/dm/comtypes)](https://pypi.org/project/comtypes/)
[![GitHub Repo stars](https://img.shields.io/github/stars/enthought/comtypes?style=social)](https://github.com/enthought/comtypes/stargazers) [![GitHub forks](https://img.shields.io/github/forks/enthought/comtypes?style=social)](https://github.com/enthought/comtypes/network/members)
[![GitHub Repo stars](https://img.shields.io/github/stars/enthought/comtypes?style=social)](https://github.com/enthought/comtypes/stargazers) [![GitHub forks](https://img.shields.io/github/forks/enthought/comtypes?style=social)](https://github.com/enthought/comtypes/network/members) [![Ruff](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/v2.json)](https://github.com/astral-sh/ruff)
[![Tidelift Subscription](https://tidelift.com/badges/package/pypi/comtypes)](https://tidelift.com/subscription/pkg/pypi-comtypes?utm_source=pypi-comtypes&utm_medium=readme)


Expand Down
1 change: 0 additions & 1 deletion comtypes/_comobject.py
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,6 @@ def _InterlockedDecrement(ob):


class LocalServer(object):

_queue = None

def run(self, classobjects):
Expand Down
2 changes: 1 addition & 1 deletion comtypes/_memberspec.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def _unpack_argspec(


def _resolve_argspec(
items: Tuple[_ArgSpecElmType, ...]
items: Tuple[_ArgSpecElmType, ...],
) -> Tuple[Tuple[_ParamFlagType, ...], Tuple[Type[_CData], ...]]:
"""Unpacks and converts from argspec to paramflags and argtypes.
Expand Down
16 changes: 8 additions & 8 deletions comtypes/_meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,26 +43,26 @@ class _coclass_meta(type):
# will QueryInterface for the default interface: the first one on
# the coclass' _com_interfaces_ list.
def __new__(cls, name, bases, namespace):
klass = type.__new__(cls, name, bases, namespace)
self = type.__new__(cls, name, bases, namespace)
if bases == (object,):
return klass
return self
# XXX We should insist that a _reg_clsid_ is present.
if "_reg_clsid_" in namespace:
clsid = namespace["_reg_clsid_"]
comtypes.com_coclass_registry[str(clsid)] = klass
comtypes.com_coclass_registry[str(clsid)] = self
PTR = _coclass_pointer_meta(
"POINTER(%s)" % klass.__name__,
(klass, c_void_p),
f"POINTER({self.__name__})",
(self, c_void_p),
{
"__ctypes_from_outparam__": _wrap_coclass,
"from_param": classmethod(_coclass_from_param),
},
)
from ctypes import _pointer_type_cache
from ctypes import _pointer_type_cache # type: ignore

_pointer_type_cache[klass] = PTR
_pointer_type_cache[self] = PTR

return klass
return self


# will not work if we change the order of the two base classes!
Expand Down
3 changes: 2 additions & 1 deletion comtypes/_npsupport.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
""" Consolidation of numpy support utilities. """
"""Consolidation of numpy support utilities."""

import sys

is_64bits = sys.maxsize > 2**32
Expand Down
1 change: 1 addition & 0 deletions comtypes/_post_coinit/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@
So it is necessary to maintain minimal settings to keep the lightweight action
when the package is initialized.
"""

from comtypes._post_coinit.unknwn import _shutdown # noqa
59 changes: 20 additions & 39 deletions comtypes/_post_coinit/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,15 +88,9 @@ def QueryService(


@overload
def CoGetObject(displayname: str, interface: None) -> IUnknown:
...


def CoGetObject(displayname: str, interface: None) -> IUnknown: ...
@overload
def CoGetObject(displayname: str, interface: Type[_T_IUnknown]) -> _T_IUnknown:
...


def CoGetObject(displayname: str, interface: Type[_T_IUnknown]) -> _T_IUnknown: ...
def CoGetObject(displayname: str, interface: Optional[Type[IUnknown]]) -> IUnknown:
"""Convert a displayname to a moniker, then bind and return the object
identified by the moniker."""
Expand All @@ -117,20 +111,14 @@ def CoCreateInstance(
interface: None = None,
clsctx: Optional[int] = None,
punkouter: Optional[_pUnkOuter] = None,
) -> IUnknown:
...


) -> IUnknown: ...
@overload
def CoCreateInstance(
clsid: GUID,
interface: Type[_T_IUnknown],
clsctx: Optional[int] = None,
punkouter: Optional[_pUnkOuter] = None,
) -> _T_IUnknown:
...


) -> _T_IUnknown: ...
def CoCreateInstance(
clsid: GUID,
interface: Optional[Type[IUnknown]] = None,
Expand All @@ -153,14 +141,19 @@ def CoCreateInstance(
if TYPE_CHECKING:

@overload
def CoGetClassObject(clsid, clsctx=None, pServerInfo=None, interface=None):
# type: (GUID, Optional[int], Optional[COSERVERINFO], None) -> hints.IClassFactory
pass

def CoGetClassObject(
clsid: GUID,
clsctx: Optional[int] = None,
pServerInfo: "Optional[COSERVERINFO]" = None,
interface: None = None,
) -> hints.IClassFactory: ...
@overload
def CoGetClassObject(clsid, clsctx=None, pServerInfo=None, interface=None):
# type: (GUID, Optional[int], Optional[COSERVERINFO], Type[_T_IUnknown]) -> _T_IUnknown
pass
def CoGetClassObject(
clsid: GUID,
clsctx: Optional[int] = None,
pServerInfo: "Optional[COSERVERINFO]" = None,
interface: Type[_T_IUnknown] = hints.IClassFactory,
) -> _T_IUnknown: ...


def CoGetClassObject(clsid, clsctx=None, pServerInfo=None, interface=None):
Expand All @@ -177,15 +170,9 @@ def CoGetClassObject(clsid, clsctx=None, pServerInfo=None, interface=None):


@overload
def GetActiveObject(clsid: GUID, interface: None = None) -> IUnknown:
...


def GetActiveObject(clsid: GUID, interface: None = None) -> IUnknown: ...
@overload
def GetActiveObject(clsid: GUID, interface: Type[_T_IUnknown]) -> _T_IUnknown:
...


def GetActiveObject(clsid: GUID, interface: Type[_T_IUnknown]) -> _T_IUnknown: ...
def GetActiveObject(
clsid: GUID, interface: Optional[Type[IUnknown]] = None
) -> IUnknown:
Expand Down Expand Up @@ -335,21 +322,15 @@ def CoCreateInstanceEx(
clsctx: Optional[int] = None,
machine: Optional[str] = None,
pServerInfo: Optional[COSERVERINFO] = None,
) -> IUnknown:
...


) -> IUnknown: ...
@overload
def CoCreateInstanceEx(
clsid: GUID,
interface: Type[_T_IUnknown],
clsctx: Optional[int] = None,
machine: Optional[str] = None,
pServerInfo: Optional[COSERVERINFO] = None,
) -> _T_IUnknown:
...


) -> _T_IUnknown: ...
def CoCreateInstanceEx(
clsid: GUID,
interface: Optional[Type[IUnknown]] = None,
Expand Down
33 changes: 17 additions & 16 deletions comtypes/_post_coinit/unknwn.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,12 @@ class _cominterface_meta(type):
def __new__(cls, name, bases, namespace):
methods = namespace.pop("_methods_", None)
dispmethods = namespace.pop("_disp_methods_", None)
new_cls = type.__new__(cls, name, bases, namespace)
self = type.__new__(cls, name, bases, namespace)

if methods is not None:
new_cls._methods_ = methods
self._methods_ = methods
if dispmethods is not None:
new_cls._disp_methods_ = dispmethods
self._disp_methods_ = dispmethods

# If we sublass a COM interface, for example:
#
Expand All @@ -85,26 +85,26 @@ def __new__(cls, name, bases, namespace):
# subclass of POINTER(IUnknown) because of the way ctypes
# typechecks work.
if bases == (object,):
_ptr_bases = (new_cls, _compointer_base)
_ptr_bases = (self, _compointer_base)
else:
_ptr_bases = (new_cls, POINTER(bases[0]))
_ptr_bases = (self, POINTER(bases[0]))

# The interface 'new_cls' is used as a mixin.
# The interface 'self' is used as a mixin.
p = type(_compointer_base)(
"POINTER(%s)" % new_cls.__name__,
f"POINTER({self.__name__})",
_ptr_bases,
{"__com_interface__": new_cls, "_needs_com_addref_": None},
{"__com_interface__": self, "_needs_com_addref_": None},
)

from ctypes import _pointer_type_cache # type: ignore

_pointer_type_cache[new_cls] = p
_pointer_type_cache[self] = p

if new_cls._case_insensitive_:
new_cls._patch_case_insensitive_to_ptr_type(p)
new_cls._patch_reference_fix_to_ptrptr_type(p)
if self._case_insensitive_:
self._patch_case_insensitive_to_ptr_type(p)
self._patch_reference_fix_to_ptrptr_type(POINTER(p)) # type: ignore

return new_cls
return self

@staticmethod
def _patch_case_insensitive_to_ptr_type(p: Type) -> None:
Expand Down Expand Up @@ -134,8 +134,8 @@ def __setattr__(self, name, value):
)

@staticmethod
def _patch_reference_fix_to_ptrptr_type(p: Type) -> None:
@patcher.Patch(POINTER(p))
def _patch_reference_fix_to_ptrptr_type(pp: Type) -> None:
@patcher.Patch(pp)
class ReferenceFix(object):
def __setitem__(self, index, value):
# We override the __setitem__ method of the
Expand All @@ -157,7 +157,7 @@ def __setitem__(self, index, value):
# CopyComPointer should do if index != 0.
if bool(value):
value.AddRef()
super(POINTER(p), self).__setitem__(index, value) # type: ignore
super(pp, self).__setitem__(index, value) # type: ignore
return
from _ctypes import CopyComPointer

Expand Down Expand Up @@ -371,6 +371,7 @@ def _make_methods(self, methods: List[_ComMemberSpec]) -> None:

class _compointer_meta(type(c_void_p), _cominterface_meta):
"metaclass for COM interface pointer classes"

# no functionality, but needed to avoid a metaclass conflict


Expand Down
52 changes: 21 additions & 31 deletions comtypes/client/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,22 +138,17 @@ def GetBestInterface(punk: Any) -> Any:
# Should we do this for POINTER(IUnknown) also?
ctypes.POINTER(automation.IDispatch).__ctypes_from_outparam__ = wrap_outparam # type: ignore


################################################################
#
# Object creation
#
@overload
def GetActiveObject(progid: _UnionT[str, CoClass, GUID]) -> Any:
...


def GetActiveObject(progid: _UnionT[str, CoClass, GUID]) -> Any: ...
@overload
def GetActiveObject(
progid: _UnionT[str, CoClass, GUID], interface: Type[_T_IUnknown]
) -> _T_IUnknown:
...


) -> _T_IUnknown: ...
def GetActiveObject(
progid: _UnionT[str, CoClass, GUID],
interface: Optional[Type[IUnknown]] = None,
Expand Down Expand Up @@ -193,14 +188,19 @@ def _manage(
if TYPE_CHECKING:

@overload
def GetClassObject(progid, clsctx=None, pServerInfo=None):
# type: (_UnionT[str, CoClass, GUID], Optional[int], Optional[comtypes.COSERVERINFO]) -> hints.IClassFactory
pass

def GetClassObject(
progid: _UnionT[str, CoClass, GUID],
clsctx: Optional[int] = None,
pServerInfo: Optional[comtypes.COSERVERINFO] = None,
interface: None = None,
) -> hints.IClassFactory: ...
@overload
def GetClassObject(progid, clsctx=None, pServerInfo=None, interface=None):
# type: (_UnionT[str, CoClass, GUID], Optional[int], Optional[comtypes.COSERVERINFO], Optional[Type[_T_IUnknown]]) -> _T_IUnknown
pass
def GetClassObject(
progid: _UnionT[str, CoClass, GUID],
clsctx: Optional[int] = None,
pServerInfo: Optional[comtypes.COSERVERINFO] = None,
interface: Type[_T_IUnknown] = hints.IClassFactory,
) -> _T_IUnknown: ...


def GetClassObject(progid, clsctx=None, pServerInfo=None, interface=None):
Expand All @@ -216,10 +216,7 @@ def GetClassObject(progid, clsctx=None, pServerInfo=None, interface=None):


@overload
def CreateObject(progid: _UnionT[str, Type[CoClass], GUID]) -> Any:
...


def CreateObject(progid: _UnionT[str, Type[CoClass], GUID]) -> Any: ...
@overload
def CreateObject(
progid: _UnionT[str, Type[CoClass], GUID],
Expand All @@ -228,10 +225,7 @@ def CreateObject(
interface: Optional[Type[_T_IUnknown]] = None,
dynamic: bool = ...,
pServerInfo: Optional[comtypes.COSERVERINFO] = None,
) -> _T_IUnknown:
...


) -> _T_IUnknown: ...
def CreateObject(
progid: _UnionT[str, Type[CoClass], GUID], # which object to create
clsctx: Optional[int] = None, # how to create the object
Expand Down Expand Up @@ -297,15 +291,11 @@ def CreateObject(


@overload
def CoGetObject(displayname: str, interface: Type[_T_IUnknown]) -> _T_IUnknown:
...


def CoGetObject(displayname: str, interface: Type[_T_IUnknown]) -> _T_IUnknown: ...
@overload
def CoGetObject(displayname: str, interface: None = None, dynamic: bool = False) -> Any:
...


def CoGetObject(
displayname: str, interface: None = None, dynamic: bool = False
) -> Any: ...
def CoGetObject(
displayname: str,
interface: Optional[Type[comtypes.IUnknown]] = None,
Expand Down
Loading

0 comments on commit e00b945

Please sign in to comment.