comtypes 1.4.9__zip → 1.4.11__zip

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (148) hide show
  1. {comtypes-1.4.9 → comtypes-1.4.11}/CHANGES.txt +41 -0
  2. {comtypes-1.4.9/comtypes.egg-info → comtypes-1.4.11}/PKG-INFO +4 -2
  3. {comtypes-1.4.9 → comtypes-1.4.11}/comtypes/GUID.py +34 -13
  4. {comtypes-1.4.9 → comtypes-1.4.11}/comtypes/__init__.py +25 -16
  5. {comtypes-1.4.9 → comtypes-1.4.11}/comtypes/_comobject.py +92 -39
  6. {comtypes-1.4.9 → comtypes-1.4.11}/comtypes/_memberspec.py +112 -78
  7. {comtypes-1.4.9 → comtypes-1.4.11}/comtypes/_meta.py +2 -1
  8. {comtypes-1.4.9 → comtypes-1.4.11}/comtypes/_post_coinit/_cominterface_meta_patcher.py +3 -6
  9. {comtypes-1.4.9 → comtypes-1.4.11}/comtypes/_post_coinit/bstr.py +12 -5
  10. {comtypes-1.4.9 → comtypes-1.4.11}/comtypes/_post_coinit/instancemethod.py +1 -2
  11. {comtypes-1.4.9 → comtypes-1.4.11}/comtypes/_post_coinit/misc.py +59 -24
  12. {comtypes-1.4.9 → comtypes-1.4.11}/comtypes/_post_coinit/unknwn.py +30 -25
  13. {comtypes-1.4.9 → comtypes-1.4.11}/comtypes/_safearray.py +2 -2
  14. {comtypes-1.4.9 → comtypes-1.4.11}/comtypes/_vtbl.py +52 -55
  15. {comtypes-1.4.9 → comtypes-1.4.11}/comtypes/automation.py +19 -14
  16. comtypes-1.4.11/comtypes/client/__init__.py +42 -0
  17. comtypes-1.4.11/comtypes/client/_activeobj.py +46 -0
  18. {comtypes-1.4.9 → comtypes-1.4.11}/comtypes/client/_code_cache.py +15 -24
  19. comtypes-1.4.11/comtypes/client/_create.py +147 -0
  20. {comtypes-1.4.9 → comtypes-1.4.11}/comtypes/client/_events.py +140 -57
  21. {comtypes-1.4.9 → comtypes-1.4.11}/comtypes/client/_generate.py +3 -5
  22. comtypes-1.4.11/comtypes/client/_managing.py +114 -0
  23. {comtypes-1.4.9 → comtypes-1.4.11}/comtypes/client/dynamic.py +2 -3
  24. {comtypes-1.4.9 → comtypes-1.4.11}/comtypes/client/lazybind.py +10 -11
  25. {comtypes-1.4.9 → comtypes-1.4.11}/comtypes/connectionpoints.py +38 -3
  26. {comtypes-1.4.9 → comtypes-1.4.11}/comtypes/errorinfo.py +72 -21
  27. {comtypes-1.4.9 → comtypes-1.4.11}/comtypes/git.py +4 -3
  28. {comtypes-1.4.9 → comtypes-1.4.11}/comtypes/hints.pyi +15 -2
  29. {comtypes-1.4.9 → comtypes-1.4.11}/comtypes/hresult.py +16 -6
  30. {comtypes-1.4.9 → comtypes-1.4.11}/comtypes/logutil.py +15 -3
  31. {comtypes-1.4.9 → comtypes-1.4.11}/comtypes/messageloop.py +15 -13
  32. {comtypes-1.4.9 → comtypes-1.4.11}/comtypes/persist.py +3 -5
  33. {comtypes-1.4.9 → comtypes-1.4.11}/comtypes/safearray.py +8 -6
  34. {comtypes-1.4.9 → comtypes-1.4.11}/comtypes/server/__init__.py +22 -4
  35. {comtypes-1.4.9 → comtypes-1.4.11}/comtypes/server/automation.py +1 -2
  36. {comtypes-1.4.9 → comtypes-1.4.11}/comtypes/server/connectionpoints.py +71 -70
  37. {comtypes-1.4.9 → comtypes-1.4.11}/comtypes/server/inprocserver.py +14 -8
  38. {comtypes-1.4.9 → comtypes-1.4.11}/comtypes/server/localserver.py +28 -16
  39. comtypes-1.4.11/comtypes/server/register.py +478 -0
  40. {comtypes-1.4.9 → comtypes-1.4.11}/comtypes/shelllink.py +13 -7
  41. {comtypes-1.4.9 → comtypes-1.4.11}/comtypes/stream.py +7 -4
  42. {comtypes-1.4.9 → comtypes-1.4.11}/comtypes/test/TestComServer.py +6 -7
  43. {comtypes-1.4.9 → comtypes-1.4.11}/comtypes/test/TestDispServer.py +4 -2
  44. {comtypes-1.4.9 → comtypes-1.4.11}/comtypes/test/__init__.py +1 -1
  45. comtypes-1.4.11/comtypes/test/find_memleak.py +72 -0
  46. {comtypes-1.4.9 → comtypes-1.4.11}/comtypes/test/runtests.py +1 -0
  47. {comtypes-1.4.9 → comtypes-1.4.11}/comtypes/test/setup.py +2 -1
  48. {comtypes-1.4.9 → comtypes-1.4.11}/comtypes/test/test_BSTR.py +7 -17
  49. {comtypes-1.4.9 → comtypes-1.4.11}/comtypes/test/test_GUID.py +7 -7
  50. {comtypes-1.4.9 → comtypes-1.4.11}/comtypes/test/test_QueryService.py +1 -0
  51. {comtypes-1.4.9 → comtypes-1.4.11}/comtypes/test/test_agilent.py +4 -3
  52. {comtypes-1.4.9 → comtypes-1.4.11}/comtypes/test/test_basic.py +18 -9
  53. {comtypes-1.4.9 → comtypes-1.4.11}/comtypes/test/test_clear_cache.py +1 -1
  54. {comtypes-1.4.9 → comtypes-1.4.11}/comtypes/test/test_client.py +2 -2
  55. {comtypes-1.4.9 → comtypes-1.4.11}/comtypes/test/test_client_dynamic.py +2 -2
  56. {comtypes-1.4.9 → comtypes-1.4.11}/comtypes/test/test_client_regenerate_modules.py +2 -2
  57. {comtypes-1.4.9 → comtypes-1.4.11}/comtypes/test/test_collections.py +7 -4
  58. {comtypes-1.4.9 → comtypes-1.4.11}/comtypes/test/test_comobject.py +22 -9
  59. {comtypes-1.4.9 → comtypes-1.4.11}/comtypes/test/test_comserver.py +56 -3
  60. {comtypes-1.4.9 → comtypes-1.4.11}/comtypes/test/test_dict.py +2 -1
  61. {comtypes-1.4.9 → comtypes-1.4.11}/comtypes/test/test_dispifc_records.py +1 -1
  62. {comtypes-1.4.9 → comtypes-1.4.11}/comtypes/test/test_dispifc_safearrays.py +1 -1
  63. {comtypes-1.4.9 → comtypes-1.4.11}/comtypes/test/test_dyndispatch.py +5 -2
  64. comtypes-1.4.11/comtypes/test/test_errorinfo.py +115 -0
  65. {comtypes-1.4.9 → comtypes-1.4.11}/comtypes/test/test_findgendir.py +1 -2
  66. {comtypes-1.4.9 → comtypes-1.4.11}/comtypes/test/test_getactiveobj.py +4 -4
  67. comtypes-1.4.11/comtypes/test/test_hresult.py +48 -0
  68. {comtypes-1.4.9 → comtypes-1.4.11}/comtypes/test/test_ie.py +19 -6
  69. {comtypes-1.4.9 → comtypes-1.4.11}/comtypes/test/test_ienum.py +2 -2
  70. {comtypes-1.4.9 → comtypes-1.4.11}/comtypes/test/test_imfattributes.py +13 -3
  71. {comtypes-1.4.9 → comtypes-1.4.11}/comtypes/test/test_inout_args.py +10 -3
  72. {comtypes-1.4.9 → comtypes-1.4.11}/comtypes/test/test_midl_safearray_create.py +6 -4
  73. {comtypes-1.4.9 → comtypes-1.4.11}/comtypes/test/test_monikers.py +23 -9
  74. {comtypes-1.4.9 → comtypes-1.4.11}/comtypes/test/test_msscript.py +2 -1
  75. {comtypes-1.4.9 → comtypes-1.4.11}/comtypes/test/test_npsupport.py +4 -4
  76. {comtypes-1.4.9 → comtypes-1.4.11}/comtypes/test/test_outparam.py +22 -6
  77. {comtypes-1.4.9 → comtypes-1.4.11}/comtypes/test/test_persist.py +2 -4
  78. {comtypes-1.4.9 → comtypes-1.4.11}/comtypes/test/test_recordinfo.py +1 -1
  79. {comtypes-1.4.9 → comtypes-1.4.11}/comtypes/test/test_safearray.py +2 -1
  80. {comtypes-1.4.9 → comtypes-1.4.11}/comtypes/test/test_sapi.py +4 -1
  81. {comtypes-1.4.9 → comtypes-1.4.11}/comtypes/test/test_server_register.py +114 -125
  82. {comtypes-1.4.9 → comtypes-1.4.11}/comtypes/test/test_shelllink.py +2 -3
  83. {comtypes-1.4.9 → comtypes-1.4.11}/comtypes/test/test_storage.py +12 -6
  84. {comtypes-1.4.9 → comtypes-1.4.11}/comtypes/test/test_stream.py +16 -5
  85. {comtypes-1.4.9 → comtypes-1.4.11}/comtypes/test/test_subinterface.py +4 -2
  86. {comtypes-1.4.9 → comtypes-1.4.11}/comtypes/test/test_typeannotator.py +1 -2
  87. {comtypes-1.4.9 → comtypes-1.4.11}/comtypes/test/test_typeinfo.py +36 -7
  88. {comtypes-1.4.9 → comtypes-1.4.11}/comtypes/test/test_urlhistory.py +7 -4
  89. {comtypes-1.4.9 → comtypes-1.4.11}/comtypes/test/test_variant.py +5 -3
  90. {comtypes-1.4.9 → comtypes-1.4.11}/comtypes/test/test_w_getopt.py +4 -4
  91. {comtypes-1.4.9 → comtypes-1.4.11}/comtypes/test/test_word.py +1 -1
  92. {comtypes-1.4.9 → comtypes-1.4.11}/comtypes/tools/codegenerator/codegenerator.py +167 -435
  93. comtypes-1.4.11/comtypes/tools/codegenerator/comments.py +79 -0
  94. comtypes-1.4.11/comtypes/tools/codegenerator/heads.py +192 -0
  95. {comtypes-1.4.9 → comtypes-1.4.11}/comtypes/tools/codegenerator/helpers.py +3 -6
  96. {comtypes-1.4.9 → comtypes-1.4.11}/comtypes/tools/codegenerator/namespaces.py +13 -5
  97. {comtypes-1.4.9 → comtypes-1.4.11}/comtypes/tools/codegenerator/typeannotator.py +13 -3
  98. {comtypes-1.4.9 → comtypes-1.4.11}/comtypes/tools/tlbparser.py +25 -25
  99. {comtypes-1.4.9 → comtypes-1.4.11}/comtypes/tools/typedesc.py +3 -2
  100. {comtypes-1.4.9 → comtypes-1.4.11}/comtypes/tools/typedesc_base.py +2 -2
  101. {comtypes-1.4.9 → comtypes-1.4.11}/comtypes/typeinfo.py +163 -51
  102. {comtypes-1.4.9 → comtypes-1.4.11}/comtypes/viewobject.py +2 -4
  103. {comtypes-1.4.9 → comtypes-1.4.11/comtypes.egg-info}/PKG-INFO +4 -2
  104. {comtypes-1.4.9 → comtypes-1.4.11}/comtypes.egg-info/SOURCES.txt +7 -0
  105. {comtypes-1.4.9 → comtypes-1.4.11}/pyproject.toml +10 -23
  106. comtypes-1.4.9/comtypes/client/__init__.py +0 -323
  107. comtypes-1.4.9/comtypes/server/register.py +0 -419
  108. comtypes-1.4.9/comtypes/test/find_memleak.py +0 -76
  109. {comtypes-1.4.9 → comtypes-1.4.11}/LICENSE.txt +0 -0
  110. {comtypes-1.4.9 → comtypes-1.4.11}/MANIFEST.in +0 -0
  111. {comtypes-1.4.9 → comtypes-1.4.11}/README.md +0 -0
  112. {comtypes-1.4.9 → comtypes-1.4.11}/comtypes/_npsupport.py +0 -0
  113. {comtypes-1.4.9 → comtypes-1.4.11}/comtypes/_post_coinit/__init__.py +0 -0
  114. {comtypes-1.4.9 → comtypes-1.4.11}/comtypes/_tlib_version_checker.py +0 -0
  115. {comtypes-1.4.9 → comtypes-1.4.11}/comtypes/clear_cache.py +0 -0
  116. {comtypes-1.4.9 → comtypes-1.4.11}/comtypes/client/_constants.py +0 -0
  117. {comtypes-1.4.9 → comtypes-1.4.11}/comtypes/patcher.py +0 -0
  118. {comtypes-1.4.9 → comtypes-1.4.11}/comtypes/server/w_getopt.py +0 -0
  119. {comtypes-1.4.9 → comtypes-1.4.11}/comtypes/test/TestComServer.idl +0 -0
  120. {comtypes-1.4.9 → comtypes-1.4.11}/comtypes/test/TestComServer.tlb +0 -0
  121. {comtypes-1.4.9 → comtypes-1.4.11}/comtypes/test/TestDispServer.idl +0 -0
  122. {comtypes-1.4.9 → comtypes-1.4.11}/comtypes/test/TestDispServer.tlb +0 -0
  123. {comtypes-1.4.9 → comtypes-1.4.11}/comtypes/test/mylib.idl +0 -0
  124. {comtypes-1.4.9 → comtypes-1.4.11}/comtypes/test/mylib.tlb +0 -0
  125. {comtypes-1.4.9 → comtypes-1.4.11}/comtypes/test/mytypelib.idl +0 -0
  126. {comtypes-1.4.9 → comtypes-1.4.11}/comtypes/test/test_DISPPARAMS.py +0 -0
  127. {comtypes-1.4.9 → comtypes-1.4.11}/comtypes/test/test_avmc.py +0 -0
  128. {comtypes-1.4.9 → comtypes-1.4.11}/comtypes/test/test_casesensitivity.py +0 -0
  129. {comtypes-1.4.9 → comtypes-1.4.11}/comtypes/test/test_createwrappers.py +0 -0
  130. {comtypes-1.4.9 → comtypes-1.4.11}/comtypes/test/test_dispinterface.py +0 -0
  131. {comtypes-1.4.9 → comtypes-1.4.11}/comtypes/test/test_excel.py +0 -0
  132. {comtypes-1.4.9 → comtypes-1.4.11}/comtypes/test/test_jscript.js +0 -0
  133. {comtypes-1.4.9 → comtypes-1.4.11}/comtypes/test/test_pump_events.py +0 -0
  134. {comtypes-1.4.9 → comtypes-1.4.11}/comtypes/test/test_server.py +0 -0
  135. {comtypes-1.4.9 → comtypes-1.4.11}/comtypes/test/test_showevents.py +0 -0
  136. {comtypes-1.4.9 → comtypes-1.4.11}/comtypes/test/test_win32com_interop.py +0 -0
  137. {comtypes-1.4.9 → comtypes-1.4.11}/comtypes/test/test_wmi.py +0 -0
  138. {comtypes-1.4.9 → comtypes-1.4.11}/comtypes/test/urlhist.tlb +0 -0
  139. {comtypes-1.4.9 → comtypes-1.4.11}/comtypes/tools/__init__.py +0 -0
  140. {comtypes-1.4.9 → comtypes-1.4.11}/comtypes/tools/codegenerator/__init__.py +0 -0
  141. {comtypes-1.4.9 → comtypes-1.4.11}/comtypes/tools/codegenerator/modulenamer.py +0 -0
  142. {comtypes-1.4.9 → comtypes-1.4.11}/comtypes/tools/codegenerator/packing.py +0 -0
  143. {comtypes-1.4.9 → comtypes-1.4.11}/comtypes/util.py +0 -0
  144. {comtypes-1.4.9 → comtypes-1.4.11}/comtypes.egg-info/dependency_links.txt +0 -0
  145. {comtypes-1.4.9 → comtypes-1.4.11}/comtypes.egg-info/entry_points.txt +0 -0
  146. {comtypes-1.4.9 → comtypes-1.4.11}/comtypes.egg-info/top_level.txt +0 -0
  147. {comtypes-1.4.9 → comtypes-1.4.11}/setup.cfg +0 -0
  148. {comtypes-1.4.9 → comtypes-1.4.11}/setup.py +0 -0
