Skip to content

Meta-ticket: Support Python's __copy__ / __deepcopy__ protocol #13811

@cnassau

Description

@cnassau

Most Sage objects are immutable. Nevertheless, copy and deepcopy make copies (through pickling/unpickling) for them because we have not provided the classes with __copy__ methods (which should just return the object) and __deepcopy__ methods (which in many cases should just return the object).

sage: a = 0
sage: copy(a) is a
False

Another symptom (from the original ticket description): Copying of (immutable!) LazyFamily objects only works for families that can be pickled:

   sage: def unpicklableFamily():
   ...       x = 10
   ...       return LazyFamily([1,2,3], lambda n: x*n)
   sage: f = unpicklableFamily() 
   sage: copy(f)
   Traceback (most recent call last):
   ...
   ValueError: Cannot pickle code objects from closures

The case of __deepcopy__ of immutable container-like objects (like Family or Sequence) is a bit trickier: Is immutability intended to imply that the elements are immutable too? In this ticket, we make no such assumption.

Tickets:

Depends on #32454

CC: @tscrim @mjungmath @nbruin @kwankyu @williamstein @mwageringel @kcrisman

Component: pickling

Keywords: LazyFamily, copy, pickle

Author: Matthias Koeppe

Branch/Commit: u/mkoeppe/__copy___and___deepcopy___methods_for_all_immutable_sage_objects @ 2bd01ae

Issue created by migration from https://trac.sagemath.org/ticket/13811

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions