Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b1e169b

Browse files
committed
ssue #27213: Reintroduce checks in _PyStack_AsDict()
1 parent 410b988 commit b1e169b

3 files changed

Lines changed: 23 additions & 9 deletions

File tree

Include/abstract.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,9 @@ xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx*/
275275

276276
PyAPI_FUNC(PyObject *) _PyStack_AsDict(
277277
PyObject **values,
278-
PyObject *kwnames);
278+
Py_ssize_t nkwargs,
279+
PyObject *kwnames,
280+
PyObject *func);
279281

280282
/* Convert (args, nargs, kwargs) into a (stack, nargs, kwnames).
281283

Objects/abstract.c

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2367,9 +2367,9 @@ _PyObject_Call_Prepend(PyObject *func,
23672367
}
23682368

23692369
PyObject *
2370-
_PyStack_AsDict(PyObject **values, PyObject *kwnames)
2370+
_PyStack_AsDict(PyObject **values, Py_ssize_t nkwargs, PyObject *kwnames,
2371+
PyObject *func)
23712372
{
2372-
Py_ssize_t nkwargs = PyTuple_GET_SIZE(kwnames);
23732373
PyObject *kwdict;
23742374
Py_ssize_t i;
23752375

@@ -2378,12 +2378,24 @@ _PyStack_AsDict(PyObject **values, PyObject *kwnames)
23782378
return NULL;
23792379
}
23802380

2381-
for (i = 0; i < nkwargs; i++) {
2381+
for (i=0; i < nkwargs; i++) {
2382+
int err;
23822383
PyObject *key = PyTuple_GET_ITEM(kwnames, i);
23832384
PyObject *value = *values++;
2384-
assert(PyUnicode_CheckExact(key));
2385-
assert(PyDict_GetItem(kwdict, key) == NULL);
2386-
if (PyDict_SetItem(kwdict, key, value)) {
2385+
2386+
if (PyDict_GetItem(kwdict, key) != NULL) {
2387+
PyErr_Format(PyExc_TypeError,
2388+
"%.200s%s got multiple values "
2389+
"for keyword argument '%U'",
2390+
PyEval_GetFuncName(func),
2391+
PyEval_GetFuncDesc(func),
2392+
key);
2393+
Py_DECREF(kwdict);
2394+
return NULL;
2395+
}
2396+
2397+
err = PyDict_SetItem(kwdict, key, value);
2398+
if (err) {
23872399
Py_DECREF(kwdict);
23882400
return NULL;
23892401
}
@@ -2467,7 +2479,7 @@ _PyObject_FastCallKeywords(PyObject *func, PyObject **stack, Py_ssize_t nargs,
24672479
}
24682480

24692481
if (nkwargs > 0) {
2470-
kwdict = _PyStack_AsDict(stack + nargs, kwnames);
2482+
kwdict = _PyStack_AsDict(stack + nargs, nkwargs, kwnames, func);
24712483
if (kwdict == NULL) {
24722484
return NULL;
24732485
}

Objects/methodobject.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ _PyCFunction_FastCallKeywords(PyObject *func, PyObject **stack,
279279

280280
nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames);
281281
if (nkwargs > 0) {
282-
kwdict = _PyStack_AsDict(stack + nargs, kwnames);
282+
kwdict = _PyStack_AsDict(stack + nargs, nkwargs, kwnames, func);
283283
if (kwdict == NULL) {
284284
return NULL;
285285
}

0 commit comments

Comments
 (0)