blob: a9e4daed9b536e61e7809d78ae77d312d0fa9fd3 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001/***********************************************************
Guido van Rossumfd71b9e2000-06-30 23:50:40 +00002Copyright (c) 2000, BeOpen.com.
3Copyright (c) 1995-2000, Corporation for National Research Initiatives.
4Copyright (c) 1990-1995, Stichting Mathematisch Centrum.
5All rights reserved.
Guido van Rossumf70e43a1991-02-19 12:39:46 +00006
Guido van Rossumfd71b9e2000-06-30 23:50:40 +00007See the file "Misc/COPYRIGHT" for information on usage and
8redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
Guido van Rossumf70e43a1991-02-19 12:39:46 +00009******************************************************************/
10
Guido van Rossum373c8691997-04-29 18:22:47 +000011/* Error handling */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000012
Guido van Rossum373c8691997-04-29 18:22:47 +000013#include "Python.h"
Guido van Rossumf22120a1990-12-20 23:05:40 +000014
Jack Jansen8fd2d941994-12-14 12:54:54 +000015#ifdef macintosh
Tim Petersdbd9ba62000-07-09 03:09:57 +000016extern char *PyMac_StrError(int);
Jack Jansen5ef86d51995-01-19 12:16:44 +000017#undef strerror
Guido van Rossume9fbc091995-02-18 14:52:19 +000018#define strerror PyMac_StrError
19#endif /* macintosh */
20
Guido van Rossum53e8d441995-03-09 12:11:31 +000021#ifndef __STDC__
Guido van Rossum7844e381997-04-11 20:44:04 +000022#ifndef MS_WINDOWS
Tim Petersdbd9ba62000-07-09 03:09:57 +000023extern char *strerror(int);
Guido van Rossum53e8d441995-03-09 12:11:31 +000024#endif
Guido van Rossum7844e381997-04-11 20:44:04 +000025#endif
Guido van Rossumf5401bd1990-11-02 17:50:28 +000026
Guido van Rossum743007d1999-04-21 15:27:31 +000027#ifdef MS_WIN32
28#include "windows.h"
29#include "winbase.h"
30#endif
31
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000032void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000033PyErr_Restore(PyObject *type, PyObject *value, PyObject *traceback)
Guido van Rossum1ae940a1995-01-02 19:04:15 +000034{
Guido van Rossum885553e1998-12-21 18:33:30 +000035 PyThreadState *tstate = PyThreadState_GET();
Guido van Rossuma027efa1997-05-05 20:56:21 +000036 PyObject *oldtype, *oldvalue, *oldtraceback;
Guido van Rossum1ae940a1995-01-02 19:04:15 +000037
Guido van Rossuma027efa1997-05-05 20:56:21 +000038 if (traceback != NULL && !PyTraceBack_Check(traceback)) {
39 /* XXX Should never happen -- fatal error instead? */
40 Py_DECREF(traceback);
41 traceback = NULL;
42 }
43
44 /* Save these in locals to safeguard against recursive
45 invocation through Py_XDECREF */
46 oldtype = tstate->curexc_type;
47 oldvalue = tstate->curexc_value;
48 oldtraceback = tstate->curexc_traceback;
49
50 tstate->curexc_type = type;
51 tstate->curexc_value = value;
52 tstate->curexc_traceback = traceback;
53
54 Py_XDECREF(oldtype);
55 Py_XDECREF(oldvalue);
56 Py_XDECREF(oldtraceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +000057}
58
59void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000060PyErr_SetObject(PyObject *exception, PyObject *value)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000061{
Guido van Rossum373c8691997-04-29 18:22:47 +000062 Py_XINCREF(exception);
63 Py_XINCREF(value);
64 PyErr_Restore(exception, value, (PyObject *)NULL);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000065}
66
67void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000068PyErr_SetNone(PyObject *exception)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000069{
Guido van Rossum373c8691997-04-29 18:22:47 +000070 PyErr_SetObject(exception, (PyObject *)NULL);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000071}
72
73void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000074PyErr_SetString(PyObject *exception, const char *string)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000075{
Guido van Rossum373c8691997-04-29 18:22:47 +000076 PyObject *value = PyString_FromString(string);
77 PyErr_SetObject(exception, value);
78 Py_XDECREF(value);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000079}
80
Guido van Rossum3a241811994-08-29 12:14:12 +000081
Guido van Rossum373c8691997-04-29 18:22:47 +000082PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000083PyErr_Occurred(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000084{
Guido van Rossuma027efa1997-05-05 20:56:21 +000085 PyThreadState *tstate = PyThreadState_Get();
86
87 return tstate->curexc_type;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000088}
89
Barry Warsawc0dc92a1997-08-22 21:22:58 +000090
91int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000092PyErr_GivenExceptionMatches(PyObject *err, PyObject *exc)
Barry Warsawc0dc92a1997-08-22 21:22:58 +000093{
Barry Warsawfa5c3152000-05-02 19:27:51 +000094 if (err == NULL || exc == NULL) {
95 /* maybe caused by "import exceptions" that failed early on */
96 return 0;
97 }
Barry Warsawc0dc92a1997-08-22 21:22:58 +000098 if (PyTuple_Check(exc)) {
99 int i, n;
100 n = PyTuple_Size(exc);
101 for (i = 0; i < n; i++) {
102 /* Test recursively */
103 if (PyErr_GivenExceptionMatches(
104 err, PyTuple_GET_ITEM(exc, i)))
105 {
106 return 1;
107 }
108 }
109 return 0;
110 }
111 /* err might be an instance, so check its class. */
112 if (PyInstance_Check(err))
113 err = (PyObject*)((PyInstanceObject*)err)->in_class;
114
115 if (PyClass_Check(err) && PyClass_Check(exc))
116 return PyClass_IsSubclass(err, exc);
117
118 return err == exc;
119}
Guido van Rossum743007d1999-04-21 15:27:31 +0000120
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000121
122int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000123PyErr_ExceptionMatches(PyObject *exc)
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000124{
125 return PyErr_GivenExceptionMatches(PyErr_Occurred(), exc);
126}
127
128
129/* Used in many places to normalize a raised exception, including in
130 eval_code2(), do_raise(), and PyErr_Print()
131*/
132void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000133PyErr_NormalizeException(PyObject **exc, PyObject **val, PyObject **tb)
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000134{
135 PyObject *type = *exc;
136 PyObject *value = *val;
137 PyObject *inclass = NULL;
138
Guido van Rossumed473a42000-08-07 19:18:27 +0000139 if (type == NULL) {
140 /* This is a bug. Should never happen. Don't dump core. */
141 PyErr_SetString(PyExc_SystemError,
142 "PyErr_NormalizeException() called without exception");
143 }
144
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000145 /* If PyErr_SetNone() was used, the value will have been actually
146 set to NULL.
147 */
148 if (!value) {
149 value = Py_None;
150 Py_INCREF(value);
151 }
152
153 if (PyInstance_Check(value))
154 inclass = (PyObject*)((PyInstanceObject*)value)->in_class;
155
156 /* Normalize the exception so that if the type is a class, the
157 value will be an instance.
158 */
159 if (PyClass_Check(type)) {
160 /* if the value was not an instance, or is not an instance
161 whose class is (or is derived from) type, then use the
162 value as an argument to instantiation of the type
163 class.
164 */
165 if (!inclass || !PyClass_IsSubclass(inclass, type)) {
166 PyObject *args, *res;
167
168 if (value == Py_None)
169 args = Py_BuildValue("()");
170 else if (PyTuple_Check(value)) {
171 Py_INCREF(value);
172 args = value;
173 }
174 else
175 args = Py_BuildValue("(O)", value);
176
177 if (args == NULL)
178 goto finally;
179 res = PyEval_CallObject(type, args);
180 Py_DECREF(args);
181 if (res == NULL)
182 goto finally;
183 Py_DECREF(value);
184 value = res;
185 }
Barry Warsaw3a749931997-09-30 15:00:18 +0000186 /* if the class of the instance doesn't exactly match the
187 class of the type, believe the instance
188 */
189 else if (inclass != type) {
190 Py_DECREF(type);
191 type = inclass;
192 Py_INCREF(type);
193 }
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000194 }
195 *exc = type;
196 *val = value;
197 return;
198finally:
Guido van Rossum19b55f21997-12-09 14:11:39 +0000199 Py_DECREF(type);
200 Py_DECREF(value);
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000201 Py_XDECREF(*tb);
202 PyErr_Fetch(exc, val, tb);
203 /* normalize recursively */
204 PyErr_NormalizeException(exc, val, tb);
205}
206
207
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000208void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000209PyErr_Fetch(PyObject **p_type, PyObject **p_value, PyObject **p_traceback)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000210{
Guido van Rossuma027efa1997-05-05 20:56:21 +0000211 PyThreadState *tstate = PyThreadState_Get();
212
213 *p_type = tstate->curexc_type;
214 *p_value = tstate->curexc_value;
215 *p_traceback = tstate->curexc_traceback;
216
217 tstate->curexc_type = NULL;
218 tstate->curexc_value = NULL;
219 tstate->curexc_traceback = NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000220}
221
222void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000223PyErr_Clear(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000224{
Guido van Rossuma027efa1997-05-05 20:56:21 +0000225 PyErr_Restore(NULL, NULL, NULL);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000226}
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000227
228/* Convenience functions to set a type error exception and return 0 */
229
230int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000231PyErr_BadArgument(void)
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000232{
Guido van Rossuma027efa1997-05-05 20:56:21 +0000233 PyErr_SetString(PyExc_TypeError,
234 "illegal argument type for built-in operation");
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000235 return 0;
236}
237
Guido van Rossum373c8691997-04-29 18:22:47 +0000238PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000239PyErr_NoMemory(void)
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000240{
Barry Warsaw2d8adff1997-08-29 21:54:35 +0000241 /* raise the pre-allocated instance if it still exists */
242 if (PyExc_MemoryErrorInst)
243 PyErr_SetObject(PyExc_MemoryError, PyExc_MemoryErrorInst);
244 else
245 /* this will probably fail since there's no memory and hee,
246 hee, we have to instantiate this class
247 */
248 PyErr_SetNone(PyExc_MemoryError);
249
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000250 return NULL;
251}
252
Guido van Rossum373c8691997-04-29 18:22:47 +0000253PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000254PyErr_SetFromErrnoWithFilename(PyObject *exc, char *filename)
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000255{
Guido van Rossum373c8691997-04-29 18:22:47 +0000256 PyObject *v;
Guido van Rossume0e59821998-10-14 20:38:13 +0000257 char *s;
Guido van Rossum3a241811994-08-29 12:14:12 +0000258 int i = errno;
Guido van Rossum795e1892000-02-17 15:19:15 +0000259#ifdef MS_WIN32
260 char *s_buf = NULL;
261#endif
Guido van Rossume9fbc091995-02-18 14:52:19 +0000262#ifdef EINTR
Guido van Rossum373c8691997-04-29 18:22:47 +0000263 if (i == EINTR && PyErr_CheckSignals())
Guido van Rossum5063bab1991-10-20 20:14:56 +0000264 return NULL;
Guido van Rossume9fbc091995-02-18 14:52:19 +0000265#endif
Guido van Rossume0e59821998-10-14 20:38:13 +0000266 if (i == 0)
267 s = "Error"; /* Sometimes errno didn't get set */
Barry Warsaw97d95151998-07-23 16:05:56 +0000268 else
Guido van Rossum743007d1999-04-21 15:27:31 +0000269#ifndef MS_WIN32
Guido van Rossume0e59821998-10-14 20:38:13 +0000270 s = strerror(i);
Guido van Rossum743007d1999-04-21 15:27:31 +0000271#else
272 {
Guido van Rossum795e1892000-02-17 15:19:15 +0000273 /* Note that the Win32 errors do not lineup with the
274 errno error. So if the error is in the MSVC error
275 table, we use it, otherwise we assume it really _is_
276 a Win32 error code
277 */
Guido van Rossum584b16a2000-02-21 16:50:31 +0000278 if (i > 0 && i < _sys_nerr) {
Guido van Rossum795e1892000-02-17 15:19:15 +0000279 s = _sys_errlist[i];
280 }
281 else {
282 int len = FormatMessage(
283 FORMAT_MESSAGE_ALLOCATE_BUFFER |
284 FORMAT_MESSAGE_FROM_SYSTEM |
285 FORMAT_MESSAGE_IGNORE_INSERTS,
286 NULL, /* no message source */
287 i,
288 MAKELANGID(LANG_NEUTRAL,
289 SUBLANG_DEFAULT),
290 /* Default language */
291 (LPTSTR) &s_buf,
292 0, /* size not used */
293 NULL); /* no args */
294 s = s_buf;
295 /* remove trailing cr/lf and dots */
296 while (len > 0 && (s[len-1] <= ' ' || s[len-1] == '.'))
297 s[--len] = '\0';
298 }
Guido van Rossum743007d1999-04-21 15:27:31 +0000299 }
300#endif
Barry Warsawfa5c3152000-05-02 19:27:51 +0000301 if (filename != NULL)
Guido van Rossume0e59821998-10-14 20:38:13 +0000302 v = Py_BuildValue("(iss)", i, s, filename);
303 else
304 v = Py_BuildValue("(is)", i, s);
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000305 if (v != NULL) {
Guido van Rossum373c8691997-04-29 18:22:47 +0000306 PyErr_SetObject(exc, v);
307 Py_DECREF(v);
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000308 }
Guido van Rossum743007d1999-04-21 15:27:31 +0000309#ifdef MS_WIN32
Guido van Rossum795e1892000-02-17 15:19:15 +0000310 LocalFree(s_buf);
Guido van Rossum743007d1999-04-21 15:27:31 +0000311#endif
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000312 return NULL;
313}
Guido van Rossum743007d1999-04-21 15:27:31 +0000314
Barry Warsaw97d95151998-07-23 16:05:56 +0000315
316PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000317PyErr_SetFromErrno(PyObject *exc)
Barry Warsaw97d95151998-07-23 16:05:56 +0000318{
319 return PyErr_SetFromErrnoWithFilename(exc, NULL);
320}
Guido van Rossum683a0721990-10-21 22:09:12 +0000321
Guido van Rossum795e1892000-02-17 15:19:15 +0000322#ifdef MS_WINDOWS
323/* Windows specific error code handling */
324PyObject *PyErr_SetFromWindowsErrWithFilename(
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000325 int ierr,
Guido van Rossum795e1892000-02-17 15:19:15 +0000326 const char *filename)
327{
328 int len;
329 char *s;
330 PyObject *v;
331 DWORD err = (DWORD)ierr;
332 if (err==0) err = GetLastError();
333 len = FormatMessage(
334 /* Error API error */
335 FORMAT_MESSAGE_ALLOCATE_BUFFER |
336 FORMAT_MESSAGE_FROM_SYSTEM |
337 FORMAT_MESSAGE_IGNORE_INSERTS,
338 NULL, /* no message source */
339 err,
340 MAKELANGID(LANG_NEUTRAL,
341 SUBLANG_DEFAULT), /* Default language */
342 (LPTSTR) &s,
343 0, /* size not used */
344 NULL); /* no args */
345 /* remove trailing cr/lf and dots */
346 while (len > 0 && (s[len-1] <= ' ' || s[len-1] == '.'))
347 s[--len] = '\0';
Barry Warsawfa5c3152000-05-02 19:27:51 +0000348 if (filename != NULL)
Guido van Rossum795e1892000-02-17 15:19:15 +0000349 v = Py_BuildValue("(iss)", err, s, filename);
350 else
351 v = Py_BuildValue("(is)", err, s);
352 if (v != NULL) {
Guido van Rossum0b556702000-03-02 13:55:01 +0000353 PyErr_SetObject(PyExc_WindowsError, v);
Guido van Rossum795e1892000-02-17 15:19:15 +0000354 Py_DECREF(v);
355 }
356 LocalFree(s);
357 return NULL;
358}
359
360PyObject *PyErr_SetFromWindowsErr(int ierr)
361{
362 return PyErr_SetFromWindowsErrWithFilename(ierr, NULL);
363
364}
365#endif /* MS_WINDOWS */
366
Guido van Rossum683a0721990-10-21 22:09:12 +0000367void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000368PyErr_BadInternalCall(void)
Guido van Rossum683a0721990-10-21 22:09:12 +0000369{
Guido van Rossuma027efa1997-05-05 20:56:21 +0000370 PyErr_SetString(PyExc_SystemError,
371 "bad argument to internal function");
Guido van Rossum683a0721990-10-21 22:09:12 +0000372}
Guido van Rossum1548bac1997-02-14 17:09:47 +0000373
374
Guido van Rossum1548bac1997-02-14 17:09:47 +0000375PyObject *
376PyErr_Format(PyObject *exception, const char *format, ...)
Guido van Rossum1548bac1997-02-14 17:09:47 +0000377{
378 va_list vargs;
379 char buffer[500]; /* Caller is responsible for limiting the format */
Guido van Rossum1548bac1997-02-14 17:09:47 +0000380
Guido van Rossum1548bac1997-02-14 17:09:47 +0000381 va_start(vargs, format);
Guido van Rossum1548bac1997-02-14 17:09:47 +0000382
383 vsprintf(buffer, format, vargs);
384 PyErr_SetString(exception, buffer);
385 return NULL;
386}
Guido van Rossum7617e051997-09-16 18:43:50 +0000387
388
389PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000390PyErr_NewException(char *name, PyObject *base, PyObject *dict)
Guido van Rossum7617e051997-09-16 18:43:50 +0000391{
Guido van Rossum2ac650f1997-10-03 19:50:55 +0000392 char *dot;
393 PyObject *modulename = NULL;
394 PyObject *classname = NULL;
395 PyObject *mydict = NULL;
396 PyObject *bases = NULL;
Guido van Rossum7617e051997-09-16 18:43:50 +0000397 PyObject *result = NULL;
Guido van Rossum2ac650f1997-10-03 19:50:55 +0000398 dot = strrchr(name, '.');
399 if (dot == NULL) {
400 PyErr_SetString(PyExc_SystemError,
401 "PyErr_NewException: name must be module.class");
Guido van Rossum7617e051997-09-16 18:43:50 +0000402 return NULL;
Guido van Rossum7617e051997-09-16 18:43:50 +0000403 }
404 if (base == NULL)
405 base = PyExc_Exception;
Guido van Rossum2ac650f1997-10-03 19:50:55 +0000406 if (!PyClass_Check(base)) {
407 /* Must be using string-based standard exceptions (-X) */
408 return PyString_FromString(name);
409 }
410 if (dict == NULL) {
411 dict = mydict = PyDict_New();
412 if (dict == NULL)
413 goto failure;
414 }
415 if (PyDict_GetItemString(dict, "__module__") == NULL) {
416 modulename = PyString_FromStringAndSize(name, (int)(dot-name));
417 if (modulename == NULL)
418 goto failure;
419 if (PyDict_SetItemString(dict, "__module__", modulename) != 0)
420 goto failure;
421 }
422 classname = PyString_FromString(dot+1);
423 if (classname == NULL)
Guido van Rossum7617e051997-09-16 18:43:50 +0000424 goto failure;
Guido van Rossum2ac650f1997-10-03 19:50:55 +0000425 bases = Py_BuildValue("(O)", base);
426 if (bases == NULL)
427 goto failure;
428 result = PyClass_New(bases, dict, classname);
Guido van Rossum7617e051997-09-16 18:43:50 +0000429 failure:
Guido van Rossum2ac650f1997-10-03 19:50:55 +0000430 Py_XDECREF(bases);
431 Py_XDECREF(mydict);
432 Py_XDECREF(classname);
433 Py_XDECREF(modulename);
Guido van Rossum7617e051997-09-16 18:43:50 +0000434 return result;
435}