@@ -1,32 +1,59 @@
1
1
  import ctypes
2
- from typing import Any, Literal, NamedTuple
3
- from typing import Dict, List, Tuple, Type
4
- from typing import Optional, Union as _UnionT
5
- from typing import Callable, Iterator
2
+ from typing import (
3
+ TYPE_CHECKING,
4
+ Any,
5
+ Callable,
6
+ Dict,
7
+ Iterator,
8
+ List,
9
+ Literal,
10
+ NamedTuple,
11
+ Optional,
12
+ Sequence,
13
+ Tuple,
14
+ Type,
15
+ )
16
+ from typing import Union as _UnionT
6
17
 
7
- from comtypes import _CData
8
18
  import comtypes
9
19
 
20
+ if TYPE_CHECKING:
21
+ from _ctypes import _PyCSimpleType
22
+ from ctypes import _CArgObject, _CDataType
10
23
 
11
- _PositionalParamFlagType = Tuple[int, Optional[str]]
12
- _OptionalParamFlagType = Tuple[int, Optional[str], Any]
13
- _ParamFlagType = _UnionT[_PositionalParamFlagType, _OptionalParamFlagType]
14
- _PositionalArgSpecElmType = Tuple[List[str], Type[_CData], str]
15
- _OptionalArgSpecElmType = Tuple[List[str], Type[_CData], str, Any]
16
- _ArgSpecElmType = _UnionT[_PositionalArgSpecElmType, _OptionalArgSpecElmType]
24
+ from comtypes import hints # type: ignore
25
+ else:
26
+ _PyCSimpleType = type(ctypes.c_int)
27
+ _CArgObject = type(ctypes.byref(ctypes.c_int()))
17
28
 
