Skip to content

Commit 69492da

Browse files
committed
Factor-out the common code for setting a KeyError.
1 parent 7f5c22c commit 69492da

File tree

4 files changed

+20
-33
lines changed

4 files changed

+20
-33
lines changed

Include/pyerrors.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ typedef PyOSErrorObject PyWindowsErrorObject;
7575

7676
PyAPI_FUNC(void) PyErr_SetNone(PyObject *);
7777
PyAPI_FUNC(void) PyErr_SetObject(PyObject *, PyObject *);
78+
PyAPI_FUNC(void) _PyErr_SetKeyError(PyObject *);
7879
PyAPI_FUNC(void) PyErr_SetString(
7980
PyObject *exception,
8081
const char *string /* decoded from utf-8 */

Objects/dictobject.c

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -95,20 +95,6 @@ To avoid slowing down lookups on a near-full table, we resize the table when
9595
it's USABLE_FRACTION (currently two-thirds) full.
9696
*/
9797

98-
/* Set a key error with the specified argument, wrapping it in a
99-
* tuple automatically so that tuple keys are not unpacked as the
100-
* exception arguments. */
101-
static void
102-
set_key_error(PyObject *arg)
103-
{
104-
PyObject *tup;
105-
tup = PyTuple_Pack(1, arg);
106-
if (!tup)
107-
return; /* caller will expect error to be set anyway */
108-
PyErr_SetObject(PyExc_KeyError, tup);
109-
Py_DECREF(tup);
110-
}
111-
11298
#define PERTURB_SHIFT 5
11399

114100
/*
@@ -1241,7 +1227,7 @@ PyDict_DelItem(PyObject *op, PyObject *key)
12411227
if (ep == NULL)
12421228
return -1;
12431229
if (*value_addr == NULL) {
1244-
set_key_error(key);
1230+
_PyErr_SetKeyError(key);
12451231
return -1;
12461232
}
12471233
old_value = *value_addr;
@@ -1530,7 +1516,7 @@ dict_subscript(PyDictObject *mp, PyObject *key)
15301516
else if (PyErr_Occurred())
15311517
return NULL;
15321518
}
1533-
set_key_error(key);
1519+
_PyErr_SetKeyError(key);
15341520
return NULL;
15351521
}
15361522
else
@@ -2302,7 +2288,7 @@ dict_pop(PyDictObject *mp, PyObject *args)
23022288
Py_INCREF(deflt);
23032289
return deflt;
23042290
}
2305-
set_key_error(key);
2291+
_PyErr_SetKeyError(key);
23062292
return NULL;
23072293
}
23082294
if (!PyUnicode_CheckExact(key) ||
@@ -2320,7 +2306,7 @@ dict_pop(PyDictObject *mp, PyObject *args)
23202306
Py_INCREF(deflt);
23212307
return deflt;
23222308
}
2323-
set_key_error(key);
2309+
_PyErr_SetKeyError(key);
23242310
return NULL;
23252311
}
23262312
*value_addr = NULL;

Objects/setobject.c

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,6 @@
1111
#include "structmember.h"
1212
#include "stringlib/eq.h"
1313

14-
/* Set a key error with the specified argument, wrapping it in a
15-
* tuple automatically so that tuple keys are not unpacked as the
16-
* exception arguments. */
17-
static void
18-
set_key_error(PyObject *arg)
19-
{
20-
PyObject *tup;
21-
tup = PyTuple_Pack(1, arg);
22-
if (!tup)
23-
return; /* caller will expect error to be set anyway */
24-
PyErr_SetObject(PyExc_KeyError, tup);
25-
Py_DECREF(tup);
26-
}
27-
2814
/* This must be >= 1. */
2915
#define PERTURB_SHIFT 5
3016
#define LINEAR_PROBES 9
@@ -1948,7 +1934,7 @@ set_remove(PySetObject *so, PyObject *key)
19481934
}
19491935

19501936
if (rv == DISCARD_NOTFOUND) {
1951-
set_key_error(key);
1937+
_PyErr_SetKeyError(key);
19521938
return NULL;
19531939
}
19541940
Py_RETURN_NONE;

Python/errors.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,20 @@ PyErr_SetObject(PyObject *exception, PyObject *value)
117117
PyErr_Restore(exception, value, tb);
118118
}
119119

120+
/* Set a key error with the specified argument, wrapping it in a
121+
* tuple automatically so that tuple keys are not unpacked as the
122+
* exception arguments. */
123+
void
124+
_PyErr_SetKeyError(PyObject *arg)
125+
{
126+
PyObject *tup;
127+
tup = PyTuple_Pack(1, arg);
128+
if (!tup)
129+
return; /* caller will expect error to be set anyway */
130+
PyErr_SetObject(PyExc_KeyError, tup);
131+
Py_DECREF(tup);
132+
}
133+
120134
void
121135
PyErr_SetNone(PyObject *exception)
122136
{

0 commit comments

Comments
 (0)