[Python-checkins] cpython (2.7): PyEval_CallObject requires a tuple of args (closes #13186)

benjamin.peterson python-checkins at python.org
Sat Oct 15 19:43:31 CEST 2011


http://hg.python.org/cpython/rev/418fbf1af875
changeset:   72942:418fbf1af875
branch:      2.7
parent:      72939:368134e10d09
user:        Benjamin Peterson <benjamin at python.org>
date:        Sat Oct 15 13:43:21 2011 -0400
summary:
  PyEval_CallObject requires a tuple of args (closes #13186)

files:
  Lib/test/test_class.py    |  13 +++++++++++++
  Misc/NEWS                 |   3 +++
  Modules/_testcapimodule.c |  14 ++++++++++++++
  Objects/classobject.c     |   2 +-
  4 files changed, 31 insertions(+), 1 deletions(-)


diff --git a/Lib/test/test_class.py b/Lib/test/test_class.py
--- a/Lib/test/test_class.py
+++ b/Lib/test/test_class.py
@@ -350,6 +350,19 @@
         AllTests.__delslice__ = delslice
 
 
+    @test_support.cpython_only
+    def testDelItem(self):
+        class A:
+            ok = False
+            def __delitem__(self, key):
+                self.ok = True
+        a = A()
+        # Subtle: we need to call PySequence_SetItem, not PyMapping_SetItem.
+        from _testcapi import sequence_delitem
+        sequence_delitem(a, 2)
+        self.assertTrue(a.ok)
+
+
     def testUnaryOps(self):
         testme = AllTests()
 
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -9,6 +9,9 @@
 Core and Builtins
 -----------------
 
+- Issue #13186: Fix __delitem__ on old-style instances when invoked through
+  PySequence_DelItem.
+
 - Issue #13156: Revert the patch for issue #10517 (reset TLS upon fork()),
   which was only relevant for the native pthread TLS implementation.
 
diff --git a/Modules/_testcapimodule.c b/Modules/_testcapimodule.c
--- a/Modules/_testcapimodule.c
+++ b/Modules/_testcapimodule.c
@@ -1639,6 +1639,19 @@
     return PyErr_NewExceptionWithDoc(name, doc, base, dict);
 }
 
+static PyObject *
+sequence_delitem(PyObject *self, PyObject *args)
+{
+    PyObject *seq;
+    Py_ssize_t i;
+
+    if (!PyArg_ParseTuple(args, "On", &seq, &i))
+        return NULL;
+    if (PySequence_DelItem(seq, i) < 0)
+        return NULL;
+    Py_RETURN_NONE;
+}
+
 static PyMethodDef TestMethods[] = {
     {"raise_exception",         raise_exception,                 METH_VARARGS},
     {"test_config",             (PyCFunction)test_config,        METH_NOARGS},
@@ -1695,6 +1708,7 @@
     {"code_newempty", code_newempty,                     METH_VARARGS},
     {"make_exception_with_doc", (PyCFunction)make_exception_with_doc,
      METH_VARARGS | METH_KEYWORDS},
+    {"sequence_delitem", (PyCFunction)sequence_delitem, METH_VARARGS},
     {NULL, NULL} /* sentinel */
 };
 
diff --git a/Objects/classobject.c b/Objects/classobject.c
--- a/Objects/classobject.c
+++ b/Objects/classobject.c
@@ -1221,7 +1221,7 @@
     if (func == NULL)
         return -1;
     if (item == NULL)
-        arg = PyInt_FromSsize_t(i);
+        arg = Py_BuildValue("(n)", i);
     else
         arg = Py_BuildValue("(nO)", i, item);
     if (arg == NULL) {

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


More information about the Python-checkins mailing list