summaryrefslogtreecommitdiffstats
path: root/Objects
diff options
context:
space:
mode:
authorVictor Stinner <vstinner@python.org>2023-08-23 21:57:40 (GMT)
committerGitHub <noreply@github.com>2023-08-23 21:57:40 (GMT)
commit4890f65ecf119a18df926d42982c8c6d02f0b9fd (patch)
tree4ecc13c619feaae8dda67c5315bfb9c59b34c272 /Objects
parent592bacb6fc0833336c0453e818e9b95016e9fd47 (diff)
downloadcpython-4890f65ecf119a18df926d42982c8c6d02f0b9fd.zip
cpython-4890f65ecf119a18df926d42982c8c6d02f0b9fd.tar.gz
cpython-4890f65ecf119a18df926d42982c8c6d02f0b9fd.tar.bz2
gh-108308: Use PyDict_GetItemRef() in moduleobject.c (#108381)
Replace PyDict_GetItemWithError() with PyDict_GetItemRef() which returns a strong reference. Cleanup also the function: move variable definition to their first assignation, rename variable names to use name longer than 1 character.
Diffstat (limited to 'Objects')
-rw-r--r--Objects/moduleobject.c68
1 files changed, 40 insertions, 28 deletions
diff --git a/Objects/moduleobject.c b/Objects/moduleobject.c
index 7e890d0..5b9f74d 100644
--- a/Objects/moduleobject.c
+++ b/Objects/moduleobject.c
@@ -510,25 +510,31 @@ PyModule_GetDict(PyObject *m)
}
PyObject*
-PyModule_GetNameObject(PyObject *m)
+PyModule_GetNameObject(PyObject *mod)
{
- PyObject *d;
- PyObject *name;
- if (!PyModule_Check(m)) {
+ if (!PyModule_Check(mod)) {
PyErr_BadArgument();
return NULL;
}
- d = ((PyModuleObject *)m)->md_dict;
- if (d == NULL || !PyDict_Check(d) ||
- (name = PyDict_GetItemWithError(d, &_Py_ID(__name__))) == NULL ||
- !PyUnicode_Check(name))
- {
- if (!PyErr_Occurred()) {
- PyErr_SetString(PyExc_SystemError, "nameless module");
- }
- return NULL;
+ PyObject *dict = ((PyModuleObject *)mod)->md_dict; // borrowed reference
+ if (dict == NULL || !PyDict_Check(dict)) {
+ goto error;
+ }
+ PyObject *name;
+ if (PyDict_GetItemRef(dict, &_Py_ID(__name__), &name) <= 0) {
+ goto error;
+ }
+ if (!PyUnicode_Check(name)) {
+ Py_DECREF(name);
+ goto error;
+ }
+ return name;
+
+error:
+ if (!PyErr_Occurred()) {
+ PyErr_SetString(PyExc_SystemError, "nameless module");
}
- return Py_NewRef(name);
+ return NULL;
}
const char *
@@ -544,25 +550,31 @@ PyModule_GetName(PyObject *m)
}
PyObject*
-PyModule_GetFilenameObject(PyObject *m)
+PyModule_GetFilenameObject(PyObject *mod)
{
- PyObject *d;
- PyObject *fileobj;
- if (!PyModule_Check(m)) {
+ if (!PyModule_Check(mod)) {
PyErr_BadArgument();
return NULL;
}
- d = ((PyModuleObject *)m)->md_dict;
- if (d == NULL ||
- (fileobj = PyDict_GetItemWithError(d, &_Py_ID(__file__))) == NULL ||
- !PyUnicode_Check(fileobj))
- {
- if (!PyErr_Occurred()) {
- PyErr_SetString(PyExc_SystemError, "module filename missing");
- }
- return NULL;
+ PyObject *dict = ((PyModuleObject *)mod)->md_dict; // borrowed reference
+ if (dict == NULL) {
+ goto error;
}
- return Py_NewRef(fileobj);
+ PyObject *fileobj;
+ if (PyDict_GetItemRef(dict, &_Py_ID(__file__), &fileobj) <= 0) {
+ goto error;
+ }
+ if (!PyUnicode_Check(fileobj)) {
+ Py_DECREF(fileobj);
+ goto error;
+ }
+ return fileobj;
+
+error:
+ if (!PyErr_Occurred()) {
+ PyErr_SetString(PyExc_SystemError, "module filename missing");
+ }
+ return NULL;
}
const char *