[Python-checkins] cpython (2.7): Issue #26822: itemgetter, attrgetter and methodcaller objects no longer

serhiy.storchaka python-checkins at python.org
Sat Apr 23 03:54:06 EDT 2016


https://hg.python.org/cpython/rev/9b565815079a
changeset:   101101:9b565815079a
branch:      2.7
parent:      101099:8d6bd32a56a8
user:        Serhiy Storchaka <storchaka at gmail.com>
date:        Sat Apr 23 10:53:28 2016 +0300
summary:
  Issue #26822: itemgetter, attrgetter and methodcaller objects no longer
silently ignore keyword arguments.

files:
  Lib/test/test_operator.py |  9 +++++++++
  Misc/NEWS                 |  3 +++
  Modules/operator.c        |  6 ++++++
  3 files changed, 18 insertions(+), 0 deletions(-)


diff --git a/Lib/test/test_operator.py b/Lib/test/test_operator.py
--- a/Lib/test/test_operator.py
+++ b/Lib/test/test_operator.py
@@ -371,6 +371,9 @@
         a.name = 'arthur'
         f = operator.attrgetter('name')
         self.assertEqual(f(a), 'arthur')
+        self.assertRaises(TypeError, f)
+        self.assertRaises(TypeError, f, a, 'dent')
+        self.assertRaises(TypeError, f, a, surname='dent')
         f = operator.attrgetter('rank')
         self.assertRaises(AttributeError, f, a)
         f = operator.attrgetter(2)
@@ -414,6 +417,9 @@
         a = 'ABCDE'
         f = operator.itemgetter(2)
         self.assertEqual(f(a), 'C')
+        self.assertRaises(TypeError, f)
+        self.assertRaises(TypeError, f, a, 3)
+        self.assertRaises(TypeError, f, a, size=3)
         f = operator.itemgetter(10)
         self.assertRaises(IndexError, f, a)
 
@@ -456,6 +462,9 @@
         self.assertRaises(IndexError, f, a)
         f = operator.methodcaller('foo', 1, 2)
         self.assertEqual(f(a), 3)
+        self.assertRaises(TypeError, f)
+        self.assertRaises(TypeError, f, a, 3)
+        self.assertRaises(TypeError, f, a, spam=3)
         f = operator.methodcaller('bar')
         self.assertEqual(f(a), 42)
         self.assertRaises(TypeError, f, a, a)
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -77,6 +77,9 @@
 Library
 -------
 
+- Issue #26822: itemgetter, attrgetter and methodcaller objects no longer
+  silently ignore keyword arguments.
+
 - Issue #26657: Fix directory traversal vulnerability with SimpleHTTPServer
   on Windows.  This fixes a regression that was introduced in 2.7.7.  Based
   on patch by Philipp Hagemeister.
diff --git a/Modules/operator.c b/Modules/operator.c
--- a/Modules/operator.c
+++ b/Modules/operator.c
@@ -511,6 +511,8 @@
     PyObject *obj, *result;
     Py_ssize_t i, nitems=ig->nitems;
 
+    if (!_PyArg_NoKeywords("itemgetter", kw))
+        return NULL;
     if (!PyArg_UnpackTuple(args, "itemgetter", 1, 1, &obj))
         return NULL;
     if (nitems == 1)
@@ -691,6 +693,8 @@
     PyObject *obj, *result;
     Py_ssize_t i, nattrs=ag->nattrs;
 
+    if (!_PyArg_NoKeywords("attrgetter", kw))
+        return NULL;
     if (!PyArg_UnpackTuple(args, "attrgetter", 1, 1, &obj))
         return NULL;
     if (ag->nattrs == 1)
@@ -838,6 +842,8 @@
 {
     PyObject *method, *obj, *result;
 
+    if (!_PyArg_NoKeywords("methodcaller", kw))
+        return NULL;
     if (!PyArg_UnpackTuple(args, "methodcaller", 1, 1, &obj))
         return NULL;
     method = PyObject_GetAttr(obj, mc->name);

-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list