@@ -145,6 +145,7 @@ _PyFunction_FromConstructor(PyFrameConstructor *constr)
145145 op -> func_typeparams = NULL ;
146146 op -> vectorcall = _PyFunction_Vectorcall ;
147147 op -> func_version = FUNC_VERSION_UNSET ;
148+ op -> func_old_codes = NULL ;
148149 // NOTE: functions created via FrameConstructor do not use deferred
149150 // reference counting because they are typically not part of cycles
150151 // nor accessed by multiple threads.
@@ -223,6 +224,7 @@ PyFunction_NewWithQualName(PyObject *code, PyObject *globals, PyObject *qualname
223224 op -> func_typeparams = NULL ;
224225 op -> vectorcall = _PyFunction_Vectorcall ;
225226 op -> func_version = FUNC_VERSION_UNSET ;
227+ op -> func_old_codes = NULL ;
226228 if (((code_obj -> co_flags & CO_NESTED ) == 0 ) ||
227229 (code_obj -> co_flags & CO_METHOD )) {
228230 // Use deferred reference counting for top-level functions, but not
@@ -686,6 +688,19 @@ func_set_code(PyObject *self, PyObject *value, void *Py_UNUSED(ignored))
686688
687689 handle_func_event (PyFunction_EVENT_MODIFY_CODE , op , value );
688690 _PyFunction_ClearVersion (op );
691+ if (op -> func_old_codes == NULL ) {
692+ op -> func_old_codes = PyList_New (0 );
693+ if (op -> func_old_codes == NULL ) {
694+ PyErr_NoMemory ();
695+ return -1 ;
696+ }
697+ }
698+
699+ if (PyList_Append (op -> func_old_codes , op -> func_code ) < 0 ) {
700+ PyErr_NoMemory ();
701+ return -1 ;
702+ }
703+
689704 Py_XSETREF (op -> func_code , Py_NewRef (value ));
690705 return 0 ;
691706}
@@ -1114,6 +1129,7 @@ func_clear(PyObject *self)
11141129 Py_CLEAR (op -> func_annotations );
11151130 Py_CLEAR (op -> func_annotate );
11161131 Py_CLEAR (op -> func_typeparams );
1132+ Py_CLEAR (op -> func_old_codes );
11171133 // Don't Py_CLEAR(op->func_code), since code is always required
11181134 // to be non-NULL. Similarly, name and qualname shouldn't be NULL.
11191135 // However, name and qualname could be str subclasses, so they
@@ -1169,6 +1185,7 @@ func_traverse(PyObject *self, visitproc visit, void *arg)
11691185 Py_VISIT (f -> func_annotate );
11701186 Py_VISIT (f -> func_typeparams );
11711187 Py_VISIT (f -> func_qualname );
1188+ Py_VISIT (f -> func_old_codes );
11721189 return 0 ;
11731190}
11741191
0 commit comments