summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBenjamin Peterson <benjamin@python.org>2014-04-26 21:27:57 -0400
committerBenjamin Peterson <benjamin@python.org>2014-04-26 21:27:57 -0400
commitcb0243d1e5f324ce043c1778cabab55ea38fb185 (patch)
tree51e021768763d73249b8cba8eb60ea40bb569709
parenta32d7e6ade58d2d8bd406867b45357a15e2eac89 (diff)
parentc09c8f008e60cfa3df123c483fc13d088ce8df5a (diff)
downloadsix-cb0243d1e5f324ce043c1778cabab55ea38fb185.tar.gz
Merged in bendavis78/six/issues/66 (pull request #35)
Fixed #66: Replace the implementation of `six.with_metaclass` with the Flask one.
-rw-r--r--six.py16
-rw-r--r--test_six.py1
2 files changed, 16 insertions, 1 deletions
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."""
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():