-
Notifications
You must be signed in to change notification settings - Fork 83
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
Correct definition of __slots__
on structs (must be defined in class body)
#183
Comments
Looks like a good idea to me.
It is a Python feat: https://wiki.python.org/moin/UsingSlots |
https://wiki.python.org/moin/UsingSlots indeed claims
This does not seem to hold true with structs, though... |
class struct__FPDF_SYSTEMTIME(Structure): It is a class, isn’t it? |
I suppose it is? >>> import pypdfium2.raw
>>> import inspect
>>> inspect.isclass(pypdfium2.raw.FPDF_SYSTEMTIME)
True However, structs do seem a bit odd, according to https://stackoverflow.com/q/76887146/15547292 overriding |
Aah, the slots definition needs to be in the class body, like so >>> import ctypes
>>> class FPDF_SYSTEMTIME (ctypes.Structure):
... _fields_ = [("wYear", ctypes.c_ushort)]
... __slots__ = ["wYear"]
...
>>> st = FPDF_SYSTEMTIME()
>>> st.w_year = 1
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'FPDF_SYSTEMTIME' object has no attribute 'w_year'
>>> st.wYear = 1 Then it actually prevents bad fields as expected. |
__slots__
on structs__slots__
on structs (must be defined in class body)
See pypdfium2-team@5148408 for corrected fix. |
Currently what struct output looks like with ctypesgen is this
We can see that field names are effectively listed twice, which increases file length unnecessarily.
__slots__
can be easily derived from_fields_
using a list comprehension, like thisApart from that, what are
__slots__
supposed to achieve here? AFAICS, they're never mentioned in the ctypes docs.And according to my testing, they don't prevent setting non-existent fields.
The text was updated successfully, but these errors were encountered: