[Python-Dev] PyPy, Jython, & IronPython: Enum convenience function and pickleablity

Stefan Behnel stefan_ml at behnel.de
Fri May 3 12:11:47 CEST 2013


Ethan Furman, 02.05.2013 21:07:
> In order for the Enum convenience function to be pickleable, we have this
> line of code in the metaclass:
> 
>     enum_class.__module__ = sys._getframe(1).f_globals['__name__']

What a hack. And fragile, too.


> This works fine for Cpython, but what about the others?

This doesn't work when used from Cython compiled code due to the lack of
frames. They are only created for exception tracebacks and not for normal
code by default (just for profiling, coverage etc.). My guess is that
no-one noticed the problem for namedtuples so far because using them is
still uncommon enough in general, let alone pickling them, and the module
name hack only leads to an error when someone tries to pickle such an object.

I think that this will be more of a problem for enums than for namedtuples,
because enums are more likely to appear in data structures that people want
to pickle.

The most simple work-around seems to be this, once you know about it:

"""
ttuple = namedtuple('ttuple', 'a b c')
ttuple.__module__ = __name__   # enable pickle support
"""

Not any worse than the hack above, IMHO, but at least guaranteed to work.

For enums, a regular class based declaration can easily avoid this hack, so
my vote is for getting rid of the "convenience" API before it starts doing
any harm. Or document it explicitly as generating unpicklable objects, as
Antoine suggests.

Stefan




More information about the Python-Dev mailing list