##// END OF EJS Templates
Make HasTraits pickleable...
Min RK -
Show More
@@ -1,26 +1,13 b''
1 1 # encoding: utf-8
2 """
3 Tests for IPython.utils.traitlets.
2 """Tests for IPython.utils.traitlets."""
4 3
5 Authors:
6
7 * Brian Granger
8 * Enthought, Inc. Some of the code in this file comes from enthought.traits
9 and is licensed under the BSD license. Also, many of the ideas also come
10 from enthought.traits even though our implementation is very different.
11 """
12
13 #-----------------------------------------------------------------------------
14 # Copyright (C) 2008-2011 The IPython Development Team
15 #
16 # Distributed under the terms of the BSD License. The full license is in
17 # the file COPYING, distributed as part of this software.
18 #-----------------------------------------------------------------------------
19
20 #-----------------------------------------------------------------------------
21 # Imports
22 #-----------------------------------------------------------------------------
4 # Copyright (c) IPython Development Team.
5 # Distributed under the terms of the Modified BSD License.
6 #
7 # Adapted from enthought.traits, Copyright (c) Enthought, Inc.,
8 # also under the terms of the Modified BSD License.
23 9
10 import pickle
24 11 import re
25 12 import sys
26 13 from unittest import TestCase
@@ -1093,3 +1080,29 b' class TestLink(TestCase):'
1093 1080 a.value = 4
1094 1081 self.assertEqual(''.join(callback_count), 'ab')
1095 1082 del callback_count[:]
1083
1084 class Pickleable(HasTraits):
1085 i = Int()
1086 j = Int()
1087
1088 def _i_default(self):
1089 return 1
1090
1091 def _i_changed(self, name, old, new):
1092 self.j = new
1093
1094 def test_pickle_hastraits():
1095 c = Pickleable()
1096 for protocol in range(pickle.HIGHEST_PROTOCOL + 1):
1097 p = pickle.dumps(c, protocol)
1098 c2 = pickle.loads(p)
1099 nt.assert_equal(c2.i, c.i)
1100 nt.assert_equal(c2.j, c.j)
1101
1102 c.i = 5
1103 for protocol in range(pickle.HIGHEST_PROTOCOL + 1):
1104 p = pickle.dumps(c, protocol)
1105 c2 = pickle.loads(p)
1106 nt.assert_equal(c2.i, c.i)
1107 nt.assert_equal(c2.j, c.j)
1108 No newline at end of file
@@ -32,25 +32,13 b' Inheritance diagram:'
32 32
33 33 .. inheritance-diagram:: IPython.utils.traitlets
34 34 :parts: 3
35
36 Authors:
37
38 * Brian Granger
39 * Enthought, Inc. Some of the code in this file comes from enthought.traits
40 and is licensed under the BSD license. Also, many of the ideas also come
41 from enthought.traits even though our implementation is very different.
42 35 """
43 36
44 #-----------------------------------------------------------------------------
45 # Copyright (C) 2008-2011 The IPython Development Team
37 # Copyright (c) IPython Development Team.
38 # Distributed under the terms of the Modified BSD License.
46 39 #
47 # Distributed under the terms of the BSD License. The full license is in
48 # the file COPYING, distributed as part of this software.
49 #-----------------------------------------------------------------------------
50
51 #-----------------------------------------------------------------------------
52 # Imports
53 #-----------------------------------------------------------------------------
40 # Adapted from enthought.traits, Copyright (c) Enthought, Inc.,
41 # also under the terms of the Modified BSD License.
54 42
55 43 import contextlib
56 44 import inspect
@@ -332,8 +320,8 b' class TraitType(object):'
332 320 obj._trait_values[self.name] = newdv
333 321 return
334 322 # Complete the dynamic initialization.
335 obj._trait_dyn_inits[self.name] = cls.__dict__[meth_name]
336
323 obj._trait_dyn_inits[self.name] = meth_name
324
337 325 def __get__(self, obj, cls=None):
338 326 """Get the value of the trait by self.name for the instance.
339 327
@@ -350,7 +338,8 b' class TraitType(object):'
350 338 except KeyError:
351 339 # Check for a dynamic initializer.
352 340 if self.name in obj._trait_dyn_inits:
353 value = obj._trait_dyn_inits[self.name](obj)
341 method = getattr(obj, obj._trait_dyn_inits[self.name])
342 value = method()
354 343 # FIXME: Do we really validate here?
355 344 value = self._validate(obj, value)
356 345 obj._trait_values[self.name] = value
General Comments 0
You need to be logged in to leave comments. Login now