Hi,
(This is a copy of a mail which I sent to formencode-discuss earlier today.
Since this affects (probably) at least some TurboGears users, I decided to
sent the mail to this list, too. I took me much to many hours tracking down
that bug - in my app I only saw duplicated items from time to time).
I was bitten by a formencode default just some minutes ago so I
thought that sharing my experience may help others to avoid this
trap:
If use you ForEach but the key is not present in your input
dictionary, you will get an empty list ([]) by default. This is
somewhat dangerous because the list instance is a mutable object.
So if your program modifies the resulting list AND your process
lives longer than just one request, you will get the old list
instance next time.
This example will show you the problem:
# -----------------------------------------
from formencode import ForEach, Schema
from formencode.validators import Int
class Foo(Schema):
baz = ForEach(Int)
schema = Foo()
fields = schema.to_python({})
fields["baz"].append(1)
print schema.to_python({})
# -----------------------------------------
Yes, I agree you probably should not modify the result dict in the
first place. However, sometimes it may be convenient to do so.
Update:
FormEncode [r3576] contains a patch which should solve that issue. If you want
to be on the save side use something like:
baz = ForEach(Int, if_empty=(), if_missing=())
So this is another example that you must avoid mutable objects like lists for
class-level symbols like named arguments.
fs
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"TurboGears" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/turbogears?hl=en
-~----------~----~----~----~------~----~------~--~---