Skip to content
Myr

Reimplement function with_metaclass()

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Mathias J. Hennig · committed by Mathias J. Hennig · parent ac12e36 · Browse files · 1 file+4 added−12 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+4 added−12 removed

  • other:+4 added−12 removed

1 file+4 added−12 removed

modified:flask/_compat.py+4 added-12 removed
54 unmodified lines
5556def with_metaclass(meta, *bases):57 # This requires a bit of explanation: the basic idea is to make a58 # dummy metaclass for one level of class instantiation that replaces59 # itself with the actual metaclass. Because of internal type checks60 # we also need to make sure that we downgrade the custom metaclass61 # for one level to something closer to type (that's why __call__ and62 # __init__ comes back from type etc.).59 # itself with the actual metaclass.60 #61 # This has the advantage over six.with_metaclass in that it does not62 # introduce dummy classes into the final MRO.66 class metaclass(meta):67 __call__ = type.__call__68 __init__ = type.__init__69 def __new__(cls, name, this_bases, d):70 if this_bases is None:71 return type.__new__(cls, name, (), d)72 return meta(name, bases, d)73 return metaclass('temporary_class', None, {})63 constructor = lambda c, name, b, dct: meta(name, bases, dct)64 metaclass = type('with_metaclass', (type,), {'__new__': constructor})65 return type.__new__(metaclass, 'temporary_class', (object,), {})666768# Certain versions of pypy have a bug where clearing the exception stack69# breaks the __exit__ function in a very peculiar way. This is currently
20 unmodified lines