18
29
 
30
+ DISPATCH_METHOD = 1
31
+ DISPATCH_PROPERTYGET = 2
32
+ DISPATCH_PROPERTYPUT = 4
33
+ DISPATCH_PROPERTYPUTREF = 8
34
+
35
+ PARAMFLAG_NONE = 0
36
+ PARAMFLAG_FIN = 1
37
+ PARAMFLAG_FOUT = 2
38
+ PARAMFLAG_FLCID = 4
39
+ PARAMFLAG_FRETVAL = 8
40
+ PARAMFLAG_FOPT = 16
41
+ PARAMFLAG_FHASDEFAULT = 32
42
+ PARAMFLAG_FHASCUSTDATA = 64
43
+
19
44
  _PARAMFLAGS = {
20
- "in": 1,
21
- "out": 2,
22
- "lcid": 4,
23
- "retval": 8,
24
- "optional": 16,
45
+ "in": PARAMFLAG_FIN,
46
+ "out": PARAMFLAG_FOUT,
47
+ "lcid": PARAMFLAG_FLCID,
48
+ "retval": PARAMFLAG_FRETVAL,
49
+ "optional": PARAMFLAG_FOPT,
25
50
  }
26
51
 
27
52
 
28
- def _encode_idl(names):
29
- # sum up all values found in _PARAMFLAGS, ignoring all others.
53
+ def _encode_idl(names: Sequence[str]) -> int:
54
+ """Sums up 'in', 'out', 'lcid', 'retval' and 'optional' values
55
+ found in _PARAMFLAGS, ignoring all other PARAMFLAG_... stuff.
56
+ """
30
57
  return sum([_PARAMFLAGS.get(n, 0) for n in names])
