Missing pickling support for generic methods

Sylvain Prat <[email protected]> Fri, 1 Feb 2013 19:19:31 +0100
Newsgroups gmane.comp.python.peak
Message-ID <CACUp1cqtNqTFo8TSj80uFecG6tU5LBoTOvkf0JD4hoJ7d++kdg@mail.gmail.com>
--===============0177516590==
Content-Type: multipart/alternative; boundary=e0cb4efe293cdf9e9104d4adc805

--e0cb4efe293cdf9e9104d4adc805
Content-Type: text/plain; charset=ISO-8859-1

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 Dispatching
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 to 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 == 0')
    def run_3(self, arg):
        return 0


if __name__ == '__main__':
    run = Test().run

    assert run(3) == 42
    assert run(0) == 0

    s = pickle.dumps(run)  # raises a "TypeError: can't pickle lock objects"
    run = pickle.loads(s)

    assert run(3) == 42
    assert run(0) == 0


-- 
Sylvain PRAT

--e0cb4efe293cdf9e9104d4adc805
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><div style>Hello,</div><div style><br></div><div style>I r=
ecently encountered a pickling problem with peak.rules. The generic 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 found that the =
Dispatching object that registers the implementations of the method is pick=
led and since it contains a lock, the pickling fails.</div>
<div style><br></div><div style>It&#39;s a little annoying because it preve=
nts us from using generic methods as action callbacks (actions performed wh=
en e.g. we click on a link in a web page) in Nagare Web applications becaus=
e Nagare pickles them in the user session.</div>
<div style><br></div><div style>I&#39;m not sure how the problem can be fix=
ed. I&#39;m wondering if the Dispatching object should be pickled (or not) =
and if regenerations have to be triggered on pickling/depickling.</div>
<div style><br></div><div style>Can someone help? Thanks in advance!</div><=
div style><br></div><div style>Here is a test case that triggers the proble=
m:</div><div style><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>=A0 =A0 def run(self, arg):</div><div>=A0 =A0 =A0 =
=A0 return 42</div><div><br></div><div>=A0 =A0 @when(run, &#39;arg =3D=3D 0=
&#39;)</div><div>
=A0 =A0 def run_3(self, arg):</div><div>=A0 =A0 =A0 =A0 return 0</div><div>=
<br></div><div><br></div><div>if __name__ =3D=3D &#39;__main__&#39;:</div><=
div>=A0 =A0 run =3D Test().run</div><div><br></div><div>=A0 =A0 assert run(=
3) =3D=3D 42</div><div>=A0 =A0 assert run(0) =3D=3D 0</div>
<div><br></div><div>=A0 =A0 s =3D pickle.dumps(run) =A0# raises a &quot;Typ=
eError: can&#39;t pickle lock objects&quot;</div><div>=A0 =A0 run =3D pickl=
e.loads(s)</div><div><br></div><div>=A0 =A0 assert run(3) =3D=3D 42</div><d=
iv>=A0 =A0 assert run(0) =3D=3D 0</div>
</div><div><br></div><div><br></div>-- <br>Sylvain PRAT<br><br></div>

--e0cb4efe293cdf9e9104d4adc805--

--===============0177516590==
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
--===============0177516590==--