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