1919#include "pycore_typeobject.h" // struct type_cache
2020#include "pycore_unionobject.h" // _Py_union_type_or
2121#include "pycore_weakref.h" // _PyWeakref_GET_REF()
22+ #include "pycore_cell.h" // PyCell_GetRef()
2223#include "opcode.h" // MAKE_CELL
2324
2425#include <stddef.h> // ptrdiff_t
@@ -11658,23 +11659,28 @@ super_init_without_args(_PyInterpreterFrame *cframe, PyTypeObject **type_p,
1165811659
1165911660 assert (_PyFrame_GetCode (cframe )-> co_nlocalsplus > 0 );
1166011661 PyObject * firstarg = PyStackRef_AsPyObjectBorrow (_PyFrame_GetLocalsArray (cframe )[0 ]);
11662+ if (firstarg == NULL ) {
11663+ PyErr_SetString (PyExc_RuntimeError , "super(): arg[0] deleted" );
11664+ return -1 ;
11665+ }
1166111666 // The first argument might be a cell.
11662- if (firstarg != NULL && (_PyLocals_GetKind (co -> co_localspluskinds , 0 ) & CO_FAST_CELL )) {
11663- // "firstarg" is a cell here unless (very unlikely) super()
11664- // was called from the C-API before the first MAKE_CELL op.
11665- if (_PyInterpreterFrame_LASTI (cframe ) >= 0 ) {
11666- // MAKE_CELL and COPY_FREE_VARS have no quickened forms, so no need
11667- // to use _PyOpcode_Deopt here:
11668- assert (_PyCode_CODE (co )[0 ].op .code == MAKE_CELL ||
11669- _PyCode_CODE (co )[0 ].op .code == COPY_FREE_VARS );
11670- assert (PyCell_Check (firstarg ));
11671- firstarg = PyCell_GET (firstarg );
11667+ // "firstarg" is a cell here unless (very unlikely) super()
11668+ // was called from the C-API before the first MAKE_CELL op.
11669+ if ((_PyLocals_GetKind (co -> co_localspluskinds , 0 ) & CO_FAST_CELL ) &&
11670+ (_PyInterpreterFrame_LASTI (cframe ) >= 0 )) {
11671+ // MAKE_CELL and COPY_FREE_VARS have no quickened forms, so no need
11672+ // to use _PyOpcode_Deopt here:
11673+ assert (_PyCode_CODE (co )[0 ].op .code == MAKE_CELL ||
11674+ _PyCode_CODE (co )[0 ].op .code == COPY_FREE_VARS );
11675+ assert (PyCell_Check (firstarg ));
11676+ firstarg = PyCell_GetRef ((PyCellObject * )firstarg );
11677+ if (firstarg == NULL ) {
11678+ PyErr_SetString (PyExc_RuntimeError , "super(): arg[0] deleted" );
11679+ return -1 ;
1167211680 }
1167311681 }
11674- if (firstarg == NULL ) {
11675- PyErr_SetString (PyExc_RuntimeError ,
11676- "super(): arg[0] deleted" );
11677- return -1 ;
11682+ else {
11683+ Py_INCREF (firstarg );
1167811684 }
1167911685
1168011686 // Look for __class__ in the free vars.
@@ -11689,18 +11695,22 @@ super_init_without_args(_PyInterpreterFrame *cframe, PyTypeObject **type_p,
1168911695 if (cell == NULL || !PyCell_Check (cell )) {
1169011696 PyErr_SetString (PyExc_RuntimeError ,
1169111697 "super(): bad __class__ cell" );
11698+ Py_DECREF (firstarg );
1169211699 return -1 ;
1169311700 }
11694- type = (PyTypeObject * ) PyCell_GET ( cell );
11701+ type = (PyTypeObject * ) PyCell_GetRef (( PyCellObject * ) cell );
1169511702 if (type == NULL ) {
1169611703 PyErr_SetString (PyExc_RuntimeError ,
1169711704 "super(): empty __class__ cell" );
11705+ Py_DECREF (firstarg );
1169811706 return -1 ;
1169911707 }
1170011708 if (!PyType_Check (type )) {
1170111709 PyErr_Format (PyExc_RuntimeError ,
1170211710 "super(): __class__ is not a type (%s)" ,
1170311711 Py_TYPE (type )-> tp_name );
11712+ Py_DECREF (type );
11713+ Py_DECREF (firstarg );
1170411714 return -1 ;
1170511715 }
1170611716 break ;
@@ -11709,6 +11719,7 @@ super_init_without_args(_PyInterpreterFrame *cframe, PyTypeObject **type_p,
1170911719 if (type == NULL ) {
1171011720 PyErr_SetString (PyExc_RuntimeError ,
1171111721 "super(): __class__ cell not found" );
11722+ Py_DECREF (firstarg );
1171211723 return -1 ;
1171311724 }
1171411725
@@ -11755,16 +11766,23 @@ super_init_impl(PyObject *self, PyTypeObject *type, PyObject *obj) {
1175511766 return -1 ;
1175611767 }
1175711768 }
11769+ else {
11770+ Py_INCREF (type );
11771+ Py_XINCREF (obj );
11772+ }
1175811773
11759- if (obj == Py_None )
11774+ if (obj == Py_None ) {
11775+ Py_DECREF (obj );
1176011776 obj = NULL ;
11777+ }
1176111778 if (obj != NULL ) {
1176211779 obj_type = supercheck (type , obj );
11763- if (obj_type == NULL )
11780+ if (obj_type == NULL ) {
11781+ Py_DECREF (obj );
1176411782 return -1 ;
11765- Py_INCREF ( obj );
11783+ }
1176611784 }
11767- Py_XSETREF (su -> type , (PyTypeObject * )Py_NewRef ( type ) );
11785+ Py_XSETREF (su -> type , (PyTypeObject * )type );
1176811786 Py_XSETREF (su -> obj , obj );
1176911787 Py_XSETREF (su -> obj_type , obj_type );
1177011788 return 0 ;
0 commit comments