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