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

Guido van Rossum guido at python.org
Thu May 2 22:39:21 CEST 2013


On Thu, May 2, 2013 at 1:18 PM, fwierzbicki at gmail.com
<fwierzbicki at gmail.com> wrote:
> On Thu, May 2, 2013 at 12:07 PM, Ethan Furman <ethan at stoneleaf.us> wrote:
>> 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__']
>>
>> This works fine for Cpython, but what about the others?
> This should work for Jython, but I can't say I like it. I believe
> IronPython has a sort of speedup mode that disallows the use of
> _getframe, and I'd like to add this to Jython someday.

This particular function is typically only called at module load time,
so speeding it up isn't worth it.

FWIW, as Eli pointed out, namedtuple() does the same thing (since
Python 2.6), so we'll just copy that code (refactoring it doesn't have
to hold up the PEP). The only other alternative I find acceptable is
not to have the convenience API at all. That's Eli's call.

[Eli]
> Would nesting the non-convenience Enum in a function or a class allow one to
> pickle it? I think programmers who want their libraries to be pickle-able
> already have to be aware of some restrictions about what can and cannot be
> pickled.

Apparently it hasn't been a problem for namedtuple. Calling
namedtuple() or Enum() in another function is similar to a class
statement inside a function -- the resulting class isn't picklable.

(But from this, don't conclude that it's not important for
namedtuple() or Enum() to return a picklable class. It is important.
It is just not important to try to make it work when they are called
through some other wrapper -- there's just not much use for such a
pattern.)

-- 
--Guido van Rossum (python.org/~guido)


More information about the Python-Dev mailing list