-- cgit v1.2.1 From 1d38c96b575efb415961a991124525cdad3ef569 Mon Sep 17 00:00:00 2001 From: Ben Davis Date: Sat, 26 Apr 2014 13:12:46 -0500 Subject: Fixed #66: Replace the implementation of `six.with_metaclass` with the Flask one. --- six.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/six.py b/six.py index 1554288..fcfab5a 100644 --- a/six.py +++ b/six.py @@ -637,7 +637,21 @@ _add_doc(reraise, """Reraise an exception.""") def with_metaclass(meta, *bases): """Create a base class with a metaclass.""" - return meta("NewBase", 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.). + 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, {}) + def add_metaclass(metaclass): """Class decorator for creating a class with a metaclass.""" -- cgit v1.2.1 From c09c8f008e60cfa3df123c483fc13d088ce8df5a Mon Sep 17 00:00:00 2001 From: Ben Davis Date: Sat, 26 Apr 2014 20:23:13 -0500 Subject: Test to ensure that with_metaclass() does not insert any intermediary bases --- test_six.py | 1 + 1 file changed, 1 insertion(+) diff --git a/test_six.py b/test_six.py index 9e39444..766da9e 100644 --- a/test_six.py +++ b/test_six.py @@ -634,6 +634,7 @@ def test_with_metaclass(): assert type(X) is Meta assert issubclass(X, Base) assert issubclass(X, Base2) + assert X.__mro__ == (X, Base, Base2, object) def test_add_metaclass(): -- cgit v1.2.1