summaryrefslogtreecommitdiffstats
path: root/Python
diff options
context:
space:
mode:
authorVictor Stinner <vstinner@python.org>2023-08-24 15:34:22 (GMT)
committerGitHub <noreply@github.com>2023-08-24 15:34:22 (GMT)
commit52c6a6e48a5fa12af401810722cfcad859e9881a (patch)
tree610a56978210c223db5ba6d5bfbe0b68ccdd4509 /Python
parentea871c9b0f08399e440baed95a3e5793d6e0ea66 (diff)
downloadcpython-52c6a6e48a5fa12af401810722cfcad859e9881a.zip
cpython-52c6a6e48a5fa12af401810722cfcad859e9881a.tar.gz
cpython-52c6a6e48a5fa12af401810722cfcad859e9881a.tar.bz2
gh-108308: Remove _PyDict_GetItemStringWithError() function (#108426)
Remove the internal _PyDict_GetItemStringWithError() function. It can now be replaced with the new public PyDict_ContainsString() and PyDict_GetItemStringRef() functions. getargs.c now now uses a strong reference for current_arg. find_keyword() returns a strong reference.
Diffstat (limited to 'Python')
-rw-r--r--Python/getargs.c58
-rw-r--r--Python/pythonrun.c1
-rw-r--r--Python/sysmodule.c7
3 files changed, 36 insertions, 30 deletions
diff --git a/Python/getargs.c b/Python/getargs.c
index 916e465..809b2d4 100644
--- a/Python/getargs.c
+++ b/Python/getargs.c
@@ -1491,7 +1491,6 @@ vgetargskeywords(PyObject *args, PyObject *kwargs, const char *format,
int i, pos, len;
int skip = 0;
Py_ssize_t nargs, nkwargs;
- PyObject *current_arg;
freelistentry_t static_entries[STATIC_FREELIST_ENTRIES];
freelist_t freelist;
@@ -1621,17 +1620,17 @@ vgetargskeywords(PyObject *args, PyObject *kwargs, const char *format,
return cleanreturn(0, &freelist);
}
if (!skip) {
+ PyObject *current_arg;
if (i < nargs) {
- current_arg = PyTuple_GET_ITEM(args, i);
+ current_arg = Py_NewRef(PyTuple_GET_ITEM(args, i));
}
else if (nkwargs && i >= pos) {
- current_arg = _PyDict_GetItemStringWithError(kwargs, kwlist[i]);
+ if (PyDict_GetItemStringRef(kwargs, kwlist[i], &current_arg) < 0) {
+ return cleanreturn(0, &freelist);
+ }
if (current_arg) {
--nkwargs;
}
- else if (PyErr_Occurred()) {
- return cleanreturn(0, &freelist);
- }
}
else {
current_arg = NULL;
@@ -1640,6 +1639,7 @@ vgetargskeywords(PyObject *args, PyObject *kwargs, const char *format,
if (current_arg) {
msg = convertitem(current_arg, &format, p_va, flags,
levels, msgbuf, sizeof(msgbuf), &freelist);
+ Py_DECREF(current_arg);
if (msg) {
seterror(i+1, msg, levels, fname, custom_msg);
return cleanreturn(0, &freelist);
@@ -1710,8 +1710,12 @@ vgetargskeywords(PyObject *args, PyObject *kwargs, const char *format,
Py_ssize_t j;
/* make sure there are no arguments given by name and position */
for (i = pos; i < nargs; i++) {
- current_arg = _PyDict_GetItemStringWithError(kwargs, kwlist[i]);
+ PyObject *current_arg;
+ if (PyDict_GetItemStringRef(kwargs, kwlist[i], &current_arg) < 0) {
+ return cleanreturn(0, &freelist);
+ }
if (current_arg) {
+ Py_DECREF(current_arg);
/* arg present in tuple and in dict */
PyErr_Format(PyExc_TypeError,
"argument for %.200s%s given by name ('%s') "
@@ -1721,9 +1725,6 @@ vgetargskeywords(PyObject *args, PyObject *kwargs, const char *format,
kwlist[i], i+1);
return cleanreturn(0, &freelist);
}
- else if (PyErr_Occurred()) {
- return cleanreturn(0, &freelist);
- }
}
/* make sure there are no extraneous keyword arguments */
j = 0;
@@ -1985,7 +1986,7 @@ find_keyword(PyObject *kwnames, PyObject *const *kwstack, PyObject *key)
/* kwname == key will normally find a match in since keyword keys
should be interned strings; if not retry below in a new loop. */
if (kwname == key) {
- return kwstack[i];
+ return Py_NewRef(kwstack[i]);
}
}
@@ -1993,7 +1994,7 @@ find_keyword(PyObject *kwnames, PyObject *const *kwstack, PyObject *key)
PyObject *kwname = PyTuple_GET_ITEM(kwnames, i);
assert(PyUnicode_Check(kwname));
if (_PyUnicode_EQ(kwname, key)) {
- return kwstack[i];
+ return Py_NewRef(kwstack[i]);
}
}
return NULL;
@@ -2013,7 +2014,6 @@ vgetargskeywordsfast_impl(PyObject *const *args, Py_ssize_t nargs,
PyObject *keyword;
int i, pos, len;
Py_ssize_t nkwargs;
- PyObject *current_arg;
freelistentry_t static_entries[STATIC_FREELIST_ENTRIES];
freelist_t freelist;
PyObject *const *kwstack = NULL;
@@ -2108,14 +2108,14 @@ vgetargskeywordsfast_impl(PyObject *const *args, Py_ssize_t nargs,
}
assert(!IS_END_OF_FORMAT(*format));
+ PyObject *current_arg;
if (i < nargs) {
- current_arg = args[i];
+ current_arg = Py_NewRef(args[i]);
}
else if (nkwargs && i >= pos) {
keyword = PyTuple_GET_ITEM(kwtuple, i - pos);
if (kwargs != NULL) {
- current_arg = PyDict_GetItemWithError(kwargs, keyword);
- if (!current_arg && PyErr_Occurred()) {
+ if (PyDict_GetItemRef(kwargs, keyword, &current_arg) < 0) {
return cleanreturn(0, &freelist);
}
}
@@ -2133,6 +2133,7 @@ vgetargskeywordsfast_impl(PyObject *const *args, Py_ssize_t nargs,
if (current_arg) {
msg = convertitem(current_arg, &format, p_va, flags,
levels, msgbuf, sizeof(msgbuf), &freelist);
+ Py_DECREF(current_arg);
if (msg) {
seterror(i+1, msg, levels, parser->fname, parser->custom_msg);
return cleanreturn(0, &freelist);
@@ -2183,10 +2184,10 @@ vgetargskeywordsfast_impl(PyObject *const *args, Py_ssize_t nargs,
if (nkwargs > 0) {
/* make sure there are no arguments given by name and position */
for (i = pos; i < nargs; i++) {
+ PyObject *current_arg;
keyword = PyTuple_GET_ITEM(kwtuple, i - pos);
if (kwargs != NULL) {
- current_arg = PyDict_GetItemWithError(kwargs, keyword);
- if (!current_arg && PyErr_Occurred()) {
+ if (PyDict_GetItemRef(kwargs, keyword, &current_arg) < 0) {
return cleanreturn(0, &freelist);
}
}
@@ -2194,6 +2195,7 @@ vgetargskeywordsfast_impl(PyObject *const *args, Py_ssize_t nargs,
current_arg = find_keyword(kwnames, kwstack, keyword);
}
if (current_arg) {
+ Py_DECREF(current_arg);
/* arg present in tuple and in dict */
PyErr_Format(PyExc_TypeError,
"argument for %.200s%s given by name ('%U') "
@@ -2248,7 +2250,6 @@ _PyArg_UnpackKeywords(PyObject *const *args, Py_ssize_t nargs,
int i, posonly, minposonly, maxargs;
int reqlimit = minkw ? maxpos + minkw : minpos;
Py_ssize_t nkwargs;
- PyObject *current_arg;
PyObject * const *kwstack = NULL;
assert(kwargs == NULL || PyDict_Check(kwargs));
@@ -2343,11 +2344,11 @@ _PyArg_UnpackKeywords(PyObject *const *args, Py_ssize_t nargs,
/* copy keyword args using kwtuple to drive process */
for (i = Py_MAX((int)nargs, posonly); i < maxargs; i++) {
+ PyObject *current_arg;
if (nkwargs) {
keyword = PyTuple_GET_ITEM(kwtuple, i - posonly);
if (kwargs != NULL) {
- current_arg = PyDict_GetItemWithError(kwargs, keyword);
- if (!current_arg && PyErr_Occurred()) {
+ if (PyDict_GetItemRef(kwargs, keyword, &current_arg) < 0) {
return NULL;
}
}
@@ -2365,6 +2366,7 @@ _PyArg_UnpackKeywords(PyObject *const *args, Py_ssize_t nargs,
buf[i] = current_arg;
if (current_arg) {
+ Py_DECREF(current_arg);
--nkwargs;
}
else if (i < minpos || (maxpos <= i && i < reqlimit)) {
@@ -2382,10 +2384,10 @@ _PyArg_UnpackKeywords(PyObject *const *args, Py_ssize_t nargs,
if (nkwargs > 0) {
/* make sure there are no arguments given by name and position */
for (i = posonly; i < nargs; i++) {
+ PyObject *current_arg;
keyword = PyTuple_GET_ITEM(kwtuple, i - posonly);
if (kwargs != NULL) {
- current_arg = PyDict_GetItemWithError(kwargs, keyword);
- if (!current_arg && PyErr_Occurred()) {
+ if (PyDict_GetItemRef(kwargs, keyword, &current_arg) < 0) {
return NULL;
}
}
@@ -2393,6 +2395,7 @@ _PyArg_UnpackKeywords(PyObject *const *args, Py_ssize_t nargs,
current_arg = find_keyword(kwnames, kwstack, keyword);
}
if (current_arg) {
+ Py_DECREF(current_arg);
/* arg present in tuple and in dict */
PyErr_Format(PyExc_TypeError,
"argument for %.200s%s given by name ('%U') "
@@ -2424,7 +2427,6 @@ _PyArg_UnpackKeywordsWithVararg(PyObject *const *args, Py_ssize_t nargs,
int i, posonly, minposonly, maxargs;
int reqlimit = minkw ? maxpos + minkw : minpos;
Py_ssize_t nkwargs;
- PyObject *current_arg;
PyObject * const *kwstack = NULL;
assert(kwargs == NULL || PyDict_Check(kwargs));
@@ -2497,13 +2499,12 @@ _PyArg_UnpackKeywordsWithVararg(PyObject *const *args, Py_ssize_t nargs,
}
/* copy keyword args using kwtuple to drive process */
- for (i = Py_MAX((int)nargs, posonly) -
- Py_SAFE_DOWNCAST(varargssize, Py_ssize_t, int); i < maxargs; i++) {
+ for (i = Py_MAX((int)nargs, posonly) - Py_SAFE_DOWNCAST(varargssize, Py_ssize_t, int); i < maxargs; i++) {
+ PyObject *current_arg;
if (nkwargs) {
keyword = PyTuple_GET_ITEM(kwtuple, i - posonly);
if (kwargs != NULL) {
- current_arg = PyDict_GetItemWithError(kwargs, keyword);
- if (!current_arg && PyErr_Occurred()) {
+ if (PyDict_GetItemRef(kwargs, keyword, &current_arg) < 0) {
goto exit;
}
}
@@ -2536,6 +2537,7 @@ _PyArg_UnpackKeywordsWithVararg(PyObject *const *args, Py_ssize_t nargs,
}
if (current_arg) {
+ Py_DECREF(current_arg);
--nkwargs;
}
else if (i < minpos || (maxpos <= i && i < reqlimit)) {
diff --git a/Python/pythonrun.c b/Python/pythonrun.c
index 0e118b0..a2b50c0 100644
--- a/Python/pythonrun.c
+++ b/Python/pythonrun.c
@@ -15,7 +15,6 @@
#include "pycore_ast.h" // PyAST_mod2obj
#include "pycore_ceval.h" // _Py_EnterRecursiveCall
#include "pycore_compile.h" // _PyAST_Compile()
-#include "pycore_dict.h" // _PyDict_GetItemStringWithError()
#include "pycore_interp.h" // PyInterpreterState.importlib
#include "pycore_object.h" // _PyDebug_PrintTotalRefs()
#include "pycore_parser.h" // _PyParser_ASTFromString()
diff --git a/Python/sysmodule.c b/Python/sysmodule.c
index 5453360..94d0f01 100644
--- a/Python/sysmodule.c
+++ b/Python/sysmodule.c
@@ -87,7 +87,12 @@ _PySys_GetObject(PyInterpreterState *interp, const char *name)
if (sysdict == NULL) {
return NULL;
}
- return _PyDict_GetItemStringWithError(sysdict, name);
+ PyObject *value;
+ if (PyDict_GetItemStringRef(sysdict, name, &value) != 1) {
+ return NULL;
+ }
+ Py_DECREF(value); // return a borrowed reference
+ return value;
}
PyObject *