|
3 | 3 |
|
4 | 4 | #include "Python.h"
|
5 | 5 | #include "pycore_ceval.h" // _PyEval_BuiltinsFromGlobals()
|
| 6 | +#include "pycore_dict.h" // _Py_INCREF_DICT() |
6 | 7 | #include "pycore_long.h" // _PyLong_GetOne()
|
7 | 8 | #include "pycore_modsupport.h" // _PyArg_NoKeywords()
|
8 | 9 | #include "pycore_object.h" // _PyObject_GC_UNTRACK()
|
@@ -112,8 +113,15 @@ _PyFunction_FromConstructor(PyFrameConstructor *constr)
|
112 | 113 | Py_XDECREF(module);
|
113 | 114 | return NULL;
|
114 | 115 | }
|
115 |
| - op->func_globals = Py_NewRef(constr->fc_globals); |
116 |
| - op->func_builtins = Py_NewRef(constr->fc_builtins); |
| 116 | + _Py_INCREF_DICT(constr->fc_globals); |
| 117 | + op->func_globals = constr->fc_globals; |
| 118 | + if (PyDict_Check(constr->fc_builtins)) { |
| 119 | + _Py_INCREF_DICT(constr->fc_builtins); |
| 120 | + } |
| 121 | + else { |
| 122 | + Py_INCREF(constr->fc_builtins); |
| 123 | + } |
| 124 | + op->func_builtins = constr->fc_builtins; |
117 | 125 | op->func_name = Py_NewRef(constr->fc_name);
|
118 | 126 | op->func_qualname = Py_NewRef(constr->fc_qualname);
|
119 | 127 | _Py_INCREF_CODE((PyCodeObject *)constr->fc_code);
|
@@ -143,7 +151,7 @@ PyFunction_NewWithQualName(PyObject *code, PyObject *globals, PyObject *qualname
|
143 | 151 | {
|
144 | 152 | assert(globals != NULL);
|
145 | 153 | assert(PyDict_Check(globals));
|
146 |
| - Py_INCREF(globals); |
| 154 | + _Py_INCREF_DICT(globals); |
147 | 155 |
|
148 | 156 | PyThreadState *tstate = _PyThreadState_GET();
|
149 | 157 |
|
@@ -184,7 +192,12 @@ PyFunction_NewWithQualName(PyObject *code, PyObject *globals, PyObject *qualname
|
184 | 192 | if (builtins == NULL) {
|
185 | 193 | goto error;
|
186 | 194 | }
|
187 |
| - Py_INCREF(builtins); |
| 195 | + if (PyDict_Check(builtins)) { |
| 196 | + _Py_INCREF_DICT(builtins); |
| 197 | + } |
| 198 | + else { |
| 199 | + Py_INCREF(builtins); |
| 200 | + } |
188 | 201 |
|
189 | 202 | PyFunctionObject *op = PyObject_GC_New(PyFunctionObject, &PyFunction_Type);
|
190 | 203 | if (op == NULL) {
|
@@ -1057,8 +1070,21 @@ func_clear(PyObject *self)
|
1057 | 1070 | {
|
1058 | 1071 | PyFunctionObject *op = _PyFunction_CAST(self);
|
1059 | 1072 | func_clear_version(_PyInterpreterState_GET(), op);
|
1060 |
| - Py_CLEAR(op->func_globals); |
1061 |
| - Py_CLEAR(op->func_builtins); |
| 1073 | + PyObject *globals = op->func_globals; |
| 1074 | + op->func_globals = NULL; |
| 1075 | + if (globals != NULL) { |
| 1076 | + _Py_DECREF_DICT(globals); |
| 1077 | + } |
| 1078 | + PyObject *builtins = op->func_builtins; |
| 1079 | + op->func_builtins = NULL; |
| 1080 | + if (builtins != NULL) { |
| 1081 | + if (PyDict_Check(builtins)) { |
| 1082 | + _Py_DECREF_DICT(builtins); |
| 1083 | + } |
| 1084 | + else { |
| 1085 | + Py_DECREF(builtins); |
| 1086 | + } |
| 1087 | + } |
1062 | 1088 | Py_CLEAR(op->func_module);
|
1063 | 1089 | Py_CLEAR(op->func_defaults);
|
1064 | 1090 | Py_CLEAR(op->func_kwdefaults);
|
|
0 commit comments