Robert Kern | 18 Jul 2012 18:28
Gravatar

Re: Pickling compatiblity between traits3 and traits4

On Wed, Jul 18, 2012 at 5:04 PM, Giraudon Cyril <cyril.giraudon@...> wrote:
> Hi everyone,
>
>  From the following example I try to pickle a traits3 object and
> unpickle it as a traits4 instance :
>
> #############################
> from enthought.traits.api import HasTraits, Str, Int
>
> class Person(HasTraits):
>      name = Str("John")
>      age = Int(0)
>
>      def __getstate__(self):
>          return {"name": self.name,
>                        "age": self.age}
>
> if __name__ == "__main__":
>      from cPickle import dump, load
>      from os import path
>      filename = "person.pkl"
>      if path.exists(filename):
>          with open(filename, "r") as in_:
>              p = load(in_)
>          print "P name : ", p.name
>          print "P age : ", p.age
>      else:
>          with open(filename, "w") as out:
>              p = Person(name="Jacques", age=23)
>              dump(p, out)
> #############################
>
> The first execution of the script in a traits3 virtualenv creates a
> person.pkl file.
> The second execution of the script in a traits4 virtualenv reads
> person.pkl file.
>
> When the file is unserialized the following error is raised :
> Traceback (most recent call last):
>    File "family.py", line 18, in <module>
>      p = load(in_)
> AttributeError: 'module' object has no attribute '__newobj__'
>
> Do you think there is any level of compatiblity between traits3 and
> traits4 ?

In general, I wouldn't get your hopes up. Pickle does not deal well
with refactoring. If you need to communicate serialized objects
between two programs that are sitting on different versions of Traits,
I recommend using more explicit conversions to and from neutral
"primitive" types.

This particular case can probably be worked around by fixing the
etsproxy module enthought/traits/traits.py to include this extra
import:

  from traits.traits import __newobj__

But I suspect you will quickly run into another problem.

--

-- 
Robert Kern
Enthought

Gmane