[Python-Dev] doctest and pickle
Steven D'Aprano
steve at pearwood.info
Sat Jun 8 08:45:49 CEST 2013
On 08/06/13 15:18, Stephen J. Turnbull wrote:
> Ethan Furman writes:
>
> > Enumerations can be pickled and unpickled::
> >
> > >>> from enum import Enum
> > >>> class Fruit(Enum):
> > ... tomato = 1
> > ... banana = 2
> > ... cherry = 3
> > ...
> > >>> from pickle import dumps, loads
> > >>> Fruit.tomato is loads(dumps(Fruit.tomato))
> > True
> > [...]
> > Still, it would be nice if this could work.
>
> Well, you could cheat and reverse the test. ;-)
>
> I assume the problem is that loads proceeds to recreate the Fruit
> enum, rather than checking if there already is one?
I don't believe so. I understand that the problem is that pickle cannot find the Fruit enum in the __main__ module.
Untested, but adding this before the call to dumps might work:
import __main__
__main__.Fruit = Fruit
although that's the sort of thing that makes me think it's time to turn this into a unittest rather than a doctest.
--
Steven
More information about the Python-Dev
mailing list