-
Notifications
You must be signed in to change notification settings - Fork 88
Resolve pickling and deepcopying bug with dynamically added traits #373
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 5 commits
833a406
6413920
2553bdc
17c6d58
cf57801
e2fa2d4
d7a7e79
498f491
a8a8399
3e47885
cd5c9ea
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,6 +3,7 @@ | |
| import sys | ||
|
|
||
| from ..has_traits import HasTraits, Property, on_trait_change | ||
| from ..trait_errors import TraitError | ||
| from ..trait_types import Bool, DelegatesTo, Instance, Int, List | ||
| from ..testing.unittest_tools import unittest | ||
|
|
||
|
|
@@ -189,6 +190,22 @@ def test_delegation_refleak(self): | |
| # All the counts should be the same. | ||
| self.assertEqual(counts[warmup:-1], counts[warmup+1:]) | ||
|
|
||
| def test_hastraits_pickle_deepcopy(self): | ||
|
||
| from pickle import dumps, loads | ||
| from copy import deepcopy | ||
| a = HasTraits() | ||
| a.add_trait('foo', Int) | ||
| a.foo = 1 | ||
| with self.assertRaises(TraitError): | ||
| a.foo = 'a' | ||
| pkld_a = dumps(a) | ||
| unpkld_a = loads(pkld_a) | ||
| with self.assertRaises(TraitError): | ||
| unpkld_a.foo = 'a' | ||
| copied_a = deepcopy(a) | ||
| with self.assertRaises(TraitError): | ||
| copied_a.foo = 'a' | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| unittest.main() | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@cfarrow I notice that here we're calling
add_traitafter doingtraits_init()and_post_init_trait_listeners(), but in theclone_traitsblock below, we're callingadd_traitbefore those calls. Do you know whether there's a reason for the difference?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know the reason. I'll look into the potential consequences. At the very least I'll make it consistent.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
According to the doc string,
traits_initshould be called at the end of__setstate__. In practice, I don't think it matters. It is a no-op by default. The only place I have found it defined in the traits source is in HasUniqueStrings.In any case, I moved it after all the
add_traitsto be consistent with the existing documentation and the behavior inclone_traits.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, Chris. I also moved the
_post_init_trait_listenerscall, so that it's consistent withclone_traits.