python / cpython

The Python programming language
https://www.python.org
Other
63.51k stars 30.42k forks source link

Inconsistency in handling None handlers in the Python and C implementations of pickle #93627

Open serhiy-storchaka opened 2 years ago

serhiy-storchaka commented 2 years ago

In the Python implementation of pickle setting some handlers to None causes a fallback to other methods.

In the C implementation all this leads to TypeError: 'NoneType' object is not callable.

The copy module follows the Python implementation of pickle, except that it fails for any false value of __reduce__, not just None.

In most other code setting a dunder method to None leads to a TypeError (either raise explicitly or just generic "'NoneType' object is not callable").

We should decide what behavior should be in the pickle and copy module. On one hand, the Python implementation was primary. On other hand, it was inconsistent between the pickle and copy modules and was already changed in the past, the C implementation of pickle is now used by most users, and it is more consistent with other code.

See also: https://mail.python.org/archives/list/python-dev@python.org/thread/YGAK34DRWJFSIV2VZ4NC2J24XO37GCMM/ https://docs.python.org/3.10/reference/datamodel.html#id2 https://github.com/python/cpython/issues/70146 https://github.com/python/cpython/issues/55781 https://github.com/python/cpython/issues/60755

Linked PRs

eendebakpt commented 1 year ago

@serhiy-storchaka How can we move forward with this issue? Adapting the different components is something I can help out with, but I do not have sufficient experience to make an informed decision on which behavior is desired and whether we can make backwards incompatible changes.