[Twisted-Python] Having touble with spread.pb un-serializing an object
Benjamin Ash
bash at intelerad.com
Thu Sep 24 21:42:23 MDT 2009
Hi Jean-Paul,
Here is a sample server and client code that demonstrates the problem
with PB's unserialization code and objects that use __slots__. The
SlotsObject class implements __slots__ and the NoSlotsObject does not.
The problem arises when de-serializing the SlotsObject. The stack trace
is a little different than the original one I posted but the net-effect
is the same; an AttributeError exception.
Thanks,
-ben
sample.py (server and copyable objects):
"""
import sys
import os
from twisted.spread import pb
from twisted.internet import reactor
class BaseObject(object, pb.Copyable, pb.RemoteCopy):
def __init__(self, name):
self.name = name
self.version = "1"
self.release = "2"
def __str__(self):
return "%s-%s-%s" % (self.name, self.version, self.release)
class SlotsObject(BaseObject):
"""
Some object that implements __slots__
"""
__slots__ = ["name", "version", "release"]
def __init__(self):
super(SlotsObject, self).__init__("slots")
class NoSlotsObject(BaseObject):
def __init__(self):
super(NoSlotsObject, self).__init__("no-slots")
class SampleServer(pb.Root, object):
"""
Serve up some objects that implement __slots__.
"""
def remote_getSlotsObject(self):
return SlotsObject()
def remote_getNoSlotsObject(self):
return NoSlotsObject()
def main():
reactor.listenTCP(12346, pb.PBServerFactory(SampleServer()))
reactor.run()
if __name__ == "__main__":
from sample import SlotsObject, NoSlotsObject
main()
"""
sample client code:
"""
import sys
import os
from twisted.internet import reactor
from twisted.spread import pb
from twisted.python import util
from sample import SlotsObject, NoSlotsObject
pb.setUnjellyableForClass(SlotsObject, SlotsObject)
pb.setUnjellyableForClass(NoSlotsObject, NoSlotsObject)
def call(factory, name):
remote = factory.getRootObject()
remote.addCallback(lambda obj: obj.callRemote(name))
remote.addCallback(lambda obj: util.println("name is %r" % obj.name))
remote.addErrback(lambda err: err.printTraceback())
return remote
factory = pb.PBClientFactory()
reactor.connectTCP("localhost", 12346, factory)
call(factory, "getSlotsObject")
remote = call(factory, "getNoSlotsObject")
remote.addCallback(lambda _: reactor.stop())
reactor.run()
"""
Output:
"""
Traceback (most recent call last):
File "/usr/lib64/python2.5/site-packages/twisted/internet/defer.py", line 304, in _startRunCallbacks
self._runCallbacks()
File "/usr/lib64/python2.5/site-packages/twisted/internet/defer.py", line 317, in _runCallbacks
self.result = callback(self.result, *args, **kw)
File "/usr/lib64/python2.5/site-packages/twisted/internet/defer.py", line 281, in _continue
self.unpause()
File "/usr/lib64/python2.5/site-packages/twisted/internet/defer.py", line 277, in unpause
self._runCallbacks()
--- <exception caught here> ---
File "/usr/lib64/python2.5/site-packages/twisted/internet/defer.py", line 317, in _runCallbacks
self.result = callback(self.result, *args, **kw)
File "./sample-client.py", line 17, in <lambda>
remote.addCallback(lambda obj: util.println("name is %r" % obj.name))
exceptions.AttributeError: name
name is 'no-slots'
"""
On Wed, Sep 23, 2009 at 09:42:26PM -0000, exarkun at twistedmatrix.com wrote:
> On 07:06 pm, bash at intelerad.com wrote:
> >Hi,
> >
> >I am trying to send new style classes that implement __slots__ with
> >spread.pb.
> >Things seem to go awry when attempting to un-serialize the objects on
> >the
> >client side. I am not sure if this is a known issue. How can I work
> >around
> >this issue?
>
> Perhaps the presence of __slots__ breaks PB's unserialization code. Can
> you share a SSCCE (<http://sscce.org/>)? Then perhaps it may make sense
> to file an enhancement ticket to support this case.
>
> Jean-Paul
>
> _______________________________________________
> Twisted-Python mailing list
> Twisted-Python at twistedmatrix.com
> http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
Intelerad Medical Systems
Central Station
895, de la Gauchetiere W., Suite 400
Montreal, QC Canada H3B 4G1
www.intelerad.com
This email or any attachments may contain confidential or legally privileged information intended for the sole use of the addressees. Any use, redistribution, disclosure, or reproduction of this information, except as intended, is prohibited. If you received this email in error, please notify the sender and remove all copies of the message, including any attachments.
More information about the Twisted-Python
mailing list