Skip to content

Commit 747e3dd

Browse files
authored
Small fixes for importing ctypes symbols. (#797)
* `TYPE_CHECKING`: `_Pointer` * Add `_CArrayType` to `stream` as a `TYPE_CHECKING` only symbol. * Adjust importing `create_unicode_buffer` in `typeinfo.py`. * Replace `ctypes.create_unicode_buffer` with `create_unicode_buffer` in `typeinfo.py`.
1 parent 50fcc84 commit 747e3dd

4 files changed

Lines changed: 18 additions & 11 deletions

File tree

comtypes/_post_coinit/misc.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
POINTER,
44
OleDLL,
55
Structure,
6-
_Pointer,
76
byref,
87
c_ulong,
98
c_ushort,
@@ -20,6 +19,8 @@
2019
from comtypes._post_coinit.unknwn import IUnknown
2120

2221
if TYPE_CHECKING:
22+
from ctypes import _Pointer
23+
2324
from comtypes import hints as hints # noqa # type: ignore
2425

2526

comtypes/server/inprocserver.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@
22
import logging
33
import sys
44
import winreg
5-
from typing import Any, Literal, Optional, Type
5+
from typing import TYPE_CHECKING, Any, Literal, Optional, Type
66

77
from comtypes import GUID, COMObject, IUnknown, hresult
88
from comtypes.server import IClassFactory
99

10+
if TYPE_CHECKING:
11+
from ctypes import _Pointer
12+
1013
logger = logging.getLogger(__name__)
1114
_debug = logger.debug
1215
_critical = logger.critical
@@ -24,8 +27,8 @@ def __init__(self, cls: Type[COMObject]) -> None:
2427
def IClassFactory_CreateInstance(
2528
self,
2629
this: Any,
27-
punkOuter: Optional[Type["ctypes._Pointer[IUnknown]"]],
28-
riid: "ctypes._Pointer[GUID]",
30+
punkOuter: Optional[Type["_Pointer[IUnknown]"]],
31+
riid: "_Pointer[GUID]",
2932
ppv: ctypes.c_void_p,
3033
) -> int:
3134
_debug("ClassFactory.CreateInstance(%s)", riid[0])

comtypes/stream.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1-
from ctypes import HRESULT, POINTER, Array, c_ubyte, c_ulong, pointer
1+
from ctypes import HRESULT, POINTER, c_ubyte, c_ulong, pointer
22
from typing import TYPE_CHECKING, Tuple
33

44
from comtypes import COMMETHOD, GUID, IUnknown
55

6+
if TYPE_CHECKING:
7+
from ctypes import Array as _CArrayType
8+
69

710
class ISequentialStream(IUnknown):
811
"""Defines methods for the stream objects in sequence."""
@@ -38,7 +41,7 @@ class ISequentialStream(IUnknown):
3841
),
3942
]
4043

41-
def RemoteRead(self, cb: int) -> Tuple["Array[c_ubyte]", int]:
44+
def RemoteRead(self, cb: int) -> Tuple["_CArrayType[c_ubyte]", int]:
4245
"""Reads a specified number of bytes from the stream object into memory
4346
starting at the current seek pointer.
4447
"""
@@ -51,7 +54,7 @@ def RemoteRead(self, cb: int) -> Tuple["Array[c_ubyte]", int]:
5154

5255
if TYPE_CHECKING:
5356

54-
def RemoteWrite(self, pv: "Array[c_ubyte]", cb: int) -> int:
57+
def RemoteWrite(self, pv: "_CArrayType[c_ubyte]", cb: int) -> int:
5558
"""Writes a specified number of bytes into the stream object starting at
5659
the current seek pointer.
5760
"""

comtypes/typeinfo.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@
1111
POINTER,
1212
OleDLL,
1313
WinDLL,
14-
_Pointer,
1514
byref,
1615
c_int,
1716
c_void_p,
1817
c_wchar_p,
18+
create_unicode_buffer,
1919
)
2020
from ctypes.wintypes import (
2121
DWORD,
@@ -57,6 +57,8 @@
5757
)
5858

5959
if TYPE_CHECKING:
60+
from ctypes import _Pointer
61+
6062
from comtypes import hints # type: ignore
6163

6264

@@ -278,8 +280,6 @@ def IsName(self, name: str, lHashVal: int = 0) -> Optional[str]:
278280
Returns the name with capitalization found in the type
279281
library, or None.
280282
"""
281-
from ctypes import create_unicode_buffer
282-
283283
namebuf = create_unicode_buffer(name)
284284
found = BOOL()
285285
self.__com_IsName(namebuf, lHashVal, byref(found)) # type: ignore
@@ -768,7 +768,7 @@ def GetModuleFileName(handle: Optional[int], maxsize: int) -> str:
768768
769769
https://learn.microsoft.com/ja-jp/windows/win32/api/libloaderapi/nf-libloaderapi-loadlibraryw
770770
"""
771-
buf = ctypes.create_unicode_buffer(maxsize)
771+
buf = create_unicode_buffer(maxsize)
772772
length = _GetModuleFileNameW(handle, buf, maxsize)
773773
return buf.value[:length]
774774

0 commit comments

Comments
 (0)