Re: Missing pickling support for generic methods

HervĂ© Coatanhay <[email protected]> Sat, 2 Feb 2013 00:32:04 +0100
Newsgroups gmane.comp.python.peak
Message-ID <CAPq7H1bHOEJosmv4B8GmPM4XK6TAzNpRWg2WwQ9R=dCwBWHZ4A@mail.gmail.com>
--===============1209900007==
Content-Type: multipart/alternative; boundary=e89a8f646e41abb76704d4b226c4

--e89a8f646e41abb76704d4b226c4
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

Hi,

As a note, the `lock` issue occurs with stackless python, which allows
instancemethod pickling. In standard cpython it is "TypeError: can't pickle
instancemethod objects" obviously.

--
Herv=C3=A9
On Fri, Feb 1, 2013 at 7:19 PM, Sylvain Prat <[email protected]> wrote=
:

> Hello,
>
> I recently encountered a pickling problem with peak.rules. The generic
> methods do not pickle: they raise a "TypeError: can't pickle lock objects=
".
> I've dugg into the code with a colleague and we found that the Dispatchin=
g
> object that registers the implementations of the method is pickled and
> since it contains a lock, the pickling fails.
>
> It's a little annoying because it prevents us from using generic methods
> as action callbacks (actions performed when e.g. we click on a link in a
> web page) in Nagare Web applications because Nagare pickles them in the
> user session.
>
> I'm not sure how the problem can be fixed. I'm wondering if the
> Dispatching object should be pickled (or not) and if regenerations have t=
o
> be triggered on pickling/depickling.
>
> Can someone help? Thanks in advance!
>
> Here is a test case that triggers the problem:
>
> -----
>
> from peak.rules import when
> import cPickle as pickle
>
>
> class Test(object):
>     def run(self, arg):
>         return 42
>
>     @when(run, 'arg =3D=3D 0')
>     def run_3(self, arg):
>         return 0
>
>
> if __name__ =3D=3D '__main__':
>     run =3D Test().run
>
>     assert run(3) =3D=3D 42
>     assert run(0) =3D=3D 0
>
>     s =3D pickle.dumps(run)  # raises a "TypeError: can't pickle lock
> objects"
>     run =3D pickle.loads(s)
>
>     assert run(3) =3D=3D 42
>     assert run(0) =3D=3D 0
>
>
> --
> Sylvain PRAT
>
>
> _______________________________________________
> PEAK mailing list
> [email protected]
> http://www.eby-sarna.com/mailman/listinfo/peak

--e89a8f646e41abb76704d4b226c4
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<div>Hi,</div><div><br></div>As a note, the `lock` issue occurs with stackl=
ess python, which allows instancemethod pickling. In standard cpython it is=
 &quot;TypeError: can&#39;t pickle instancemethod objects&quot; obviously.<=
div>
<br></div><div>--</div><div>Herv=C3=A9<br><div class=3D"gmail_quote">On Fri=
, Feb 1, 2013 at 7:19 PM, Sylvain Prat <span dir=3D"ltr">&lt;<a href=3D"mai=
lto:[email protected]" target=3D"_blank">[email protected]</a>&gt=
;</span> wrote:<br>
<blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1p=
x #ccc solid;padding-left:1ex"><div dir=3D"ltr"><div>Hello,</div><div><br><=
/div><div>I recently encountered a pickling problem with peak.rules. The ge=
neric methods do not pickle: they raise a &quot;TypeError: can&#39;t pickle=
 lock objects&quot;. I&#39;ve dugg into the code with a colleague and we fo=
und that the Dispatching object that registers the implementations of the m=
ethod is pickled and since it contains a lock, the pickling fails.</div>

<div><br></div><div>It&#39;s a little annoying because it prevents us from =
using generic methods as action callbacks (actions performed when e.g. we c=
lick on a link in a web page) in Nagare Web applications because Nagare pic=
kles them in the user session.</div>

<div><br></div><div>I&#39;m not sure how the problem can be fixed. I&#39;m =
wondering if the Dispatching object should be pickled (or not) and if regen=
erations have to be triggered on pickling/depickling.</div>
<div><br></div><div>Can someone help? Thanks in advance!</div><div><br></di=
v><div>Here is a test case that triggers the problem:</div><div><br></div><=
div>-----</div><div><br></div><div><div>from peak.rules import when</div>

<div>import cPickle as pickle</div><div><br></div><div><br></div><div>class=
 Test(object):</div><div>=C2=A0 =C2=A0 def run(self, arg):</div><div>=C2=A0=
 =C2=A0 =C2=A0 =C2=A0 return 42</div><div><br></div><div>=C2=A0 =C2=A0 @whe=
n(run, &#39;arg =3D=3D 0&#39;)</div><div>

=C2=A0 =C2=A0 def run_3(self, arg):</div><div>=C2=A0 =C2=A0 =C2=A0 =C2=A0 r=
eturn 0</div><div><br></div><div><br></div><div>if __name__ =3D=3D &#39;__m=
ain__&#39;:</div><div>=C2=A0 =C2=A0 run =3D Test().run</div><div><br></div>=
<div>=C2=A0 =C2=A0 assert run(3) =3D=3D 42</div><div>=C2=A0 =C2=A0 assert r=
un(0) =3D=3D 0</div>

<div><br></div><div>=C2=A0 =C2=A0 s =3D pickle.dumps(run) =C2=A0# raises a =
&quot;TypeError: can&#39;t pickle lock objects&quot;</div><div>=C2=A0 =C2=
=A0 run =3D pickle.loads(s)</div><div><br></div><div>=C2=A0 =C2=A0 assert r=
un(3) =3D=3D 42</div><div>=C2=A0 =C2=A0 assert run(0) =3D=3D 0</div>
<span class=3D"HOEnZb"><font color=3D"#888888">
</font></span></div><span class=3D"HOEnZb"><font color=3D"#888888"><div><br=
></div><div><br></div>-- <br>Sylvain PRAT<br><br></font></span></div>
<br>_______________________________________________<br>
PEAK mailing list<br>
<a href=3D"mailto:[email protected]">[email protected]</a><br>
<a href=3D"http://www.eby-sarna.com/mailman/listinfo/peak" target=3D"_blank=
">http://www.eby-sarna.com/mailman/listinfo/peak</a></blockquote></div></di=
v>

--e89a8f646e41abb76704d4b226c4--

--===============1209900007==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

_______________________________________________
PEAK mailing list
[email protected]
http://www.eby-sarna.com/mailman/listinfo/peak
--===============1209900007==--