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

Skip to content

Commit 72b715d

Browse files
committed
(initerrors): Make sure that the exception tuples ("base-classes" when
string-based exceptions are used) reflect the real class hierarchy, i.e. that SystemExit derives from Exception not StandardError.
1 parent 40db48c commit 72b715d

1 file changed

Lines changed: 18 additions & 8 deletions

File tree

Python/bltinmodule.c

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2153,7 +2153,7 @@ static void
21532153
initerrors(dict)
21542154
PyObject *dict;
21552155
{
2156-
int i;
2156+
int i, j;
21572157
int exccnt = 0;
21582158
for (i = 0; bltin_exc[i].name; i++, exccnt++) {
21592159
Py_XDECREF(*bltin_exc[i].exc);
@@ -2190,23 +2190,33 @@ initerrors(dict)
21902190
PyTuple_SET_ITEM(PyExc_EnvironmentError, 1, PyExc_OSError);
21912191
PyDict_SetItemString(dict, "EnvironmentError", PyExc_EnvironmentError);
21922192

2193-
PyExc_StandardError = PyTuple_New(exccnt-2);
2194-
for (i = 2; bltin_exc[i].name; i++) {
2193+
/* missing from the StandardError tuple: Exception, StandardError,
2194+
* and SystemExit
2195+
*/
2196+
PyExc_StandardError = PyTuple_New(exccnt-3);
2197+
for (i = 2, j = 0; bltin_exc[i].name; i++) {
21952198
PyObject *exc = *bltin_exc[i].exc;
2196-
Py_INCREF(exc);
2197-
PyTuple_SET_ITEM(PyExc_StandardError, i-2, exc);
2199+
/* SystemExit is not an error, but it is an exception */
2200+
if (exc != PyExc_SystemExit) {
2201+
Py_INCREF(exc);
2202+
PyTuple_SET_ITEM(PyExc_StandardError, j++, exc);
2203+
}
21982204
}
21992205
PyDict_SetItemString(dict, "StandardError", PyExc_StandardError);
22002206

2201-
/* Exception is treated differently; for now, it's == StandardError */
2202-
PyExc_Exception = PyExc_StandardError;
2203-
Py_INCREF(PyExc_Exception);
2207+
/* Exception is a 2-tuple */
2208+
PyExc_Exception = PyTuple_New(2);
2209+
Py_INCREF(PyExc_SystemExit);
2210+
PyTuple_SET_ITEM(PyExc_Exception, 0, PyExc_SystemExit);
2211+
Py_INCREF(PyExc_StandardError);
2212+
PyTuple_SET_ITEM(PyExc_Exception, 1, PyExc_StandardError);
22042213
PyDict_SetItemString(dict, "Exception", PyExc_Exception);
22052214

22062215
if (PyErr_Occurred())
22072216
Py_FatalError("Could not initialize built-in string exceptions");
22082217
}
22092218

2219+
22102220
static void
22112221
finierrors()
22122222
{

0 commit comments

Comments
 (0)