Sage: Ticket #13811: LazyFamily cannot be copied if it can't be pickled
https://trac.sagemath.org/ticket/13811
<p>
Currently, copying of <code>LazyFamily</code> objects only works for families that can be pickled:
</p>
<pre class="wiki"> 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
</pre><p>
I suggest adding a <code>LazyFamily.__copy__</code> method that fixes this.
</p>
en-usSagehttps://trac.sagemath.org/chrome/site/logo_sagemath_trac.png
https://trac.sagemath.org/ticket/13811
Trac 1.1.6cnassauSat, 08 Dec 2012 14:11:59 GMTstatus changed
https://trac.sagemath.org/ticket/13811#comment:1
https://trac.sagemath.org/ticket/13811#comment:1
<ul>
<li><strong>status</strong>
changed from <em>new</em> to <em>needs_review</em>
</li>
</ul>
TicketcnassauSat, 08 Dec 2012 14:13:28 GMTauthor set
https://trac.sagemath.org/ticket/13811#comment:2
https://trac.sagemath.org/ticket/13811#comment:2
<ul>
<li><strong>author</strong>
set to <em>cnassau</em>
</li>
</ul>
TicketcnassauSat, 08 Dec 2012 14:14:15 GMTauthor changed
https://trac.sagemath.org/ticket/13811#comment:3
https://trac.sagemath.org/ticket/13811#comment:3
<ul>
<li><strong>author</strong>
changed from <em>cnassau</em> to <em>Christian Nassau</em>
</li>
</ul>
TicketnthieryThu, 13 Dec 2012 09:16:43 GMT
https://trac.sagemath.org/ticket/13811#comment:4
https://trac.sagemath.org/ticket/13811#comment:4
<blockquote>
<p>
Hi Christian!
</p>
</blockquote>
<p>
Families are (semantically) immutable objects. Why would we want to copy them?
</p>
<p>
Cheers,
</p>
<blockquote>
<p>
Nicolas
</p>
</blockquote>
TicketcnassauThu, 13 Dec 2012 10:17:48 GMT
https://trac.sagemath.org/ticket/13811#comment:5
https://trac.sagemath.org/ticket/13811#comment:5
<p>
Replying to <a class="ticket" href="https://trac.sagemath.org/ticket/13811#comment:4" title="Comment 4">nthiery</a>:
</p>
<blockquote class="citation">
<blockquote>
<p>
Hi Christian!
</p>
</blockquote>
<p>
Families are (semantically) immutable objects. Why would we want to copy them?
</p>
</blockquote>
<p>
Good point... I stumbled across this issue while I was struggling to create a disjoint union of a dynamically generated family of <code>LazyFamily</code> objects, while the <em>real</em> problem was that I was using closures with reference to a loop variable. I eventually resolved this issue, and my solution does not requiry any copies now. So I agree that there's no need to create shallow copies.
</p>
<p>
I <strong>do</strong> think that user code can expect that <code>copy(any_sage_object)</code> does not throw an error. Maybe <code>LazyFamily.__copy__(self)</code> should just return <code>self</code>? This would be in accordance with
</p>
<pre class="wiki">sage: copy(Integers()) is Integers()
True
</pre><p>
Cheers,
Christian
</p>
TicketnthieryThu, 13 Dec 2012 10:29:42 GMT
https://trac.sagemath.org/ticket/13811#comment:6
https://trac.sagemath.org/ticket/13811#comment:6
<p>
Replying to <a class="ticket" href="https://trac.sagemath.org/ticket/13811#comment:5" title="Comment 5">cnassau</a>:
</p>
<blockquote class="citation">
<p>
I <strong>do</strong> think that user code can expect that <code>copy(any_sage_object)</code> does not throw an error. Maybe <code>LazyFamily.__copy__(self)</code> should just return <code>self</code>? This would be in accordance with
</p>
<pre class="wiki">sage: copy(Integers()) is Integers()
True
</pre></blockquote>
<p>
Yes, it would make sense to have copy(x) return x for all immutable objects in Sage. I am not sure how to achieve this though: we do not (yet?) have a class for providing code for all immutable objects in Sage, and I would not want to force every relevant class to reimplement a trivial <span class="underline">copy</span> method.
</p>
<p>
For the case at hand (families), maybe one could add a method Parent.<span class="underline">copy</span> returning <code>self</code>? As far as I can tell, parents are required to be immutable anyway. But one should double check this.
</p>
<p>
Cheers,
</p>
<blockquote>
<p>
Nicolas
</p>
</blockquote>
TicketcnassauSat, 26 Jan 2013 12:13:11 GMTattachment set
https://trac.sagemath.org/ticket/13811
https://trac.sagemath.org/ticket/13811
<ul>
<li><strong>attachment</strong>
set to <em>lazyfamcopy.patch</em>
</li>
</ul>
<p>
A patch for this problem
</p>
TicketjdemeyerTue, 13 Aug 2013 15:35:53 GMTmilestone changed
https://trac.sagemath.org/ticket/13811#comment:7
https://trac.sagemath.org/ticket/13811#comment:7
<ul>
<li><strong>milestone</strong>
changed from <em>sage-5.11</em> to <em>sage-5.12</em>
</li>
</ul>
Ticketvbraun_spamThu, 30 Jan 2014 21:20:52 GMTmilestone changed
https://trac.sagemath.org/ticket/13811#comment:8
https://trac.sagemath.org/ticket/13811#comment:8
<ul>
<li><strong>milestone</strong>
changed from <em>sage-6.1</em> to <em>sage-6.2</em>
</li>
</ul>
Ticketvbraun_spamTue, 06 May 2014 15:20:58 GMTmilestone changed
https://trac.sagemath.org/ticket/13811#comment:9
https://trac.sagemath.org/ticket/13811#comment:9
<ul>
<li><strong>milestone</strong>
changed from <em>sage-6.2</em> to <em>sage-6.3</em>
</li>
</ul>
Ticketvbraun_spamSun, 10 Aug 2014 16:51:03 GMTmilestone changed
https://trac.sagemath.org/ticket/13811#comment:10
https://trac.sagemath.org/ticket/13811#comment:10
<ul>
<li><strong>milestone</strong>
changed from <em>sage-6.3</em> to <em>sage-6.4</em>
</li>
</ul>
TicketvdelecroixSun, 01 Feb 2015 11:29:07 GMTstatus changed
https://trac.sagemath.org/ticket/13811#comment:11
https://trac.sagemath.org/ticket/13811#comment:11
<ul>
<li><strong>status</strong>
changed from <em>needs_review</em> to <em>needs_work</em>
</li>
</ul>
<p>
Hi,
</p>
<p>
As Nicolas suggested in <a class="ticket" href="https://trac.sagemath.org/ticket/13811#comment:6" title="Comment 6">his comnent 6</a>, <code>__copy__</code> should be implemented as
</p>
<pre class="wiki">def __copy__(self):
return self
</pre><p>
It is standard Python for immutable objects
</p>
<pre class="wiki">sage: t = (1,2,3)
sage: copy(t) is t
True
sage: i = 1231491283r
sage: copy(i) is i
True
</pre><p>
Vincent
</p>
Ticket