[Python-checkins] cpython: ssue #27213: Reintroduce checks in _PyStack_AsDict()

victor.stinner python-checkins at python.org
Mon Sep 12 07:06:46 EDT 2016


https://hg.python.org/cpython/rev/f860b7a775c5
changeset:   103703:f860b7a775c5
user:        Victor Stinner <victor.stinner at gmail.com>
date:        Mon Sep 12 12:55:28 2016 +0200
summary:
  ssue #27213: Reintroduce checks in _PyStack_AsDict()

files:
  Include/abstract.h     |   4 +++-
  Objects/abstract.c     |  26 +++++++++++++++++++-------
  Objects/methodobject.c |   2 +-
  3 files changed, 23 insertions(+), 9 deletions(-)


diff --git a/Include/abstract.h b/Include/abstract.h
--- a/Include/abstract.h
+++ b/Include/abstract.h
@@ -275,7 +275,9 @@
 
     PyAPI_FUNC(PyObject *) _PyStack_AsDict(
         PyObject **values,
-        PyObject *kwnames);
+        Py_ssize_t nkwargs,
+        PyObject *kwnames,
+        PyObject *func);
 
     /* Convert (args, nargs, kwargs) into a (stack, nargs, kwnames).
 
diff --git a/Objects/abstract.c b/Objects/abstract.c
--- a/Objects/abstract.c
+++ b/Objects/abstract.c
@@ -2367,9 +2367,9 @@
 }
 
 PyObject *
-_PyStack_AsDict(PyObject **values, PyObject *kwnames)
+_PyStack_AsDict(PyObject **values, Py_ssize_t nkwargs, PyObject *kwnames,
+                PyObject *func)
 {
-    Py_ssize_t nkwargs = PyTuple_GET_SIZE(kwnames);
     PyObject *kwdict;
     Py_ssize_t i;
 
@@ -2378,12 +2378,24 @@
         return NULL;
     }
 
-    for (i = 0; i < nkwargs; i++) {
+    for (i=0; i < nkwargs; i++) {
+        int err;
         PyObject *key = PyTuple_GET_ITEM(kwnames, i);
         PyObject *value = *values++;
-        assert(PyUnicode_CheckExact(key));
-        assert(PyDict_GetItem(kwdict, key) == NULL);
-        if (PyDict_SetItem(kwdict, key, value)) {
+
+        if (PyDict_GetItem(kwdict, key) != NULL) {
+            PyErr_Format(PyExc_TypeError,
+                         "%.200s%s got multiple values "
+                         "for keyword argument '%U'",
+                         PyEval_GetFuncName(func),
+                         PyEval_GetFuncDesc(func),
+                         key);
+            Py_DECREF(kwdict);
+            return NULL;
+        }
+
+        err = PyDict_SetItem(kwdict, key, value);
+        if (err) {
             Py_DECREF(kwdict);
             return NULL;
         }
@@ -2467,7 +2479,7 @@
     }
 
     if (nkwargs > 0) {
-        kwdict = _PyStack_AsDict(stack + nargs, kwnames);
+        kwdict = _PyStack_AsDict(stack + nargs, nkwargs, kwnames, func);
         if (kwdict == NULL) {
             return NULL;
         }
diff --git a/Objects/methodobject.c b/Objects/methodobject.c
--- a/Objects/methodobject.c
+++ b/Objects/methodobject.c
@@ -279,7 +279,7 @@
 
     nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames);
     if (nkwargs > 0) {
-        kwdict = _PyStack_AsDict(stack + nargs, kwnames);
+        kwdict = _PyStack_AsDict(stack + nargs, nkwargs, kwnames, func);
         if (kwdict == NULL) {
             return NULL;
         }

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


More information about the Python-checkins mailing list