[Python-checkins] cpython: Factor-out the common code for setting a KeyError.

raymond.hettinger python-checkins at python.org
Tue Sep 3 00:59:34 CEST 2013


http://hg.python.org/cpython/rev/bfaf3a2907bd
changeset:   85502:bfaf3a2907bd
user:        Raymond Hettinger <python at rcn.com>
date:        Mon Sep 02 15:59:26 2013 -0700
summary:
  Factor-out the common code for setting a KeyError.

files:
  Include/pyerrors.h   |   1 +
  Objects/dictobject.c |  22 ++++------------------
  Objects/setobject.c  |  16 +---------------
  Python/errors.c      |  14 ++++++++++++++
  4 files changed, 20 insertions(+), 33 deletions(-)


diff --git a/Include/pyerrors.h b/Include/pyerrors.h
--- a/Include/pyerrors.h
+++ b/Include/pyerrors.h
@@ -75,6 +75,7 @@
 
 PyAPI_FUNC(void) PyErr_SetNone(PyObject *);
 PyAPI_FUNC(void) PyErr_SetObject(PyObject *, PyObject *);
+PyAPI_FUNC(void) _PyErr_SetKeyError(PyObject *);
 PyAPI_FUNC(void) PyErr_SetString(
     PyObject *exception,
     const char *string   /* decoded from utf-8 */
diff --git a/Objects/dictobject.c b/Objects/dictobject.c
--- a/Objects/dictobject.c
+++ b/Objects/dictobject.c
@@ -95,20 +95,6 @@
 it's USABLE_FRACTION (currently two-thirds) full.
 */
 
-/* Set a key error with the specified argument, wrapping it in a
- * tuple automatically so that tuple keys are not unpacked as the
- * exception arguments. */
-static void
-set_key_error(PyObject *arg)
-{
-    PyObject *tup;
-    tup = PyTuple_Pack(1, arg);
-    if (!tup)
-        return; /* caller will expect error to be set anyway */
-    PyErr_SetObject(PyExc_KeyError, tup);
-    Py_DECREF(tup);
-}
-
 #define PERTURB_SHIFT 5
 
 /*
@@ -1241,7 +1227,7 @@
     if (ep == NULL)
         return -1;
     if (*value_addr == NULL) {
-        set_key_error(key);
+        _PyErr_SetKeyError(key);
         return -1;
     }
     old_value = *value_addr;
@@ -1530,7 +1516,7 @@
             else if (PyErr_Occurred())
                 return NULL;
         }
-        set_key_error(key);
+        _PyErr_SetKeyError(key);
         return NULL;
     }
     else
@@ -2302,7 +2288,7 @@
             Py_INCREF(deflt);
             return deflt;
         }
-        set_key_error(key);
+        _PyErr_SetKeyError(key);
         return NULL;
     }
     if (!PyUnicode_CheckExact(key) ||
@@ -2320,7 +2306,7 @@
             Py_INCREF(deflt);
             return deflt;
         }
-        set_key_error(key);
+        _PyErr_SetKeyError(key);
         return NULL;
     }
     *value_addr = NULL;
diff --git a/Objects/setobject.c b/Objects/setobject.c
--- a/Objects/setobject.c
+++ b/Objects/setobject.c
@@ -11,20 +11,6 @@
 #include "structmember.h"
 #include "stringlib/eq.h"
 
-/* Set a key error with the specified argument, wrapping it in a
- * tuple automatically so that tuple keys are not unpacked as the
- * exception arguments. */
-static void
-set_key_error(PyObject *arg)
-{
-    PyObject *tup;
-    tup = PyTuple_Pack(1, arg);
-    if (!tup)
-        return; /* caller will expect error to be set anyway */
-    PyErr_SetObject(PyExc_KeyError, tup);
-    Py_DECREF(tup);
-}
-
 /* This must be >= 1. */
 #define PERTURB_SHIFT 5
 #define LINEAR_PROBES 9
@@ -1948,7 +1934,7 @@
     }
 
     if (rv == DISCARD_NOTFOUND) {
-        set_key_error(key);
+        _PyErr_SetKeyError(key);
         return NULL;
     }
     Py_RETURN_NONE;
diff --git a/Python/errors.c b/Python/errors.c
--- a/Python/errors.c
+++ b/Python/errors.c
@@ -117,6 +117,20 @@
     PyErr_Restore(exception, value, tb);
 }
 
+/* Set a key error with the specified argument, wrapping it in a
+ * tuple automatically so that tuple keys are not unpacked as the
+ * exception arguments. */
+void
+_PyErr_SetKeyError(PyObject *arg)
+{
+    PyObject *tup;
+    tup = PyTuple_Pack(1, arg);
+    if (!tup)
+        return; /* caller will expect error to be set anyway */
+    PyErr_SetObject(PyExc_KeyError, tup);
+    Py_DECREF(tup);
+}
+
 void
 PyErr_SetNone(PyObject *exception)
 {

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


More information about the Python-checkins mailing list