-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Using ctypes.CField
for annotations of fields within ctypes.Structure
-like
#10567
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
I know python/cpython#104533. It might be necessary to communicate with stakeholders in https://discuss.python.org/t/annotation-based-sugar-for-ctypes/26579/ . |
I've only looked briefly at your proposal, but using the descriptor protocol looks indeed to be the right solution in this case. An exploratory PR to judge the effects of such a change would be welcome! |
While working, I noticed something. >>> import ctypes
>>> class Foo(ctypes.Structure):
... pass
...
>>> Foo._fields_ = [('x', (ctypes.c_wchar * 14))]
>>> Foo.x
<Field type=c_wchar_Array_14, ofs=0, size=28>
>>> foo = Foo()
>>> foo.x
''
>>> foo.x = 'spam'
>>> foo.x
'spam'
>>> y = (ctypes.c_wchar * 14)()
>>> y.value = 'ham'
>>> foo.x = y
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unicode string expected instead of c_wchar_Array_14 instance Therefore, I believe it would be more appropriate to have a descriptor protocol with below three type parameters;
They are such as |
In the initial PR, I intend to make changes only to |
I PRed #10595. |
#10595 is merged. |
Sorry to comment on a closed issue. I posted the following issue on pyright: microsoft/pyright#5932 (comment) which redirected me back here. In the following example, both using import ctypes
from typing import ClassVar
class MyStruct(ctypes.Structure):
_fields_ = [("a", ctypes.c_uint32)]
class MyClass:
_struct: ClassVar = MyStruct
@classmethod
def something(cls, encoded: bytes):
return cls._struct.from_buffer_copy(encoded)
MyStruct.from_buffer_copy(b"1234") I have not used the ctypes library a lot, am i doing something wrong by calling a classmethod (e.g |
The curious thing is that
However, Before I changed the type stubs, Is there some interaction between the descriptor protocol and metaclasses that I'm not aware of? |
I have the understanding that the issue has been resolved, as mentioned below. #10777 |
Uh oh!
There was an error while loading. Please reload this page.
Currently, attributes of
ctypes.Structure
, such asctypes.wintypes.POINT
, are annotated with names and types that specified in_fields_
.typeshed/stdlib/ctypes/wintypes.pyi
Lines 126 to 128 in df08fce
The problem with this approach is that at runtime, the attributes return types like
int
orbytes
, while type checkers interpret them as returningc_int
orc_char
.Below is the simplest example of a structure created for explanation purposes.
At runtime, the types of these fields are
ctypes.CField
. In the example below,x
is a data descriptor that returnsint
as the getter and accepts bothc_int
andint
as the setter.Having stubs with types that deviate from the runtime is not an ideal situation.
Therefore, I propose modifying
CField
as shown below for use in annotating fields.another idea
I thought it elegant to specify only subclasses of
_SimpleCData[_T]
as type parameters, given the potential for inferring_T
, as shown below.However, current static type system cannot that.
Moreover, considering that there exist not only subclasses of
_SimpleCData
defined withinctypes
, but also third-party developers who define_SimpleCData
subclasses within their own projects.There is the redundancy, but I believe that utilizing the two type parameters would enhance flexibility.
The text was updated successfully, but these errors were encountered: