python / python/cpython

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

Abierto
#153,612 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

stdlib type-feature
Lenguaje dominante
Python
Estrellas
77.2k
Forks
35.9k
Métricas de merge de PR
Métricas de PR pendientes

Descripción

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

Guía de contribución

Abrir la guía de contribución

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Línea de trabajo

Comienza leyendo la propuesta de copyreg.copy() y copyreg.deepcopy() junto con los puntos de entrada copy.copy() y copy.deepcopy(). Revisa el PR vinculado gh-153613 y verifica que los registros de tipos exactos tienen prioridad sobre los métodos especiales y las interfaces de pickle, mientras que la copia sigue estando separada del pickling.

Escrito por el modelo de indexación a partir del texto del issue.

Evaluación

Stack tecnológico
python
Área
tooling
Tipo de issue
Nueva funcionalidad
Dificultad
3/5
Tiempo estimado
1-2 días
Estado de actividad
Estancado
Claridad
Bien especificado
Aptitud para principiantes
25/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.