Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 | |
Guido van Rossum | 373c869 | 1997-04-29 18:22:47 +0000 | [diff] [blame] | 2 | /* Error handling */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3 | |
Guido van Rossum | 373c869 | 1997-04-29 18:22:47 +0000 | [diff] [blame] | 4 | #include "Python.h" |
Guido van Rossum | f22120a | 1990-12-20 23:05:40 +0000 | [diff] [blame] | 5 | |
Guido van Rossum | 53e8d44 | 1995-03-09 12:11:31 +0000 | [diff] [blame] | 6 | #ifndef __STDC__ |
Guido van Rossum | 7844e38 | 1997-04-11 20:44:04 +0000 | [diff] [blame] | 7 | #ifndef MS_WINDOWS |
Tim Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame] | 8 | extern char *strerror(int); |
Guido van Rossum | 53e8d44 | 1995-03-09 12:11:31 +0000 | [diff] [blame] | 9 | #endif |
Guido van Rossum | 7844e38 | 1997-04-11 20:44:04 +0000 | [diff] [blame] | 10 | #endif |
Guido van Rossum | f5401bd | 1990-11-02 17:50:28 +0000 | [diff] [blame] | 11 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 12 | #ifdef MS_WINDOWS |
Martin v. Löwis | 5d12abe | 2007-09-03 07:40:24 +0000 | [diff] [blame] | 13 | #include <windows.h> |
| 14 | #include <winbase.h> |
Guido van Rossum | 743007d | 1999-04-21 15:27:31 +0000 | [diff] [blame] | 15 | #endif |
| 16 | |
Jeremy Hylton | b69a27e | 2000-09-01 03:49:47 +0000 | [diff] [blame] | 17 | #include <ctype.h> |
| 18 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 19 | #ifdef __cplusplus |
| 20 | extern "C" { |
| 21 | #endif |
| 22 | |
Victor Stinner | bd303c1 | 2013-11-07 23:07:29 +0100 | [diff] [blame] | 23 | _Py_IDENTIFIER(builtins); |
| 24 | _Py_IDENTIFIER(stderr); |
| 25 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 26 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 27 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 28 | PyErr_Restore(PyObject *type, PyObject *value, PyObject *traceback) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 29 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 30 | PyThreadState *tstate = PyThreadState_GET(); |
| 31 | PyObject *oldtype, *oldvalue, *oldtraceback; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 32 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 33 | if (traceback != NULL && !PyTraceBack_Check(traceback)) { |
| 34 | /* XXX Should never happen -- fatal error instead? */ |
| 35 | /* Well, it could be None. */ |
| 36 | Py_DECREF(traceback); |
| 37 | traceback = NULL; |
| 38 | } |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 39 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 40 | /* Save these in locals to safeguard against recursive |
| 41 | invocation through Py_XDECREF */ |
| 42 | oldtype = tstate->curexc_type; |
| 43 | oldvalue = tstate->curexc_value; |
| 44 | oldtraceback = tstate->curexc_traceback; |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 45 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 46 | tstate->curexc_type = type; |
| 47 | tstate->curexc_value = value; |
| 48 | tstate->curexc_traceback = traceback; |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 49 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 50 | Py_XDECREF(oldtype); |
| 51 | Py_XDECREF(oldvalue); |
| 52 | Py_XDECREF(oldtraceback); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 53 | } |
| 54 | |
Victor Stinner | 3a84097 | 2016-08-22 23:59:08 +0200 | [diff] [blame] | 55 | static PyObject* |
| 56 | _PyErr_CreateException(PyObject *exception, PyObject *value) |
| 57 | { |
| 58 | if (value == NULL || value == Py_None) { |
| 59 | return _PyObject_CallNoArg(exception); |
| 60 | } |
| 61 | else if (PyTuple_Check(value)) { |
| 62 | return PyObject_Call(exception, value, NULL); |
| 63 | } |
| 64 | else { |
Victor Stinner | 7bfb42d | 2016-12-05 17:04:32 +0100 | [diff] [blame] | 65 | return PyObject_CallFunctionObjArgs(exception, value, NULL); |
Victor Stinner | 3a84097 | 2016-08-22 23:59:08 +0200 | [diff] [blame] | 66 | } |
| 67 | } |
| 68 | |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 69 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 70 | PyErr_SetObject(PyObject *exception, PyObject *value) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 71 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 72 | PyThreadState *tstate = PyThreadState_GET(); |
| 73 | PyObject *exc_value; |
| 74 | PyObject *tb = NULL; |
Guido van Rossum | b4fb6e4 | 2008-06-14 20:20:24 +0000 | [diff] [blame] | 75 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 76 | if (exception != NULL && |
| 77 | !PyExceptionClass_Check(exception)) { |
| 78 | PyErr_Format(PyExc_SystemError, |
| 79 | "exception %R not a BaseException subclass", |
| 80 | exception); |
| 81 | return; |
| 82 | } |
Victor Stinner | 3a84097 | 2016-08-22 23:59:08 +0200 | [diff] [blame] | 83 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 84 | Py_XINCREF(value); |
| 85 | exc_value = tstate->exc_value; |
| 86 | if (exc_value != NULL && exc_value != Py_None) { |
| 87 | /* Implicit exception chaining */ |
| 88 | Py_INCREF(exc_value); |
| 89 | if (value == NULL || !PyExceptionInstance_Check(value)) { |
| 90 | /* We must normalize the value right now */ |
Victor Stinner | 3a84097 | 2016-08-22 23:59:08 +0200 | [diff] [blame] | 91 | PyObject *fixed_value; |
Victor Stinner | de821be | 2015-03-24 12:41:23 +0100 | [diff] [blame] | 92 | |
Victor Stinner | 3a84097 | 2016-08-22 23:59:08 +0200 | [diff] [blame] | 93 | /* Issue #23571: functions must not be called with an |
Victor Stinner | de821be | 2015-03-24 12:41:23 +0100 | [diff] [blame] | 94 | exception set */ |
Victor Stinner | ace47d7 | 2013-07-18 01:41:08 +0200 | [diff] [blame] | 95 | PyErr_Clear(); |
Victor Stinner | de821be | 2015-03-24 12:41:23 +0100 | [diff] [blame] | 96 | |
Victor Stinner | 3a84097 | 2016-08-22 23:59:08 +0200 | [diff] [blame] | 97 | fixed_value = _PyErr_CreateException(exception, value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 98 | Py_XDECREF(value); |
Victor Stinner | 3a84097 | 2016-08-22 23:59:08 +0200 | [diff] [blame] | 99 | if (fixed_value == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 100 | return; |
Victor Stinner | 3a84097 | 2016-08-22 23:59:08 +0200 | [diff] [blame] | 101 | } |
| 102 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 103 | value = fixed_value; |
| 104 | } |
Victor Stinner | 3a84097 | 2016-08-22 23:59:08 +0200 | [diff] [blame] | 105 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 106 | /* Avoid reference cycles through the context chain. |
| 107 | This is O(chain length) but context chains are |
| 108 | usually very short. Sensitive readers may try |
| 109 | to inline the call to PyException_GetContext. */ |
| 110 | if (exc_value != value) { |
| 111 | PyObject *o = exc_value, *context; |
| 112 | while ((context = PyException_GetContext(o))) { |
| 113 | Py_DECREF(context); |
| 114 | if (context == value) { |
| 115 | PyException_SetContext(o, NULL); |
| 116 | break; |
| 117 | } |
| 118 | o = context; |
| 119 | } |
| 120 | PyException_SetContext(value, exc_value); |
Victor Stinner | 3a84097 | 2016-08-22 23:59:08 +0200 | [diff] [blame] | 121 | } |
| 122 | else { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 123 | Py_DECREF(exc_value); |
| 124 | } |
| 125 | } |
| 126 | if (value != NULL && PyExceptionInstance_Check(value)) |
| 127 | tb = PyException_GetTraceback(value); |
| 128 | Py_XINCREF(exception); |
| 129 | PyErr_Restore(exception, value, tb); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 130 | } |
| 131 | |
Raymond Hettinger | 69492da | 2013-09-02 15:59:26 -0700 | [diff] [blame] | 132 | /* Set a key error with the specified argument, wrapping it in a |
| 133 | * tuple automatically so that tuple keys are not unpacked as the |
| 134 | * exception arguments. */ |
| 135 | void |
| 136 | _PyErr_SetKeyError(PyObject *arg) |
| 137 | { |
| 138 | PyObject *tup; |
| 139 | tup = PyTuple_Pack(1, arg); |
| 140 | if (!tup) |
| 141 | return; /* caller will expect error to be set anyway */ |
| 142 | PyErr_SetObject(PyExc_KeyError, tup); |
| 143 | Py_DECREF(tup); |
| 144 | } |
| 145 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 146 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 147 | PyErr_SetNone(PyObject *exception) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 148 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 149 | PyErr_SetObject(exception, (PyObject *)NULL); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 153 | PyErr_SetString(PyObject *exception, const char *string) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 154 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 155 | PyObject *value = PyUnicode_FromString(string); |
| 156 | PyErr_SetObject(exception, value); |
| 157 | Py_XDECREF(value); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 158 | } |
| 159 | |
Guido van Rossum | 3a24181 | 1994-08-29 12:14:12 +0000 | [diff] [blame] | 160 | |
Victor Stinner | c6944e7 | 2016-11-11 02:13:35 +0100 | [diff] [blame] | 161 | PyObject* _Py_HOT_FUNCTION |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 162 | PyErr_Occurred(void) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 163 | { |
Victor Stinner | 0cae609 | 2016-11-11 01:43:56 +0100 | [diff] [blame] | 164 | PyThreadState *tstate = PyThreadState_GET(); |
Jeffrey Yasskin | 8e0bdfd | 2010-05-13 18:31:05 +0000 | [diff] [blame] | 165 | return tstate == NULL ? NULL : tstate->curexc_type; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 166 | } |
| 167 | |
Barry Warsaw | c0dc92a | 1997-08-22 21:22:58 +0000 | [diff] [blame] | 168 | |
| 169 | int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 170 | PyErr_GivenExceptionMatches(PyObject *err, PyObject *exc) |
Barry Warsaw | c0dc92a | 1997-08-22 21:22:58 +0000 | [diff] [blame] | 171 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 172 | if (err == NULL || exc == NULL) { |
| 173 | /* maybe caused by "import exceptions" that failed early on */ |
| 174 | return 0; |
| 175 | } |
| 176 | if (PyTuple_Check(exc)) { |
| 177 | Py_ssize_t i, n; |
| 178 | n = PyTuple_Size(exc); |
| 179 | for (i = 0; i < n; i++) { |
| 180 | /* Test recursively */ |
| 181 | if (PyErr_GivenExceptionMatches( |
| 182 | err, PyTuple_GET_ITEM(exc, i))) |
| 183 | { |
| 184 | return 1; |
| 185 | } |
| 186 | } |
| 187 | return 0; |
| 188 | } |
| 189 | /* err might be an instance, so check its class. */ |
| 190 | if (PyExceptionInstance_Check(err)) |
| 191 | err = PyExceptionInstance_Class(err); |
Barry Warsaw | c0dc92a | 1997-08-22 21:22:58 +0000 | [diff] [blame] | 192 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 193 | if (PyExceptionClass_Check(err) && PyExceptionClass_Check(exc)) { |
| 194 | int res = 0; |
| 195 | PyObject *exception, *value, *tb; |
| 196 | PyErr_Fetch(&exception, &value, &tb); |
| 197 | /* PyObject_IsSubclass() can recurse and therefore is |
| 198 | not safe (see test_bad_getattr in test.pickletester). */ |
| 199 | res = PyType_IsSubtype((PyTypeObject *)err, (PyTypeObject *)exc); |
| 200 | /* This function must not fail, so print the error here */ |
| 201 | if (res == -1) { |
| 202 | PyErr_WriteUnraisable(err); |
| 203 | res = 0; |
| 204 | } |
| 205 | PyErr_Restore(exception, value, tb); |
| 206 | return res; |
| 207 | } |
Barry Warsaw | c0dc92a | 1997-08-22 21:22:58 +0000 | [diff] [blame] | 208 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 209 | return err == exc; |
Barry Warsaw | c0dc92a | 1997-08-22 21:22:58 +0000 | [diff] [blame] | 210 | } |
Guido van Rossum | 743007d | 1999-04-21 15:27:31 +0000 | [diff] [blame] | 211 | |
Barry Warsaw | c0dc92a | 1997-08-22 21:22:58 +0000 | [diff] [blame] | 212 | |
| 213 | int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 214 | PyErr_ExceptionMatches(PyObject *exc) |
Barry Warsaw | c0dc92a | 1997-08-22 21:22:58 +0000 | [diff] [blame] | 215 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 216 | return PyErr_GivenExceptionMatches(PyErr_Occurred(), exc); |
Barry Warsaw | c0dc92a | 1997-08-22 21:22:58 +0000 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | |
| 220 | /* Used in many places to normalize a raised exception, including in |
| 221 | eval_code2(), do_raise(), and PyErr_Print() |
Benjamin Peterson | e652821 | 2008-07-15 15:32:09 +0000 | [diff] [blame] | 222 | |
| 223 | XXX: should PyErr_NormalizeException() also call |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 224 | PyException_SetTraceback() with the resulting value and tb? |
Barry Warsaw | c0dc92a | 1997-08-22 21:22:58 +0000 | [diff] [blame] | 225 | */ |
| 226 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 227 | PyErr_NormalizeException(PyObject **exc, PyObject **val, PyObject **tb) |
Barry Warsaw | c0dc92a | 1997-08-22 21:22:58 +0000 | [diff] [blame] | 228 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 229 | PyObject *type = *exc; |
| 230 | PyObject *value = *val; |
| 231 | PyObject *inclass = NULL; |
| 232 | PyObject *initial_tb = NULL; |
| 233 | PyThreadState *tstate = NULL; |
Barry Warsaw | c0dc92a | 1997-08-22 21:22:58 +0000 | [diff] [blame] | 234 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 235 | if (type == NULL) { |
| 236 | /* There was no exception, so nothing to do. */ |
| 237 | return; |
| 238 | } |
Guido van Rossum | ed473a4 | 2000-08-07 19:18:27 +0000 | [diff] [blame] | 239 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 240 | /* If PyErr_SetNone() was used, the value will have been actually |
| 241 | set to NULL. |
| 242 | */ |
| 243 | if (!value) { |
| 244 | value = Py_None; |
| 245 | Py_INCREF(value); |
| 246 | } |
Barry Warsaw | c0dc92a | 1997-08-22 21:22:58 +0000 | [diff] [blame] | 247 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 248 | if (PyExceptionInstance_Check(value)) |
| 249 | inclass = PyExceptionInstance_Class(value); |
Barry Warsaw | c0dc92a | 1997-08-22 21:22:58 +0000 | [diff] [blame] | 250 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 251 | /* Normalize the exception so that if the type is a class, the |
| 252 | value will be an instance. |
| 253 | */ |
| 254 | if (PyExceptionClass_Check(type)) { |
Victor Stinner | 74a7fa6 | 2013-07-17 00:44:53 +0200 | [diff] [blame] | 255 | int is_subclass; |
| 256 | if (inclass) { |
| 257 | is_subclass = PyObject_IsSubclass(inclass, type); |
| 258 | if (is_subclass < 0) |
| 259 | goto finally; |
| 260 | } |
| 261 | else |
| 262 | is_subclass = 0; |
| 263 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 264 | /* if the value was not an instance, or is not an instance |
| 265 | whose class is (or is derived from) type, then use the |
| 266 | value as an argument to instantiation of the type |
| 267 | class. |
| 268 | */ |
Victor Stinner | 74a7fa6 | 2013-07-17 00:44:53 +0200 | [diff] [blame] | 269 | if (!inclass || !is_subclass) { |
Victor Stinner | 3a84097 | 2016-08-22 23:59:08 +0200 | [diff] [blame] | 270 | PyObject *fixed_value; |
Barry Warsaw | c0dc92a | 1997-08-22 21:22:58 +0000 | [diff] [blame] | 271 | |
Victor Stinner | 3a84097 | 2016-08-22 23:59:08 +0200 | [diff] [blame] | 272 | fixed_value = _PyErr_CreateException(type, value); |
| 273 | if (fixed_value == NULL) { |
| 274 | goto finally; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 275 | } |
Barry Warsaw | c0dc92a | 1997-08-22 21:22:58 +0000 | [diff] [blame] | 276 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 277 | Py_DECREF(value); |
Victor Stinner | 3a84097 | 2016-08-22 23:59:08 +0200 | [diff] [blame] | 278 | value = fixed_value; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 279 | } |
| 280 | /* if the class of the instance doesn't exactly match the |
| 281 | class of the type, believe the instance |
| 282 | */ |
| 283 | else if (inclass != type) { |
| 284 | Py_DECREF(type); |
| 285 | type = inclass; |
| 286 | Py_INCREF(type); |
| 287 | } |
| 288 | } |
| 289 | *exc = type; |
| 290 | *val = value; |
| 291 | return; |
Barry Warsaw | c0dc92a | 1997-08-22 21:22:58 +0000 | [diff] [blame] | 292 | finally: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 293 | Py_DECREF(type); |
| 294 | Py_DECREF(value); |
| 295 | /* If the new exception doesn't set a traceback and the old |
| 296 | exception had a traceback, use the old traceback for the |
| 297 | new exception. It's better than nothing. |
| 298 | */ |
| 299 | initial_tb = *tb; |
| 300 | PyErr_Fetch(exc, val, tb); |
| 301 | if (initial_tb != NULL) { |
| 302 | if (*tb == NULL) |
| 303 | *tb = initial_tb; |
| 304 | else |
| 305 | Py_DECREF(initial_tb); |
| 306 | } |
| 307 | /* normalize recursively */ |
| 308 | tstate = PyThreadState_GET(); |
| 309 | if (++tstate->recursion_depth > Py_GetRecursionLimit()) { |
| 310 | --tstate->recursion_depth; |
Serhiy Storchaka | 191321d | 2015-12-27 15:41:34 +0200 | [diff] [blame] | 311 | /* throw away the old exception and use the recursion error instead */ |
| 312 | Py_INCREF(PyExc_RecursionError); |
Serhiy Storchaka | 57a01d3 | 2016-04-10 18:05:40 +0300 | [diff] [blame] | 313 | Py_SETREF(*exc, PyExc_RecursionError); |
Serhiy Storchaka | 191321d | 2015-12-27 15:41:34 +0200 | [diff] [blame] | 314 | Py_INCREF(PyExc_RecursionErrorInst); |
Serhiy Storchaka | 57a01d3 | 2016-04-10 18:05:40 +0300 | [diff] [blame] | 315 | Py_SETREF(*val, PyExc_RecursionErrorInst); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 316 | /* just keeping the old traceback */ |
| 317 | return; |
| 318 | } |
| 319 | PyErr_NormalizeException(exc, val, tb); |
| 320 | --tstate->recursion_depth; |
Barry Warsaw | c0dc92a | 1997-08-22 21:22:58 +0000 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 324 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 325 | PyErr_Fetch(PyObject **p_type, PyObject **p_value, PyObject **p_traceback) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 326 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 327 | PyThreadState *tstate = PyThreadState_GET(); |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 328 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 329 | *p_type = tstate->curexc_type; |
| 330 | *p_value = tstate->curexc_value; |
| 331 | *p_traceback = tstate->curexc_traceback; |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 332 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 333 | tstate->curexc_type = NULL; |
| 334 | tstate->curexc_value = NULL; |
| 335 | tstate->curexc_traceback = NULL; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 339 | PyErr_Clear(void) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 340 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 341 | PyErr_Restore(NULL, NULL, NULL); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 342 | } |
Guido van Rossum | 7d310eb | 1990-10-14 20:00:05 +0000 | [diff] [blame] | 343 | |
Martin v. Löwis | aa2efcb | 2012-04-19 14:33:43 +0200 | [diff] [blame] | 344 | void |
| 345 | PyErr_GetExcInfo(PyObject **p_type, PyObject **p_value, PyObject **p_traceback) |
| 346 | { |
| 347 | PyThreadState *tstate = PyThreadState_GET(); |
| 348 | |
| 349 | *p_type = tstate->exc_type; |
| 350 | *p_value = tstate->exc_value; |
| 351 | *p_traceback = tstate->exc_traceback; |
| 352 | |
| 353 | Py_XINCREF(*p_type); |
| 354 | Py_XINCREF(*p_value); |
| 355 | Py_XINCREF(*p_traceback); |
| 356 | } |
| 357 | |
| 358 | void |
| 359 | PyErr_SetExcInfo(PyObject *p_type, PyObject *p_value, PyObject *p_traceback) |
| 360 | { |
| 361 | PyObject *oldtype, *oldvalue, *oldtraceback; |
| 362 | PyThreadState *tstate = PyThreadState_GET(); |
| 363 | |
| 364 | oldtype = tstate->exc_type; |
| 365 | oldvalue = tstate->exc_value; |
| 366 | oldtraceback = tstate->exc_traceback; |
| 367 | |
| 368 | tstate->exc_type = p_type; |
| 369 | tstate->exc_value = p_value; |
| 370 | tstate->exc_traceback = p_traceback; |
| 371 | |
| 372 | Py_XDECREF(oldtype); |
| 373 | Py_XDECREF(oldvalue); |
| 374 | Py_XDECREF(oldtraceback); |
| 375 | } |
| 376 | |
Serhiy Storchaka | e2bd2a7 | 2014-10-08 22:31:52 +0300 | [diff] [blame] | 377 | /* Like PyErr_Restore(), but if an exception is already set, |
| 378 | set the context associated with it. |
| 379 | */ |
| 380 | void |
| 381 | _PyErr_ChainExceptions(PyObject *exc, PyObject *val, PyObject *tb) |
| 382 | { |
| 383 | if (exc == NULL) |
| 384 | return; |
| 385 | |
| 386 | if (PyErr_Occurred()) { |
| 387 | PyObject *exc2, *val2, *tb2; |
| 388 | PyErr_Fetch(&exc2, &val2, &tb2); |
| 389 | PyErr_NormalizeException(&exc, &val, &tb); |
Serhiy Storchaka | 9e373be | 2016-10-21 16:19:59 +0300 | [diff] [blame] | 390 | if (tb != NULL) { |
| 391 | PyException_SetTraceback(val, tb); |
| 392 | Py_DECREF(tb); |
| 393 | } |
Serhiy Storchaka | e2bd2a7 | 2014-10-08 22:31:52 +0300 | [diff] [blame] | 394 | Py_DECREF(exc); |
Serhiy Storchaka | e2bd2a7 | 2014-10-08 22:31:52 +0300 | [diff] [blame] | 395 | PyErr_NormalizeException(&exc2, &val2, &tb2); |
| 396 | PyException_SetContext(val2, val); |
| 397 | PyErr_Restore(exc2, val2, tb2); |
| 398 | } |
| 399 | else { |
| 400 | PyErr_Restore(exc, val, tb); |
| 401 | } |
| 402 | } |
| 403 | |
Serhiy Storchaka | 467ab19 | 2016-10-21 17:09:17 +0300 | [diff] [blame] | 404 | static PyObject * |
| 405 | _PyErr_FormatVFromCause(PyObject *exception, const char *format, va_list vargs) |
| 406 | { |
| 407 | PyObject *exc, *val, *val2, *tb; |
| 408 | |
| 409 | assert(PyErr_Occurred()); |
| 410 | PyErr_Fetch(&exc, &val, &tb); |
| 411 | PyErr_NormalizeException(&exc, &val, &tb); |
| 412 | if (tb != NULL) { |
| 413 | PyException_SetTraceback(val, tb); |
| 414 | Py_DECREF(tb); |
| 415 | } |
| 416 | Py_DECREF(exc); |
| 417 | assert(!PyErr_Occurred()); |
| 418 | |
| 419 | PyErr_FormatV(exception, format, vargs); |
| 420 | |
| 421 | PyErr_Fetch(&exc, &val2, &tb); |
| 422 | PyErr_NormalizeException(&exc, &val2, &tb); |
| 423 | Py_INCREF(val); |
| 424 | PyException_SetCause(val2, val); |
| 425 | PyException_SetContext(val2, val); |
| 426 | PyErr_Restore(exc, val2, tb); |
| 427 | |
| 428 | return NULL; |
| 429 | } |
| 430 | |
| 431 | PyObject * |
| 432 | _PyErr_FormatFromCause(PyObject *exception, const char *format, ...) |
| 433 | { |
| 434 | va_list vargs; |
| 435 | #ifdef HAVE_STDARG_PROTOTYPES |
| 436 | va_start(vargs, format); |
| 437 | #else |
| 438 | va_start(vargs); |
| 439 | #endif |
| 440 | _PyErr_FormatVFromCause(exception, format, vargs); |
| 441 | va_end(vargs); |
| 442 | return NULL; |
| 443 | } |
| 444 | |
Guido van Rossum | 7d310eb | 1990-10-14 20:00:05 +0000 | [diff] [blame] | 445 | /* Convenience functions to set a type error exception and return 0 */ |
| 446 | |
| 447 | int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 448 | PyErr_BadArgument(void) |
Guido van Rossum | 7d310eb | 1990-10-14 20:00:05 +0000 | [diff] [blame] | 449 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 450 | PyErr_SetString(PyExc_TypeError, |
| 451 | "bad argument type for built-in operation"); |
| 452 | return 0; |
Guido van Rossum | 7d310eb | 1990-10-14 20:00:05 +0000 | [diff] [blame] | 453 | } |
| 454 | |
Guido van Rossum | 373c869 | 1997-04-29 18:22:47 +0000 | [diff] [blame] | 455 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 456 | PyErr_NoMemory(void) |
Guido van Rossum | 7d310eb | 1990-10-14 20:00:05 +0000 | [diff] [blame] | 457 | { |
Victor Stinner | f54a574 | 2013-07-22 22:28:37 +0200 | [diff] [blame] | 458 | if (Py_TYPE(PyExc_MemoryError) == NULL) { |
| 459 | /* PyErr_NoMemory() has been called before PyExc_MemoryError has been |
| 460 | initialized by _PyExc_Init() */ |
| 461 | Py_FatalError("Out of memory and PyExc_MemoryError is not " |
| 462 | "initialized yet"); |
| 463 | } |
Antoine Pitrou | 07e20ef | 2010-10-28 22:56:58 +0000 | [diff] [blame] | 464 | PyErr_SetNone(PyExc_MemoryError); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 465 | return NULL; |
Guido van Rossum | 7d310eb | 1990-10-14 20:00:05 +0000 | [diff] [blame] | 466 | } |
| 467 | |
Guido van Rossum | 373c869 | 1997-04-29 18:22:47 +0000 | [diff] [blame] | 468 | PyObject * |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 469 | PyErr_SetFromErrnoWithFilenameObject(PyObject *exc, PyObject *filenameObject) |
Guido van Rossum | 7d310eb | 1990-10-14 20:00:05 +0000 | [diff] [blame] | 470 | { |
Larry Hastings | b082731 | 2014-02-09 22:05:19 -0800 | [diff] [blame] | 471 | return PyErr_SetFromErrnoWithFilenameObjects(exc, filenameObject, NULL); |
| 472 | } |
| 473 | |
| 474 | PyObject * |
| 475 | PyErr_SetFromErrnoWithFilenameObjects(PyObject *exc, PyObject *filenameObject, PyObject *filenameObject2) |
| 476 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 477 | PyObject *message; |
Antoine Pitrou | 5d6fbe8 | 2011-10-12 19:39:57 +0200 | [diff] [blame] | 478 | PyObject *v, *args; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 479 | int i = errno; |
Victor Stinner | 1f33f2b | 2011-12-17 04:45:09 +0100 | [diff] [blame] | 480 | #ifdef MS_WINDOWS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 481 | WCHAR *s_buf = NULL; |
Martin v. Löwis | 5d12abe | 2007-09-03 07:40:24 +0000 | [diff] [blame] | 482 | #endif /* Unix/Windows */ |
Martin v. Löwis | 5d12abe | 2007-09-03 07:40:24 +0000 | [diff] [blame] | 483 | |
Guido van Rossum | e9fbc09 | 1995-02-18 14:52:19 +0000 | [diff] [blame] | 484 | #ifdef EINTR |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 485 | if (i == EINTR && PyErr_CheckSignals()) |
| 486 | return NULL; |
Guido van Rossum | e9fbc09 | 1995-02-18 14:52:19 +0000 | [diff] [blame] | 487 | #endif |
Martin v. Löwis | 5d12abe | 2007-09-03 07:40:24 +0000 | [diff] [blame] | 488 | |
Martin v. Löwis | 5d12abe | 2007-09-03 07:40:24 +0000 | [diff] [blame] | 489 | #ifndef MS_WINDOWS |
Victor Stinner | 1f33f2b | 2011-12-17 04:45:09 +0100 | [diff] [blame] | 490 | if (i != 0) { |
| 491 | char *s = strerror(i); |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 492 | message = PyUnicode_DecodeLocale(s, "surrogateescape"); |
Victor Stinner | 1f33f2b | 2011-12-17 04:45:09 +0100 | [diff] [blame] | 493 | } |
| 494 | else { |
| 495 | /* Sometimes errno didn't get set */ |
| 496 | message = PyUnicode_FromString("Error"); |
| 497 | } |
Guido van Rossum | 743007d | 1999-04-21 15:27:31 +0000 | [diff] [blame] | 498 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 499 | if (i == 0) |
| 500 | message = PyUnicode_FromString("Error"); /* Sometimes errno didn't get set */ |
| 501 | else |
| 502 | { |
| 503 | /* Note that the Win32 errors do not lineup with the |
| 504 | errno error. So if the error is in the MSVC error |
| 505 | table, we use it, otherwise we assume it really _is_ |
| 506 | a Win32 error code |
| 507 | */ |
| 508 | if (i > 0 && i < _sys_nerr) { |
| 509 | message = PyUnicode_FromString(_sys_errlist[i]); |
| 510 | } |
| 511 | else { |
| 512 | int len = FormatMessageW( |
| 513 | FORMAT_MESSAGE_ALLOCATE_BUFFER | |
| 514 | FORMAT_MESSAGE_FROM_SYSTEM | |
| 515 | FORMAT_MESSAGE_IGNORE_INSERTS, |
| 516 | NULL, /* no message source */ |
| 517 | i, |
| 518 | MAKELANGID(LANG_NEUTRAL, |
| 519 | SUBLANG_DEFAULT), |
| 520 | /* Default language */ |
| 521 | (LPWSTR) &s_buf, |
| 522 | 0, /* size not used */ |
| 523 | NULL); /* no args */ |
| 524 | if (len==0) { |
| 525 | /* Only ever seen this in out-of-mem |
| 526 | situations */ |
| 527 | s_buf = NULL; |
Serhiy Storchaka | f41f8f9 | 2015-04-02 09:47:27 +0300 | [diff] [blame] | 528 | message = PyUnicode_FromFormat("Windows Error 0x%x", i); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 529 | } else { |
| 530 | /* remove trailing cr/lf and dots */ |
| 531 | while (len > 0 && (s_buf[len-1] <= L' ' || s_buf[len-1] == L'.')) |
| 532 | s_buf[--len] = L'\0'; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 533 | message = PyUnicode_FromWideChar(s_buf, len); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 534 | } |
| 535 | } |
| 536 | } |
Martin v. Löwis | 3484a18 | 2002-03-09 12:07:51 +0000 | [diff] [blame] | 537 | #endif /* Unix/Windows */ |
Martin v. Löwis | 5d12abe | 2007-09-03 07:40:24 +0000 | [diff] [blame] | 538 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 539 | if (message == NULL) |
| 540 | { |
Martin v. Löwis | 5d12abe | 2007-09-03 07:40:24 +0000 | [diff] [blame] | 541 | #ifdef MS_WINDOWS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 542 | LocalFree(s_buf); |
Martin v. Löwis | 5d12abe | 2007-09-03 07:40:24 +0000 | [diff] [blame] | 543 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 544 | return NULL; |
| 545 | } |
Martin v. Löwis | 5d12abe | 2007-09-03 07:40:24 +0000 | [diff] [blame] | 546 | |
Larry Hastings | b082731 | 2014-02-09 22:05:19 -0800 | [diff] [blame] | 547 | if (filenameObject != NULL) { |
| 548 | if (filenameObject2 != NULL) |
| 549 | args = Py_BuildValue("(iOOiO)", i, message, filenameObject, 0, filenameObject2); |
| 550 | else |
| 551 | args = Py_BuildValue("(iOO)", i, message, filenameObject); |
| 552 | } else { |
| 553 | assert(filenameObject2 == NULL); |
Antoine Pitrou | 5d6fbe8 | 2011-10-12 19:39:57 +0200 | [diff] [blame] | 554 | args = Py_BuildValue("(iO)", i, message); |
Larry Hastings | b082731 | 2014-02-09 22:05:19 -0800 | [diff] [blame] | 555 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 556 | Py_DECREF(message); |
Martin v. Löwis | 5d12abe | 2007-09-03 07:40:24 +0000 | [diff] [blame] | 557 | |
Antoine Pitrou | 5d6fbe8 | 2011-10-12 19:39:57 +0200 | [diff] [blame] | 558 | if (args != NULL) { |
| 559 | v = PyObject_Call(exc, args, NULL); |
| 560 | Py_DECREF(args); |
| 561 | if (v != NULL) { |
| 562 | PyErr_SetObject((PyObject *) Py_TYPE(v), v); |
| 563 | Py_DECREF(v); |
| 564 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 565 | } |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 566 | #ifdef MS_WINDOWS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 567 | LocalFree(s_buf); |
Guido van Rossum | 743007d | 1999-04-21 15:27:31 +0000 | [diff] [blame] | 568 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 569 | return NULL; |
Guido van Rossum | 7d310eb | 1990-10-14 20:00:05 +0000 | [diff] [blame] | 570 | } |
Guido van Rossum | 743007d | 1999-04-21 15:27:31 +0000 | [diff] [blame] | 571 | |
Barry Warsaw | 97d9515 | 1998-07-23 16:05:56 +0000 | [diff] [blame] | 572 | PyObject * |
Neal Norwitz | b382b84 | 2007-08-24 20:00:37 +0000 | [diff] [blame] | 573 | PyErr_SetFromErrnoWithFilename(PyObject *exc, const char *filename) |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 574 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 575 | PyObject *name = filename ? PyUnicode_DecodeFSDefault(filename) : NULL; |
Larry Hastings | b082731 | 2014-02-09 22:05:19 -0800 | [diff] [blame] | 576 | PyObject *result = PyErr_SetFromErrnoWithFilenameObjects(exc, name, NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 577 | Py_XDECREF(name); |
| 578 | return result; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 579 | } |
| 580 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 581 | #ifdef MS_WINDOWS |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 582 | PyObject * |
Neal Norwitz | b382b84 | 2007-08-24 20:00:37 +0000 | [diff] [blame] | 583 | PyErr_SetFromErrnoWithUnicodeFilename(PyObject *exc, const Py_UNICODE *filename) |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 584 | { |
Serhiy Storchaka | 460bd0d | 2016-11-20 12:16:46 +0200 | [diff] [blame] | 585 | PyObject *name = filename ? PyUnicode_FromWideChar(filename, -1) : NULL; |
Larry Hastings | b082731 | 2014-02-09 22:05:19 -0800 | [diff] [blame] | 586 | PyObject *result = PyErr_SetFromErrnoWithFilenameObjects(exc, name, NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 587 | Py_XDECREF(name); |
| 588 | return result; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 589 | } |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 590 | #endif /* MS_WINDOWS */ |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 591 | |
| 592 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 593 | PyErr_SetFromErrno(PyObject *exc) |
Barry Warsaw | 97d9515 | 1998-07-23 16:05:56 +0000 | [diff] [blame] | 594 | { |
Larry Hastings | b082731 | 2014-02-09 22:05:19 -0800 | [diff] [blame] | 595 | return PyErr_SetFromErrnoWithFilenameObjects(exc, NULL, NULL); |
Barry Warsaw | 97d9515 | 1998-07-23 16:05:56 +0000 | [diff] [blame] | 596 | } |
Guido van Rossum | 683a072 | 1990-10-21 22:09:12 +0000 | [diff] [blame] | 597 | |
Brett Cannon | bf36409 | 2006-03-01 04:25:17 +0000 | [diff] [blame] | 598 | #ifdef MS_WINDOWS |
Guido van Rossum | 795e189 | 2000-02-17 15:19:15 +0000 | [diff] [blame] | 599 | /* Windows specific error code handling */ |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 600 | PyObject *PyErr_SetExcFromWindowsErrWithFilenameObject( |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 601 | PyObject *exc, |
| 602 | int ierr, |
| 603 | PyObject *filenameObject) |
Guido van Rossum | 795e189 | 2000-02-17 15:19:15 +0000 | [diff] [blame] | 604 | { |
Larry Hastings | b082731 | 2014-02-09 22:05:19 -0800 | [diff] [blame] | 605 | return PyErr_SetExcFromWindowsErrWithFilenameObjects(exc, ierr, |
| 606 | filenameObject, NULL); |
| 607 | } |
| 608 | |
| 609 | PyObject *PyErr_SetExcFromWindowsErrWithFilenameObjects( |
| 610 | PyObject *exc, |
| 611 | int ierr, |
| 612 | PyObject *filenameObject, |
| 613 | PyObject *filenameObject2) |
| 614 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 615 | int len; |
| 616 | WCHAR *s_buf = NULL; /* Free via LocalFree */ |
| 617 | PyObject *message; |
Victor Stinner | 9ea8e4c | 2011-10-17 20:18:58 +0200 | [diff] [blame] | 618 | PyObject *args, *v; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 619 | DWORD err = (DWORD)ierr; |
| 620 | if (err==0) err = GetLastError(); |
| 621 | len = FormatMessageW( |
| 622 | /* Error API error */ |
| 623 | FORMAT_MESSAGE_ALLOCATE_BUFFER | |
| 624 | FORMAT_MESSAGE_FROM_SYSTEM | |
| 625 | FORMAT_MESSAGE_IGNORE_INSERTS, |
| 626 | NULL, /* no message source */ |
| 627 | err, |
| 628 | MAKELANGID(LANG_NEUTRAL, |
| 629 | SUBLANG_DEFAULT), /* Default language */ |
| 630 | (LPWSTR) &s_buf, |
| 631 | 0, /* size not used */ |
| 632 | NULL); /* no args */ |
| 633 | if (len==0) { |
| 634 | /* Only seen this in out of mem situations */ |
Serhiy Storchaka | f41f8f9 | 2015-04-02 09:47:27 +0300 | [diff] [blame] | 635 | message = PyUnicode_FromFormat("Windows Error 0x%x", err); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 636 | s_buf = NULL; |
| 637 | } else { |
| 638 | /* remove trailing cr/lf and dots */ |
| 639 | while (len > 0 && (s_buf[len-1] <= L' ' || s_buf[len-1] == L'.')) |
| 640 | s_buf[--len] = L'\0'; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 641 | message = PyUnicode_FromWideChar(s_buf, len); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 642 | } |
Martin v. Löwis | 5d12abe | 2007-09-03 07:40:24 +0000 | [diff] [blame] | 643 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 644 | if (message == NULL) |
| 645 | { |
| 646 | LocalFree(s_buf); |
| 647 | return NULL; |
| 648 | } |
Martin v. Löwis | 5d12abe | 2007-09-03 07:40:24 +0000 | [diff] [blame] | 649 | |
Larry Hastings | b082731 | 2014-02-09 22:05:19 -0800 | [diff] [blame] | 650 | if (filenameObject == NULL) { |
| 651 | assert(filenameObject2 == NULL); |
| 652 | filenameObject = filenameObject2 = Py_None; |
| 653 | } |
| 654 | else if (filenameObject2 == NULL) |
| 655 | filenameObject2 = Py_None; |
| 656 | /* This is the constructor signature for OSError. |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 657 | The POSIX translation will be figured out by the constructor. */ |
Larry Hastings | b082731 | 2014-02-09 22:05:19 -0800 | [diff] [blame] | 658 | args = Py_BuildValue("(iOOiO)", 0, message, filenameObject, err, filenameObject2); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 659 | Py_DECREF(message); |
Martin v. Löwis | 5d12abe | 2007-09-03 07:40:24 +0000 | [diff] [blame] | 660 | |
Victor Stinner | 9ea8e4c | 2011-10-17 20:18:58 +0200 | [diff] [blame] | 661 | if (args != NULL) { |
| 662 | v = PyObject_Call(exc, args, NULL); |
| 663 | Py_DECREF(args); |
| 664 | if (v != NULL) { |
| 665 | PyErr_SetObject((PyObject *) Py_TYPE(v), v); |
| 666 | Py_DECREF(v); |
| 667 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 668 | } |
| 669 | LocalFree(s_buf); |
| 670 | return NULL; |
Guido van Rossum | 795e189 | 2000-02-17 15:19:15 +0000 | [diff] [blame] | 671 | } |
| 672 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 673 | PyObject *PyErr_SetExcFromWindowsErrWithFilename( |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 674 | PyObject *exc, |
| 675 | int ierr, |
| 676 | const char *filename) |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 677 | { |
Victor Stinner | 92be939 | 2010-12-28 00:28:21 +0000 | [diff] [blame] | 678 | PyObject *name = filename ? PyUnicode_DecodeFSDefault(filename) : NULL; |
Larry Hastings | b082731 | 2014-02-09 22:05:19 -0800 | [diff] [blame] | 679 | PyObject *ret = PyErr_SetExcFromWindowsErrWithFilenameObjects(exc, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 680 | ierr, |
Larry Hastings | b082731 | 2014-02-09 22:05:19 -0800 | [diff] [blame] | 681 | name, |
| 682 | NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 683 | Py_XDECREF(name); |
| 684 | return ret; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 685 | } |
| 686 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 687 | PyObject *PyErr_SetExcFromWindowsErrWithUnicodeFilename( |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 688 | PyObject *exc, |
| 689 | int ierr, |
| 690 | const Py_UNICODE *filename) |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 691 | { |
Serhiy Storchaka | 460bd0d | 2016-11-20 12:16:46 +0200 | [diff] [blame] | 692 | PyObject *name = filename ? PyUnicode_FromWideChar(filename, -1) : NULL; |
Larry Hastings | b082731 | 2014-02-09 22:05:19 -0800 | [diff] [blame] | 693 | PyObject *ret = PyErr_SetExcFromWindowsErrWithFilenameObjects(exc, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 694 | ierr, |
Larry Hastings | b082731 | 2014-02-09 22:05:19 -0800 | [diff] [blame] | 695 | name, |
| 696 | NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 697 | Py_XDECREF(name); |
| 698 | return ret; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 699 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 700 | |
Thomas Heller | 085358a | 2002-07-29 14:27:41 +0000 | [diff] [blame] | 701 | PyObject *PyErr_SetExcFromWindowsErr(PyObject *exc, int ierr) |
| 702 | { |
Larry Hastings | 8f9f0f1 | 2014-02-10 03:43:57 -0800 | [diff] [blame] | 703 | return PyErr_SetExcFromWindowsErrWithFilename(exc, ierr, NULL); |
Thomas Heller | 085358a | 2002-07-29 14:27:41 +0000 | [diff] [blame] | 704 | } |
| 705 | |
Guido van Rossum | 795e189 | 2000-02-17 15:19:15 +0000 | [diff] [blame] | 706 | PyObject *PyErr_SetFromWindowsErr(int ierr) |
| 707 | { |
Larry Hastings | 8f9f0f1 | 2014-02-10 03:43:57 -0800 | [diff] [blame] | 708 | return PyErr_SetExcFromWindowsErrWithFilename(PyExc_OSError, |
| 709 | ierr, NULL); |
Larry Hastings | b082731 | 2014-02-09 22:05:19 -0800 | [diff] [blame] | 710 | } |
| 711 | |
Thomas Heller | 085358a | 2002-07-29 14:27:41 +0000 | [diff] [blame] | 712 | PyObject *PyErr_SetFromWindowsErrWithFilename( |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 713 | int ierr, |
| 714 | const char *filename) |
Thomas Heller | 085358a | 2002-07-29 14:27:41 +0000 | [diff] [blame] | 715 | { |
Victor Stinner | 92be939 | 2010-12-28 00:28:21 +0000 | [diff] [blame] | 716 | PyObject *name = filename ? PyUnicode_DecodeFSDefault(filename) : NULL; |
Larry Hastings | b082731 | 2014-02-09 22:05:19 -0800 | [diff] [blame] | 717 | PyObject *result = PyErr_SetExcFromWindowsErrWithFilenameObjects( |
Andrew Svetlov | 2606a6f | 2012-12-19 14:33:35 +0200 | [diff] [blame] | 718 | PyExc_OSError, |
Larry Hastings | b082731 | 2014-02-09 22:05:19 -0800 | [diff] [blame] | 719 | ierr, name, NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 720 | Py_XDECREF(name); |
| 721 | return result; |
Guido van Rossum | 795e189 | 2000-02-17 15:19:15 +0000 | [diff] [blame] | 722 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 723 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 724 | PyObject *PyErr_SetFromWindowsErrWithUnicodeFilename( |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 725 | int ierr, |
| 726 | const Py_UNICODE *filename) |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 727 | { |
Serhiy Storchaka | 460bd0d | 2016-11-20 12:16:46 +0200 | [diff] [blame] | 728 | PyObject *name = filename ? PyUnicode_FromWideChar(filename, -1) : NULL; |
Larry Hastings | b082731 | 2014-02-09 22:05:19 -0800 | [diff] [blame] | 729 | PyObject *result = PyErr_SetExcFromWindowsErrWithFilenameObjects( |
Andrew Svetlov | 2606a6f | 2012-12-19 14:33:35 +0200 | [diff] [blame] | 730 | PyExc_OSError, |
Larry Hastings | b082731 | 2014-02-09 22:05:19 -0800 | [diff] [blame] | 731 | ierr, name, NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 732 | Py_XDECREF(name); |
| 733 | return result; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 734 | } |
Guido van Rossum | 795e189 | 2000-02-17 15:19:15 +0000 | [diff] [blame] | 735 | #endif /* MS_WINDOWS */ |
| 736 | |
Brett Cannon | 79ec55e | 2012-04-12 20:24:54 -0400 | [diff] [blame] | 737 | PyObject * |
Eric Snow | 46f97b8 | 2016-09-07 16:56:15 -0700 | [diff] [blame] | 738 | PyErr_SetImportErrorSubclass(PyObject *exception, PyObject *msg, |
| 739 | PyObject *name, PyObject *path) |
Brett Cannon | 79ec55e | 2012-04-12 20:24:54 -0400 | [diff] [blame] | 740 | { |
Eric Snow | 46f97b8 | 2016-09-07 16:56:15 -0700 | [diff] [blame] | 741 | int issubclass; |
Victor Stinner | f45a561 | 2016-08-23 00:04:41 +0200 | [diff] [blame] | 742 | PyObject *kwargs, *error; |
Brian Curtin | 09b86d1 | 2012-04-17 16:57:09 -0500 | [diff] [blame] | 743 | |
Eric Snow | 46f97b8 | 2016-09-07 16:56:15 -0700 | [diff] [blame] | 744 | issubclass = PyObject_IsSubclass(exception, PyExc_ImportError); |
| 745 | if (issubclass < 0) { |
| 746 | return NULL; |
| 747 | } |
| 748 | else if (!issubclass) { |
| 749 | PyErr_SetString(PyExc_TypeError, "expected a subclass of ImportError"); |
Brian Curtin | 94c001b | 2012-04-18 08:30:51 -0500 | [diff] [blame] | 750 | return NULL; |
Victor Stinner | f45a561 | 2016-08-23 00:04:41 +0200 | [diff] [blame] | 751 | } |
Brian Curtin | 09b86d1 | 2012-04-17 16:57:09 -0500 | [diff] [blame] | 752 | |
Eric Snow | 46f97b8 | 2016-09-07 16:56:15 -0700 | [diff] [blame] | 753 | if (msg == NULL) { |
| 754 | PyErr_SetString(PyExc_TypeError, "expected a message argument"); |
Brian Curtin | 09b86d1 | 2012-04-17 16:57:09 -0500 | [diff] [blame] | 755 | return NULL; |
Benjamin Peterson | da20cd2 | 2012-04-18 10:48:00 -0400 | [diff] [blame] | 756 | } |
Brian Curtin | 09b86d1 | 2012-04-17 16:57:09 -0500 | [diff] [blame] | 757 | |
Brian Curtin | 94c001b | 2012-04-18 08:30:51 -0500 | [diff] [blame] | 758 | if (name == NULL) { |
Brian Curtin | 09b86d1 | 2012-04-17 16:57:09 -0500 | [diff] [blame] | 759 | name = Py_None; |
Brian Curtin | 94c001b | 2012-04-18 08:30:51 -0500 | [diff] [blame] | 760 | } |
Brian Curtin | 94c001b | 2012-04-18 08:30:51 -0500 | [diff] [blame] | 761 | if (path == NULL) { |
Brian Curtin | 09b86d1 | 2012-04-17 16:57:09 -0500 | [diff] [blame] | 762 | path = Py_None; |
Brian Curtin | 94c001b | 2012-04-18 08:30:51 -0500 | [diff] [blame] | 763 | } |
Brian Curtin | 09b86d1 | 2012-04-17 16:57:09 -0500 | [diff] [blame] | 764 | |
Eric Snow | 46f97b8 | 2016-09-07 16:56:15 -0700 | [diff] [blame] | 765 | kwargs = PyDict_New(); |
| 766 | if (kwargs == NULL) { |
| 767 | return NULL; |
| 768 | } |
Victor Stinner | f45a561 | 2016-08-23 00:04:41 +0200 | [diff] [blame] | 769 | if (PyDict_SetItemString(kwargs, "name", name) < 0) { |
Berker Peksag | ec766d3 | 2016-05-01 09:06:36 +0300 | [diff] [blame] | 770 | goto done; |
Victor Stinner | f45a561 | 2016-08-23 00:04:41 +0200 | [diff] [blame] | 771 | } |
| 772 | if (PyDict_SetItemString(kwargs, "path", path) < 0) { |
Berker Peksag | ec766d3 | 2016-05-01 09:06:36 +0300 | [diff] [blame] | 773 | goto done; |
Victor Stinner | f45a561 | 2016-08-23 00:04:41 +0200 | [diff] [blame] | 774 | } |
Brett Cannon | 79ec55e | 2012-04-12 20:24:54 -0400 | [diff] [blame] | 775 | |
Eric Snow | 46f97b8 | 2016-09-07 16:56:15 -0700 | [diff] [blame] | 776 | error = _PyObject_FastCallDict(exception, &msg, 1, kwargs); |
Benjamin Peterson | da20cd2 | 2012-04-18 10:48:00 -0400 | [diff] [blame] | 777 | if (error != NULL) { |
| 778 | PyErr_SetObject((PyObject *)Py_TYPE(error), error); |
Brian Curtin | 09b86d1 | 2012-04-17 16:57:09 -0500 | [diff] [blame] | 779 | Py_DECREF(error); |
Brett Cannon | 79ec55e | 2012-04-12 20:24:54 -0400 | [diff] [blame] | 780 | } |
| 781 | |
Berker Peksag | ec766d3 | 2016-05-01 09:06:36 +0300 | [diff] [blame] | 782 | done: |
Brett Cannon | 79ec55e | 2012-04-12 20:24:54 -0400 | [diff] [blame] | 783 | Py_DECREF(kwargs); |
Brian Curtin | 09b86d1 | 2012-04-17 16:57:09 -0500 | [diff] [blame] | 784 | return NULL; |
Brett Cannon | 79ec55e | 2012-04-12 20:24:54 -0400 | [diff] [blame] | 785 | } |
| 786 | |
Eric Snow | 46f97b8 | 2016-09-07 16:56:15 -0700 | [diff] [blame] | 787 | PyObject * |
| 788 | PyErr_SetImportError(PyObject *msg, PyObject *name, PyObject *path) |
| 789 | { |
| 790 | return PyErr_SetImportErrorSubclass(PyExc_ImportError, msg, name, path); |
| 791 | } |
| 792 | |
Guido van Rossum | 683a072 | 1990-10-21 22:09:12 +0000 | [diff] [blame] | 793 | void |
Neal Norwitz | b382b84 | 2007-08-24 20:00:37 +0000 | [diff] [blame] | 794 | _PyErr_BadInternalCall(const char *filename, int lineno) |
Fred Drake | 6d63adf | 2000-08-24 22:38:39 +0000 | [diff] [blame] | 795 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 796 | PyErr_Format(PyExc_SystemError, |
| 797 | "%s:%d: bad argument to internal function", |
| 798 | filename, lineno); |
Fred Drake | 6d63adf | 2000-08-24 22:38:39 +0000 | [diff] [blame] | 799 | } |
| 800 | |
| 801 | /* Remove the preprocessor macro for PyErr_BadInternalCall() so that we can |
| 802 | export the entry point for existing object code: */ |
| 803 | #undef PyErr_BadInternalCall |
| 804 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 805 | PyErr_BadInternalCall(void) |
Guido van Rossum | 683a072 | 1990-10-21 22:09:12 +0000 | [diff] [blame] | 806 | { |
Victor Stinner | fb3a630 | 2013-07-12 00:37:30 +0200 | [diff] [blame] | 807 | assert(0 && "bad argument to internal function"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 808 | PyErr_Format(PyExc_SystemError, |
| 809 | "bad argument to internal function"); |
Guido van Rossum | 683a072 | 1990-10-21 22:09:12 +0000 | [diff] [blame] | 810 | } |
Fred Drake | 6d63adf | 2000-08-24 22:38:39 +0000 | [diff] [blame] | 811 | #define PyErr_BadInternalCall() _PyErr_BadInternalCall(__FILE__, __LINE__) |
| 812 | |
Guido van Rossum | 1548bac | 1997-02-14 17:09:47 +0000 | [diff] [blame] | 813 | |
Guido van Rossum | 1548bac | 1997-02-14 17:09:47 +0000 | [diff] [blame] | 814 | PyObject * |
Antoine Pitrou | 0676a40 | 2014-09-30 21:16:27 +0200 | [diff] [blame] | 815 | PyErr_FormatV(PyObject *exception, const char *format, va_list vargs) |
Guido van Rossum | 1548bac | 1997-02-14 17:09:47 +0000 | [diff] [blame] | 816 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 817 | PyObject* string; |
Guido van Rossum | 1548bac | 1997-02-14 17:09:47 +0000 | [diff] [blame] | 818 | |
Victor Stinner | de821be | 2015-03-24 12:41:23 +0100 | [diff] [blame] | 819 | /* Issue #23571: PyUnicode_FromFormatV() must not be called with an |
| 820 | exception set, it calls arbitrary Python code like PyObject_Repr() */ |
Victor Stinner | ace47d7 | 2013-07-18 01:41:08 +0200 | [diff] [blame] | 821 | PyErr_Clear(); |
Victor Stinner | ace47d7 | 2013-07-18 01:41:08 +0200 | [diff] [blame] | 822 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 823 | string = PyUnicode_FromFormatV(format, vargs); |
Victor Stinner | de821be | 2015-03-24 12:41:23 +0100 | [diff] [blame] | 824 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 825 | PyErr_SetObject(exception, string); |
| 826 | Py_XDECREF(string); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 827 | return NULL; |
Guido van Rossum | 1548bac | 1997-02-14 17:09:47 +0000 | [diff] [blame] | 828 | } |
Guido van Rossum | 7617e05 | 1997-09-16 18:43:50 +0000 | [diff] [blame] | 829 | |
| 830 | |
Antoine Pitrou | 0676a40 | 2014-09-30 21:16:27 +0200 | [diff] [blame] | 831 | PyObject * |
| 832 | PyErr_Format(PyObject *exception, const char *format, ...) |
| 833 | { |
| 834 | va_list vargs; |
| 835 | #ifdef HAVE_STDARG_PROTOTYPES |
| 836 | va_start(vargs, format); |
| 837 | #else |
| 838 | va_start(vargs); |
| 839 | #endif |
| 840 | PyErr_FormatV(exception, format, vargs); |
| 841 | va_end(vargs); |
| 842 | return NULL; |
| 843 | } |
| 844 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 845 | |
Guido van Rossum | 7617e05 | 1997-09-16 18:43:50 +0000 | [diff] [blame] | 846 | PyObject * |
Neal Norwitz | b382b84 | 2007-08-24 20:00:37 +0000 | [diff] [blame] | 847 | PyErr_NewException(const char *name, PyObject *base, PyObject *dict) |
Guido van Rossum | 7617e05 | 1997-09-16 18:43:50 +0000 | [diff] [blame] | 848 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 849 | const char *dot; |
| 850 | PyObject *modulename = NULL; |
| 851 | PyObject *classname = NULL; |
| 852 | PyObject *mydict = NULL; |
| 853 | PyObject *bases = NULL; |
| 854 | PyObject *result = NULL; |
| 855 | dot = strrchr(name, '.'); |
| 856 | if (dot == NULL) { |
| 857 | PyErr_SetString(PyExc_SystemError, |
| 858 | "PyErr_NewException: name must be module.class"); |
| 859 | return NULL; |
| 860 | } |
| 861 | if (base == NULL) |
| 862 | base = PyExc_Exception; |
| 863 | if (dict == NULL) { |
| 864 | dict = mydict = PyDict_New(); |
| 865 | if (dict == NULL) |
| 866 | goto failure; |
| 867 | } |
| 868 | if (PyDict_GetItemString(dict, "__module__") == NULL) { |
| 869 | modulename = PyUnicode_FromStringAndSize(name, |
| 870 | (Py_ssize_t)(dot-name)); |
| 871 | if (modulename == NULL) |
| 872 | goto failure; |
| 873 | if (PyDict_SetItemString(dict, "__module__", modulename) != 0) |
| 874 | goto failure; |
| 875 | } |
| 876 | if (PyTuple_Check(base)) { |
| 877 | bases = base; |
| 878 | /* INCREF as we create a new ref in the else branch */ |
| 879 | Py_INCREF(bases); |
| 880 | } else { |
| 881 | bases = PyTuple_Pack(1, base); |
| 882 | if (bases == NULL) |
| 883 | goto failure; |
| 884 | } |
Florent Xicluna | aa6c1d2 | 2011-12-12 18:54:29 +0100 | [diff] [blame] | 885 | /* Create a real class. */ |
Victor Stinner | 7eeb5b5 | 2010-06-07 19:57:46 +0000 | [diff] [blame] | 886 | result = PyObject_CallFunction((PyObject *)&PyType_Type, "sOO", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 887 | dot+1, bases, dict); |
Guido van Rossum | 7617e05 | 1997-09-16 18:43:50 +0000 | [diff] [blame] | 888 | failure: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 889 | Py_XDECREF(bases); |
| 890 | Py_XDECREF(mydict); |
| 891 | Py_XDECREF(classname); |
| 892 | Py_XDECREF(modulename); |
| 893 | return result; |
Guido van Rossum | 7617e05 | 1997-09-16 18:43:50 +0000 | [diff] [blame] | 894 | } |
Jeremy Hylton | b709df3 | 2000-09-01 02:47:25 +0000 | [diff] [blame] | 895 | |
Georg Brandl | 1e28a27 | 2009-12-28 08:41:01 +0000 | [diff] [blame] | 896 | |
| 897 | /* Create an exception with docstring */ |
| 898 | PyObject * |
| 899 | PyErr_NewExceptionWithDoc(const char *name, const char *doc, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 900 | PyObject *base, PyObject *dict) |
Georg Brandl | 1e28a27 | 2009-12-28 08:41:01 +0000 | [diff] [blame] | 901 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 902 | int result; |
| 903 | PyObject *ret = NULL; |
| 904 | PyObject *mydict = NULL; /* points to the dict only if we create it */ |
| 905 | PyObject *docobj; |
Georg Brandl | 1e28a27 | 2009-12-28 08:41:01 +0000 | [diff] [blame] | 906 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 907 | if (dict == NULL) { |
| 908 | dict = mydict = PyDict_New(); |
| 909 | if (dict == NULL) { |
| 910 | return NULL; |
| 911 | } |
| 912 | } |
Georg Brandl | 1e28a27 | 2009-12-28 08:41:01 +0000 | [diff] [blame] | 913 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 914 | if (doc != NULL) { |
| 915 | docobj = PyUnicode_FromString(doc); |
| 916 | if (docobj == NULL) |
| 917 | goto failure; |
| 918 | result = PyDict_SetItemString(dict, "__doc__", docobj); |
| 919 | Py_DECREF(docobj); |
| 920 | if (result < 0) |
| 921 | goto failure; |
| 922 | } |
Georg Brandl | 1e28a27 | 2009-12-28 08:41:01 +0000 | [diff] [blame] | 923 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 924 | ret = PyErr_NewException(name, base, dict); |
Georg Brandl | 1e28a27 | 2009-12-28 08:41:01 +0000 | [diff] [blame] | 925 | failure: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 926 | Py_XDECREF(mydict); |
| 927 | return ret; |
Georg Brandl | 1e28a27 | 2009-12-28 08:41:01 +0000 | [diff] [blame] | 928 | } |
| 929 | |
| 930 | |
Jeremy Hylton | b709df3 | 2000-09-01 02:47:25 +0000 | [diff] [blame] | 931 | /* Call when an exception has occurred but there is no way for Python |
| 932 | to handle it. Examples: exception in __del__ or during GC. */ |
| 933 | void |
| 934 | PyErr_WriteUnraisable(PyObject *obj) |
| 935 | { |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 936 | _Py_IDENTIFIER(__module__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 937 | PyObject *f, *t, *v, *tb; |
Victor Stinner | c82bfd8 | 2013-08-26 14:04:10 +0200 | [diff] [blame] | 938 | PyObject *moduleName = NULL; |
| 939 | char* className; |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 940 | |
Victor Stinner | c82bfd8 | 2013-08-26 14:04:10 +0200 | [diff] [blame] | 941 | PyErr_Fetch(&t, &v, &tb); |
| 942 | |
Victor Stinner | bd303c1 | 2013-11-07 23:07:29 +0100 | [diff] [blame] | 943 | f = _PySys_GetObjectId(&PyId_stderr); |
Victor Stinner | c82bfd8 | 2013-08-26 14:04:10 +0200 | [diff] [blame] | 944 | if (f == NULL || f == Py_None) |
| 945 | goto done; |
| 946 | |
| 947 | if (obj) { |
| 948 | if (PyFile_WriteString("Exception ignored in: ", f) < 0) |
| 949 | goto done; |
Martin Panter | 3263f68 | 2016-02-28 03:16:11 +0000 | [diff] [blame] | 950 | if (PyFile_WriteObject(obj, f, 0) < 0) { |
| 951 | PyErr_Clear(); |
| 952 | if (PyFile_WriteString("<object repr() failed>", f) < 0) { |
| 953 | goto done; |
| 954 | } |
| 955 | } |
Victor Stinner | c82bfd8 | 2013-08-26 14:04:10 +0200 | [diff] [blame] | 956 | if (PyFile_WriteString("\n", f) < 0) |
| 957 | goto done; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 958 | } |
Victor Stinner | c82bfd8 | 2013-08-26 14:04:10 +0200 | [diff] [blame] | 959 | |
| 960 | if (PyTraceBack_Print(tb, f) < 0) |
| 961 | goto done; |
| 962 | |
| 963 | if (!t) |
| 964 | goto done; |
| 965 | |
| 966 | assert(PyExceptionClass_Check(t)); |
| 967 | className = PyExceptionClass_Name(t); |
| 968 | if (className != NULL) { |
| 969 | char *dot = strrchr(className, '.'); |
| 970 | if (dot != NULL) |
| 971 | className = dot+1; |
| 972 | } |
| 973 | |
| 974 | moduleName = _PyObject_GetAttrId(t, &PyId___module__); |
| 975 | if (moduleName == NULL) { |
| 976 | PyErr_Clear(); |
| 977 | if (PyFile_WriteString("<unknown>", f) < 0) |
| 978 | goto done; |
| 979 | } |
| 980 | else { |
Serhiy Storchaka | f5894dd | 2016-11-16 15:40:39 +0200 | [diff] [blame] | 981 | if (!_PyUnicode_EqualToASCIIId(moduleName, &PyId_builtins)) { |
Victor Stinner | c82bfd8 | 2013-08-26 14:04:10 +0200 | [diff] [blame] | 982 | if (PyFile_WriteObject(moduleName, f, Py_PRINT_RAW) < 0) |
| 983 | goto done; |
| 984 | if (PyFile_WriteString(".", f) < 0) |
| 985 | goto done; |
| 986 | } |
| 987 | } |
| 988 | if (className == NULL) { |
| 989 | if (PyFile_WriteString("<unknown>", f) < 0) |
| 990 | goto done; |
| 991 | } |
| 992 | else { |
| 993 | if (PyFile_WriteString(className, f) < 0) |
| 994 | goto done; |
| 995 | } |
| 996 | |
| 997 | if (v && v != Py_None) { |
| 998 | if (PyFile_WriteString(": ", f) < 0) |
| 999 | goto done; |
Martin Panter | 3263f68 | 2016-02-28 03:16:11 +0000 | [diff] [blame] | 1000 | if (PyFile_WriteObject(v, f, Py_PRINT_RAW) < 0) { |
| 1001 | PyErr_Clear(); |
| 1002 | if (PyFile_WriteString("<exception str() failed>", f) < 0) { |
| 1003 | goto done; |
| 1004 | } |
| 1005 | } |
Victor Stinner | c82bfd8 | 2013-08-26 14:04:10 +0200 | [diff] [blame] | 1006 | } |
| 1007 | if (PyFile_WriteString("\n", f) < 0) |
| 1008 | goto done; |
| 1009 | |
| 1010 | done: |
| 1011 | Py_XDECREF(moduleName); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1012 | Py_XDECREF(t); |
| 1013 | Py_XDECREF(v); |
| 1014 | Py_XDECREF(tb); |
Victor Stinner | c82bfd8 | 2013-08-26 14:04:10 +0200 | [diff] [blame] | 1015 | PyErr_Clear(); /* Just in case */ |
Jeremy Hylton | b709df3 | 2000-09-01 02:47:25 +0000 | [diff] [blame] | 1016 | } |
Guido van Rossum | cfd42b5 | 2000-12-15 21:58:52 +0000 | [diff] [blame] | 1017 | |
Armin Rigo | 092381a | 2003-10-25 14:29:27 +0000 | [diff] [blame] | 1018 | extern PyObject *PyModule_GetWarningsModule(void); |
Guido van Rossum | cfd42b5 | 2000-12-15 21:58:52 +0000 | [diff] [blame] | 1019 | |
Guido van Rossum | 2fd4565 | 2001-02-28 21:46:24 +0000 | [diff] [blame] | 1020 | |
Benjamin Peterson | 2c53971 | 2010-09-20 22:42:10 +0000 | [diff] [blame] | 1021 | void |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 1022 | PyErr_SyntaxLocation(const char *filename, int lineno) |
| 1023 | { |
Benjamin Peterson | 2c53971 | 2010-09-20 22:42:10 +0000 | [diff] [blame] | 1024 | PyErr_SyntaxLocationEx(filename, lineno, -1); |
| 1025 | } |
| 1026 | |
| 1027 | |
Martin v. Löwis | cfeb3b6 | 2002-03-03 21:30:27 +0000 | [diff] [blame] | 1028 | /* Set file and line information for the current exception. |
| 1029 | If the exception is not a SyntaxError, also sets additional attributes |
| 1030 | to make printing of exceptions believe it is a syntax error. */ |
Guido van Rossum | 2fd4565 | 2001-02-28 21:46:24 +0000 | [diff] [blame] | 1031 | |
Jeremy Hylton | ad3d3f2 | 2001-02-28 17:47:12 +0000 | [diff] [blame] | 1032 | void |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 1033 | PyErr_SyntaxLocationObject(PyObject *filename, int lineno, int col_offset) |
Jeremy Hylton | ad3d3f2 | 2001-02-28 17:47:12 +0000 | [diff] [blame] | 1034 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1035 | PyObject *exc, *v, *tb, *tmp; |
Martin v. Löwis | 1c67dd9 | 2011-10-14 15:16:45 +0200 | [diff] [blame] | 1036 | _Py_IDENTIFIER(filename); |
| 1037 | _Py_IDENTIFIER(lineno); |
| 1038 | _Py_IDENTIFIER(msg); |
| 1039 | _Py_IDENTIFIER(offset); |
| 1040 | _Py_IDENTIFIER(print_file_and_line); |
| 1041 | _Py_IDENTIFIER(text); |
Jeremy Hylton | ad3d3f2 | 2001-02-28 17:47:12 +0000 | [diff] [blame] | 1042 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1043 | /* add attributes for the line number and filename for the error */ |
| 1044 | PyErr_Fetch(&exc, &v, &tb); |
| 1045 | PyErr_NormalizeException(&exc, &v, &tb); |
| 1046 | /* XXX check that it is, indeed, a syntax error. It might not |
| 1047 | * be, though. */ |
| 1048 | tmp = PyLong_FromLong(lineno); |
| 1049 | if (tmp == NULL) |
| 1050 | PyErr_Clear(); |
| 1051 | else { |
Martin v. Löwis | 1c67dd9 | 2011-10-14 15:16:45 +0200 | [diff] [blame] | 1052 | if (_PyObject_SetAttrId(v, &PyId_lineno, tmp)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1053 | PyErr_Clear(); |
| 1054 | Py_DECREF(tmp); |
| 1055 | } |
Serhiy Storchaka | 8b58339 | 2016-12-11 14:39:01 +0200 | [diff] [blame] | 1056 | tmp = NULL; |
Benjamin Peterson | 2c53971 | 2010-09-20 22:42:10 +0000 | [diff] [blame] | 1057 | if (col_offset >= 0) { |
| 1058 | tmp = PyLong_FromLong(col_offset); |
| 1059 | if (tmp == NULL) |
| 1060 | PyErr_Clear(); |
Benjamin Peterson | 2c53971 | 2010-09-20 22:42:10 +0000 | [diff] [blame] | 1061 | } |
Serhiy Storchaka | 8b58339 | 2016-12-11 14:39:01 +0200 | [diff] [blame] | 1062 | if (_PyObject_SetAttrId(v, &PyId_offset, tmp ? tmp : Py_None)) |
| 1063 | PyErr_Clear(); |
| 1064 | Py_XDECREF(tmp); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1065 | if (filename != NULL) { |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 1066 | if (_PyObject_SetAttrId(v, &PyId_filename, filename)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1067 | PyErr_Clear(); |
Jeremy Hylton | ad3d3f2 | 2001-02-28 17:47:12 +0000 | [diff] [blame] | 1068 | |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 1069 | tmp = PyErr_ProgramTextObject(filename, lineno); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1070 | if (tmp) { |
Martin v. Löwis | 1c67dd9 | 2011-10-14 15:16:45 +0200 | [diff] [blame] | 1071 | if (_PyObject_SetAttrId(v, &PyId_text, tmp)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1072 | PyErr_Clear(); |
| 1073 | Py_DECREF(tmp); |
| 1074 | } |
| 1075 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1076 | if (exc != PyExc_SyntaxError) { |
Martin v. Löwis | 1c67dd9 | 2011-10-14 15:16:45 +0200 | [diff] [blame] | 1077 | if (!_PyObject_HasAttrId(v, &PyId_msg)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1078 | tmp = PyObject_Str(v); |
| 1079 | if (tmp) { |
Martin v. Löwis | 1c67dd9 | 2011-10-14 15:16:45 +0200 | [diff] [blame] | 1080 | if (_PyObject_SetAttrId(v, &PyId_msg, tmp)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1081 | PyErr_Clear(); |
| 1082 | Py_DECREF(tmp); |
| 1083 | } else { |
| 1084 | PyErr_Clear(); |
| 1085 | } |
| 1086 | } |
Martin v. Löwis | 1c67dd9 | 2011-10-14 15:16:45 +0200 | [diff] [blame] | 1087 | if (!_PyObject_HasAttrId(v, &PyId_print_file_and_line)) { |
| 1088 | if (_PyObject_SetAttrId(v, &PyId_print_file_and_line, |
| 1089 | Py_None)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1090 | PyErr_Clear(); |
| 1091 | } |
| 1092 | } |
| 1093 | PyErr_Restore(exc, v, tb); |
Jeremy Hylton | ad3d3f2 | 2001-02-28 17:47:12 +0000 | [diff] [blame] | 1094 | } |
| 1095 | |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 1096 | void |
| 1097 | PyErr_SyntaxLocationEx(const char *filename, int lineno, int col_offset) |
| 1098 | { |
| 1099 | PyObject *fileobj; |
| 1100 | if (filename != NULL) { |
| 1101 | fileobj = PyUnicode_DecodeFSDefault(filename); |
| 1102 | if (fileobj == NULL) |
| 1103 | PyErr_Clear(); |
| 1104 | } |
| 1105 | else |
| 1106 | fileobj = NULL; |
| 1107 | PyErr_SyntaxLocationObject(fileobj, lineno, col_offset); |
| 1108 | Py_XDECREF(fileobj); |
| 1109 | } |
| 1110 | |
Guido van Rossum | ebe8f8a | 2007-10-10 18:53:36 +0000 | [diff] [blame] | 1111 | /* Attempt to load the line of text that the exception refers to. If it |
| 1112 | fails, it will return NULL but will not set an exception. |
Jeremy Hylton | ad3d3f2 | 2001-02-28 17:47:12 +0000 | [diff] [blame] | 1113 | |
| 1114 | XXX The functionality of this function is quite similar to the |
Guido van Rossum | ebe8f8a | 2007-10-10 18:53:36 +0000 | [diff] [blame] | 1115 | functionality in tb_displayline() in traceback.c. */ |
Jeremy Hylton | ad3d3f2 | 2001-02-28 17:47:12 +0000 | [diff] [blame] | 1116 | |
Antoine Pitrou | 409b538 | 2013-10-12 22:41:17 +0200 | [diff] [blame] | 1117 | static PyObject * |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 1118 | err_programtext(FILE *fp, int lineno) |
Jeremy Hylton | ad3d3f2 | 2001-02-28 17:47:12 +0000 | [diff] [blame] | 1119 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1120 | int i; |
| 1121 | char linebuf[1000]; |
Jeremy Hylton | ad3d3f2 | 2001-02-28 17:47:12 +0000 | [diff] [blame] | 1122 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1123 | if (fp == NULL) |
| 1124 | return NULL; |
| 1125 | for (i = 0; i < lineno; i++) { |
| 1126 | char *pLastChar = &linebuf[sizeof(linebuf) - 2]; |
| 1127 | do { |
| 1128 | *pLastChar = '\0'; |
| 1129 | if (Py_UniversalNewlineFgets(linebuf, sizeof linebuf, |
| 1130 | fp, NULL) == NULL) |
| 1131 | break; |
| 1132 | /* fgets read *something*; if it didn't get as |
| 1133 | far as pLastChar, it must have found a newline |
| 1134 | or hit the end of the file; if pLastChar is \n, |
| 1135 | it obviously found a newline; else we haven't |
| 1136 | yet seen a newline, so must continue */ |
| 1137 | } while (*pLastChar != '\0' && *pLastChar != '\n'); |
| 1138 | } |
| 1139 | fclose(fp); |
| 1140 | if (i == lineno) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1141 | PyObject *res; |
Martin Panter | ca3263c | 2016-12-11 00:18:36 +0000 | [diff] [blame] | 1142 | res = PyUnicode_FromString(linebuf); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1143 | if (res == NULL) |
| 1144 | PyErr_Clear(); |
| 1145 | return res; |
| 1146 | } |
| 1147 | return NULL; |
Jeremy Hylton | ad3d3f2 | 2001-02-28 17:47:12 +0000 | [diff] [blame] | 1148 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1149 | |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 1150 | PyObject * |
| 1151 | PyErr_ProgramText(const char *filename, int lineno) |
| 1152 | { |
| 1153 | FILE *fp; |
| 1154 | if (filename == NULL || *filename == '\0' || lineno <= 0) |
| 1155 | return NULL; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 1156 | fp = _Py_fopen(filename, "r" PY_STDIOTEXTMODE); |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 1157 | return err_programtext(fp, lineno); |
| 1158 | } |
| 1159 | |
| 1160 | PyObject * |
| 1161 | PyErr_ProgramTextObject(PyObject *filename, int lineno) |
| 1162 | { |
| 1163 | FILE *fp; |
| 1164 | if (filename == NULL || lineno <= 0) |
| 1165 | return NULL; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 1166 | fp = _Py_fopen_obj(filename, "r" PY_STDIOTEXTMODE); |
Victor Stinner | e42ccd2 | 2015-03-18 01:39:23 +0100 | [diff] [blame] | 1167 | if (fp == NULL) { |
| 1168 | PyErr_Clear(); |
| 1169 | return NULL; |
| 1170 | } |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 1171 | return err_programtext(fp, lineno); |
| 1172 | } |
| 1173 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1174 | #ifdef __cplusplus |
| 1175 | } |
| 1176 | #endif |