[Zope] creating and using a python object

Dieter Maurer dieter@handshake.de
Wed, 21 Feb 2001 20:10:39 +0100 (CET)


Joh Johannsen writes:
 > 
 > --------------196F8623CF0502D1DE9F15CC
 > Content-Type: text/plain; charset=us-ascii
 > Content-Transfer-Encoding: 7bit
Please, do not post MIME messages!

 > At the moment, I guess it is impossible to use Python instances within
 > Zope.  (I'm surprised, something does not sound right about this...)
That, of course, is not right. It is just that such instances must
fulfill some conditions.

 > Here is what the Zope Book says:
 > 
 > > Problems can occurr if you hand instances of your own classes to Zope and expect them to work like Zope objects. For example, you cannot define a class in an
 > > External Method script file and assign it as an attribute of a Zope object. This causes problems with Zope's persistence machinary.
Thus, you define them outside of an "External Method" source file,
in a standard Python module.

 > > You also cannot easily hand
 > > instances of your own classes over to DTML or Scripts. The issue here is that your instances won't have Zope security information. You can define and use your
 > > own classes and instances to your heart's delight, just don't expect Zope to use them directly. Limit yourself to handing Zope simple Python structures like
 > > dictionaries and lists or Zope objects
Alternatively, provide the necessary security information.
There is a document from Brian at Zope.org that tells you what you need
to do.

 > I'm not so sure about this "causes problems with Zope's persistence
 > machinery"... what does that mean?
Zope stores pickles (--> Python documentation) for objects.
A pickle for a class instance (usually) consists of a class reference
and the pickles for the instance's members.
A class reference consists of the module the class is defined in
and the class name.

Due to this implementation, there are a number of restrictions:

 * Python's pickle module requires the class to be defined
   at the global level of the module.
   This is because unpickling will import the module
   and then needs to access the class through its name.
   This would not be possible, if the class were defined
   in deeper structures.

 * "External Method" source files are not imported as
   modules by Zope but are instead read.

   For Python's pickle module, it appears as though
   they belong to the module "__main__".

   Of course, if Python imports "__main__" and looks
   for your class (defined in the External Method source file),
   it will not find the class.
   Thus, your object cannot be unpickled.



Dieter