python / python/cpython

Add copyreg.copy() and copyreg.deepcopy()

Aperta
#153,612 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

stdlib type-feature
Lingua principale
Python
Stelle
77.2k
Fork
35.9k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

Feature or enhancement

Proposal:

Add two registration functions to copyreg, following the copyreg.pickle() precedent:

  • copyreg.copy(type, function) — registers the shallow copy function for objects of type type; called as function(obj), like __copy__.
  • copyreg.deepcopy(type, function) — registers the deep copy function; called as function(obj, memo), like __deepcopy__.

copy.copy() and copy.deepcopy() use the registered functions in preference to the special methods and the pickle interfaces. Registration is by exact type and does not affect pickling. This allows customizing copying of instances of types that cannot be modified — for example, treating an external library's immutable objects as atomic:

copyreg.deepcopy(thirdparty.Handle, lambda obj, memo: obj)

Today the only way to customize copying of a foreign type is copyreg.pickle(), which forces copy and pickle semantics to be the same. The private _copy_dispatch and _deepcopy_dispatch tables have been monkey-patched in the wild for lack of a public API — even by our own test_descr. The new registries replace this non-public way; _deepcopy_dispatch is now removed, as _copy_dispatch already was in gh-128118.

This was requested in gh-141737. Implementation is ready; PR to follow.

Has this already been discussed elsewhere?

Companion of PEP 837, whose Rationale notes that registries for other protocols will be proposed separately: https://discuss.python.org/t/pep-837-extensible-json-serialization/108124

Links to previous discussion of this feature:
  • gh-141737 — the direct feature request
  • gh-128118 — removal of the historical _copy_dispatch table
Linked PRs
  • gh-153613

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia leggendo la proposta per copyreg.copy() e copyreg.deepcopy() insieme ai punti di ingresso copy.copy() e copy.deepcopy(). Esamina la PR collegata gh-153613 e verifica che le registrazioni dei tipi esatti abbiano la precedenza sui metodi speciali e sulle interfacce pickle, mentre la copia rimane separata dal pickling.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
python
Ambito
tooling
Tipo di issue
Funzionalità
Difficoltà
3/5
Tempo stimato
1-2 giorni
Stato di attività
Ferma
Chiarezza
Specificata chiaramente
Idoneità per principianti
25/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.