Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/autofmt.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
- name: Set up Python
uses: actions/setup-python@v6
with:
python-version: 3.9
python-version: "3.10"
- name: Install ruff
run: pip install ruff==0.6.9
- name: Check format
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/autotest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
fail-fast: false
matrix:
os: [windows-latest]
python-version: ['3.9', '3.10', '3.11', '3.12', '3.13', '3.14', '3.15']
python-version: ['3.10', '3.11', '3.12', '3.13', '3.14', '3.15']
architecture: ['x86', 'x64']
support: ['with 3rd parties', 'without 3rd parties']
steps:
Expand Down Expand Up @@ -62,7 +62,7 @@ jobs:
strategy:
matrix:
os: [windows-2025, windows-2022]
python-version: ['3.9', '3.10', '3.11', '3.12', '3.13', '3.14', '3.15']
python-version: ['3.10', '3.11', '3.12', '3.13', '3.14', '3.15']
architecture: ['x86', 'x64']
steps:
- uses: actions/checkout@v6
Expand Down
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@

`comtypes` allows you to define, call, and implement custom and dispatch-based COM interfaces in pure Python.

`comtypes` requires Windows and Python 3.9 or later.
`comtypes` requires Windows and Python 3.10 or later.

- Version [1.5.0](https://pypi.org/project/comtypes/1.5.0/) is the last version to support Python 3.9.
- Version <= [1.4.17](https://pypi.org/project/comtypes/1.4.17/) has issues with enum behavior on Python 3.15 as reported in [GH-894](https://github.com/enthought/comtypes/issues/894). Version [1.5.0](https://pypi.org/project/comtypes/1.5.0/) supports Python 3.15.
- Version [1.4.12](https://pypi.org/project/comtypes/1.4.12/) is the last version to support Python 3.8.
- Version <= [1.4.7](https://pypi.org/project/comtypes/1.4.7/) does not work with Python 3.13 as reported in [GH-618](https://github.com/enthought/comtypes/issues/618). Version [1.4.8](https://pypi.org/project/comtypes/1.4.8/) can work with Python 3.13.
- Version [1.4.6](https://pypi.org/project/comtypes/1.4.6/) is the last version to support Python 3.7.
- Version [1.2.1](https://pypi.org/project/comtypes/1.2.1/) is the last version to support Python 2.7 and 3.3–3.6.
- Certain `comtypes` functions may not work correctly in Python 3.8 and 3.9 as reported in [GH-212](https://github.com/enthought/comtypes/issues/212). This bug has been fixed in Python >= 3.10.10 and >= 3.11.2.
- In older releases that support Python 3.8 or 3.9, certain `comtypes` functions may not work correctly as reported in [GH-212](https://github.com/enthought/comtypes/issues/212). This bug has been fixed in Python >= 3.10.10 and >= 3.11.2.

## Installation

Expand Down
6 changes: 3 additions & 3 deletions comtypes/_automation.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
)
from ctypes import Array as _CArrayType
from ctypes.wintypes import DWORD, LONG, UINT, VARIANT_BOOL, WORD
from typing import TYPE_CHECKING, Any, ClassVar, Optional
from typing import TYPE_CHECKING, Any, ClassVar

import comtypes
import comtypes.patcher
Expand Down Expand Up @@ -863,8 +863,8 @@ class tagEXCEPINFO(Structure):
bstrDescription: str
bstrHelpFile: str
dwHelpContext: int
pvReserved: Optional[int]
pfnDeferredFillIn: Optional[int]
pvReserved: int | None
pfnDeferredFillIn: int | None
scode: int

def __repr__(self):
Expand Down
9 changes: 4 additions & 5 deletions comtypes/_comobject.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@
pointer,
)
from ctypes.wintypes import INT, LONG, LPVOID, UINT, ULONG, WORD
from typing import TYPE_CHECKING, Any, ClassVar, Optional, TypeVar
from typing import Union as _UnionT
from typing import TYPE_CHECKING, Any, ClassVar, TypeVar

import comtypes
from comtypes import GUID, IPersist, IUnknown, _CoUninitialize, hresult
Expand Down Expand Up @@ -135,7 +134,7 @@ def _InterlockedDecrement(ob: c_long) -> int:


class LocalServer:
_queue: Optional[queue.Queue] = None
_queue: queue.Queue | None = None

def run(self, classobjects: Sequence["hints.localserver.ClassFactory"]) -> None:
hr = _CoInitialize(None)
Expand Down Expand Up @@ -270,7 +269,7 @@ def _get_method_finder_(self, itf: type[IUnknown]) -> _MethodFinder:

################################################################
# LocalServer / InprocServer stuff
__server__: _UnionT[None, InprocServer, LocalServer] = None
__server__: None | InprocServer | LocalServer = None

@staticmethod
def __run_inprocserver__() -> None:
Expand Down Expand Up @@ -355,7 +354,7 @@ def IUnknown_QueryInterface(
self,
this: Any,
riid: "_Pointer[GUID]",
ppvObj: _UnionT[c_void_p, "_CArgObject"],
ppvObj: "c_void_p | _CArgObject",
_debug=_debug,
) -> int:
# XXX This is probably too slow.
Expand Down
43 changes: 21 additions & 22 deletions comtypes/_memberspec.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import ctypes
from collections.abc import Callable, Iterator, Sequence
from typing import TYPE_CHECKING, Any, Literal, NamedTuple, Optional
from typing import Union as _UnionT
from typing import TYPE_CHECKING, Any, Literal, NamedTuple

import comtypes

Expand Down Expand Up @@ -51,9 +50,9 @@ def _encode_idl(names: Sequence[str]) -> int:
def _unpack_argspec(
idl: list[str],
typ: type["_CDataType"],
name: Optional[str] = None,
name: str | None = None,
defval: Any = _NOTHING,
) -> tuple[list[str], type["_CDataType"], Optional[str], Any]:
) -> tuple[list[str], type["_CDataType"], str | None, Any]:
return idl, typ, name, defval


Expand Down Expand Up @@ -95,19 +94,19 @@ def _resolve_argspec(
_VarFlagsWithDispIdHelpstr = tuple["dispid", "helpstring", hints.Unpack[_VarFlags]]
_VarFlagsWithDispId = tuple["dispid", hints.Unpack[_VarFlags]]
_VarFlagsWithHelpstr = tuple["helpstring", hints.Unpack[_VarFlags]]
_DispIdlFlags = _UnionT[_VarFlagsWithDispIdHelpstr, _VarFlagsWithDispId]
_ComIdlFlags = _UnionT[_VarFlags, _VarFlagsWithHelpstr]
_DispIdlFlags = _VarFlagsWithDispIdHelpstr | _VarFlagsWithDispId
_ComIdlFlags = _VarFlags | _VarFlagsWithHelpstr


class _ComMemberSpec(NamedTuple):
"""Specifier for a slot of COM method or property."""

restype: Optional[type["_CDataType"]]
restype: type["_CDataType"] | None
name: str
argtypes: tuple[type["_CDataType"], ...]
paramflags: Optional[tuple["hints.ParamFlagType", ...]]
idlflags: _UnionT["_ComIdlFlags", "_DispIdlFlags"]
doc: Optional[str]
paramflags: tuple["hints.ParamFlagType", ...] | None
idlflags: "_ComIdlFlags | _DispIdlFlags"
doc: str | None

def is_prop(self) -> bool:
return _is_spec_prop(self)
Expand All @@ -119,7 +118,7 @@ class _DispMemberSpec(NamedTuple):
what: Literal["DISPMETHOD", "DISPPROPERTY"]
name: str
idlflags: "_DispIdlFlags"
restype: Optional[type["_CDataType"]]
restype: type["_CDataType"] | None
argspec: tuple["hints.ArgSpecElmType", ...]

@property
Expand All @@ -135,7 +134,7 @@ def is_prop(self) -> bool:

# Specifier of a slot of method or property.
# This should be `typing.Protocol` if supporting Py3.8+ only.
_MemberSpec = _UnionT[_ComMemberSpec, _DispMemberSpec]
_MemberSpec = _ComMemberSpec | _DispMemberSpec


def _is_spec_prop(m: _MemberSpec):
Expand Down Expand Up @@ -166,7 +165,7 @@ class dispid(int):


def STDMETHOD(
restype: Optional[type["_CDataType"]],
restype: type["_CDataType"] | None,
name: str,
argtypes: Sequence[type["_CDataType"]] = (),
) -> _ComMemberSpec:
Expand All @@ -190,8 +189,8 @@ def STDMETHOD(


def DISPMETHOD(
idlflags: Sequence[_UnionT[int, str]],
restype: Optional[type["_CDataType"]],
idlflags: Sequence[int | str],
restype: type["_CDataType"] | None,
name: str,
*argspec: "hints.ArgSpecElmType",
) -> _DispMemberSpec:
Expand Down Expand Up @@ -226,7 +225,7 @@ def DISPMETHOD(


def DISPPROPERTY(
idlflags: Sequence[_UnionT[int, str]],
idlflags: Sequence[int | str],
proptype: type["_CDataType"],
name: str,
) -> _DispMemberSpec:
Expand Down Expand Up @@ -259,8 +258,8 @@ def DISPPROPERTY(


def COMMETHOD(
idlflags: Sequence[_UnionT[int, str]],
restype: Optional[type["_CDataType"]],
idlflags: Sequence[int | str],
restype: type["_CDataType"] | None,
methodname: str,
*argspec: "hints.ArgSpecElmType",
) -> _ComMemberSpec:
Expand Down Expand Up @@ -439,8 +438,8 @@ def call_with_inout(self, *args, **kw):
################################################################


_PropFunc = Optional[Callable[..., Any]]
_DocType = Optional[str]
_PropFunc = Callable[..., Any] | None
_DocType = str | None


class PropertyMapping:
Expand Down Expand Up @@ -504,7 +503,7 @@ def add(self, m: _MemberSpec, func: Callable[..., Any]) -> None:

# The following code assumes that the docstrings for
# propget and propput are identical.
def __iter__(self) -> Iterator[tuple[str, _UnionT[property, "named_property"]]]:
def __iter__(self) -> Iterator[tuple[str, "property | named_property"]]:
for name, doc, nargs, fget, fset in self._mapping:
if nargs == 0:
prop = property(fget, fset, None, doc)
Expand Down Expand Up @@ -610,7 +609,7 @@ class DispMemberGenerator:
def __init__(self, cls_name: str) -> None:
self._props = DispPropertyGenerator(cls_name)
# sequence of (name: str, func_or_prop: Callable | property, is_prop: bool)
self._items: list[tuple[str, _UnionT[Callable[..., Any], property], bool]] = []
self._items: list[tuple[str, Callable[..., Any] | property, bool]] = []

def add(self, m: _DispMemberSpec) -> None:
if m.what == "DISPPROPERTY": # DISPPROPERTY
Expand Down
9 changes: 3 additions & 6 deletions comtypes/_post_coinit/activeobj.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from ctypes import HRESULT, POINTER, OleDLL, byref, c_ulong, c_void_p
from ctypes.wintypes import DWORD, LPVOID
from typing import TYPE_CHECKING, Optional, TypeVar, overload
from typing import Union as _UnionT
from typing import TYPE_CHECKING, TypeVar, overload

from comtypes import GUID
from comtypes._post_coinit.unknwn import IUnknown
Expand All @@ -18,7 +17,7 @@


def RegisterActiveObject(
punk: "_UnionT[IUnknown, hints.LP_LP_Vtbl]", clsid: GUID, flags: int
punk: "IUnknown | hints.LP_LP_Vtbl", clsid: GUID, flags: int
) -> int:
"""Registers a pointer as the active object for its class and returns the handle."""
handle = c_ulong()
Expand All @@ -35,9 +34,7 @@ def RevokeActiveObject(handle: int) -> None:
def GetActiveObject(clsid: GUID, interface: None = None) -> IUnknown: ...
@overload
def GetActiveObject(clsid: GUID, interface: type[_T_IUnknown]) -> _T_IUnknown: ...
def GetActiveObject(
clsid: GUID, interface: Optional[type[IUnknown]] = None
) -> IUnknown:
def GetActiveObject(clsid: GUID, interface: type[IUnknown] | None = None) -> IUnknown:
"""Retrieves a pointer to a running object"""
p = POINTER(IUnknown)()
_GetActiveObject(byref(clsid), None, byref(p))
Expand Down
55 changes: 27 additions & 28 deletions comtypes/_post_coinit/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@
pointer,
)
from ctypes.wintypes import DWORD, LPCWSTR, LPVOID
from typing import TYPE_CHECKING, Any, Optional, TypeVar, overload
from typing import Union as _UnionT
from typing import TYPE_CHECKING, Any, TypeVar, overload

from comtypes import CLSCTX_LOCAL_SERVER, CLSCTX_REMOTE_SERVER, CLSCTX_SERVER, GUID
from comtypes._memberspec import COMMETHOD
Expand Down Expand Up @@ -96,7 +95,7 @@ def QueryService(
def CoGetObject(displayname: str, interface: None) -> IUnknown: ...
@overload
def CoGetObject(displayname: str, interface: type[_T_IUnknown]) -> _T_IUnknown: ...
def CoGetObject(displayname: str, interface: Optional[type[IUnknown]]) -> IUnknown:
def CoGetObject(displayname: str, interface: type[IUnknown] | None) -> IUnknown:
"""Convert a displayname to a moniker, then bind and return the object
identified by the moniker."""
if interface is None:
Expand All @@ -114,21 +113,21 @@ def CoGetObject(displayname: str, interface: Optional[type[IUnknown]]) -> IUnkno
def CoCreateInstance(
clsid: GUID,
interface: None = None,
clsctx: Optional[int] = None,
punkouter: Optional[_pUnkOuter] = None,
clsctx: int | None = None,
punkouter: _pUnkOuter | None = None,
) -> IUnknown: ...
@overload
def CoCreateInstance(
clsid: GUID,
interface: type[_T_IUnknown],
clsctx: Optional[int] = None,
punkouter: Optional[_pUnkOuter] = None,
clsctx: int | None = None,
punkouter: _pUnkOuter | None = None,
) -> _T_IUnknown: ...
def CoCreateInstance(
clsid: GUID,
interface: Optional[type[IUnknown]] = None,
clsctx: Optional[int] = None,
punkouter: Optional[_pUnkOuter] = None,
interface: type[IUnknown] | None = None,
clsctx: int | None = None,
punkouter: _pUnkOuter | None = None,
) -> IUnknown:
"""The basic windows api to create a COM class object and return a
pointer to an interface.
Expand All @@ -146,22 +145,22 @@ def CoCreateInstance(
@overload
def CoGetClassObject(
clsid: GUID,
clsctx: Optional[int] = None,
pServerInfo: "_UnionT[None, _CArgObject, COSERVERINFO]" = None,
clsctx: int | None = None,
pServerInfo: "None | _CArgObject | COSERVERINFO" = None,
interface: None = None,
) -> "hints.IClassFactory": ...
@overload
def CoGetClassObject(
clsid: GUID,
clsctx: Optional[int] = None,
pServerInfo: "_UnionT[None, _CArgObject, COSERVERINFO]" = None,
clsctx: int | None = None,
pServerInfo: "None | _CArgObject | COSERVERINFO" = None,
interface: type[_T_IUnknown] = IUnknown,
) -> _T_IUnknown: ...
def CoGetClassObject(
clsid: GUID,
clsctx: Optional[int] = None,
pServerInfo: "_UnionT[None, _CArgObject, COSERVERINFO]" = None,
interface: Optional[type[IUnknown]] = None,
clsctx: int | None = None,
pServerInfo: "None | _CArgObject | COSERVERINFO" = None,
interface: type[IUnknown] | None = None,
) -> IUnknown:
if clsctx is None:
clsctx = CLSCTX_SERVER
Expand Down Expand Up @@ -221,7 +220,7 @@ class _COSERVERINFO(Structure):
]
if TYPE_CHECKING:
dwReserved1: int
pwszName: Optional[str]
pwszName: str | None
pAuthInfo: _COAUTHINFO
dwReserved2: int

Expand Down Expand Up @@ -336,24 +335,24 @@ class _SOLE_AUTHENTICATION_LIST(Structure):
def CoCreateInstanceEx(
clsid: GUID,
interface: None = None,
clsctx: Optional[int] = None,
machine: Optional[str] = None,
pServerInfo: "_UnionT[None, _CArgObject, COSERVERINFO]" = None,
clsctx: int | None = None,
machine: str | None = None,
pServerInfo: "None | _CArgObject | COSERVERINFO" = None,
) -> IUnknown: ...
@overload
def CoCreateInstanceEx(
clsid: GUID,
interface: type[_T_IUnknown],
clsctx: Optional[int] = None,
machine: Optional[str] = None,
pServerInfo: "_UnionT[None, _CArgObject, COSERVERINFO]" = None,
clsctx: int | None = None,
machine: str | None = None,
pServerInfo: "None | _CArgObject | COSERVERINFO" = None,
) -> _T_IUnknown: ...
def CoCreateInstanceEx(
clsid: GUID,
interface: Optional[type[IUnknown]] = None,
clsctx: Optional[int] = None,
machine: Optional[str] = None,
pServerInfo: "_UnionT[None, _CArgObject, COSERVERINFO]" = None,
interface: type[IUnknown] | None = None,
clsctx: int | None = None,
machine: str | None = None,
pServerInfo: "None | _CArgObject | COSERVERINFO" = None,
) -> IUnknown:
"""The basic windows api to create a COM class object and return a
pointer to an interface, possibly on another machine.
Expand Down
Loading
Loading