31
58
 
32
59
 
@@ -35,20 +62,20 @@ _NOTHING = object()
35
62
 
36
63
  def _unpack_argspec(
37
64
  idl: List[str],
38
- typ: Type[_CData],
65
+ typ: Type["_CDataType"],
39
66
  name: Optional[str] = None,
40
67
  defval: Any = _NOTHING,
41
- ) -> Tuple[List[str], Type[_CData], Optional[str], Any]:
68
+ ) -> Tuple[List[str], Type["_CDataType"], Optional[str], Any]:
42
69
  return idl, typ, name, defval
43
70
 
44
71
 
45
72
  def _resolve_argspec(
46
- items: Tuple[_ArgSpecElmType, ...],
47
- ) -> Tuple[Tuple[_ParamFlagType, ...], Tuple[Type[_CData], ...]]:
73
+ items: Tuple["hints.ArgSpecElmType", ...],
74
+ ) -> Tuple[Tuple["hints.ParamFlagType", ...], Tuple[Type["_CDataType"], ...]]:
48
75
  """Unpacks and converts from argspec to paramflags and argtypes.
49
76
 
50
77
  - paramflags is a sequence of `(pflags: int, argname: str, | None[, defval: Any])`.
51
- - argtypes is a sequence of `type[_CData]`.
78
+ - argtypes is a sequence of `type[_CDataType]`.
52
79
  """
53
80
  from comtypes.automation import VARIANT
54
81
 
