blob: 9a2597b0ce1dabb5d0e74abb9ef0555e273d4cb5 [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
Martin v. Löwis6238d2b2002-06-30 15:26:10 +000018#ifdef MS_WINDOWS
Guido van Rossum743007d1999-04-21 15:27:31 +000019#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 *
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000262PyErr_SetFromErrnoWithFilenameObject(PyObject *exc, PyObject *filenameObject)
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;
Martin v. Löwis3484a182002-03-09 12:07:51 +0000267#ifdef PLAN9
268 char errbuf[ERRMAX];
269#endif
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000270#ifdef MS_WINDOWS
Guido van Rossum795e1892000-02-17 15:19:15 +0000271 char *s_buf = NULL;
272#endif
Guido van Rossume9fbc091995-02-18 14:52:19 +0000273#ifdef EINTR
Guido van Rossum373c8691997-04-29 18:22:47 +0000274 if (i == EINTR && PyErr_CheckSignals())
Guido van Rossum5063bab1991-10-20 20:14:56 +0000275 return NULL;
Guido van Rossume9fbc091995-02-18 14:52:19 +0000276#endif
Martin v. Löwis3484a182002-03-09 12:07:51 +0000277#ifdef PLAN9
278 rerrstr(errbuf, sizeof errbuf);
279 s = errbuf;
280#else
Guido van Rossume0e59821998-10-14 20:38:13 +0000281 if (i == 0)
282 s = "Error"; /* Sometimes errno didn't get set */
Barry Warsaw97d95151998-07-23 16:05:56 +0000283 else
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000284#ifndef MS_WINDOWS
Guido van Rossume0e59821998-10-14 20:38:13 +0000285 s = strerror(i);
Guido van Rossum743007d1999-04-21 15:27:31 +0000286#else
287 {
Guido van Rossum795e1892000-02-17 15:19:15 +0000288 /* Note that the Win32 errors do not lineup with the
289 errno error. So if the error is in the MSVC error
290 table, we use it, otherwise we assume it really _is_
291 a Win32 error code
292 */
Guido van Rossum584b16a2000-02-21 16:50:31 +0000293 if (i > 0 && i < _sys_nerr) {
Guido van Rossum795e1892000-02-17 15:19:15 +0000294 s = _sys_errlist[i];
295 }
296 else {
297 int len = FormatMessage(
298 FORMAT_MESSAGE_ALLOCATE_BUFFER |
299 FORMAT_MESSAGE_FROM_SYSTEM |
300 FORMAT_MESSAGE_IGNORE_INSERTS,
301 NULL, /* no message source */
302 i,
303 MAKELANGID(LANG_NEUTRAL,
304 SUBLANG_DEFAULT),
305 /* Default language */
306 (LPTSTR) &s_buf,
307 0, /* size not used */
308 NULL); /* no args */
309 s = s_buf;
310 /* remove trailing cr/lf and dots */
311 while (len > 0 && (s[len-1] <= ' ' || s[len-1] == '.'))
312 s[--len] = '\0';
313 }
Guido van Rossum743007d1999-04-21 15:27:31 +0000314 }
Martin v. Löwis3484a182002-03-09 12:07:51 +0000315#endif /* Unix/Windows */
316#endif /* PLAN 9*/
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000317 if (filenameObject != NULL)
318 v = Py_BuildValue("(isO)", i, s, filenameObject);
Guido van Rossume0e59821998-10-14 20:38:13 +0000319 else
320 v = Py_BuildValue("(is)", i, s);
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000321 if (v != NULL) {
Guido van Rossum373c8691997-04-29 18:22:47 +0000322 PyErr_SetObject(exc, v);
323 Py_DECREF(v);
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000324 }
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000325#ifdef MS_WINDOWS
Guido van Rossum795e1892000-02-17 15:19:15 +0000326 LocalFree(s_buf);
Guido van Rossum743007d1999-04-21 15:27:31 +0000327#endif
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000328 return NULL;
329}
Guido van Rossum743007d1999-04-21 15:27:31 +0000330
Barry Warsaw97d95151998-07-23 16:05:56 +0000331
332PyObject *
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000333PyErr_SetFromErrnoWithFilename(PyObject *exc, char *filename)
334{
335 PyObject *name = filename ? PyString_FromString(filename) : NULL;
336 PyObject *result = PyErr_SetFromErrnoWithFilenameObject(exc, name);
Mark Hammondda7efaa2002-10-04 00:09:38 +0000337 Py_XDECREF(name);
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000338 return result;
339}
340
341#ifdef Py_WIN_WIDE_FILENAMES
342PyObject *
343PyErr_SetFromErrnoWithUnicodeFilename(PyObject *exc, Py_UNICODE *filename)
344{
345 PyObject *name = filename ?
346 PyUnicode_FromUnicode(filename, wcslen(filename)) :
347 NULL;
348 PyObject *result = PyErr_SetFromErrnoWithFilenameObject(exc, name);
349 Py_XDECREF(name);
350 return result;
351}
352#endif /* Py_WIN_WIDE_FILENAMES */
353
354PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000355PyErr_SetFromErrno(PyObject *exc)
Barry Warsaw97d95151998-07-23 16:05:56 +0000356{
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000357 return PyErr_SetFromErrnoWithFilenameObject(exc, NULL);
Barry Warsaw97d95151998-07-23 16:05:56 +0000358}
Guido van Rossum683a0721990-10-21 22:09:12 +0000359
Guido van Rossum795e1892000-02-17 15:19:15 +0000360#ifdef MS_WINDOWS
361/* Windows specific error code handling */
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000362PyObject *PyErr_SetExcFromWindowsErrWithFilenameObject(
Thomas Heller085358a2002-07-29 14:27:41 +0000363 PyObject *exc,
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000364 int ierr,
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000365 PyObject *filenameObject)
Guido van Rossum795e1892000-02-17 15:19:15 +0000366{
367 int len;
368 char *s;
369 PyObject *v;
370 DWORD err = (DWORD)ierr;
371 if (err==0) err = GetLastError();
372 len = FormatMessage(
373 /* Error API error */
374 FORMAT_MESSAGE_ALLOCATE_BUFFER |
375 FORMAT_MESSAGE_FROM_SYSTEM |
376 FORMAT_MESSAGE_IGNORE_INSERTS,
377 NULL, /* no message source */
378 err,
379 MAKELANGID(LANG_NEUTRAL,
380 SUBLANG_DEFAULT), /* Default language */
381 (LPTSTR) &s,
382 0, /* size not used */
383 NULL); /* no args */
384 /* remove trailing cr/lf and dots */
385 while (len > 0 && (s[len-1] <= ' ' || s[len-1] == '.'))
386 s[--len] = '\0';
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000387 if (filenameObject != NULL)
388 v = Py_BuildValue("(isO)", err, s, filenameObject);
Guido van Rossum795e1892000-02-17 15:19:15 +0000389 else
390 v = Py_BuildValue("(is)", err, s);
391 if (v != NULL) {
Thomas Heller085358a2002-07-29 14:27:41 +0000392 PyErr_SetObject(exc, v);
Guido van Rossum795e1892000-02-17 15:19:15 +0000393 Py_DECREF(v);
394 }
395 LocalFree(s);
396 return NULL;
397}
398
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000399PyObject *PyErr_SetExcFromWindowsErrWithFilename(
400 PyObject *exc,
401 int ierr,
402 const char *filename)
403{
404 PyObject *name = filename ? PyString_FromString(filename) : NULL;
405 PyObject *ret = PyErr_SetExcFromWindowsErrWithFilenameObject(exc,
406 ierr,
407 name);
408 Py_XDECREF(name);
409 return ret;
410}
411
412#ifdef Py_WIN_WIDE_FILENAMES
413PyObject *PyErr_SetExcFromWindowsErrWithUnicodeFilename(
414 PyObject *exc,
415 int ierr,
416 const Py_UNICODE *filename)
417{
418 PyObject *name = filename ?
419 PyUnicode_FromUnicode(filename, wcslen(filename)) :
420 NULL;
421 PyObject *ret = PyErr_SetExcFromWindowsErrWithFilenameObject(exc,
422 ierr,
423 name);
424 Py_XDECREF(name);
425 return ret;
426}
427#endif /* Py_WIN_WIDE_FILENAMES */
428
Thomas Heller085358a2002-07-29 14:27:41 +0000429PyObject *PyErr_SetExcFromWindowsErr(PyObject *exc, int ierr)
430{
431 return PyErr_SetExcFromWindowsErrWithFilename(exc, ierr, NULL);
432}
433
Guido van Rossum795e1892000-02-17 15:19:15 +0000434PyObject *PyErr_SetFromWindowsErr(int ierr)
435{
Thomas Heller085358a2002-07-29 14:27:41 +0000436 return PyErr_SetExcFromWindowsErrWithFilename(PyExc_WindowsError,
437 ierr, NULL);
438}
439PyObject *PyErr_SetFromWindowsErrWithFilename(
440 int ierr,
441 const char *filename)
442{
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000443 PyObject *name = filename ? PyString_FromString(filename) : NULL;
444 PyObject *result = PyErr_SetExcFromWindowsErrWithFilenameObject(
445 PyExc_WindowsError,
446 ierr, name);
Mark Hammondda7efaa2002-10-04 00:09:38 +0000447 Py_XDECREF(name);
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000448 return result;
Guido van Rossum795e1892000-02-17 15:19:15 +0000449}
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000450
451#ifdef Py_WIN_WIDE_FILENAMES
452PyObject *PyErr_SetFromWindowsErrWithUnicodeFilename(
453 int ierr,
454 const Py_UNICODE *filename)
455{
456 PyObject *name = filename ?
457 PyUnicode_FromUnicode(filename, wcslen(filename)) :
458 NULL;
459 PyObject *result = PyErr_SetExcFromWindowsErrWithFilenameObject(
460 PyExc_WindowsError,
461 ierr, name);
Mark Hammondda7efaa2002-10-04 00:09:38 +0000462 Py_XDECREF(name);
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000463 return result;
464}
465#endif /* Py_WIN_WIDE_FILENAMES */
Guido van Rossum795e1892000-02-17 15:19:15 +0000466#endif /* MS_WINDOWS */
467
Guido van Rossum683a0721990-10-21 22:09:12 +0000468void
Fred Drake6d63adf2000-08-24 22:38:39 +0000469_PyErr_BadInternalCall(char *filename, int lineno)
470{
471 PyErr_Format(PyExc_SystemError,
472 "%s:%d: bad argument to internal function",
473 filename, lineno);
474}
475
476/* Remove the preprocessor macro for PyErr_BadInternalCall() so that we can
477 export the entry point for existing object code: */
478#undef PyErr_BadInternalCall
479void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000480PyErr_BadInternalCall(void)
Guido van Rossum683a0721990-10-21 22:09:12 +0000481{
Fred Drake6d63adf2000-08-24 22:38:39 +0000482 PyErr_Format(PyExc_SystemError,
483 "bad argument to internal function");
Guido van Rossum683a0721990-10-21 22:09:12 +0000484}
Fred Drake6d63adf2000-08-24 22:38:39 +0000485#define PyErr_BadInternalCall() _PyErr_BadInternalCall(__FILE__, __LINE__)
486
Guido van Rossum1548bac1997-02-14 17:09:47 +0000487
488
Guido van Rossum1548bac1997-02-14 17:09:47 +0000489PyObject *
490PyErr_Format(PyObject *exception, const char *format, ...)
Guido van Rossum1548bac1997-02-14 17:09:47 +0000491{
492 va_list vargs;
Jeremy Hyltonb69a27e2000-09-01 03:49:47 +0000493 PyObject* string;
Guido van Rossum1548bac1997-02-14 17:09:47 +0000494
Jeremy Hyltonb69a27e2000-09-01 03:49:47 +0000495#ifdef HAVE_STDARG_PROTOTYPES
Guido van Rossum1548bac1997-02-14 17:09:47 +0000496 va_start(vargs, format);
Jeremy Hyltonb69a27e2000-09-01 03:49:47 +0000497#else
498 va_start(vargs);
499#endif
Guido van Rossum1548bac1997-02-14 17:09:47 +0000500
Barry Warsaw876c8cb2001-08-24 18:35:23 +0000501 string = PyString_FromFormatV(format, vargs);
Jeremy Hyltonb69a27e2000-09-01 03:49:47 +0000502 PyErr_SetObject(exception, string);
503 Py_XDECREF(string);
Tim Petersc15c4f12001-10-02 21:32:07 +0000504 va_end(vargs);
Guido van Rossum1548bac1997-02-14 17:09:47 +0000505 return NULL;
506}
Guido van Rossum7617e051997-09-16 18:43:50 +0000507
508
509PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000510PyErr_NewException(char *name, PyObject *base, PyObject *dict)
Guido van Rossum7617e051997-09-16 18:43:50 +0000511{
Guido van Rossum2ac650f1997-10-03 19:50:55 +0000512 char *dot;
513 PyObject *modulename = NULL;
514 PyObject *classname = NULL;
515 PyObject *mydict = NULL;
516 PyObject *bases = NULL;
Guido van Rossum7617e051997-09-16 18:43:50 +0000517 PyObject *result = NULL;
Guido van Rossum2ac650f1997-10-03 19:50:55 +0000518 dot = strrchr(name, '.');
519 if (dot == NULL) {
520 PyErr_SetString(PyExc_SystemError,
521 "PyErr_NewException: name must be module.class");
Guido van Rossum7617e051997-09-16 18:43:50 +0000522 return NULL;
Guido van Rossum7617e051997-09-16 18:43:50 +0000523 }
524 if (base == NULL)
525 base = PyExc_Exception;
Guido van Rossum2ac650f1997-10-03 19:50:55 +0000526 if (!PyClass_Check(base)) {
527 /* Must be using string-based standard exceptions (-X) */
528 return PyString_FromString(name);
529 }
530 if (dict == NULL) {
531 dict = mydict = PyDict_New();
532 if (dict == NULL)
533 goto failure;
534 }
535 if (PyDict_GetItemString(dict, "__module__") == NULL) {
536 modulename = PyString_FromStringAndSize(name, (int)(dot-name));
537 if (modulename == NULL)
538 goto failure;
539 if (PyDict_SetItemString(dict, "__module__", modulename) != 0)
540 goto failure;
541 }
542 classname = PyString_FromString(dot+1);
543 if (classname == NULL)
Guido van Rossum7617e051997-09-16 18:43:50 +0000544 goto failure;
Guido van Rossum2ac650f1997-10-03 19:50:55 +0000545 bases = Py_BuildValue("(O)", base);
546 if (bases == NULL)
547 goto failure;
548 result = PyClass_New(bases, dict, classname);
Guido van Rossum7617e051997-09-16 18:43:50 +0000549 failure:
Guido van Rossum2ac650f1997-10-03 19:50:55 +0000550 Py_XDECREF(bases);
551 Py_XDECREF(mydict);
552 Py_XDECREF(classname);
553 Py_XDECREF(modulename);
Guido van Rossum7617e051997-09-16 18:43:50 +0000554 return result;
555}
Jeremy Hyltonb709df32000-09-01 02:47:25 +0000556
557/* Call when an exception has occurred but there is no way for Python
558 to handle it. Examples: exception in __del__ or during GC. */
559void
560PyErr_WriteUnraisable(PyObject *obj)
561{
562 PyObject *f, *t, *v, *tb;
563 PyErr_Fetch(&t, &v, &tb);
564 f = PySys_GetObject("stderr");
565 if (f != NULL) {
566 PyFile_WriteString("Exception ", f);
567 if (t) {
568 PyFile_WriteObject(t, f, Py_PRINT_RAW);
569 if (v && v != Py_None) {
570 PyFile_WriteString(": ", f);
571 PyFile_WriteObject(v, f, 0);
572 }
573 }
574 PyFile_WriteString(" in ", f);
575 PyFile_WriteObject(obj, f, 0);
576 PyFile_WriteString(" ignored\n", f);
577 PyErr_Clear(); /* Just in case */
578 }
579 Py_XDECREF(t);
580 Py_XDECREF(v);
581 Py_XDECREF(tb);
582}
Guido van Rossumcfd42b52000-12-15 21:58:52 +0000583
584
585/* Function to issue a warning message; may raise an exception. */
586int
587PyErr_Warn(PyObject *category, char *message)
588{
589 PyObject *mod, *dict, *func = NULL;
590
591 mod = PyImport_ImportModule("warnings");
592 if (mod != NULL) {
593 dict = PyModule_GetDict(mod);
594 func = PyDict_GetItemString(dict, "warn");
595 Py_DECREF(mod);
596 }
597 if (func == NULL) {
598 PySys_WriteStderr("warning: %s\n", message);
599 return 0;
600 }
601 else {
602 PyObject *args, *res;
603
604 if (category == NULL)
605 category = PyExc_RuntimeWarning;
606 args = Py_BuildValue("(sO)", message, category);
607 if (args == NULL)
608 return -1;
609 res = PyEval_CallObject(func, args);
610 Py_DECREF(args);
611 if (res == NULL)
612 return -1;
613 Py_DECREF(res);
614 return 0;
615 }
616}
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000617
Guido van Rossum2fd45652001-02-28 21:46:24 +0000618
619/* Warning with explicit origin */
620int
621PyErr_WarnExplicit(PyObject *category, char *message,
622 char *filename, int lineno,
623 char *module, PyObject *registry)
624{
625 PyObject *mod, *dict, *func = NULL;
626
627 mod = PyImport_ImportModule("warnings");
628 if (mod != NULL) {
629 dict = PyModule_GetDict(mod);
630 func = PyDict_GetItemString(dict, "warn_explicit");
631 Py_DECREF(mod);
632 }
633 if (func == NULL) {
634 PySys_WriteStderr("warning: %s\n", message);
635 return 0;
636 }
637 else {
638 PyObject *args, *res;
639
640 if (category == NULL)
641 category = PyExc_RuntimeWarning;
642 if (registry == NULL)
643 registry = Py_None;
644 args = Py_BuildValue("(sOsizO)", message, category,
645 filename, lineno, module, registry);
646 if (args == NULL)
647 return -1;
648 res = PyEval_CallObject(func, args);
649 Py_DECREF(args);
650 if (res == NULL)
651 return -1;
652 Py_DECREF(res);
653 return 0;
654 }
655}
656
657
Martin v. Löwiscfeb3b62002-03-03 21:30:27 +0000658/* Set file and line information for the current exception.
659 If the exception is not a SyntaxError, also sets additional attributes
660 to make printing of exceptions believe it is a syntax error. */
Guido van Rossum2fd45652001-02-28 21:46:24 +0000661
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000662void
663PyErr_SyntaxLocation(char *filename, int lineno)
664{
665 PyObject *exc, *v, *tb, *tmp;
666
667 /* add attributes for the line number and filename for the error */
668 PyErr_Fetch(&exc, &v, &tb);
669 PyErr_NormalizeException(&exc, &v, &tb);
670 /* XXX check that it is, indeed, a syntax error */
671 tmp = PyInt_FromLong(lineno);
672 if (tmp == NULL)
673 PyErr_Clear();
674 else {
675 if (PyObject_SetAttrString(v, "lineno", tmp))
676 PyErr_Clear();
677 Py_DECREF(tmp);
678 }
679 if (filename != NULL) {
680 tmp = PyString_FromString(filename);
681 if (tmp == NULL)
682 PyErr_Clear();
683 else {
684 if (PyObject_SetAttrString(v, "filename", tmp))
685 PyErr_Clear();
686 Py_DECREF(tmp);
687 }
688
689 tmp = PyErr_ProgramText(filename, lineno);
690 if (tmp) {
691 PyObject_SetAttrString(v, "text", tmp);
692 Py_DECREF(tmp);
693 }
694 }
Martin v. Löwiscfeb3b62002-03-03 21:30:27 +0000695 if (PyObject_SetAttrString(v, "offset", Py_None)) {
696 PyErr_Clear();
697 }
698 if (exc != PyExc_SyntaxError) {
699 if (!PyObject_HasAttrString(v, "msg")) {
700 tmp = PyObject_Str(v);
701 if (tmp) {
702 if (PyObject_SetAttrString(v, "msg", tmp))
703 PyErr_Clear();
704 Py_DECREF(tmp);
705 } else {
706 PyErr_Clear();
707 }
708 }
709 if (!PyObject_HasAttrString(v, "print_file_and_line")) {
710 if (PyObject_SetAttrString(v, "print_file_and_line",
711 Py_None))
712 PyErr_Clear();
713 }
714 }
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000715 PyErr_Restore(exc, v, tb);
716}
717
718/* com_fetch_program_text will attempt to load the line of text that
719 the exception refers to. If it fails, it will return NULL but will
720 not set an exception.
721
722 XXX The functionality of this function is quite similar to the
723 functionality in tb_displayline() in traceback.c.
724*/
725
726PyObject *
727PyErr_ProgramText(char *filename, int lineno)
728{
729 FILE *fp;
730 int i;
731 char linebuf[1000];
732
733 if (filename == NULL || lineno <= 0)
734 return NULL;
Jack Jansen7b8c7542002-04-14 20:12:41 +0000735 fp = fopen(filename, "r" PY_STDIOTEXTMODE);
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000736 if (fp == NULL)
737 return NULL;
738 for (i = 0; i < lineno; i++) {
739 char *pLastChar = &linebuf[sizeof(linebuf) - 2];
740 do {
741 *pLastChar = '\0';
Jack Jansen7b8c7542002-04-14 20:12:41 +0000742 if (Py_UniversalNewlineFgets(linebuf, sizeof linebuf, fp, NULL) == NULL)
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000743 break;
744 /* fgets read *something*; if it didn't get as
745 far as pLastChar, it must have found a newline
746 or hit the end of the file; if pLastChar is \n,
747 it obviously found a newline; else we haven't
748 yet seen a newline, so must continue */
749 } while (*pLastChar != '\0' && *pLastChar != '\n');
750 }
751 fclose(fp);
752 if (i == lineno) {
753 char *p = linebuf;
754 while (*p == ' ' || *p == '\t' || *p == '\014')
755 p++;
756 return PyString_FromString(p);
757 }
758 return NULL;
759}