summaryrefslogtreecommitdiffstats
path: root/Python
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2023-07-18 06:42:05 (GMT)
committerGitHub <noreply@github.com>2023-07-18 06:42:05 (GMT)
commit83ac1284909433f3f77c0a4f459996b1ba3f1a4d (patch)
tree82d86e1e07ab40873f6487625e10077baba76a15 /Python
parent745492355b94d109e47827e5865846f25ae42d26 (diff)
downloadcpython-83ac1284909433f3f77c0a4f459996b1ba3f1a4d.zip
cpython-83ac1284909433f3f77c0a4f459996b1ba3f1a4d.tar.gz
cpython-83ac1284909433f3f77c0a4f459996b1ba3f1a4d.tar.bz2
bpo-42327: C API: Add PyModule_Add() function (GH-23443)
It is a fixed implementation of PyModule_AddObject() which consistently steals reference both on success and on failure.
Diffstat (limited to 'Python')
-rw-r--r--Python/modsupport.c29
1 files changed, 10 insertions, 19 deletions
diff --git a/Python/modsupport.c b/Python/modsupport.c
index 3db95f1..18b3322 100644
--- a/Python/modsupport.c
+++ b/Python/modsupport.c
@@ -606,13 +606,16 @@ PyModule_AddObjectRef(PyObject *mod, const char *name, PyObject *value)
PyModule_GetName(mod));
return -1;
}
-
- if (PyDict_SetItemString(dict, name, value)) {
- return -1;
- }
- return 0;
+ return PyDict_SetItemString(dict, name, value);
}
+int
+PyModule_Add(PyObject *mod, const char *name, PyObject *value)
+{
+ int res = PyModule_AddObjectRef(mod, name, value);
+ Py_XDECREF(value);
+ return res;
+}
int
PyModule_AddObject(PyObject *mod, const char *name, PyObject *value)
@@ -627,25 +630,13 @@ PyModule_AddObject(PyObject *mod, const char *name, PyObject *value)
int
PyModule_AddIntConstant(PyObject *m, const char *name, long value)
{
- PyObject *obj = PyLong_FromLong(value);
- if (!obj) {
- return -1;
- }
- int res = PyModule_AddObjectRef(m, name, obj);
- Py_DECREF(obj);
- return res;
+ return PyModule_Add(m, name, PyLong_FromLong(value));
}
int
PyModule_AddStringConstant(PyObject *m, const char *name, const char *value)
{
- PyObject *obj = PyUnicode_FromString(value);
- if (!obj) {
- return -1;
- }
- int res = PyModule_AddObjectRef(m, name, obj);
- Py_DECREF(obj);
- return res;
+ return PyModule_Add(m, name, PyUnicode_FromString(value));
}
int