This issue tracker has been migrated to GitHub ,
and is currently read-only.
For more information,
see the GitHub FAQs in the Python's Developer Guide.
Created on 2016年03月17日 08:43 by serhiy.storchaka, last changed 2022年04月11日 14:58 by admin. This issue is now closed.
| Files | ||||
|---|---|---|---|---|
| File name | Uploaded | Description | Edit | |
| copyreg_getstate.patch | serhiy.storchaka, 2016年03月17日 08:43 | review | ||
| copyreg_getstate2.patch | serhiy.storchaka, 2016年06月18日 19:59 | review | ||
| object_getstate.patch | serhiy.storchaka, 2016年07月14日 11:50 | Add object.__getstate__ | review | |
| Pull Requests | |||
|---|---|---|---|
| URL | Status | Linked | Edit |
| PR 2821 | merged | serhiy.storchaka, 2017年07月23日 08:32 | |
| Messages (5) | |||
|---|---|---|---|
| msg261903 - (view) | Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) | Date: 2016年03月17日 08:43 | |
Pickling and copying instances of subclasses of some basic classes pickles and copies instance attributes. Example:
>>> class BA(bytearray):
... pass
...
>>> b = BA(b'abc')
>>> b.x = 10
>>> c = copy.copy(b)
>>> c.x
10
>>> c = pickle.loads(pickle.dumps(b))
>>> c.x
10
But this doesn't work if attributes are saved not in instance dictionary, but in slots.
>>> class BA(bytearray):
... __slots__ = ('x',)
...
>>> b = BA(b'abc')
>>> b.x = 10
>>> c = copy.copy(b)
>>> c.x
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: x
>>> c = pickle.loads(pickle.dumps(b))
>>> c.x
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: x
Since using __slots__ is implementation detail, this failure can be considered as a bug.
Proposed patch adds support of pickling and copying slots in subclasses of all classes that already support pickling and copying non-slot attributes. It is backward compatible, classes with slots can be unpickled on older Python versions without slots. Affected classes: bytearray, set, frozenset, weakref.WeakSet, collections.OrderedDict, collections.deque, datetime.tzinfo.
The patch adds the copyreg._getstate() function for Python classes and exposes the _PyObject_GetState() function for extension classes. An alternative (and simpler for end user) solution would be to add default implementation as object.__getstate__(). But this is not easy to reject non-pickleable classes (issue22995) in this case, since __getstate__ is looked up as instance attribute, not as other special methods.
|
|||
| msg268830 - (view) | Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) | Date: 2016年06月18日 19:59 | |
Synchronized with current sources. |
|||
| msg270403 - (view) | Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) | Date: 2016年07月14日 11:50 | |
An alternative way is to expose a default state as object.__getstate__(). It is more efficient since it is implemented in C. Following patch implements this approach. |
|||
| msg416888 - (view) | Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) | Date: 2022年04月06日 17:00 | |
New changeset 884eba3c76916889fd6bff3b37b8552bfb4f9566 by Serhiy Storchaka in branch 'main': bpo-26579: Add object.__getstate__(). (GH-2821) https://github.com/python/cpython/commit/884eba3c76916889fd6bff3b37b8552bfb4f9566 |
|||
| msg416931 - (view) | Author: STINNER Victor (vstinner) * (Python committer) | Date: 2022年04月07日 14:57 | |
> bpo-26579: Add object.__getstate__(). (GH-2821) > https://github.com/python/cpython/commit/884eba3c76916889fd6bff3b37b8552bfb4f9566 This change introduced reference leaks: see bpo-47250. |
|||
| History | |||
|---|---|---|---|
| Date | User | Action | Args |
| 2022年04月11日 14:58:28 | admin | set | github: 70766 |
| 2022年04月07日 14:57:28 | vstinner | set | messages: + msg416931 |
| 2022年04月06日 17:02:02 | serhiy.storchaka | set | status: open -> closed stage: patch review -> resolved resolution: fixed versions: + Python 3.11, - Python 3.7 |
| 2022年04月06日 17:00:19 | serhiy.storchaka | set | messages: + msg416888 |
| 2020年08月14日 06:25:45 | serhiy.storchaka | link | issue41547 superseder |
| 2017年11月15日 21:15:23 | serhiy.storchaka | set | pull_requests: - pull_request4360 |
| 2017年11月15日 21:14:27 | yselivanov | set | pull_requests: + pull_request4360 |
| 2017年07月23日 08:33:50 | serhiy.storchaka | set | nosy:
+ vstinner, r.david.murray versions: + Python 3.7, - Python 3.6 |
| 2017年07月23日 08:32:55 | serhiy.storchaka | set | pull_requests: + pull_request2873 |
| 2016年07月14日 11:50:18 | serhiy.storchaka | set | files:
+ object_getstate.patch messages: + msg270403 |
| 2016年06月18日 19:59:45 | serhiy.storchaka | set | files:
+ copyreg_getstate2.patch messages: + msg268830 |
| 2016年03月19日 12:04:32 | berker.peksag | set | nosy:
+ berker.peksag |
| 2016年03月17日 08:43:06 | serhiy.storchaka | create | |