[Python-checkins] cpython: when an argument is a cell, set the local copy to NULL (see #17927)

benjamin.peterson python-checkins at python.org
Mon May 13 01:18:17 CEST 2013


http://hg.python.org/cpython/rev/f6223bab5657
changeset:   83755:f6223bab5657
user:        Benjamin Peterson <benjamin at python.org>
date:        Sun May 12 18:16:06 2013 -0500
summary:
  when an argument is a cell, set the local copy to NULL (see #17927)

files:
  Lib/test/test_super.py |  13 +++++++++++++
  Objects/typeobject.c   |  14 +++++++++++---
  Python/ceval.c         |  12 ++++--------
  3 files changed, 28 insertions(+), 11 deletions(-)


diff --git a/Lib/test/test_super.py b/Lib/test/test_super.py
--- a/Lib/test/test_super.py
+++ b/Lib/test/test_super.py
@@ -130,6 +130,19 @@
                 super()
         self.assertRaises(RuntimeError, X().f)
 
+    def test_cell_as_self(self):
+        class X:
+            def meth(self):
+                super()
+
+        def f():
+            k = X()
+            def g():
+                return k
+            return g
+        c = f().__closure__[0]
+        self.assertRaises(TypeError, X.meth, c)
+
 
 def test_main():
     support.run_unittest(TestSuper)
diff --git a/Objects/typeobject.c b/Objects/typeobject.c
--- a/Objects/typeobject.c
+++ b/Objects/typeobject.c
@@ -6510,9 +6510,17 @@
             return -1;
         }
         obj = f->f_localsplus[0];
-        if (obj != NULL && PyCell_Check(obj)) {
-            /* It might be a cell.  See cell var initialization in ceval.c. */
-            obj = PyCell_GET(obj);
+        if (obj == NULL && co->co_cell2arg) {
+            /* The first argument might be a cell. */
+            n = PyTuple_GET_SIZE(co->co_cellvars);
+            for (i = 0; i < n; i++) {
+                if (co->co_cell2arg[i] == 0) {
+                    PyObject *cell = f->f_localsplus[co->co_nlocals + i];
+                    assert(PyCell_Check(cell));
+                    obj = PyCell_GET(cell);
+                    break;
+                }
+            }
         }
         if (obj == NULL) {
             PyErr_SetString(PyExc_RuntimeError,
diff --git a/Python/ceval.c b/Python/ceval.c
--- a/Python/ceval.c
+++ b/Python/ceval.c
@@ -3521,18 +3521,14 @@
         if (co->co_cell2arg != NULL &&
             (arg = co->co_cell2arg[i]) != CO_CELL_NOT_AN_ARG) {
             c = PyCell_New(GETLOCAL(arg));
-            if (c == NULL)
-                goto fail;
-            /* Reference the cell from the argument slot, for super().
-               See typeobject.c. */
-            Py_INCREF(c);
-            SETLOCAL(arg, c);
+            /* Clear the local copy. */
+            SETLOCAL(arg, NULL);
         }
         else {
             c = PyCell_New(NULL);
-            if (c == NULL)
-                goto fail;
         }
+        if (c == NULL)
+            goto fail;
         SETLOCAL(co->co_nlocals + i, c);
     }
     for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {

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


More information about the Python-checkins mailing list