1
+ import _typeshed
1
2
import sys
2
3
from _typeshed import ReadableBuffer , WriteableBuffer
3
4
from abc import abstractmethod
4
5
from collections .abc import Callable , Iterable , Iterator , Mapping , Sequence
5
6
from ctypes import CDLL , ArgumentError as ArgumentError , c_void_p
6
- from typing import Any , ClassVar , Generic , TypeVar , overload
7
+ from typing import Any , ClassVar , Generic , TypeVar , final , overload , type_check_only
7
8
from typing_extensions import Self , TypeAlias
8
9
9
10
if sys .version_info >= (3 , 9 ):
@@ -47,46 +48,73 @@ if sys.platform == "win32":
47
48
def LoadLibrary (name : str , load_flags : int = 0 , / ) -> int : ...
48
49
def FreeLibrary (handle : int , / ) -> None : ...
49
50
50
- class _CDataMeta (type ):
51
- # By default mypy complains about the following two methods, because strictly speaking cls
52
- # might not be a Type[_CT]. However this can never actually happen, because the only class that
53
- # uses _CDataMeta as its metaclass is _CData. So it's safe to ignore the errors here.
54
- def __mul__ (cls : type [_CT ], other : int ) -> type [Array [_CT ]]: ... # type: ignore[misc] # pyright: ignore[reportGeneralTypeIssues]
55
- def __rmul__ (cls : type [_CT ], other : int ) -> type [Array [_CT ]]: ... # type: ignore[misc] # pyright: ignore[reportGeneralTypeIssues]
51
+ if sys .version_info >= (3 , 13 ):
52
+ # This class is not exposed. It calls itself _ctypes.CType_Type.
53
+ @type_check_only
54
+ class _CType_Type (type ):
55
+ # By default mypy complains about the following two methods, because strictly speaking cls
56
+ # might not be a Type[_CT]. However this doesn't happen because this is only a
57
+ # metaclass for subclasses of _CData.
58
+ def __mul__ (cls : type [_CT ], other : int ) -> type [Array [_CT ]]: ... # type: ignore[misc] # pyright: ignore[reportGeneralTypeIssues]
59
+ def __rmul__ (cls : type [_CT ], other : int ) -> type [Array [_CT ]]: ... # type: ignore[misc] # pyright: ignore[reportGeneralTypeIssues]
56
60
57
- class _CData (metaclass = _CDataMeta ):
61
+ _CTypeBaseType = _CType_Type
62
+
63
+ else :
64
+ _CTypeBaseType = type
65
+
66
+ # This class is not exposed.
67
+ @type_check_only
68
+ class _CData :
58
69
_b_base_ : int
59
70
_b_needsfree_ : bool
60
71
_objects : Mapping [Any , int ] | None
61
- # At runtime the following classmethods are available only on classes, not
62
- # on instances. This can't be reflected properly in the type system:
63
- #
64
- # Structure.from_buffer(...) # valid at runtime
65
- # Structure(...).from_buffer(...) # invalid at runtime
66
- #
67
- @classmethod
68
- def from_buffer (cls , source : WriteableBuffer , offset : int = ...) -> Self : ...
69
- @classmethod
70
- def from_buffer_copy (cls , source : ReadableBuffer , offset : int = ...) -> Self : ...
71
- @classmethod
72
- def from_address (cls , address : int ) -> Self : ...
73
- @classmethod
74
- def from_param (cls , value : Any , / ) -> Self | _CArgObject : ...
75
- @classmethod
76
- def in_dll (cls , library : CDLL , name : str ) -> Self : ...
77
72
def __buffer__ (self , flags : int , / ) -> memoryview : ...
78
- def __release_buffer__ (self , buffer : memoryview , / ) -> None : ...
73
+ def __ctypes_from_outparam__ (self , / ) -> Self : ...
74
+
75
+ # this is a union of all the subclasses of _CData, which is useful because of
76
+ # the methods that are present on each of those subclasses which are not present
77
+ # on _CData itself.
78
+ _CDataType : TypeAlias = _SimpleCData [Any ] | _Pointer [Any ] | CFuncPtr | Union | Structure | Array [Any ]
79
79
80
- class _SimpleCData (_CData , Generic [_T ]):
80
+ # This class is not exposed. It calls itself _ctypes.PyCSimpleType.
81
+ @type_check_only
82
+ class _PyCSimpleType (_CTypeBaseType ):
83
+ def from_address (self : type [_typeshed .Self ], value : int , / ) -> _typeshed .Self : ...
84
+ def from_buffer (self : type [_typeshed .Self ], obj : WriteableBuffer , offset : int = 0 , / ) -> _typeshed .Self : ...
85
+ def from_buffer_copy (self : type [_typeshed .Self ], buffer : ReadableBuffer , offset : int = 0 , / ) -> _typeshed .Self : ...
86
+ def from_param (self : type [_typeshed .Self ], value : Any , / ) -> _typeshed .Self | _CArgObject : ...
87
+ def in_dll (self : type [_typeshed .Self ], dll : CDLL , name : str , / ) -> _typeshed .Self : ...
88
+ if sys .version_info < (3 , 13 ):
89
+ # Inherited from CType_Type starting on 3.13
90
+ def __mul__ (self : type [_CT ], value : int , / ) -> type [Array [_CT ]]: ... # type: ignore[misc] # pyright: ignore[reportGeneralTypeIssues]
91
+ def __rmul__ (self : type [_CT ], value : int , / ) -> type [Array [_CT ]]: ... # type: ignore[misc] # pyright: ignore[reportGeneralTypeIssues]
92
+
93
+ class _SimpleCData (_CData , Generic [_T ], metaclass = _PyCSimpleType ):
81
94
value : _T
82
95
# The TypeVar can be unsolved here,
83
96
# but we can't use overloads without creating many, many mypy false-positive errors
84
97
def __init__ (self , value : _T = ...) -> None : ... # pyright: ignore[reportInvalidTypeVarUse]
98
+ def __ctypes_from_outparam__ (self , / ) -> _T : ... # type: ignore[override]
85
99
86
100
class _CanCastTo (_CData ): ...
87
101
class _PointerLike (_CanCastTo ): ...
88
102
89
- class _Pointer (_PointerLike , _CData , Generic [_CT ]):
103
+ # This type is not exposed. It calls itself _ctypes.PyCPointerType.
104
+ @type_check_only
105
+ class _PyCPointerType (_CTypeBaseType ):
106
+ def from_address (self : type [_typeshed .Self ], value : int , / ) -> _typeshed .Self : ...
107
+ def from_buffer (self : type [_typeshed .Self ], obj : WriteableBuffer , offset : int = 0 , / ) -> _typeshed .Self : ...
108
+ def from_buffer_copy (self : type [_typeshed .Self ], buffer : ReadableBuffer , offset : int = 0 , / ) -> _typeshed .Self : ...
109
+ def from_param (self : type [_typeshed .Self ], value : Any , / ) -> _typeshed .Self | _CArgObject : ...
110
+ def in_dll (self : type [_typeshed .Self ], dll : CDLL , name : str , / ) -> _typeshed .Self : ...
111
+ def set_type (self , type : Any , / ) -> None : ...
112
+ if sys .version_info < (3 , 13 ):
113
+ # Inherited from CType_Type starting on 3.13
114
+ def __mul__ (cls : type [_CT ], other : int ) -> type [Array [_CT ]]: ... # type: ignore[misc] # pyright: ignore[reportGeneralTypeIssues]
115
+ def __rmul__ (cls : type [_CT ], other : int ) -> type [Array [_CT ]]: ... # type: ignore[misc] # pyright: ignore[reportGeneralTypeIssues]
116
+
117
+ class _Pointer (_PointerLike , _CData , Generic [_CT ], metaclass = _PyCPointerType ):
90
118
_type_ : type [_CT ]
91
119
contents : _CT
92
120
@overload
@@ -105,16 +133,32 @@ def POINTER(type: None, /) -> type[c_void_p]: ...
105
133
def POINTER (type : type [_CT ], / ) -> type [_Pointer [_CT ]]: ...
106
134
def pointer (obj : _CT , / ) -> _Pointer [_CT ]: ...
107
135
136
+ # This class is not exposed. It calls itself _ctypes.CArgObject.
137
+ @final
138
+ @type_check_only
108
139
class _CArgObject : ...
109
140
110
- def byref (obj : _CData , offset : int = ...) -> _CArgObject : ...
141
+ def byref (obj : _CData | _CDataType , offset : int = ...) -> _CArgObject : ...
111
142
112
- _ECT : TypeAlias = Callable [[_CData | None , CFuncPtr , tuple [_CData , ...]], _CData ]
143
+ _ECT : TypeAlias = Callable [[_CData | _CDataType | None , CFuncPtr , tuple [_CData | _CDataType , ...]], _CDataType ]
113
144
_PF : TypeAlias = tuple [int ] | tuple [int , str | None ] | tuple [int , str | None , Any ]
114
145
115
- class CFuncPtr (_PointerLike , _CData ):
116
- restype : type [_CData ] | Callable [[int ], Any ] | None
117
- argtypes : Sequence [type [_CData ]]
146
+ # This class is not exposed. It calls itself _ctypes.PyCFuncPtrType.
147
+ @type_check_only
148
+ class _PyCFuncPtrType (_CTypeBaseType ):
149
+ def from_address (self : type [_typeshed .Self ], value : int , / ) -> _typeshed .Self : ...
150
+ def from_buffer (self : type [_typeshed .Self ], obj : WriteableBuffer , offset : int = 0 , / ) -> _typeshed .Self : ...
151
+ def from_buffer_copy (self : type [_typeshed .Self ], buffer : ReadableBuffer , offset : int = 0 , / ) -> _typeshed .Self : ...
152
+ def from_param (self : type [_typeshed .Self ], value : Any , / ) -> _typeshed .Self | _CArgObject : ...
153
+ def in_dll (self : type [_typeshed .Self ], dll : CDLL , name : str , / ) -> _typeshed .Self : ...
154
+ if sys .version_info < (3 , 13 ):
155
+ # Inherited from CType_Type starting on 3.13
156
+ def __mul__ (cls : type [_CT ], other : int ) -> type [Array [_CT ]]: ... # type: ignore[misc] # pyright: ignore[reportGeneralTypeIssues]
157
+ def __rmul__ (cls : type [_CT ], other : int ) -> type [Array [_CT ]]: ... # type: ignore[misc] # pyright: ignore[reportGeneralTypeIssues]
158
+
159
+ class CFuncPtr (_PointerLike , _CData , metaclass = _PyCFuncPtrType ):
160
+ restype : type [_CDataType ] | Callable [[int ], Any ] | None
161
+ argtypes : Sequence [type [_CDataType ]]
118
162
errcheck : _ECT
119
163
# Abstract attribute that must be defined on subclasses
120
164
_flags_ : ClassVar [int ]
@@ -129,38 +173,103 @@ class CFuncPtr(_PointerLike, _CData):
129
173
if sys .platform == "win32" :
130
174
@overload
131
175
def __init__ (
132
- self , vtbl_index : int , name : str , paramflags : tuple [_PF , ...] | None = ..., iid : _CData | None = ..., /
176
+ self , vtbl_index : int , name : str , paramflags : tuple [_PF , ...] | None = ..., iid : _CData | _CDataType | None = ..., /
133
177
) -> None : ...
134
178
135
179
def __call__ (self , * args : Any , ** kwargs : Any ) -> Any : ...
136
180
137
181
_GetT = TypeVar ("_GetT" )
138
182
_SetT = TypeVar ("_SetT" )
139
183
184
+ # This class is not exposed. It calls itself _ctypes.CField.
185
+ @final
186
+ @type_check_only
140
187
class _CField (Generic [_CT , _GetT , _SetT ]):
141
188
offset : int
142
189
size : int
143
- @overload
144
- def __get__ (self , instance : None , owner : type [Any ] | None , / ) -> Self : ...
145
- @overload
146
- def __get__ (self , instance : Any , owner : type [Any ] | None , / ) -> _GetT : ...
190
+ if sys .version_info >= (3 , 10 ):
191
+ @overload
192
+ def __get__ (self , instance : None , owner : type [Any ] | None = None , / ) -> Self : ...
193
+ @overload
194
+ def __get__ (self , instance : Any , owner : type [Any ] | None = None , / ) -> _GetT : ...
195
+ else :
196
+ @overload
197
+ def __get__ (self , instance : None , owner : type [Any ] | None , / ) -> Self : ...
198
+ @overload
199
+ def __get__ (self , instance : Any , owner : type [Any ] | None , / ) -> _GetT : ...
200
+
147
201
def __set__ (self , instance : Any , value : _SetT , / ) -> None : ...
148
202
149
- class _StructUnionMeta (_CDataMeta ):
150
- _fields_ : Sequence [tuple [str , type [_CData ]] | tuple [str , type [_CData ], int ]]
151
- _pack_ : int
152
- _anonymous_ : Sequence [str ]
203
+ # This class is not exposed. It calls itself _ctypes.UnionType.
204
+ @type_check_only
205
+ class _UnionType (_CTypeBaseType ):
206
+ def from_address (self : type [_typeshed .Self ], value : int , / ) -> _typeshed .Self : ...
207
+ def from_buffer (self : type [_typeshed .Self ], obj : WriteableBuffer , offset : int = 0 , / ) -> _typeshed .Self : ...
208
+ def from_buffer_copy (self : type [_typeshed .Self ], buffer : ReadableBuffer , offset : int = 0 , / ) -> _typeshed .Self : ...
209
+ def from_param (self : type [_typeshed .Self ], value : Any , / ) -> _typeshed .Self | _CArgObject : ...
210
+ def in_dll (self : type [_typeshed .Self ], dll : CDLL , name : str , / ) -> _typeshed .Self : ...
211
+ # At runtime, various attributes are created on a Union subclass based
212
+ # on its _fields_. This method doesn't exist, but represents those
213
+ # dynamically created attributes.
214
+ def __getattr__ (self , name : str ) -> _CField [Any , Any , Any ]: ...
215
+ if sys .version_info < (3 , 13 ):
216
+ # Inherited from CType_Type starting on 3.13
217
+ def __mul__ (cls : type [_CT ], other : int ) -> type [Array [_CT ]]: ... # type: ignore[misc] # pyright: ignore[reportGeneralTypeIssues]
218
+ def __rmul__ (cls : type [_CT ], other : int ) -> type [Array [_CT ]]: ... # type: ignore[misc] # pyright: ignore[reportGeneralTypeIssues]
219
+
220
+ class Union (_CData , metaclass = _UnionType ):
221
+ _fields_ : ClassVar [Sequence [tuple [str , type [_CDataType ]] | tuple [str , type [_CDataType ], int ]]]
222
+ _pack_ : ClassVar [int ]
223
+ _anonymous_ : ClassVar [Sequence [str ]]
224
+ if sys .version_info >= (3 , 13 ):
225
+ _align_ : ClassVar [int ]
226
+
227
+ def __init__ (self , * args : Any , ** kw : Any ) -> None : ...
228
+ def __getattr__ (self , name : str ) -> Any : ...
229
+ def __setattr__ (self , name : str , value : Any ) -> None : ...
230
+
231
+ # This class is not exposed. It calls itself _ctypes.PyCStructType.
232
+ @type_check_only
233
+ class _PyCStructType (_CTypeBaseType ):
234
+ def from_address (self : type [_typeshed .Self ], value : int , / ) -> _typeshed .Self : ...
235
+ def from_buffer (self : type [_typeshed .Self ], obj : WriteableBuffer , offset : int = 0 , / ) -> _typeshed .Self : ...
236
+ def from_buffer_copy (self : type [_typeshed .Self ], buffer : ReadableBuffer , offset : int = 0 , / ) -> _typeshed .Self : ...
237
+ def from_param (self : type [_typeshed .Self ], value : Any , / ) -> _typeshed .Self | _CArgObject : ...
238
+ def in_dll (self : type [_typeshed .Self ], dll : CDLL , name : str , / ) -> _typeshed .Self : ...
239
+ # At runtime, various attributes are created on a Structure subclass based
240
+ # on its _fields_. This method doesn't exist, but represents those
241
+ # dynamically created attributes.
153
242
def __getattr__ (self , name : str ) -> _CField [Any , Any , Any ]: ...
243
+ if sys .version_info < (3 , 13 ):
244
+ # Inherited from CType_Type starting on 3.13
245
+ def __mul__ (cls : type [_CT ], other : int ) -> type [Array [_CT ]]: ... # type: ignore[misc] # pyright: ignore[reportGeneralTypeIssues]
246
+ def __rmul__ (cls : type [_CT ], other : int ) -> type [Array [_CT ]]: ... # type: ignore[misc] # pyright: ignore[reportGeneralTypeIssues]
247
+
248
+ class Structure (_CData , metaclass = _PyCStructType ):
249
+ _fields_ : ClassVar [Sequence [tuple [str , type [_CDataType ]] | tuple [str , type [_CDataType ], int ]]]
250
+ _pack_ : ClassVar [int ]
251
+ _anonymous_ : ClassVar [Sequence [str ]]
252
+ if sys .version_info >= (3 , 13 ):
253
+ _align_ : ClassVar [int ]
154
254
155
- class _StructUnionBase (_CData , metaclass = _StructUnionMeta ):
156
255
def __init__ (self , * args : Any , ** kw : Any ) -> None : ...
157
256
def __getattr__ (self , name : str ) -> Any : ...
158
257
def __setattr__ (self , name : str , value : Any ) -> None : ...
159
258
160
- class Union (_StructUnionBase ): ...
161
- class Structure (_StructUnionBase ): ...
259
+ # This class is not exposed. It calls itself _ctypes.PyCArrayType.
260
+ @type_check_only
261
+ class _PyCArrayType (_CTypeBaseType ):
262
+ def from_address (self : type [_typeshed .Self ], value : int , / ) -> _typeshed .Self : ...
263
+ def from_buffer (self : type [_typeshed .Self ], obj : WriteableBuffer , offset : int = 0 , / ) -> _typeshed .Self : ...
264
+ def from_buffer_copy (self : type [_typeshed .Self ], buffer : ReadableBuffer , offset : int = 0 , / ) -> _typeshed .Self : ...
265
+ def from_param (self : type [_typeshed .Self ], value : Any , / ) -> _typeshed .Self | _CArgObject : ...
266
+ def in_dll (self : type [_typeshed .Self ], dll : CDLL , name : str , / ) -> _typeshed .Self : ...
267
+ if sys .version_info < (3 , 13 ):
268
+ # Inherited from CType_Type starting on 3.13
269
+ def __mul__ (cls : type [_CT ], other : int ) -> type [Array [_CT ]]: ... # type: ignore[misc] # pyright: ignore[reportGeneralTypeIssues]
270
+ def __rmul__ (cls : type [_CT ], other : int ) -> type [Array [_CT ]]: ... # type: ignore[misc] # pyright: ignore[reportGeneralTypeIssues]
162
271
163
- class Array (_CData , Generic [_CT ]):
272
+ class Array (_CData , Generic [_CT ], metaclass = _PyCArrayType ):
164
273
@property
165
274
@abstractmethod
166
275
def _length_ (self ) -> int : ...
@@ -205,9 +314,9 @@ class Array(_CData, Generic[_CT]):
205
314
if sys .version_info >= (3 , 9 ):
206
315
def __class_getitem__ (cls , item : Any , / ) -> GenericAlias : ...
207
316
208
- def addressof (obj : _CData , / ) -> int : ...
209
- def alignment (obj_or_type : _CData | type [_CData ], / ) -> int : ...
317
+ def addressof (obj : _CData | _CDataType , / ) -> int : ...
318
+ def alignment (obj_or_type : _CData | _CDataType | type [_CData | _CDataType ], / ) -> int : ...
210
319
def get_errno () -> int : ...
211
- def resize (obj : _CData , size : int , / ) -> None : ...
320
+ def resize (obj : _CData | _CDataType , size : int , / ) -> None : ...
212
321
def set_errno (value : int , / ) -> int : ...
213
- def sizeof (obj_or_type : _CData | type [_CData ], / ) -> int : ...
322
+ def sizeof (obj_or_type : _CData | _CDataType | type [_CData | _CDataType ], / ) -> int : ...
0 commit comments