Benjamin Peterson avatar Benjamin Peterson committed f6223ba

when an argument is a cell, set the local copy to NULL (see #17927)

Comments (0)

Files changed (3)

Lib/test/test_super.py

                 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)

Objects/typeobject.c

             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,
         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) {
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.