Python pickling error when using sessions

人走茶凉 提交于 2019-12-10 19:14:35

问题


In my django app I was creating an extended user profile using session vars. But when registration form was saved and user was about to create, I got following error :

Traceback (most recent call last):

  File "\Python26\Lib\site-packages\django\core\servers\basehttp.py", line 279, in run
    self.result = application(self.environ, self.start_response)

  File "\Python26\Lib\site-packages\django\core\servers\basehttp.py", line 651, in __call__
    return self.application(environ, start_response)

  File "\Python26\Lib\site-packages\django\core\handlers\wsgi.py", line 245, in __call__
    response = middleware_method(request, response)

  File "\Python26\Lib\site-packages\django\contrib\sessions\middleware.py", line 36, in process_response
    request.session.save()

  File "\Python26\Lib\site-packages\django\contrib\sessions\backends\db.py", line 53, in save
    session_data = self.encode(self._get_session(no_load=must_create)),

  File "\Python26\Lib\site-packages\django\contrib\sessions\backends\base.py", line 88, in encode
    pickled = pickle.dumps(session_dict, pickle.HIGHEST_PROTOCOL)

PicklingError: Can't pickle <type 'cStringIO.StringO'>: attribute lookup cStringIO.StringO failed

I've googled for an answer, but found nothing interesting. Any workarounds for this ?


回答1:


It appears you have a cStringIO object in your session (perhaps an uploaded file?), these cannot be pickled. Either write custom pickling code or make sure all your session data can be serialized.




回答2:


Something weird going on here, because the error refers to cStringIO.StringO whereas the class is actually cStringIO.StringIO, with an extra I. Have you misspelled the name somewhere?




回答3:


In support of Ivo's answer, here's a reference I found which may explain this: http://bugs.python.org/issue5345

This is not a typo. cStringIO.StringIO is a factory function that returns either a cStringO object (for writing) or cStringI (for reading). If this behavior causes a problem to you, then consider using StringIO.StringIO.

Alternatively, you could upgrade to Python 2.7 or 3.0 and use io.StringIO() which doesn't have this limitation.



来源:https://stackoverflow.com/questions/3642107/python-pickling-error-when-using-sessions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!