[mod_python] Pickle can't save classes instances in mod_python ?

Seb seb.dailly at gmail.com
Thu Jun 14 17:00:56 EDT 2007


Hello !

I've found something a bit strange in mod_python : the pickle
instruction doesn't work when we want to save classes ( there is no
problème with tupple or list.. )

I made this code for explain it:

-->
import mod_python
import pickle

#Create a very simple class object
class another_class:

       def __init__(self, text):
               self.text =     text

#Another one class
class a_class:

       def __init__(self):
               self.elements = []

               #Add an element of the second class
               self.add_entry("test")
               self.save()

       def add_entry(self, name):
               self.elements.append( another_class(name) )

       def save(self):
               #Try to save it with pickle.. works but
               result = pickle.dumps(self.elements)
               #We can't load the data after !
               self.elements = pickle.loads(result)

#If all works good, send a message
def handler(req):
       req.write( 'ok' )
       return(mod_python.apache.OK)

#We run this class outside any mod_python event
my_class = a_class()
<--

And here is the result :

-->
MOD_PYTHON ERROR

ProcessId:      5104
Interpreter:    'localhost.localdomain'

ServerName:     'localhost.localdomain'
DocumentRoot:   '/var/www/'

URI:            '/~sebastien/test/'
Location:       None
Directory:      '/home/sebastien/public_html/test/'
Filename:       '/home/sebastien/public_html/test/'
PathInfo:       ''

Phase:          'PythonHandler'
Handler:        'handler'

Traceback (most recent call last):

 File "/usr/lib/python2.4/site-packages/mod_python/importer.py", line
1537, in HandlerDispatch
   default=default_handler, arg=req, silent=hlist.silent)

 File "/usr/lib/python2.4/site-packages/mod_python/importer.py", line
1202, in _process_target
   module = import_module(module_name, path=path)

 File "/usr/lib/python2.4/site-packages/mod_python/importer.py", line
296, in import_module
   log, import_path)

 File "/usr/lib/python2.4/site-packages/mod_python/importer.py", line
680, in import_module
   execfile(file, module.__dict__)

 File "/home/sebastien/public_html/test/handler.py", line 35, in ?
   my_class = a_class()

 File "/home/sebastien/public_html/test/handler.py", line 18, in __init__
   self.save()

 File "/home/sebastien/public_html/test/handler.py", line 27, in save
   self.elements = pickle.loads(result)

 File "pickle.py", line 1394, in loads
   return Unpickler(file).load()

 File "pickle.py", line 872, in load
   dispatch[key](self)

 File "pickle.py", line 1083, in load_inst
   klass = self.find_class(module, name)

 File "pickle.py", line 1138, in find_class
   __import__(module)

ImportError: No module named _mp_e3b5c44c50230d33c470783bc2809f23


MODULE CACHE DETAILS

Accessed:       Thu Jun 14 22:49:06 2007
Generation:     3

_mp_e3b5c44c50230d33c470783bc2809f23 {
 FileName:     '/home/sebastien/public_html/test/handler.py'
 Instance:     3 [RELOAD]
 Generation:   3 [ERROR]
 Modified:     Thu Jun 14 22:48:49 2007
 Imported:     Thu Jun 14 22:37:06 2007
}
<--

When I launch this program inside a terminal, all works fine, there
are no error message, so I don't understand what happend..



More information about the Mod_python mailing list