Author: Armin Rigo <[email protected]>
Branch: 
Changeset: r84005:cc64a16862ce
Date: 2016-04-28 11:26 +0200
http://bitbucket.org/pypy/pypy/changeset/cc64a16862ce/

Log:    fix: operator.attrgetter("name", not_a_string) would raise a
        confusing error message

diff --git a/pypy/module/operator/app_operator.py 
b/pypy/module/operator/app_operator.py
--- a/pypy/module/operator/app_operator.py
+++ b/pypy/module/operator/app_operator.py
@@ -72,16 +72,14 @@
 
 class attrgetter(object):
     def __init__(self, attr, *attrs):
-        if (
-            not isinstance(attr, basestring) or
-            not all(isinstance(a, basestring) for a in attrs)
-        ):
-            def _raise_typeerror(obj):
-                raise TypeError(
-                    "argument must be a string, not %r" % type(attr).__name__
-                )
-            self._call = _raise_typeerror
-        elif attrs:
+        if not isinstance(attr, basestring):
+            self._error(attr)
+            return
+        if attrs:
+            for a in attrs:
+                if not isinstance(a, basestring):
+                    self._error(a)
+                    return
             self._multi_attrs = [
                 a.split(".") for a in [attr] + list(attrs)
             ]
@@ -93,6 +91,13 @@
             self._single_attr = attr.split(".")
             self._call = self._single_attrgetter
 
+    def _error(self, attr):
+        def _raise_typeerror(obj):
+            raise TypeError(
+                "attribute name must be a string, not %r" % type(attr).__name__
+            )
+        self._call = _raise_typeerror
+
     def __call__(self, obj):
         return self._call(obj)
 
diff --git a/pypy/module/operator/test/test_operator.py 
b/pypy/module/operator/test/test_operator.py
--- a/pypy/module/operator/test/test_operator.py
+++ b/pypy/module/operator/test/test_operator.py
@@ -33,7 +33,8 @@
         a.z = 'Z'
 
         assert operator.attrgetter('x','z','y')(a) == ('X', 'Z', 'Y')
-        raises(TypeError, operator.attrgetter('x', (), 'y'), a)
+        e = raises(TypeError, operator.attrgetter('x', (), 'y'), a)
+        assert str(e.value) == "attribute name must be a string, not 'tuple'"
 
         data = map(str, range(20))
         assert operator.itemgetter(2,10,5)(data) == ('2', '10', '5')
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to