jcea · GitHub

@jcea

Bug report

Bug description:

In Python releases previous to 3.13, the following code used to work (this idiom is used in Durus and ZODB persistence systems):

Python 3.12.7 (main, Oct  3 2024, 03:21:52) [GCC 10.5.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from pickle import Unpickler
>>> a=Unpickler(open('/dev/zero'))
>>> a.persistent_load=lambda x: x
>>>

Fine so far.

In Python 3.13, this doesn't work anymore:

Python 3.13.0 (main, Oct  9 2024, 14:54:06) [GCC 10.5.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from pickle import Unpickler
>>> a=Unpickler(open('/dev/zero'))
>>> a.persistent_load=lambda x: x
Traceback (most recent call last):
  File "<python-input-2>", line 1, in <module>
    a.persistent_load=lambda x: x
    ^^^^^^^^^^^^^^^^^
AttributeError: '_pickle.Unpickler' object attribute 'persistent_load' is read-only

I don't know if this is an intended change or a regression.

PS: The subclass approach works:

Python 3.13.0 (main, Oct  9 2024, 14:54:06) [GCC 10.5.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from pickle import Unpickler
>>> def x(Unpickler):
...     def persistent_load(self, x):
...         return x
...
>>> b=x(open("/dev/zero"))
>>>

CPython versions tested on:

3.13

Operating systems tested on:

Linux

Linked PRs

Read the original on github.com ↗