[Python-checkins] cpython: Issue #29360: _PyStack_AsDict() doesn't check kwnames

victor.stinner python-checkins at python.org
Tue Jan 24 09:09:12 EST 2017


https://hg.python.org/cpython/rev/2d2210b36b25
changeset:   106298:2d2210b36b25
user:        Victor Stinner <victor.stinner at gmail.com>
date:        Tue Jan 24 15:05:30 2017 +0100
summary:
  Issue #29360: _PyStack_AsDict() doesn't check kwnames

Remove two assertions which can fail on legit code. Keyword arguments are
checked later with better tests and raise a regular (TypeError) exception.

files:
  Include/abstract.h |  15 +++++++++------
  Objects/abstract.c |   3 +--
  2 files changed, 10 insertions(+), 8 deletions(-)


diff --git a/Include/abstract.h b/Include/abstract.h
--- a/Include/abstract.h
+++ b/Include/abstract.h
@@ -166,13 +166,16 @@
     Py_ssize_t start,
     Py_ssize_t end);
 
-/* Convert keyword arguments from the (stack, kwnames) format to a Python
-   dictionary.
+/* Convert keyword arguments from the FASTCALL (stack: C array, kwnames: tuple)
+   format to a Python dictionary ("kwargs" dict).
 
-   kwnames must only contains str strings, no subclass, and all keys must be
-   unique. kwnames is not checked, usually these checks are done before or
-   later calling _PyStack_AsDict(). For example, _PyArg_ParseStackAndKeywords() raises an
-   error if a key is not a string. */
+   The type of kwnames keys is not checked. The final function getting
+   arguments is reponsible to check if all keys are strings, for example using
+   PyArg_ParseTupleAndKeywords() or PyArg_ValidateKeywordArguments().
+
+   Duplicate keys are merged using the last value. If duplicate keys must raise
+   an exception, the caller is responsible to implement an explicit keys on
+   kwnames. */
 PyAPI_FUNC(PyObject *) _PyStack_AsDict(
     PyObject **values,
     PyObject *kwnames);
diff --git a/Objects/abstract.c b/Objects/abstract.c
--- a/Objects/abstract.c
+++ b/Objects/abstract.c
@@ -2413,8 +2413,7 @@
     for (i = 0; i < nkwargs; i++) {
         PyObject *key = PyTuple_GET_ITEM(kwnames, i);
         PyObject *value = *values++;
-        assert(PyUnicode_CheckExact(key));
-        assert(PyDict_GetItem(kwdict, key) == NULL);
+        /* If key already exists, replace it with the new value */
         if (PyDict_SetItem(kwdict, key, value)) {
             Py_DECREF(kwdict);
             return NULL;

-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list