Skip to content

Commit e265bfa

Browse files
committed
Merge pull request #461 from satra/fix/removetraitschanges
Fix/removetraitschanges
2 parents 039cb6b + 11571f7 commit e265bfa

File tree

2 files changed

+14
-26
lines changed

2 files changed

+14
-26
lines changed

nipype/interfaces/base.py

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -499,17 +499,6 @@ def _get_sorteddict(self, object, dictwithhash=False, hash_method=None, hash_fil
499499
out = object
500500
return out
501501

502-
def __getstate__(self):
503-
state = super(BaseTraitedSpec, self).__getstate__()
504-
inst_traits = self._instance_traits()
505-
state['__instance_traits__'] = inst_traits
506-
return state
507-
508-
def __setstate__(self, state):
509-
inst_traits = state.pop('__instance_traits__', {})
510-
for attr, trait in inst_traits.iteritems():
511-
self.add_trait(attr, trait)
512-
super(BaseTraitedSpec, self).__setstate__(state)
513502

514503
class DynamicTraitedSpec(BaseTraitedSpec):
515504
""" A subclass to handle dynamic traits
@@ -524,12 +513,22 @@ def __deepcopy__(self, memo):
524513
id_self = id(self)
525514
if id_self in memo:
526515
return memo[id_self]
527-
dup_dict = deepcopy(self.__getstate__(), memo)
516+
dup_dict = deepcopy(self.get(), memo)
517+
# access all keys
518+
for key in self.copyable_trait_names():
519+
_ = getattr(self, key)
520+
# clone once
528521
dup = self.clone_traits(memo=memo)
529-
dup.__setstate__(dup_dict)
522+
for key in self.copyable_trait_names():
523+
try:
524+
_ = getattr(dup, key)
525+
except:
526+
pass
527+
# clone twice
528+
dup = self.clone_traits(memo=memo)
529+
dup.set(**dup_dict)
530530
return dup
531531

532-
533532
class TraitedSpec(BaseTraitedSpec):
534533
""" Create a subclass with strict traits.
535534

nipype/interfaces/tests/test_base.py

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ class spec(nib.TraitedSpec):
9191
#yield assert_equal, infields.hashval[1], hashval[1]
9292
yield assert_equal, infields.__repr__(), '\nfoo = 1\ngoo = 0.0\n'
9393

94-
def test_TraitedSpec_pickle():
94+
def test_TraitedSpec_dynamic():
9595
from cPickle import dumps, loads
9696
a = nib.BaseTraitedSpec()
9797
a.add_trait('foo', nib.traits.Int)
@@ -103,17 +103,6 @@ def test_TraitedSpec_pickle():
103103
assign_a_again = lambda : setattr(unpkld_a, 'foo', 'a')
104104
yield assert_raises, Exception, assign_a_again
105105

106-
def test_TraitedSpec_deepcopy():
107-
from copy import deepcopy
108-
a = nib.DynamicTraitedSpec()
109-
a.add_trait('foo', nib.traits.Int)
110-
a.foo = 1
111-
assign_a = lambda : setattr(a, 'foo', 'a')
112-
yield assert_raises, Exception, assign_a
113-
unpkld_a = deepcopy(a)
114-
assign_a_again = lambda : setattr(unpkld_a, 'foo', 'a')
115-
yield assert_raises, Exception, assign_a_again
116-
117106
def test_TraitedSpec_logic():
118107
class spec3(nib.TraitedSpec):
119108
_xor_inputs = ('foo', 'bar')

0 commit comments

Comments
 (0)