python / python/cpython

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

Open
#93,627 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

3.10 3.11 3.12 type-bug
Dominant language
Python
Stars
77.2k
Forks
36k
PR merge metrics
PR metrics pending

Description

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

  • Setting Pickler.reducer_override to None has the same effect as not setting this attribute -- falling back to other methods.
  • Setting the dispatch_table to None leads to falling back to __reduce_ex__.
  • Setting the __reduce_ex__ attribute to None leads to falling back to __reduce__.
  • Setting the __setstate__ attribute to None leads to falling back to the default implementation of __setstate__ (setting __dict__ and slots).

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
  • gh-103035
  • gh-109498

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading the Python and C implementations of pickle and the copy module, then review the linked pull requests gh-103035 and gh-109498. Done requires an agreed behavior for None handlers and consistent handling across the affected implementations.

Written by the indexing model from the issue text.

Assessment

Tech stack
c, python
Domain
backend
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.