@@ -75,14 +102,23 @@ def _resolve_argspec(
75
102
  return tuple(paramflags), tuple(argtypes)
76
103
 
77
104
 
105
+ if TYPE_CHECKING:
106
+ _VarFlags = Tuple[str, ...]
107
+ _VarFlagsWithDispIdHelpstr = Tuple["dispid", "helpstring", hints.Unpack[_VarFlags]]
108
+ _VarFlagsWithDispId = Tuple["dispid", hints.Unpack[_VarFlags]]
109
+ _VarFlagsWithHelpstr = Tuple["helpstring", hints.Unpack[_VarFlags]]
110
+ _DispIdlFlags = _UnionT[_VarFlagsWithDispIdHelpstr, _VarFlagsWithDispId]
111
+ _ComIdlFlags = _UnionT[_VarFlags, _VarFlagsWithHelpstr]
112
+
113
+
78
114
  class _ComMemberSpec(NamedTuple):
79
115
  """Specifier for a slot of COM method or property."""
80
116
 
81
- restype: Optional[Type[_CData]]
117
+ restype: Optional[Type["_CDataType"]]
82
118
  name: str
83
- argtypes: Tuple[Type[_CData], ...]
84
- paramflags: Optional[Tuple[_ParamFlagType, ...]]
85
- idlflags: Tuple[_UnionT[str, int], ...]
119
+ argtypes: Tuple[Type["_CDataType"], ...]
120
+ paramflags: Optional[Tuple["hints.ParamFlagType", ...]]
121
+ idlflags: _UnionT["_ComIdlFlags", "_DispIdlFlags"]
86
122
  doc: Optional[str]
87
123
 
88
124
  def is_prop(self) -> bool:
@@ -94,9 +130,9 @@ class _DispMemberSpec(NamedTuple):
94
130
 
95
131
  what: Literal["DISPMETHOD", "DISPPROPERTY"]
96
132
  name: str
97
- idlflags: Tuple[_UnionT[str, int], ...]
98
- restype: Optional[Type[_CData]]
99
- argspec: Tuple[_ArgSpecElmType, ...]
133
+ idlflags: "_DispIdlFlags"
134
+ restype: Optional[Type["_CDataType"]]
135
+ argspec: Tuple["hints.ArgSpecElmType", ...]
100
136
 
101
137
  @property
102
138
  def memid(self) -> int:
@@ -189,15 +225,32 @@ def COMMETHOD(idlflags, restype, methodname, *argspec) -> _ComMemberSpec:
189
225
 
190
226
 
191
227
  ################################################################
192
-
193
- _PropFunc = Optional[Callable[..., Any]]
194
- _DocType = Optional[str]
228
+ # workarounds for ctypes functions and parameters
229
+
230
+
231
+ def _prepare_parameter(value: Any, atyp: Type["_CDataType"]) -> "_CDataType":
232
+ # parameter was passed, call `from_param()` to
233
+ # convert it to a `ctypes` type.
234
+ if getattr(value, "_type_", None) is atyp:
235
+ # Array of or pointer to type `atyp` was passed,
236
+ # pointer to `atyp` expected.
237
+ v = value
238
+ elif type(atyp) is _PyCSimpleType: # type: ignore
239
+ # The `from_param` method of simple types
240
+ # (`c_int`, `c_double`, ...) returns a `byref` object which
241
+ # we cannot use since later it will be wrapped in a pointer.
242
+ # Simply call the constructor with the argument in that case.
243
+ v = atyp(value)
244
+ else:
245
+ v = atyp.from_param(value)
246
+ assert not isinstance(v, _CArgObject) # type: ignore
247
+ return v
195
248
 
196
249
 
197
250
  def _fix_inout_args(
198
251
  func: Callable[..., Any],
199
- argtypes: Tuple[Type[_CData], ...],
200
- paramflags: Tuple[_ParamFlagType, ...],
252
+ argtypes: Tuple[Type["_CDataType"], ...],
253
+ paramflags: Tuple["hints.ParamFlagType", ...],
201
254
  ) -> Callable[..., Any]:
202
255
  """This function provides a workaround for a bug in `ctypes`.
203
256
 
@@ -209,13 +262,11 @@ def _fix_inout_args(
209
262
  #
210
263
  # TODO: The workaround should be disabled when a ctypes
211
264
  # version is used where the bug is fixed.
212
- SIMPLETYPE = type(ctypes.c_int)
213
- BYREFTYPE = type(ctypes.byref(ctypes.c_int()))
214
265
 
215
266
  def call_with_inout(self, *args, **kw):
216
267
  args = list(args)
217
268
  # Indexed by order in the output
218
- outargs: Dict[int, _UnionT[_CData, "ctypes._CArgObject"]] = {}
269
+ outargs: Dict[int, "_CDataType"] = {}
219
270
  outnum = 0
220
271
  param_index = 0
221
272
  # Go through all expected arguments and match them to the provided arguments.
@@ -223,8 +274,8 @@ def _fix_inout_args(
223
274
  # through the keyword arguments.
224
275
  for i, info in enumerate(paramflags):
225
276
  direction = info[0]
226
- dir_in = direction & 1 == 1
227
- dir_out = direction & 2 == 2
277
+ dir_in = bool(direction & PARAMFLAG_FIN)
278
+ dir_out = bool(direction & PARAMFLAG_FOUT)
228
279
  is_positional = param_index < len(args)
229
280
  if not (dir_in or dir_out):
230
281
  # The original code here did not check for this special case and
@@ -241,34 +292,16 @@ def _fix_inout_args(
241
292
  name = info[1]
242
293
  # [in, out] parameters are passed as pointers,
243
294
  # this is the pointed-to type:
244
- atyp: Type[_CData] = getattr(argtypes[i], "_type_")
295
+ atyp: Type["_CDataType"] = getattr(argtypes[i], "_type_")
245
296
 
246
297
  # Get the actual parameter, either as positional or
247
298
  # keyword arg.
248
299
 
249
- def prepare_parameter(v):
250
- # parameter was passed, call `from_param()` to
251
- # convert it to a `ctypes` type.
252
- if getattr(v, "_type_", None) is atyp:
253
- # Array of or pointer to type `atyp` was passed,
254
- # pointer to `atyp` expected.
255
- pass
256
- elif type(atyp) is SIMPLETYPE:
257
- # The `from_param` method of simple types
258
- # (`c_int`, `c_double`, ...) returns a `byref` object which
259
- # we cannot use since later it will be wrapped in a pointer.
260
- # Simply call the constructor with the argument in that case.
261
- v = atyp(v)
262
- else:
263
- v = atyp.from_param(v)
264
- assert not isinstance(v, BYREFTYPE)
265
- return v
266
-
267
300
  if is_positional:
268
- v = prepare_parameter(args[param_index])
301
+ v = _prepare_parameter(args[param_index], atyp)
269
302
  args[param_index] = v
270
303
  elif name in kw:
271
- v = prepare_parameter(kw[name])
304
+ v = _prepare_parameter(kw[name], atyp)
272
305
  kw[name] = v
273
306
  else:
274
307
  # no parameter was passed, make an empty one of the required type
@@ -313,6 +346,13 @@ def _fix_inout_args(
313
346
  return call_with_inout
314
347
 
315
348
 
349
+ ################################################################
350
+
351
+
352
+ _PropFunc = Optional[Callable[..., Any]]
353
+ _DocType = Optional[str]
354
+
355
+
316
356
  class PropertyMapping(object):
317
357
  def __init__(self):
318
358
  self._data: Dict[Tuple[str, _DocType, int], List[_PropFunc]] = {}
@@ -464,8 +504,8 @@ class ComMemberGenerator(object):
464
504
  ) -> Callable[..., Any]:
465
505
  """This is a workaround. See `_fix_inout_args` docstring and comments."""
466
506
  if m.paramflags:
467
- dirflags = [(p[0] & 3) for p in m.paramflags]
468
- if 3 in dirflags:
507
+ dirflags = [(p[0] & (PARAMFLAG_FIN | PARAMFLAG_FOUT)) for p in m.paramflags]
508
+ if (PARAMFLAG_FIN | PARAMFLAG_FOUT) in dirflags:
469
509
  return _fix_inout_args(func, m.argtypes, m.paramflags)
470
510
  return func
471
511
 
@@ -502,15 +542,15 @@ class DispMemberGenerator(object):
502
542
  memid = m.memid
503
543
 
504
544
  def fget(obj):
505
- return obj.Invoke(memid, _invkind=2) # DISPATCH_PROPERTYGET
545
+ return obj.Invoke(memid, _invkind=DISPATCH_PROPERTYGET)
506
546
 
507
547
  if "readonly" in m.idlflags:
508
548
  return property(fget)
509
549
 
510
550
  def fset(obj, value):
511
- # Detect whether to use DISPATCH_PROPERTYPUT or
512
- # DISPATCH_PROPERTYPUTREF
513
- invkind = 8 if comtypes._is_object(value) else 4
551
+ # Detect whether to use PUT or PUTREF
552
+ is_ref = comtypes._is_object(value)
553
+ invkind = DISPATCH_PROPERTYPUTREF if is_ref else DISPATCH_PROPERTYPUT
514
554
  return obj.Invoke(memid, value, _invkind=invkind)
515
555
 
516
556
  return property(fget, fset)
@@ -521,25 +561,19 @@ class DispMemberGenerator(object):
521
561
  if "propget" in m.idlflags:
522
562
 
523
563
  def getfunc(obj, *args, **kw):
524
- return obj.Invoke(
525
- memid, _invkind=2, *args, **kw
526
- ) # DISPATCH_PROPERTYGET
564
+ return obj.Invoke(memid, _invkind=DISPATCH_PROPERTYGET, *args, **kw)
527
565
 
528
566
  return getfunc
529
567
  elif "propput" in m.idlflags:
530
568
 
531
569
  def putfunc(obj, *args, **kw):
532
- return obj.Invoke(
533
- memid, _invkind=4, *args, **kw
534
- ) # DISPATCH_PROPERTYPUT
570
+ return obj.Invoke(memid, _invkind=DISPATCH_PROPERTYPUT, *args, **kw)
535
571
 
536
572
  return putfunc
537
573
  elif "propputref" in m.idlflags:
538
574
 
539
575
  def putreffunc(obj, *args, **kw):
540
- return obj.Invoke(
541
- memid, _invkind=8, *args, **kw
542
- ) # DISPATCH_PROPERTYPUTREF
576
+ return obj.Invoke(memid, _invkind=DISPATCH_PROPERTYPUTREF, *args, **kw)
543
577
 
544
578
  return putreffunc
545
579
  # a first attempt to make use of the restype. Still, support for
@@ -548,7 +582,7 @@ class DispMemberGenerator(object):
548
582
  interface = m.restype.__com_interface__ # type: ignore
549
583
 
550
584
  def comitffunc(obj, *args, **kw):
551
- result = obj.Invoke(memid, _invkind=1, *args, **kw)
585
+ result = obj.Invoke(memid, _invkind=DISPATCH_METHOD, *args, **kw)
552
586
  if result is None:
553
587
  return
554
588
  return result.QueryInterface(interface)
@@ -556,7 +590,7 @@ class DispMemberGenerator(object):
556
590
  return comitffunc
557
591
 
558
592
  def func(obj, *args, **kw):
559
- return obj.Invoke(memid, _invkind=1, *args, **kw) # DISPATCH_METHOD
593
+ return obj.Invoke(memid, _invkind=DISPATCH_METHOD, *args, **kw)
560
594
 
561
595
  return func
562
596
 
@@ -1,5 +1,6 @@
1
1
  # comtypes._meta helper module
2
2
  from ctypes import POINTER, c_void_p, cast
3
+
3
4
  import comtypes
4
5
 
5
6
  ################################################################
@@ -60,7 +61,7 @@ class _coclass_meta(type):
60
61
  # XXX We should insist that a _reg_clsid_ is present.
61
62
  if "_reg_clsid_" in namespace:
62
63
  clsid = namespace["_reg_clsid_"]
63
- comtypes.com_coclass_registry[str(clsid)] = self
64
+ comtypes.com_coclass_registry[str(clsid)] = self # type: ignore
64
65
 
65
66
  # `_coclass_pointer_meta` is a subclass inherited from `_coclass_meta`.
66
67
  # In other words, when the `__new__` method of this metaclass is called, an
@@ -2,8 +2,7 @@ import types
2
2
  from _ctypes import COMError
3
3
  from typing import Type
4
4
 
5
- from comtypes import patcher
6
-
5
+ from comtypes import hresult, patcher
7
6
 
8
7
  _all_slice = slice(None, None, None)
9
8
 
@@ -97,8 +96,7 @@ def callable_and_subscriptable(itf: Type) -> None:
97
96
  try:
98
97
  result = self.Item(*args)
99
98
  except COMError as err:
100
- (hresult, text, details) = err.args
101
- if hresult == -2147352565: # DISP_E_BADINDEX
99
+ if err.hresult == hresult.DISP_E_BADINDEX:
102
100
  raise IndexError("invalid index")
103
101
  else:
104
102
  raise
@@ -116,8 +114,7 @@ def callable_and_subscriptable(itf: Type) -> None:
116
114
  try:
117
115
  self.Item[index] = value
118
116
  except COMError as err:
119
- (hresult, text, details) = err.args
120
- if hresult == -2147352565: # DISP_E_BADINDEX
117
+ if err.hresult == hresult.DISP_E_BADINDEX:
121
118
  raise IndexError("invalid index")
122
119
  else:
123
120
  raise
@@ -1,10 +1,15 @@
1
- from ctypes import _SimpleCData, windll
2
- from typing import Any, Callable, TYPE_CHECKING
1
+ from ctypes import WinDLL, _SimpleCData
2
+ from typing import TYPE_CHECKING, Any, Callable
3
3
 
4
4
  if TYPE_CHECKING:
5
5
  from comtypes import hints # type: ignore
6
6
 
7
7
 
8
+ _oleaut32 = WinDLL("oleaut32")
9
+
10
+ _SysFreeString = _oleaut32.SysFreeString
11
+
12
+
8
13
  class BSTR(_SimpleCData):
9
14
  """The windows BSTR data type"""
10
15
 
@@ -18,9 +23,7 @@ class BSTR(_SimpleCData):
18
23
  self._needsfree = True
19
24
  return self.value
20
25
 
21
- def __del__(
22
- self, _free: Callable[["BSTR"], Any] = windll.oleaut32.SysFreeString
23
- ) -> None:
26
+ def __del__(self, _free: Callable[["BSTR"], Any] = _SysFreeString) -> None:
24
27
  # Free the string if self owns the memory
25
28
  # or if instructed by __ctypes_from_outparam__.
26
29
  if self._b_base_ is None or self._needsfree:
@@ -35,3 +38,7 @@ class BSTR(_SimpleCData):
35
38
  # right thing, it doesn't ensure that SysFreeString is called
36
39
  # on destruction.
37
40
  return cls(value)
41
+
42
+
43
+ _SysFreeString.argtypes = [BSTR]
44
+ _SysFreeString.restype = None
@@ -1,5 +1,4 @@
1
- from ctypes import pythonapi, py_object
2
-
1
+ from ctypes import py_object, pythonapi
3
2
 
4
3
  pythonapi.PyInstanceMethod_New.argtypes = [py_object]
5
4
  pythonapi.PyInstanceMethod_New.restype = py_object
@@ -1,26 +1,28 @@
1
- from ctypes import c_ulong, c_ushort, c_void_p, c_wchar_p, HRESULT, Structure
2
- from ctypes import byref, cast, _Pointer, POINTER, pointer
3
-
4
- # fmt: off
5
- from typing import ( # noqa
6
- Any, overload, TYPE_CHECKING, TypeVar,
7
- # instead of `builtins`. see PEP585
8
- Type,
9
- # instead of `collections.abc`. see PEP585
10
- Callable,
11
- # instead of `A | B` and `None | A`. see PEP604
12
- Optional,
1
+ from ctypes import (
2
+ HRESULT,
3
+ POINTER,
4
+ OleDLL,
5
+ Structure,
6
+ byref,
7
+ c_ulong,
8
+ c_ushort,
9
+ c_void_p,
10
+ c_wchar_p,
11
+ cast,
12
+ pointer,
13
13
  )
14
- # fmt: on
15
- if TYPE_CHECKING:
16
- from comtypes import hints as hints # noqa # type: ignore
14
+ from ctypes.wintypes import DWORD, LPCWSTR, LPVOID
15
+ from typing import TYPE_CHECKING, Any, Callable, Optional, Type, TypeVar, overload
17
16
 
18
- from comtypes import GUID
19
- from comtypes import CLSCTX_SERVER, CLSCTX_LOCAL_SERVER, CLSCTX_REMOTE_SERVER
20
- from comtypes import _ole32, oledll, DWORD
17
+ from comtypes import CLSCTX_LOCAL_SERVER, CLSCTX_REMOTE_SERVER, CLSCTX_SERVER, GUID
21
18
  from comtypes._memberspec import COMMETHOD
22
19
  from comtypes._post_coinit.unknwn import IUnknown
23
20
 
21
+ if TYPE_CHECKING:
22
+ from ctypes import _Pointer
23
+
24
+ from comtypes import hints as hints # noqa # type: ignore
25
+
24
26
 
25
27
  def _is_object(obj):
26
28
  """This function determines if the argument is a COM object. It
@@ -98,7 +100,7 @@ def CoGetObject(displayname: str, interface: Optional[Type[IUnknown]]) -> IUnkno
98
100
  interface = IUnknown
99
101
  punk = POINTER(interface)()
100
102
  # Do we need a way to specify the BIND_OPTS parameter?
101
- _ole32.CoGetObject(str(displayname), None, byref(interface._iid_), byref(punk))
103
+ _CoGetObject(str(displayname), None, byref(interface._iid_), byref(punk))
102
104
  return punk # type: ignore
103
105
 
104
106
 
@@ -134,7 +136,7 @@ def CoCreateInstance(
134
136
  interface = IUnknown
135
137
  p = POINTER(interface)()
136
138
  iid = interface._iid_
137
- _ole32.CoCreateInstance(byref(clsid), punkouter, clsctx, byref(iid), byref(p))
139
+ _CoCreateInstance(byref(clsid), punkouter, clsctx, byref(iid), byref(p))
138
140
  return p # type: ignore
139
141
 
140
142
 
@@ -178,7 +180,7 @@ def GetActiveObject(
178
180
  ) -> IUnknown:
179
181
  """Retrieves a pointer to a running object"""
180
182
  p = POINTER(IUnknown)()
181
- oledll.oleaut32.GetActiveObject(byref(clsid), None, byref(p))
183
+ _GetActiveObject(byref(clsid), None, byref(p))
182
184
  if interface is not None:
183
185
  p = p.QueryInterface(interface) # type: ignore
184
186
  return p # type: ignore
@@ -236,6 +238,16 @@ class _COSERVERINFO(Structure):
236
238
  dwReserved2: int
237
239
 
238
240
 
241
+ _oleaut32 = OleDLL("oleaut32")
242
+
243
+ REFCLSID = POINTER(GUID)
244
+ _GetActiveObject = _oleaut32.GetActiveObject
245
+ _GetActiveObject.argtypes = [REFCLSID, LPVOID, POINTER(POINTER(IUnknown))]
246
+ _GetActiveObject.restype = HRESULT
247
+
248
+
249
+ _ole32 = OleDLL("ole32")
250
+
239
251
  COSERVERINFO = _COSERVERINFO
240
252
  _CoGetClassObject = _ole32.CoGetClassObject
241
253
  _CoGetClassObject.argtypes = [
@@ -245,6 +257,28 @@ _CoGetClassObject.argtypes = [
245
257
  POINTER(GUID),
246
258
  POINTER(c_void_p),
247
259
  ]
260
+ _CoGetClassObject.restype = HRESULT
261
+
262
+ _CoCreateInstance = _ole32.CoCreateInstance
263
+ _CoCreateInstance.argtypes = [
264
+ REFCLSID,
265
+ POINTER(IUnknown),
266
+ DWORD,
267
+ POINTER(GUID),
268
+ POINTER(LPVOID),
269
+ ]
270
+ _CoCreateInstance.restype = HRESULT
271
+
272
+ _CoCreateInstanceEx = _ole32.CoCreateInstanceEx
273
+ _CoCreateInstanceEx.argtypes = [
274
+ REFCLSID,
275
+ POINTER(IUnknown),
276
+ DWORD,
277
+ POINTER(COSERVERINFO),
278
+ DWORD,
279
+ POINTER(MULTI_QI),
280
+ ]
281
+ _CoCreateInstanceEx.restype = HRESULT
248
282
 
249
283
 
250
284
  class tagBIND_OPTS(Structure):
@@ -258,6 +292,9 @@ class tagBIND_OPTS(Structure):
258
292
 
259
293
  # XXX Add __init__ which sets cbStruct?
260
294
  BIND_OPTS = tagBIND_OPTS
295
+ _CoGetObject = _ole32.CoGetObject
296
+ _CoGetObject.argtypes = [LPCWSTR, POINTER(BIND_OPTS), POINTER(GUID), POINTER(LPVOID)]
297
+ _CoGetObject.restype = HRESULT
261
298
 
262
299
 
263
300
  class tagBIND_OPTS2(Structure):
@@ -360,7 +397,5 @@ def CoCreateInstanceEx(
360
397
  interface = IUnknown
361
398
  multiqi = MULTI_QI()
362
399
  multiqi.pIID = pointer(interface._iid_) # type: ignore
363
- _ole32.CoCreateInstanceEx(
364
- byref(clsid), None, clsctx, pServerInfo, 1, byref(multiqi)
365
- )
400
+ _CoCreateInstanceEx(byref(clsid), None, clsctx, pServerInfo, 1, byref(multiqi))
366
401
  return cast(multiqi.pItf, POINTER(interface)) # type: ignore