blob: 2799cffc93ba2fa42692f86cb6321a9409f05ef4 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum373c8691997-04-29 18:22:47 +00002/* Error handling */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003
Guido van Rossum373c8691997-04-29 18:22:47 +00004#include "Python.h"
Guido van Rossumf22120a1990-12-20 23:05:40 +00005
Jack Jansen8fd2d941994-12-14 12:54:54 +00006#ifdef macintosh
Tim Petersdbd9ba62000-07-09 03:09:57 +00007extern char *PyMac_StrError(int);
Jack Jansen5ef86d51995-01-19 12:16:44 +00008#undef strerror
Guido van Rossume9fbc091995-02-18 14:52:19 +00009#define strerror PyMac_StrError
10#endif /* macintosh */
11
Guido van Rossum53e8d441995-03-09 12:11:31 +000012#ifndef __STDC__
Guido van Rossum7844e381997-04-11 20:44:04 +000013#ifndef MS_WINDOWS
Tim Petersdbd9ba62000-07-09 03:09:57 +000014extern char *strerror(int);
Guido van Rossum53e8d441995-03-09 12:11:31 +000015#endif
Guido van Rossum7844e381997-04-11 20:44:04 +000016#endif
Guido van Rossumf5401bd1990-11-02 17:50:28 +000017
Guido van Rossum743007d1999-04-21 15:27:31 +000018#ifdef MS_WIN32
19#include "windows.h"
20#include "winbase.h"
21#endif
22
Jeremy Hyltonb69a27e2000-09-01 03:49:47 +000023#include <ctype.h>
24
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000025void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000026PyErr_Restore(PyObject *type, PyObject *value, PyObject *traceback)
Guido van Rossum1ae940a1995-01-02 19:04:15 +000027{
Guido van Rossum885553e1998-12-21 18:33:30 +000028 PyThreadState *tstate = PyThreadState_GET();
Guido van Rossuma027efa1997-05-05 20:56:21 +000029 PyObject *oldtype, *oldvalue, *oldtraceback;
Guido van Rossum1ae940a1995-01-02 19:04:15 +000030
Guido van Rossuma027efa1997-05-05 20:56:21 +000031 if (traceback != NULL && !PyTraceBack_Check(traceback)) {
32 /* XXX Should never happen -- fatal error instead? */
33 Py_DECREF(traceback);
34 traceback = NULL;
35 }
36
37 /* Save these in locals to safeguard against recursive
38 invocation through Py_XDECREF */
39 oldtype = tstate->curexc_type;
40 oldvalue = tstate->curexc_value;
41 oldtraceback = tstate->curexc_traceback;
42
43 tstate->curexc_type = type;
44 tstate->curexc_value = value;
45 tstate->curexc_traceback = traceback;
46
47 Py_XDECREF(oldtype);
48 Py_XDECREF(oldvalue);
49 Py_XDECREF(oldtraceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +000050}
51
52void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000053PyErr_SetObject(PyObject *exception, PyObject *value)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000054{
Guido van Rossum373c8691997-04-29 18:22:47 +000055 Py_XINCREF(exception);
56 Py_XINCREF(value);
57 PyErr_Restore(exception, value, (PyObject *)NULL);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000058}
59
60void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000061PyErr_SetNone(PyObject *exception)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000062{
Guido van Rossum373c8691997-04-29 18:22:47 +000063 PyErr_SetObject(exception, (PyObject *)NULL);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000064}
65
66void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000067PyErr_SetString(PyObject *exception, const char *string)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000068{
Guido van Rossum373c8691997-04-29 18:22:47 +000069 PyObject *value = PyString_FromString(string);
70 PyErr_SetObject(exception, value);
71 Py_XDECREF(value);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000072}
73
Guido van Rossum3a241811994-08-29 12:14:12 +000074
Guido van Rossum373c8691997-04-29 18:22:47 +000075PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000076PyErr_Occurred(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000077{
Tim Peters024da352001-05-30 06:09:50 +000078 PyThreadState *tstate = PyThreadState_GET();
Guido van Rossuma027efa1997-05-05 20:56:21 +000079
80 return tstate->curexc_type;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000081}
82
Barry Warsawc0dc92a1997-08-22 21:22:58 +000083
84int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000085PyErr_GivenExceptionMatches(PyObject *err, PyObject *exc)
Barry Warsawc0dc92a1997-08-22 21:22:58 +000086{
Barry Warsawfa5c3152000-05-02 19:27:51 +000087 if (err == NULL || exc == NULL) {
88 /* maybe caused by "import exceptions" that failed early on */
89 return 0;
90 }
Barry Warsawc0dc92a1997-08-22 21:22:58 +000091 if (PyTuple_Check(exc)) {
92 int i, n;
93 n = PyTuple_Size(exc);
94 for (i = 0; i < n; i++) {
95 /* Test recursively */
96 if (PyErr_GivenExceptionMatches(
97 err, PyTuple_GET_ITEM(exc, i)))
98 {
99 return 1;
100 }
101 }
102 return 0;
103 }
104 /* err might be an instance, so check its class. */
105 if (PyInstance_Check(err))
106 err = (PyObject*)((PyInstanceObject*)err)->in_class;
107
108 if (PyClass_Check(err) && PyClass_Check(exc))
109 return PyClass_IsSubclass(err, exc);
110
111 return err == exc;
112}
Guido van Rossum743007d1999-04-21 15:27:31 +0000113
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000114
115int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000116PyErr_ExceptionMatches(PyObject *exc)
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000117{
118 return PyErr_GivenExceptionMatches(PyErr_Occurred(), exc);
119}
120
121
122/* Used in many places to normalize a raised exception, including in
123 eval_code2(), do_raise(), and PyErr_Print()
124*/
125void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000126PyErr_NormalizeException(PyObject **exc, PyObject **val, PyObject **tb)
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000127{
128 PyObject *type = *exc;
129 PyObject *value = *val;
130 PyObject *inclass = NULL;
Jeremy Hyltone2e2c9f2001-09-26 19:58:38 +0000131 PyObject *initial_tb = NULL;
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000132
Guido van Rossumed473a42000-08-07 19:18:27 +0000133 if (type == NULL) {
134 /* This is a bug. Should never happen. Don't dump core. */
135 PyErr_SetString(PyExc_SystemError,
136 "PyErr_NormalizeException() called without exception");
137 }
138
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000139 /* If PyErr_SetNone() was used, the value will have been actually
140 set to NULL.
141 */
142 if (!value) {
143 value = Py_None;
144 Py_INCREF(value);
145 }
146
147 if (PyInstance_Check(value))
148 inclass = (PyObject*)((PyInstanceObject*)value)->in_class;
149
150 /* Normalize the exception so that if the type is a class, the
151 value will be an instance.
152 */
153 if (PyClass_Check(type)) {
154 /* if the value was not an instance, or is not an instance
155 whose class is (or is derived from) type, then use the
156 value as an argument to instantiation of the type
157 class.
158 */
159 if (!inclass || !PyClass_IsSubclass(inclass, type)) {
160 PyObject *args, *res;
161
162 if (value == Py_None)
163 args = Py_BuildValue("()");
164 else if (PyTuple_Check(value)) {
165 Py_INCREF(value);
166 args = value;
167 }
168 else
169 args = Py_BuildValue("(O)", value);
170
171 if (args == NULL)
172 goto finally;
173 res = PyEval_CallObject(type, args);
174 Py_DECREF(args);
175 if (res == NULL)
176 goto finally;
177 Py_DECREF(value);
178 value = res;
179 }
Barry Warsaw3a749931997-09-30 15:00:18 +0000180 /* if the class of the instance doesn't exactly match the
181 class of the type, believe the instance
182 */
183 else if (inclass != type) {
184 Py_DECREF(type);
185 type = inclass;
186 Py_INCREF(type);
187 }
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000188 }
189 *exc = type;
190 *val = value;
191 return;
192finally:
Guido van Rossum19b55f21997-12-09 14:11:39 +0000193 Py_DECREF(type);
194 Py_DECREF(value);
Jeremy Hyltone2e2c9f2001-09-26 19:58:38 +0000195 /* If the new exception doesn't set a traceback and the old
196 exception had a traceback, use the old traceback for the
197 new exception. It's better than nothing.
198 */
199 initial_tb = *tb;
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000200 PyErr_Fetch(exc, val, tb);
Jeremy Hyltone2e2c9f2001-09-26 19:58:38 +0000201 if (initial_tb != NULL) {
202 if (*tb == NULL)
203 *tb = initial_tb;
204 else
205 Py_DECREF(initial_tb);
206 }
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000207 /* normalize recursively */
208 PyErr_NormalizeException(exc, val, tb);
209}
210
211
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000212void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000213PyErr_Fetch(PyObject **p_type, PyObject **p_value, PyObject **p_traceback)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000214{
Guido van Rossuma027efa1997-05-05 20:56:21 +0000215 PyThreadState *tstate = PyThreadState_Get();
216
217 *p_type = tstate->curexc_type;
218 *p_value = tstate->curexc_value;
219 *p_traceback = tstate->curexc_traceback;
220
221 tstate->curexc_type = NULL;
222 tstate->curexc_value = NULL;
223 tstate->curexc_traceback = NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000224}
225
226void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000227PyErr_Clear(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000228{
Guido van Rossuma027efa1997-05-05 20:56:21 +0000229 PyErr_Restore(NULL, NULL, NULL);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000230}
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000231
232/* Convenience functions to set a type error exception and return 0 */
233
234int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000235PyErr_BadArgument(void)
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000236{
Guido van Rossuma027efa1997-05-05 20:56:21 +0000237 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +0000238 "bad argument type for built-in operation");
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000239 return 0;
240}
241
Guido van Rossum373c8691997-04-29 18:22:47 +0000242PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000243PyErr_NoMemory(void)
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000244{
Vladimir Marangozov0888ff12000-08-18 18:01:06 +0000245 if (PyErr_ExceptionMatches(PyExc_MemoryError))
246 /* already current */
247 return NULL;
248
Barry Warsaw2d8adff1997-08-29 21:54:35 +0000249 /* raise the pre-allocated instance if it still exists */
250 if (PyExc_MemoryErrorInst)
251 PyErr_SetObject(PyExc_MemoryError, PyExc_MemoryErrorInst);
252 else
253 /* this will probably fail since there's no memory and hee,
254 hee, we have to instantiate this class
255 */
256 PyErr_SetNone(PyExc_MemoryError);
257
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000258 return NULL;
259}
260
Guido van Rossum373c8691997-04-29 18:22:47 +0000261PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000262PyErr_SetFromErrnoWithFilename(PyObject *exc, char *filename)
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000263{
Guido van Rossum373c8691997-04-29 18:22:47 +0000264 PyObject *v;
Guido van Rossume0e59821998-10-14 20:38:13 +0000265 char *s;
Guido van Rossum3a241811994-08-29 12:14:12 +0000266 int i = errno;
Guido van Rossum795e1892000-02-17 15:19:15 +0000267#ifdef MS_WIN32
268 char *s_buf = NULL;
269#endif
Guido van Rossume9fbc091995-02-18 14:52:19 +0000270#ifdef EINTR
Guido van Rossum373c8691997-04-29 18:22:47 +0000271 if (i == EINTR && PyErr_CheckSignals())
Guido van Rossum5063bab1991-10-20 20:14:56 +0000272 return NULL;
Guido van Rossume9fbc091995-02-18 14:52:19 +0000273#endif
Guido van Rossume0e59821998-10-14 20:38:13 +0000274 if (i == 0)
275 s = "Error"; /* Sometimes errno didn't get set */
Barry Warsaw97d95151998-07-23 16:05:56 +0000276 else
Guido van Rossum743007d1999-04-21 15:27:31 +0000277#ifndef MS_WIN32
Guido van Rossume0e59821998-10-14 20:38:13 +0000278 s = strerror(i);
Guido van Rossum743007d1999-04-21 15:27:31 +0000279#else
280 {
Guido van Rossum795e1892000-02-17 15:19:15 +0000281 /* Note that the Win32 errors do not lineup with the
282 errno error. So if the error is in the MSVC error
283 table, we use it, otherwise we assume it really _is_
284 a Win32 error code
285 */
Guido van Rossum584b16a2000-02-21 16:50:31 +0000286 if (i > 0 && i < _sys_nerr) {
Guido van Rossum795e1892000-02-17 15:19:15 +0000287 s = _sys_errlist[i];
288 }
289 else {
290 int len = FormatMessage(
291 FORMAT_MESSAGE_ALLOCATE_BUFFER |
292 FORMAT_MESSAGE_FROM_SYSTEM |
293 FORMAT_MESSAGE_IGNORE_INSERTS,
294 NULL, /* no message source */
295 i,
296 MAKELANGID(LANG_NEUTRAL,
297 SUBLANG_DEFAULT),
298 /* Default language */
299 (LPTSTR) &s_buf,
300 0, /* size not used */
301 NULL); /* no args */
302 s = s_buf;
303 /* remove trailing cr/lf and dots */
304 while (len > 0 && (s[len-1] <= ' ' || s[len-1] == '.'))
305 s[--len] = '\0';
306 }
Guido van Rossum743007d1999-04-21 15:27:31 +0000307 }
308#endif
Barry Warsawfa5c3152000-05-02 19:27:51 +0000309 if (filename != NULL)
Guido van Rossume0e59821998-10-14 20:38:13 +0000310 v = Py_BuildValue("(iss)", i, s, filename);
311 else
312 v = Py_BuildValue("(is)", i, s);
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000313 if (v != NULL) {
Guido van Rossum373c8691997-04-29 18:22:47 +0000314 PyErr_SetObject(exc, v);
315 Py_DECREF(v);
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000316 }
Guido van Rossum743007d1999-04-21 15:27:31 +0000317#ifdef MS_WIN32
Guido van Rossum795e1892000-02-17 15:19:15 +0000318 LocalFree(s_buf);
Guido van Rossum743007d1999-04-21 15:27:31 +0000319#endif
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000320 return NULL;
321}
Guido van Rossum743007d1999-04-21 15:27:31 +0000322
Barry Warsaw97d95151998-07-23 16:05:56 +0000323
324PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000325PyErr_SetFromErrno(PyObject *exc)
Barry Warsaw97d95151998-07-23 16:05:56 +0000326{
327 return PyErr_SetFromErrnoWithFilename(exc, NULL);
328}
Guido van Rossum683a0721990-10-21 22:09:12 +0000329
Guido van Rossum795e1892000-02-17 15:19:15 +0000330#ifdef MS_WINDOWS
331/* Windows specific error code handling */
332PyObject *PyErr_SetFromWindowsErrWithFilename(
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000333 int ierr,
Guido van Rossum795e1892000-02-17 15:19:15 +0000334 const char *filename)
335{
336 int len;
337 char *s;
338 PyObject *v;
339 DWORD err = (DWORD)ierr;
340 if (err==0) err = GetLastError();
341 len = FormatMessage(
342 /* Error API error */
343 FORMAT_MESSAGE_ALLOCATE_BUFFER |
344 FORMAT_MESSAGE_FROM_SYSTEM |
345 FORMAT_MESSAGE_IGNORE_INSERTS,
346 NULL, /* no message source */
347 err,
348 MAKELANGID(LANG_NEUTRAL,
349 SUBLANG_DEFAULT), /* Default language */
350 (LPTSTR) &s,
351 0, /* size not used */
352 NULL); /* no args */
353 /* remove trailing cr/lf and dots */
354 while (len > 0 && (s[len-1] <= ' ' || s[len-1] == '.'))
355 s[--len] = '\0';
Barry Warsawfa5c3152000-05-02 19:27:51 +0000356 if (filename != NULL)
Guido van Rossum795e1892000-02-17 15:19:15 +0000357 v = Py_BuildValue("(iss)", err, s, filename);
358 else
359 v = Py_BuildValue("(is)", err, s);
360 if (v != NULL) {
Guido van Rossum0b556702000-03-02 13:55:01 +0000361 PyErr_SetObject(PyExc_WindowsError, v);
Guido van Rossum795e1892000-02-17 15:19:15 +0000362 Py_DECREF(v);
363 }
364 LocalFree(s);
365 return NULL;
366}
367
368PyObject *PyErr_SetFromWindowsErr(int ierr)
369{
370 return PyErr_SetFromWindowsErrWithFilename(ierr, NULL);
Guido van Rossum795e1892000-02-17 15:19:15 +0000371}
372#endif /* MS_WINDOWS */
373
Guido van Rossum683a0721990-10-21 22:09:12 +0000374void
Fred Drake6d63adf2000-08-24 22:38:39 +0000375_PyErr_BadInternalCall(char *filename, int lineno)
376{
377 PyErr_Format(PyExc_SystemError,
378 "%s:%d: bad argument to internal function",
379 filename, lineno);
380}
381
382/* Remove the preprocessor macro for PyErr_BadInternalCall() so that we can
383 export the entry point for existing object code: */
384#undef PyErr_BadInternalCall
385void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000386PyErr_BadInternalCall(void)
Guido van Rossum683a0721990-10-21 22:09:12 +0000387{
Fred Drake6d63adf2000-08-24 22:38:39 +0000388 PyErr_Format(PyExc_SystemError,
389 "bad argument to internal function");
Guido van Rossum683a0721990-10-21 22:09:12 +0000390}
Fred Drake6d63adf2000-08-24 22:38:39 +0000391#define PyErr_BadInternalCall() _PyErr_BadInternalCall(__FILE__, __LINE__)
392
Guido van Rossum1548bac1997-02-14 17:09:47 +0000393
394
Guido van Rossum1548bac1997-02-14 17:09:47 +0000395PyObject *
396PyErr_Format(PyObject *exception, const char *format, ...)
Guido van Rossum1548bac1997-02-14 17:09:47 +0000397{
398 va_list vargs;
Jeremy Hyltonb69a27e2000-09-01 03:49:47 +0000399 PyObject* string;
Guido van Rossum1548bac1997-02-14 17:09:47 +0000400
Jeremy Hyltonb69a27e2000-09-01 03:49:47 +0000401#ifdef HAVE_STDARG_PROTOTYPES
Guido van Rossum1548bac1997-02-14 17:09:47 +0000402 va_start(vargs, format);
Jeremy Hyltonb69a27e2000-09-01 03:49:47 +0000403#else
404 va_start(vargs);
405#endif
Guido van Rossum1548bac1997-02-14 17:09:47 +0000406
Barry Warsaw876c8cb2001-08-24 18:35:23 +0000407 string = PyString_FromFormatV(format, vargs);
Jeremy Hyltonb69a27e2000-09-01 03:49:47 +0000408 PyErr_SetObject(exception, string);
409 Py_XDECREF(string);
Tim Petersc15c4f12001-10-02 21:32:07 +0000410 va_end(vargs);
Guido van Rossum1548bac1997-02-14 17:09:47 +0000411 return NULL;
412}
Guido van Rossum7617e051997-09-16 18:43:50 +0000413
414
415PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000416PyErr_NewException(char *name, PyObject *base, PyObject *dict)
Guido van Rossum7617e051997-09-16 18:43:50 +0000417{
Guido van Rossum2ac650f1997-10-03 19:50:55 +0000418 char *dot;
419 PyObject *modulename = NULL;
420 PyObject *classname = NULL;
421 PyObject *mydict = NULL;
422 PyObject *bases = NULL;
Guido van Rossum7617e051997-09-16 18:43:50 +0000423 PyObject *result = NULL;
Guido van Rossum2ac650f1997-10-03 19:50:55 +0000424 dot = strrchr(name, '.');
425 if (dot == NULL) {
426 PyErr_SetString(PyExc_SystemError,
427 "PyErr_NewException: name must be module.class");
Guido van Rossum7617e051997-09-16 18:43:50 +0000428 return NULL;
Guido van Rossum7617e051997-09-16 18:43:50 +0000429 }
430 if (base == NULL)
431 base = PyExc_Exception;
Guido van Rossum2ac650f1997-10-03 19:50:55 +0000432 if (!PyClass_Check(base)) {
433 /* Must be using string-based standard exceptions (-X) */
434 return PyString_FromString(name);
435 }
436 if (dict == NULL) {
437 dict = mydict = PyDict_New();
438 if (dict == NULL)
439 goto failure;
440 }
441 if (PyDict_GetItemString(dict, "__module__") == NULL) {
442 modulename = PyString_FromStringAndSize(name, (int)(dot-name));
443 if (modulename == NULL)
444 goto failure;
445 if (PyDict_SetItemString(dict, "__module__", modulename) != 0)
446 goto failure;
447 }
448 classname = PyString_FromString(dot+1);
449 if (classname == NULL)
Guido van Rossum7617e051997-09-16 18:43:50 +0000450 goto failure;
Guido van Rossum2ac650f1997-10-03 19:50:55 +0000451 bases = Py_BuildValue("(O)", base);
452 if (bases == NULL)
453 goto failure;
454 result = PyClass_New(bases, dict, classname);
Guido van Rossum7617e051997-09-16 18:43:50 +0000455 failure:
Guido van Rossum2ac650f1997-10-03 19:50:55 +0000456 Py_XDECREF(bases);
457 Py_XDECREF(mydict);
458 Py_XDECREF(classname);
459 Py_XDECREF(modulename);
Guido van Rossum7617e051997-09-16 18:43:50 +0000460 return result;
461}
Jeremy Hyltonb709df32000-09-01 02:47:25 +0000462
463/* Call when an exception has occurred but there is no way for Python
464 to handle it. Examples: exception in __del__ or during GC. */
465void
466PyErr_WriteUnraisable(PyObject *obj)
467{
468 PyObject *f, *t, *v, *tb;
469 PyErr_Fetch(&t, &v, &tb);
470 f = PySys_GetObject("stderr");
471 if (f != NULL) {
472 PyFile_WriteString("Exception ", f);
473 if (t) {
474 PyFile_WriteObject(t, f, Py_PRINT_RAW);
475 if (v && v != Py_None) {
476 PyFile_WriteString(": ", f);
477 PyFile_WriteObject(v, f, 0);
478 }
479 }
480 PyFile_WriteString(" in ", f);
481 PyFile_WriteObject(obj, f, 0);
482 PyFile_WriteString(" ignored\n", f);
483 PyErr_Clear(); /* Just in case */
484 }
485 Py_XDECREF(t);
486 Py_XDECREF(v);
487 Py_XDECREF(tb);
488}
Guido van Rossumcfd42b52000-12-15 21:58:52 +0000489
490
491/* Function to issue a warning message; may raise an exception. */
492int
493PyErr_Warn(PyObject *category, char *message)
494{
495 PyObject *mod, *dict, *func = NULL;
496
497 mod = PyImport_ImportModule("warnings");
498 if (mod != NULL) {
499 dict = PyModule_GetDict(mod);
500 func = PyDict_GetItemString(dict, "warn");
501 Py_DECREF(mod);
502 }
503 if (func == NULL) {
504 PySys_WriteStderr("warning: %s\n", message);
505 return 0;
506 }
507 else {
508 PyObject *args, *res;
509
510 if (category == NULL)
511 category = PyExc_RuntimeWarning;
512 args = Py_BuildValue("(sO)", message, category);
513 if (args == NULL)
514 return -1;
515 res = PyEval_CallObject(func, args);
516 Py_DECREF(args);
517 if (res == NULL)
518 return -1;
519 Py_DECREF(res);
520 return 0;
521 }
522}
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000523
Guido van Rossum2fd45652001-02-28 21:46:24 +0000524
525/* Warning with explicit origin */
526int
527PyErr_WarnExplicit(PyObject *category, char *message,
528 char *filename, int lineno,
529 char *module, PyObject *registry)
530{
531 PyObject *mod, *dict, *func = NULL;
532
533 mod = PyImport_ImportModule("warnings");
534 if (mod != NULL) {
535 dict = PyModule_GetDict(mod);
536 func = PyDict_GetItemString(dict, "warn_explicit");
537 Py_DECREF(mod);
538 }
539 if (func == NULL) {
540 PySys_WriteStderr("warning: %s\n", message);
541 return 0;
542 }
543 else {
544 PyObject *args, *res;
545
546 if (category == NULL)
547 category = PyExc_RuntimeWarning;
548 if (registry == NULL)
549 registry = Py_None;
550 args = Py_BuildValue("(sOsizO)", message, category,
551 filename, lineno, module, registry);
552 if (args == NULL)
553 return -1;
554 res = PyEval_CallObject(func, args);
555 Py_DECREF(args);
556 if (res == NULL)
557 return -1;
558 Py_DECREF(res);
559 return 0;
560 }
561}
562
563
564/* XXX There's a comment missing here */
565
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000566void
567PyErr_SyntaxLocation(char *filename, int lineno)
568{
569 PyObject *exc, *v, *tb, *tmp;
570
571 /* add attributes for the line number and filename for the error */
572 PyErr_Fetch(&exc, &v, &tb);
573 PyErr_NormalizeException(&exc, &v, &tb);
574 /* XXX check that it is, indeed, a syntax error */
575 tmp = PyInt_FromLong(lineno);
576 if (tmp == NULL)
577 PyErr_Clear();
578 else {
579 if (PyObject_SetAttrString(v, "lineno", tmp))
580 PyErr_Clear();
581 Py_DECREF(tmp);
582 }
583 if (filename != NULL) {
584 tmp = PyString_FromString(filename);
585 if (tmp == NULL)
586 PyErr_Clear();
587 else {
588 if (PyObject_SetAttrString(v, "filename", tmp))
589 PyErr_Clear();
590 Py_DECREF(tmp);
591 }
592
593 tmp = PyErr_ProgramText(filename, lineno);
594 if (tmp) {
595 PyObject_SetAttrString(v, "text", tmp);
596 Py_DECREF(tmp);
597 }
598 }
599 PyErr_Restore(exc, v, tb);
600}
601
602/* com_fetch_program_text will attempt to load the line of text that
603 the exception refers to. If it fails, it will return NULL but will
604 not set an exception.
605
606 XXX The functionality of this function is quite similar to the
607 functionality in tb_displayline() in traceback.c.
608*/
609
610PyObject *
611PyErr_ProgramText(char *filename, int lineno)
612{
613 FILE *fp;
614 int i;
615 char linebuf[1000];
616
617 if (filename == NULL || lineno <= 0)
618 return NULL;
619 fp = fopen(filename, "r");
620 if (fp == NULL)
621 return NULL;
622 for (i = 0; i < lineno; i++) {
623 char *pLastChar = &linebuf[sizeof(linebuf) - 2];
624 do {
625 *pLastChar = '\0';
626 if (fgets(linebuf, sizeof linebuf, fp) == NULL)
627 break;
628 /* fgets read *something*; if it didn't get as
629 far as pLastChar, it must have found a newline
630 or hit the end of the file; if pLastChar is \n,
631 it obviously found a newline; else we haven't
632 yet seen a newline, so must continue */
633 } while (*pLastChar != '\0' && *pLastChar != '\n');
634 }
635 fclose(fp);
636 if (i == lineno) {
637 char *p = linebuf;
638 while (*p == ' ' || *p == '\t' || *p == '\014')
639 p++;
640 return PyString_FromString(p);
641 }
642 return NULL;
643}