Skip to content

gh-108308: Remove _PyDict_GetItemStringWithError() function #108426

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion Include/internal/pycore_dict.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ extern "C" {

// Unsafe flavor of PyDict_GetItemWithError(): no error checking
extern PyObject* _PyDict_GetItemWithError(PyObject *dp, PyObject *key);
extern PyObject* _PyDict_GetItemStringWithError(PyObject *, const char *);

extern int _PyDict_Contains_KnownHash(PyObject *, PyObject *, Py_hash_t);

Expand Down
13 changes: 0 additions & 13 deletions Objects/dictobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -1829,19 +1829,6 @@ _PyDict_GetItemIdWithError(PyObject *dp, _Py_Identifier *key)
return _PyDict_GetItem_KnownHash(dp, kv, hash); // borrowed reference
}

PyObject *
_PyDict_GetItemStringWithError(PyObject *v, const char *key)
{
PyObject *kv, *rv;
kv = PyUnicode_FromString(key);
if (kv == NULL) {
return NULL;
}
rv = PyDict_GetItemWithError(v, kv);
Py_DECREF(kv);
return rv; // borrowed reference
}

/* Fast version of global value lookup (LOAD_GLOBAL).
* Lookup in globals, then builtins.
*
Expand Down
58 changes: 30 additions & 28 deletions Python/getargs.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -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') "
Expand All @@ -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;
Expand Down Expand Up @@ -1985,15 +1986,15 @@ 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]);
}
}

for (i = 0; i < nkwargs; i++) {
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;
Expand All @@ -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;
Expand Down Expand Up @@ -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);
}
}
Expand All @@ -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);
Expand Down Expand Up @@ -2183,17 +2184,18 @@ 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);
}
}
else {
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') "
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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;
}
}
Expand All @@ -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)) {
Expand All @@ -2382,17 +2384,18 @@ _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;
}
}
else {
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') "
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -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)) {
Expand Down
1 change: 0 additions & 1 deletion Python/pythonrun.c
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
7 changes: 6 additions & 1 deletion Python/sysmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 *
Expand Down