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
a SystemError and a crash in PyCData_setstate() when __dict__ is bad #75492
Comments
The following code causes PyCData_setstate() (in Modules/_ctypes/_ctypes.c) to import ctypes
class BadStruct(ctypes.Structure):
def __dict__(self):
pass BadStruct().__setstate__({}, b'foo') this is because PyCData_setstate() assumes that the __dict__ attribute is a dict. while we are here, I wonder whether we should change the format given to import ctypes
class MyStruct(ctypes.Structure):
pass MyStruct().__setstate__(42, b'foo') AttributeError: 'int' object has no attribute 'keys' what do you think? |
typo - change the format to "O!s#" |
And the following example results in segmentation fault. import ctypes
class BadStruct(ctypes.Structure):
@property
def __dict__(self):
raise AttributeError BadStruct().__setstate__({}, b'') As for change the format given to PyArg_ParseTuple(), I think it can be done only in master. It is better to make a separate PR for this. |
just in case it was missed - I have opened two PRs for this issue. |
I thought about this issue so long because I can't find good cause for __dict__ to be not a dict. Defining the __dict__ method or property doesn't affect instance dictionary and attributes lookup. It just breaks the __setstate__ method. Without having good example of overriding __dict__ I have no good test cases and don't know what a way of handling this error is better. There is yet one disadvantage of the current implementation. The instance's dict can be lazy. It can be created only on demand, when instance's attribute is set or the __dict__ attribute is read. PyObject_GetAttrString(myself, "__dict__") creates it if it was not created. It would be more efficient to use _PyObject_GetDictPtr(). But this is a separate issue, 3.7 only. |
Anyway, i updated the PR according to your comments. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: