My actual application is that I'm trying to demo/test the "pickability" of some objects in a doctest, but doctest and pickle don't seem to play along well (in general, actually). Below is some minimal code to reproduce problem.
The following code works find if I run it in a console or a module.
>>> import pickle
>>> class A: ...
>>> unpickled_A = pickle.loads(pickle.dumps(A))
>>> assert unpickled_A == A
But if I include it as a doctest and run it, I'll get:
Traceback (most recent call last):
...
exec(compile(example.source, filename, "single",
File "<doctest __main__.foo[3]>", line 1, in <module>
unpickled_A = pickle.loads(pickle.dumps(A))
_pickle.PicklingError: Can't pickle <class '__main__.A'>: attribute lookup A on __main__ failed
How can I tell doctest to play nice with pickle, or (shame to have to do this though!) define my class so it'll play nicely with doctest+pickle?