forked from orbit-oss/flask
Reimplement function with_metaclass()
This commit is contained in:
parent
ac12e362c4
commit
c8f19f0afc
1 changed files with 4 additions and 12 deletions
|
|
@ -56,21 +56,13 @@ else:
|
||||||
def with_metaclass(meta, *bases):
|
def with_metaclass(meta, *bases):
|
||||||
# This requires a bit of explanation: the basic idea is to make a
|
# This requires a bit of explanation: the basic idea is to make a
|
||||||
# dummy metaclass for one level of class instantiation that replaces
|
# dummy metaclass for one level of class instantiation that replaces
|
||||||
# itself with the actual metaclass. Because of internal type checks
|
# itself with the actual metaclass.
|
||||||
# we also need to make sure that we downgrade the custom metaclass
|
|
||||||
# for one level to something closer to type (that's why __call__ and
|
|
||||||
# __init__ comes back from type etc.).
|
|
||||||
#
|
#
|
||||||
# This has the advantage over six.with_metaclass in that it does not
|
# This has the advantage over six.with_metaclass in that it does not
|
||||||
# introduce dummy classes into the final MRO.
|
# introduce dummy classes into the final MRO.
|
||||||
class metaclass(meta):
|
constructor = lambda c, name, b, dct: meta(name, bases, dct)
|
||||||
__call__ = type.__call__
|
metaclass = type('with_metaclass', (type,), {'__new__': constructor})
|
||||||
__init__ = type.__init__
|
return type.__new__(metaclass, 'temporary_class', (object,), {})
|
||||||
def __new__(cls, name, this_bases, d):
|
|
||||||
if this_bases is None:
|
|
||||||
return type.__new__(cls, name, (), d)
|
|
||||||
return meta(name, bases, d)
|
|
||||||
return metaclass('temporary_class', None, {})
|
|
||||||
|
|
||||||
|
|
||||||
# Certain versions of pypy have a bug where clearing the exception stack
|
# Certain versions of pypy have a bug where clearing the exception stack
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue