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