summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBenjamin Peterson <benjamin@python.org>2013-12-20 22:14:45 -0600
committerBenjamin Peterson <benjamin@python.org>2013-12-20 22:14:45 -0600
commit54e88aec5f7156dd6dcd440abb5b7e81cdb38ca5 (patch)
tree69e08818d2bfa52962e846ac01bbdbf0dc86f9e5
parent4ba1d2432cbb5217226194b5a3941c6de01fef6e (diff)
downloadsix-54e88aec5f7156dd6dcd440abb5b7e81cdb38ca5.tar.gz
fix add_metaclass when __slots__ is a string (fixes #47)
-rw-r--r--CHANGES3
-rw-r--r--six.py8
-rw-r--r--test_six.py11
3 files changed, 20 insertions, 2 deletions
diff --git a/CHANGES b/CHANGES
index 19d3843..6aca0dd 100644
--- a/CHANGES
+++ b/CHANGES
@@ -6,6 +6,9 @@ This file lists the changes in each six version.
Development version
-------------------
+- Issue #47: Fix add_metaclass on classes with a string for the __slots__
+ variable.
+
- Issue #44: Fix interpretation of backslashes on Python 2 in the u() function.
- Pull request #21: Add import mapping for urllib's proxy_bypass function.
diff --git a/six.py b/six.py
index f782f9a..a0eea9a 100644
--- a/six.py
+++ b/six.py
@@ -581,7 +581,11 @@ def add_metaclass(metaclass):
orig_vars = cls.__dict__.copy()
orig_vars.pop('__dict__', None)
orig_vars.pop('__weakref__', None)
- for slots_var in orig_vars.get('__slots__', ()):
- orig_vars.pop(slots_var)
+ slots = orig_vars.get('__slots__')
+ if slots is not None:
+ if isinstance(slots, str):
+ slots = [slots]
+ for slots_var in slots:
+ orig_vars.pop(slots_var)
return metaclass(cls.__name__, cls.__bases__, orig_vars)
return wrapper
diff --git a/test_six.py b/test_six.py
index 81b424f..7f92617 100644
--- a/test_six.py
+++ b/test_six.py
@@ -630,3 +630,14 @@ def test_add_metaclass():
instance = MySlots()
instance.a = "foo"
py.test.raises(AttributeError, setattr, instance, "c", "baz")
+
+ # Test a class with string for slots.
+ class MyStringSlots(object):
+ __slots__ = "ab"
+ MyStringSlots = six.add_metaclass(Meta1)(MyStringSlots)
+ assert MyStringSlots.__slots__ == "ab"
+ instance = MyStringSlots()
+ instance.ab = "foo"
+ py.test.raises(AttributeError, setattr, instance, "a", "baz")
+ py.test.raises(AttributeError, setattr, instance, "b", "baz")
+