Skip to content

Commit

Permalink
Merge pull request #5554 from minrk/pickle-traitlets2
Browse files Browse the repository at this point in the history
Make HasTraits pickleable
  • Loading branch information
takluyver committed Apr 8, 2014
2 parents 5f909c1 + 9283047 commit 6ab319b
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 39 deletions.
53 changes: 33 additions & 20 deletions IPython/utils/tests/test_traitlets.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,13 @@
# encoding: utf-8
"""
Tests for IPython.utils.traitlets.
"""Tests for IPython.utils.traitlets."""

Authors:
* Brian Granger
* Enthought, Inc. Some of the code in this file comes from enthought.traits
and is licensed under the BSD license. Also, many of the ideas also come
from enthought.traits even though our implementation is very different.
"""

#-----------------------------------------------------------------------------
# Copyright (C) 2008-2011 The IPython Development Team
#
# Distributed under the terms of the BSD License. The full license is in
# the file COPYING, distributed as part of this software.
#-----------------------------------------------------------------------------

#-----------------------------------------------------------------------------
# Imports
#-----------------------------------------------------------------------------
# Copyright (c) IPython Development Team.
# Distributed under the terms of the Modified BSD License.
#
# Adapted from enthought.traits, Copyright (c) Enthought, Inc.,
# also under the terms of the Modified BSD License.

import pickle
import re
import sys
from unittest import TestCase
Expand Down Expand Up @@ -1093,3 +1080,29 @@ def b_callback(name, old, new):
a.value = 4
self.assertEqual(''.join(callback_count), 'ab')
del callback_count[:]

class Pickleable(HasTraits):
i = Int()
j = Int()

def _i_default(self):
return 1

def _i_changed(self, name, old, new):
self.j = new

def test_pickle_hastraits():
c = Pickleable()
for protocol in range(pickle.HIGHEST_PROTOCOL + 1):
p = pickle.dumps(c, protocol)
c2 = pickle.loads(p)
nt.assert_equal(c2.i, c.i)
nt.assert_equal(c2.j, c.j)

c.i = 5
for protocol in range(pickle.HIGHEST_PROTOCOL + 1):
p = pickle.dumps(c, protocol)
c2 = pickle.loads(p)
nt.assert_equal(c2.i, c.i)
nt.assert_equal(c2.j, c.j)

27 changes: 8 additions & 19 deletions IPython/utils/traitlets.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,25 +32,13 @@
.. inheritance-diagram:: IPython.utils.traitlets
:parts: 3
Authors:
* Brian Granger
* Enthought, Inc. Some of the code in this file comes from enthought.traits
and is licensed under the BSD license. Also, many of the ideas also come
from enthought.traits even though our implementation is very different.
"""

#-----------------------------------------------------------------------------
# Copyright (C) 2008-2011 The IPython Development Team
# Copyright (c) IPython Development Team.
# Distributed under the terms of the Modified BSD License.
#
# Distributed under the terms of the BSD License. The full license is in
# the file COPYING, distributed as part of this software.
#-----------------------------------------------------------------------------

#-----------------------------------------------------------------------------
# Imports
#-----------------------------------------------------------------------------
# Adapted from enthought.traits, Copyright (c) Enthought, Inc.,
# also under the terms of the Modified BSD License.

import contextlib
import inspect
Expand Down Expand Up @@ -332,8 +320,8 @@ class has been instantiated.
obj._trait_values[self.name] = newdv
return
# Complete the dynamic initialization.
obj._trait_dyn_inits[self.name] = cls.__dict__[meth_name]

obj._trait_dyn_inits[self.name] = meth_name
def __get__(self, obj, cls=None):
"""Get the value of the trait by self.name for the instance.
Expand All @@ -350,7 +338,8 @@ def __get__(self, obj, cls=None):
except KeyError:
# Check for a dynamic initializer.
if self.name in obj._trait_dyn_inits:
value = obj._trait_dyn_inits[self.name](obj)
method = getattr(obj, obj._trait_dyn_inits[self.name])
value = method()
# FIXME: Do we really validate here?
value = self._validate(obj, value)
obj._trait_values[self.name] = value
Expand Down

0 comments on commit 6ab319b

Please sign in to comment.