[Zope-CMF] UnpickleableError in new portal type
Dieter Maurer
dieter@handshake.de
Wed, 20 Feb 2002 22:03:21 +0100
Gitte Wange writes:
> I needed a mail client TTW and have started on implemeting both POP3 and IMAP
> connections into my Online PIM ...
> I not that far yet :-)
>
> I have created a ImapConnection class ..
> It has a openConnection function which logs a user into the imap account ..
> This is a code snip of the function:
>
> def openConnection(self):
> port = int(self.port)
> self.socket = imaplib.IMAP4(self.server, port)
> self.socket.login(self.username, self.password)
>
> The self.socket=imaplib.IMAP4() works fine but the next line produces a
> Error Type UnpickleableError
> Error Value Cannot pickle objects
Apparently, your "self" is a persistent object.
Zope serializes (technical term: "pickles") persistent objects
to store them in the ZODB. The serial form is a byte stream
with the property that the original object can be reconstructed
from the stream.
You may not be surprised that it is not easy to serialize a
socket (or an object containing a socket as almost surely
the "imaolib.IMAP4" instance is).
Your options:
maybe, you do not really need to save the object persistently.
maybe, a simple local variable is enough?
If the object need to live only for the current request
but must be globally accessible during this request,
you may use an attribute the name of which starts
with "_v_". Such attributes are not saved in the ZODB.
They are lost, when the object is flushed from the Zope cache.
When the object needs to live across request boundaries,
e.g. in a session, a session product comes into mind.
You need one that is *NOT* based on pickling, i.e.
neither Zope's build-in session support (2.5 upward)
nor FSSession or SQLSession are useable.
However, RAM based session product such as ZSession
or HappySession may be adequate.
When you need the object life across request boundaries
and not associated with a session (or something similar),
my product "SharedResource" might help you.
Probably more options...
Of course, you have the same problem for the POP3 connection...
Dieter