##// END OF EJS Templates
Make HasTraits pickleable...
Min RK -
Show More
@@ -1,26 +1,13 b''
1 # encoding: utf-8
1 # encoding: utf-8
2 """
2 """Tests for IPython.utils.traitlets."""
3 Tests for IPython.utils.traitlets.
4
3
5 Authors:
4 # Copyright (c) IPython Development Team.
6
5 # Distributed under the terms of the Modified BSD License.
7 * Brian Granger
6 #
8 * Enthought, Inc. Some of the code in this file comes from enthought.traits
7 # Adapted from enthought.traits, Copyright (c) Enthought, Inc.,
9 and is licensed under the BSD license. Also, many of the ideas also come
8 # also under the terms of the Modified BSD License.
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 #-----------------------------------------------------------------------------
23
9
10 import pickle
24 import re
11 import re
25 import sys
12 import sys
26 from unittest import TestCase
13 from unittest import TestCase
@@ -1093,3 +1080,29 b' class TestLink(TestCase):'
1093 a.value = 4
1080 a.value = 4
1094 self.assertEqual(''.join(callback_count), 'ab')
1081 self.assertEqual(''.join(callback_count), 'ab')
1095 del callback_count[:]
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 .. inheritance-diagram:: IPython.utils.traitlets
33 .. inheritance-diagram:: IPython.utils.traitlets
34 :parts: 3
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 #-----------------------------------------------------------------------------
37 # Copyright (c) IPython Development Team.
45 # Copyright (C) 2008-2011 The 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
40 # Adapted from enthought.traits, Copyright (c) Enthought, Inc.,
48 # the file COPYING, distributed as part of this software.
41 # also under the terms of the Modified BSD License.
49 #-----------------------------------------------------------------------------
50
51 #-----------------------------------------------------------------------------
52 # Imports
53 #-----------------------------------------------------------------------------
54
42
55 import contextlib
43 import contextlib
56 import inspect
44 import inspect
@@ -332,8 +320,8 b' class TraitType(object):'
332 obj._trait_values[self.name] = newdv
320 obj._trait_values[self.name] = newdv
333 return
321 return
334 # Complete the dynamic initialization.
322 # Complete the dynamic initialization.
335 obj._trait_dyn_inits[self.name] = cls.__dict__[meth_name]
323 obj._trait_dyn_inits[self.name] = meth_name
336
324
337 def __get__(self, obj, cls=None):
325 def __get__(self, obj, cls=None):
338 """Get the value of the trait by self.name for the instance.
326 """Get the value of the trait by self.name for the instance.
339
327
@@ -350,7 +338,8 b' class TraitType(object):'
350 except KeyError:
338 except KeyError:
351 # Check for a dynamic initializer.
339 # Check for a dynamic initializer.
352 if self.name in obj._trait_dyn_inits:
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 # FIXME: Do we really validate here?
343 # FIXME: Do we really validate here?
355 value = self._validate(obj, value)
344 value = self._validate(obj, value)
356 obj._trait_values[self.name] = value
345 obj._trait_values[self.name] = value
General Comments 0
You need to be logged in to leave comments. Login now