[Zope-dev] Storing modules in the ZODB

Chris McDonough chrism@zope.com
Sun, 05 Aug 2001 11:45:26 -0400


The Python pickler cannot pickle module objects.  You might
try the same concept, but with class objects instead.

On Sun, 29 Jul 2001 20:20:28 -0400
 Joseph Barillari <jbarilla@princeton.edu> wrote:
> Hello,
> 
> I'm having trouble storing to the ZODB. 
> 
> The code in question reads a string argument and selects
> a module from
> the current directory that matches the string argument,
> importing it
> into the current class:
> 
> class Division(Persistence.Persistent):
>     "A collection of rounds."
>     def __init__(self, name, type, flags = []):
>         self.name = name
>         self.typename = type
>         self.rules = __import__(type)
>         self.rounds = []
>         self.teams = {}
>         self.judges = []
>         self.flags = flags
> 
> (rest of class snipped in the interests of brevity)
> 
> Here's the result of a call using the arguments ('Test',
> 'APDA') in
> Python 1.5.2 and Zope 2.4.0
> 
> Traceback (innermost last):
>   File "<stdin>", line 1, in ?
>   File "t3s.py", line 909, in add_division
>     get_transaction().commit()
>   File "/usr/lib/python1.5/ZODB/Transaction.py", line
> 300, in commit
>     j.commit(o,self)
>   File "/usr/lib/python1.5/ZODB/Connection.py", line 375,
> in commit
>     dump(state)
> cPickle.UnpickleableError: Cannot pickle <type 'module'>
> objects
> 
> (add_division is a function that creates the division and
> stores it to
> a global database; essentially, a wrapper.)
> 
> I tried the native cPickle module in Python 2.1.1 (to
> ensure that I
> had a clean version). Same problem -- it chokes on module
> objects.
> 
> The purpose of this code is to enable the user to select
> from several
> different sets of 'rules' ('APDA' is currently the only
> one written),
> which are implemented as Python modules. At run-time,
> when a
> 'division' is created, the ruleset is imported into the
> object
> instance as self
> 
> Do you have any suggestions as to how I might implement
> this?
> 
> Thanks,
> 
> Joe