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