Discussion:
[FE-discuss] Can't store Invalid object in session
Chris Vickerson
2009-06-01 00:50:07 UTC
Permalink
I'm cross posting 2 messages that was originally posted on the pylons list.
The reason I'm cross posting it now is I now think this is specific to
formencode. If the problem is that Invalid objects can't be pickled is
there a fix or a work around?

Thanks

[message 1]
Hi - I'm trying to set some session variables inside an exception handler of
FormEncode validation testing. It seems that if I try to save the result of
"error.error_dict" into a session variable all my session variables are
deleted. I experimented with a bunch of different ways to narrow this down.
All I have to do is not try to save error.error_dict and everything works
as expected. My code looks like this:

import formencode
...

class FilesController(BaseController):
def view_by_id(self):
schema = ListForm()

try:
form_result = schema.to_python(dict (request.params))
except formencode.Invalid, error:
session['defaults'] = error.value
session['errors'] = error.error_dict or {} #comment out
this line and it saves ok
session.save()
redirect_to(controller='files', action='list')

class ListForm(formencode.Schema):
allow_extra_fields = True
filter_extra_fields = True
file_select =
formencode.validators.Int<http://formencode.validators.int/>(not_empty=True)


I think I've posted enough of the code - it's pretty basic I think. I
searched google/google groups but didn't find any similar problems so I must
be doing something wrong.
Any help is appreciated

[message 2]

I've found a conversation that led me to this open bug:
http://bugs.python.org/issue1692335 I think this is the problem that is not
allowing me to store this
object in session.
--
Chris Vickerson
***@vickerson.net
Ian Bicking
2009-06-03 22:02:51 UTC
Permalink
Sorry for the slow reply...
Post by Chris Vickerson
schema = ListForm()
form_result = schema.to_python(dict (request.params))
session['defaults'] = error.value
session['errors'] = error.error_dict or {} #comment out
this line and it saves ok
A workaround: save error.unpack_errors(), which won't contain any exception
objects.
Post by Chris Vickerson
http://bugs.python.org/issue1692335 I think this is the problem that is
not allowing me to store this
object in session.
Yes, that's what is causing the problem. It seems best just to allow this
to be fixed upstream in Python, especially since unpack_errors has a very
reasonable return value anyway.
--
Ian Bicking | http://blog.ianbicking.org
--
Ian Bicking | http://blog.ianbicking.org
Loading...