12
12
LPVOID ,
13
13
ULONG ,
14
14
)
15
+ from typing import Any , Callable , Optional , Protocol , Type , TypeVar
15
16
16
17
import comtypes
17
18
import comtypes .automation
18
19
import comtypes .connectionpoints
19
20
import comtypes .hresult
20
21
import comtypes .typeinfo
22
+ from comtypes import COMObject , IUnknown
21
23
from comtypes .client ._generate import GetModule
22
24
23
25
logger = logging .getLogger (__name__ )
@@ -58,29 +60,46 @@ class SECURITY_ATTRIBUTES(Structure):
58
60
_CloseHandle .restype = BOOL
59
61
60
62
63
+ _T_IUnknown = TypeVar ("_T_IUnknown" , bound = IUnknown )
64
+
65
+
66
+ class _SupportsQueryInterface (Protocol ):
67
+ def QueryInterface (self , interface : Type [_T_IUnknown ]) -> _T_IUnknown : ...
68
+
69
+
61
70
class _AdviseConnection (object ):
62
- def __init__ (self , source , interface , receiver ):
71
+ def __init__ (
72
+ self ,
73
+ source : IUnknown ,
74
+ interface : Type [IUnknown ],
75
+ receiver : _SupportsQueryInterface ,
76
+ ) -> None :
63
77
self .cp = None
64
78
self .cookie = None
65
79
self .receiver = None
66
80
self ._connect (source , interface , receiver )
67
81
68
- def _connect (self , source , interface , receiver ):
82
+ def _connect (
83
+ self ,
84
+ source : IUnknown ,
85
+ interface : Type [IUnknown ],
86
+ receiver : _SupportsQueryInterface ,
87
+ ) -> None :
69
88
cpc = source .QueryInterface (comtypes .connectionpoints .IConnectionPointContainer )
70
89
self .cp = cpc .FindConnectionPoint (ctypes .byref (interface ._iid_ ))
71
90
logger .debug ("Start advise %s" , interface )
72
91
self .cookie = self .cp .Advise (receiver )
73
92
self .receiver = receiver
74
93
75
- def disconnect (self ):
94
+ def disconnect (self ) -> None :
76
95
if self .cookie :
77
96
self .cp .Unadvise (self .cookie )
78
97
logger .debug ("Unadvised %s" , self .cp )
79
98
self .cp = None
80
99
self .cookie = None
81
100
del self .receiver
82
101
83
- def __del__ (self ):
102
+ def __del__ (self ) -> None :
84
103
try :
85
104
if self .cookie is not None :
86
105
self .cp .Unadvise (self .cookie )
@@ -89,7 +108,7 @@ def __del__(self):
89
108
pass
90
109
91
110
92
- def FindOutgoingInterface (source ) :
111
+ def FindOutgoingInterface (source : IUnknown ) -> Type [ IUnknown ] :
93
112
"""XXX Describe the strategy that is used..."""
94
113
# If the COM object implements IProvideClassInfo2, it is easy to
95
114
# find the default outgoing interface.
@@ -129,7 +148,7 @@ def FindOutgoingInterface(source):
129
148
raise TypeError ("cannot determine source interface" )
130
149
131
150
132
- def find_single_connection_interface (source ) :
151
+ def find_single_connection_interface (source : IUnknown ) -> Optional [ Type [ IUnknown ]] :
133
152
# Enumerate the connection interfaces. If we find a single one,
134
153
# return it, if there are more, we give up since we cannot
135
154
# determine which one to use.
@@ -187,11 +206,11 @@ class _SinkMethodFinder(_MethodFinder):
187
206
event handlers.
188
207
"""
189
208
190
- def __init__ (self , inst , sink ) :
209
+ def __init__ (self , inst : COMObject , sink : Any ) -> None :
191
210
super (_SinkMethodFinder , self ).__init__ (inst )
192
211
self .sink = sink
193
212
194
- def find_method (self , fq_name , mthname ) :
213
+ def find_method (self , fq_name : str , mthname : str ) -> Callable [..., Any ] :
195
214
impl = self ._find_method (fq_name , mthname )
196
215
# Caller of this method catches AttributeError,
197
216
# so we need to be careful in the following code
@@ -206,7 +225,7 @@ def find_method(self, fq_name, mthname):
206
225
except AttributeError as details :
207
226
raise RuntimeError (details )
208
227
209
- def _find_method (self , fq_name , mthname ) :
228
+ def _find_method (self , fq_name : str , mthname : str ) -> Callable [..., Any ] :
210
229
try :
211
230
return super (_SinkMethodFinder , self ).find_method (fq_name , mthname )
212
231
except AttributeError :
@@ -216,7 +235,7 @@ def _find_method(self, fq_name, mthname):
216
235
return getattr (self .sink , mthname )
217
236
218
237
219
- def CreateEventReceiver (interface , handler ) :
238
+ def CreateEventReceiver (interface : Type [ IUnknown ] , handler : Any ) -> COMObject :
220
239
class Sink (comtypes .COMObject ):
221
240
_com_interfaces_ = [interface ]
222
241
0 commit comments