summaryrefslogtreecommitdiffstats
path: root/Objects/funcobject.c
diff options
context:
space:
mode:
authorMiss Skeleton (bot) <31488909+miss-islington@users.noreply.github.com>2020-10-29 10:24:12 (GMT)
committerGitHub <noreply@github.com>2020-10-29 10:24:12 (GMT)
commit9ede1b071bf5250ba5f2d04d7ba86a24c06d41a1 (patch)
tree34f41ff0031a5be113242c60154f1e8acba777b7 /Objects/funcobject.c
parentb1ce0440bfe87e092ca5e2e57875fb7fc1129137 (diff)
downloadcpython-9ede1b071bf5250ba5f2d04d7ba86a24c06d41a1.zip
cpython-9ede1b071bf5250ba5f2d04d7ba86a24c06d41a1.tar.gz
cpython-9ede1b071bf5250ba5f2d04d7ba86a24c06d41a1.tar.bz2
bpo-42143: Ensure PyFunction_NewWithQualName() can't fail after creating the func object (GH-22953)
func_dealloc() does not handle partially-created objects. Best not to give it any. (cherry picked from commit 350526105fa9b131d8b941ae753378b741dabb2f) Co-authored-by: Yonatan Goldschmidt <yon.goldschmidt@gmail.com>
Diffstat (limited to 'Objects/funcobject.c')
-rw-r--r--Objects/funcobject.c29
1 files changed, 16 insertions, 13 deletions
diff --git a/Objects/funcobject.c b/Objects/funcobject.c
index df5cc2d..0fc4e12 100644
--- a/Objects/funcobject.c
+++ b/Objects/funcobject.c
@@ -22,9 +22,23 @@ PyFunction_NewWithQualName(PyObject *code, PyObject *globals, PyObject *qualname
return NULL;
}
+ /* __module__: If module name is in globals, use it.
+ Otherwise, use None. */
+ module = PyDict_GetItemWithError(globals, __name__);
+ if (module) {
+ Py_INCREF(module);
+ }
+ else if (PyErr_Occurred()) {
+ return NULL;
+ }
+
op = PyObject_GC_New(PyFunctionObject, &PyFunction_Type);
- if (op == NULL)
+ if (op == NULL) {
+ Py_XDECREF(module);
return NULL;
+ }
+ /* Note: No failures from this point on, since func_dealloc() does not
+ expect a partially-created object. */
op->func_weakreflist = NULL;
Py_INCREF(code);
@@ -37,6 +51,7 @@ PyFunction_NewWithQualName(PyObject *code, PyObject *globals, PyObject *qualname
op->func_kwdefaults = NULL; /* No keyword only defaults */
op->func_closure = NULL;
op->vectorcall = _PyFunction_Vectorcall;
+ op->func_module = module;
consts = ((PyCodeObject *)code)->co_consts;
if (PyTuple_Size(consts) >= 1) {
@@ -50,20 +65,8 @@ PyFunction_NewWithQualName(PyObject *code, PyObject *globals, PyObject *qualname
op->func_doc = doc;
op->func_dict = NULL;
- op->func_module = NULL;
op->func_annotations = NULL;
- /* __module__: If module name is in globals, use it.
- Otherwise, use None. */
- module = PyDict_GetItemWithError(globals, __name__);
- if (module) {
- Py_INCREF(module);
- op->func_module = module;
- }
- else if (PyErr_Occurred()) {
- Py_DECREF(op);
- return NULL;
- }
if (qualname)
op->func_qualname = qualname;
else