From c8f19f0afc8d49a68ec969097e6127d17706b6dc Mon Sep 17 00:00:00 2001 From: "Mathias J. Hennig" Date: Wed, 22 Jul 2015 23:35:13 +0200 Subject: [PATCH 1/2] Reimplement function with_metaclass() --- flask/_compat.py | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/flask/_compat.py b/flask/_compat.py index 8ea7719f..fbbf914f 100644 --- a/flask/_compat.py +++ b/flask/_compat.py @@ -56,21 +56,13 @@ else: def with_metaclass(meta, *bases): # This requires a bit of explanation: the basic idea is to make a # dummy metaclass for one level of class instantiation that replaces - # itself with the actual metaclass. Because of internal type checks - # 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.). + # itself with the actual metaclass. # # This has the advantage over six.with_metaclass in that it does not # introduce dummy classes into the final MRO. - class metaclass(meta): - __call__ = type.__call__ - __init__ = type.__init__ - 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, {}) + constructor = lambda c, name, b, dct: meta(name, bases, dct) + metaclass = type('with_metaclass', (type,), {'__new__': constructor}) + return type.__new__(metaclass, 'temporary_class', (object,), {}) # Certain versions of pypy have a bug where clearing the exception stack From b3767ae59f8d916333ebc2cc04d962fa33f76580 Mon Sep 17 00:00:00 2001 From: "Mathias J. Hennig" Date: Mon, 27 Jul 2015 15:32:23 +0200 Subject: [PATCH 2/2] Addressing feedback from pull request --- flask/_compat.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/flask/_compat.py b/flask/_compat.py index fbbf914f..bfe607d6 100644 --- a/flask/_compat.py +++ b/flask/_compat.py @@ -54,15 +54,14 @@ else: def with_metaclass(meta, *bases): + """Create a base class with a metaclass.""" # This requires a bit of explanation: the basic idea is to make a # dummy metaclass for one level of class instantiation that replaces # itself with the actual metaclass. - # - # This has the advantage over six.with_metaclass in that it does not - # introduce dummy classes into the final MRO. - constructor = lambda c, name, b, dct: meta(name, bases, dct) - metaclass = type('with_metaclass', (type,), {'__new__': constructor}) - return type.__new__(metaclass, 'temporary_class', (object,), {}) + class metaclass(type): + def __new__(cls, name, this_bases, d): + return meta(name, bases, d) + return type.__new__(metaclass, 'temporary_class', (), {}) # Certain versions of pypy have a bug where clearing the exception stack