msg301034 - (view) |
Author: Oren Milman (Oren Milman) * |
Date: 2017-08-30 19:29 |
The following code causes PyCData_setstate() (in Modules/_ctypes/_ctypes.c) to raise a SystemError: 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 PyArg_ParseTuple() to "!Os#", so that the following would raise a TypeError: import ctypes class MyStruct(ctypes.Structure): pass MyStruct().__setstate__(42, b'foo') AttributeError: 'int' object has no attribute 'keys' what do you think? |
|
|
msg301035 - (view) |
Author: Oren Milman (Oren Milman) * |
Date: 2017-08-30 19:31 |
typo - change the format to "O!s#" |
|
|
msg301036 - (view) |
Author: Serhiy Storchaka (serhiy.storchaka) *  |
Date: 2017-08-30 20:58 |
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. |
|
|
msg301807 - (view) |
Author: Oren Milman (Oren Milman) * |
Date: 2017-09-10 10:34 |
just in case it was missed - I have opened two PRs for this issue. |
|
|
msg302840 - (view) |
Author: Serhiy Storchaka (serhiy.storchaka) *  |
Date: 2017-09-24 09:21 |
New changeset 4facdf523aa6967487a9425f124da9661b59fd43 by Serhiy Storchaka (Oren Milman) in branch 'master': bpo-31311: Impove error reporting in case the first argument to PyCData_setstate() isn't a dictionary. (#3255) https://github.com/python/cpython/commit/4facdf523aa6967487a9425f124da9661b59fd43 |
|
|
msg302841 - (view) |
Author: Serhiy Storchaka (serhiy.storchaka) *  |
Date: 2017-09-24 09:49 |
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. |
|
|
msg302914 - (view) |
Author: Oren Milman (Oren Milman) * |
Date: 2017-09-25 07:20 |
> But this is a separate issue, 3.7 only. I don't think i understand what this issue would include. Anyway, i updated the PR according to your comments. |
|
|
msg302919 - (view) |
Author: Serhiy Storchaka (serhiy.storchaka) *  |
Date: 2017-09-25 08:09 |
New changeset 57c2561c8c5663aef55b00e3f29cba575ff36ccd by Serhiy Storchaka (Oren Milman) in branch 'master': bpo-31311: Fix a SystemError and a crash in ctypes._CData.__setstate__(), in case of a bad __dict__. (#3254) https://github.com/python/cpython/commit/57c2561c8c5663aef55b00e3f29cba575ff36ccd |
|
|
msg302928 - (view) |
Author: Serhiy Storchaka (serhiy.storchaka) *  |
Date: 2017-09-25 08:49 |
New changeset a6bddb8e4397df30926eb1ade6420a2277174227 by Serhiy Storchaka (Miss Islington (bot)) in branch '3.6': [3.6] bpo-31311: Fix a SystemError and a crash in ctypes._CData.__setstate__(), in case of a bad __dict__. (GH-3254) (#3743) https://github.com/python/cpython/commit/a6bddb8e4397df30926eb1ade6420a2277174227 |
|
|
msg303099 - (view) |
Author: Serhiy Storchaka (serhiy.storchaka) *  |
Date: 2017-09-27 06:24 |
New changeset 81691b05484a68df9cebb1310dd7dcabb3c6eb0a by Serhiy Storchaka in branch '2.7': [2.7] bpo-31311: Fix a SystemError and a crash in ctypes._CData.__setstate__(), in case of a bad __dict__. (GH-3254). (#3781) https://github.com/python/cpython/commit/81691b05484a68df9cebb1310dd7dcabb3c6eb0a |
|
|