blob: d2aa3dd603f67a25b82f52ba1ec352f409fd0f11 [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 Janseneceb3e31995-06-27 13:15:15 +000015#ifdef SYMANTEC__CFM68K__
Guido van Rossume9fbc091995-02-18 14:52:19 +000016#pragma lib_export on
17#endif
18
Jack Jansen8fd2d941994-12-14 12:54:54 +000019#ifdef macintosh
Guido van Rossum373c8691997-04-29 18:22:47 +000020extern char *PyMac_StrError Py_PROTO((int));
Jack Jansen5ef86d51995-01-19 12:16:44 +000021#undef strerror
Guido van Rossume9fbc091995-02-18 14:52:19 +000022#define strerror PyMac_StrError
23#endif /* macintosh */
24
Guido van Rossum53e8d441995-03-09 12:11:31 +000025#ifndef __STDC__
Guido van Rossum7844e381997-04-11 20:44:04 +000026#ifndef MS_WINDOWS
Guido van Rossum373c8691997-04-29 18:22:47 +000027extern char *strerror Py_PROTO((int));
Guido van Rossum53e8d441995-03-09 12:11:31 +000028#endif
Guido van Rossum7844e381997-04-11 20:44:04 +000029#endif
Guido van Rossumf5401bd1990-11-02 17:50:28 +000030
Guido van Rossum743007d1999-04-21 15:27:31 +000031#ifdef MS_WIN32
32#include "windows.h"
33#include "winbase.h"
34#endif
35
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000036void
Guido van Rossuma027efa1997-05-05 20:56:21 +000037PyErr_Restore(type, value, traceback)
38 PyObject *type;
Guido van Rossum373c8691997-04-29 18:22:47 +000039 PyObject *value;
40 PyObject *traceback;
Guido van Rossum1ae940a1995-01-02 19:04:15 +000041{
Guido van Rossum885553e1998-12-21 18:33:30 +000042 PyThreadState *tstate = PyThreadState_GET();
Guido van Rossuma027efa1997-05-05 20:56:21 +000043 PyObject *oldtype, *oldvalue, *oldtraceback;
Guido van Rossum1ae940a1995-01-02 19:04:15 +000044
Guido van Rossuma027efa1997-05-05 20:56:21 +000045 if (traceback != NULL && !PyTraceBack_Check(traceback)) {
46 /* XXX Should never happen -- fatal error instead? */
47 Py_DECREF(traceback);
48 traceback = NULL;
49 }
50
51 /* Save these in locals to safeguard against recursive
52 invocation through Py_XDECREF */
53 oldtype = tstate->curexc_type;
54 oldvalue = tstate->curexc_value;
55 oldtraceback = tstate->curexc_traceback;
56
57 tstate->curexc_type = type;
58 tstate->curexc_value = value;
59 tstate->curexc_traceback = traceback;
60
61 Py_XDECREF(oldtype);
62 Py_XDECREF(oldvalue);
63 Py_XDECREF(oldtraceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +000064}
65
66void
Guido van Rossum373c8691997-04-29 18:22:47 +000067PyErr_SetObject(exception, value)
68 PyObject *exception;
69 PyObject *value;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000070{
Guido van Rossum373c8691997-04-29 18:22:47 +000071 Py_XINCREF(exception);
72 Py_XINCREF(value);
73 PyErr_Restore(exception, value, (PyObject *)NULL);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000074}
75
76void
Guido van Rossum373c8691997-04-29 18:22:47 +000077PyErr_SetNone(exception)
78 PyObject *exception;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000079{
Guido van Rossum373c8691997-04-29 18:22:47 +000080 PyErr_SetObject(exception, (PyObject *)NULL);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000081}
82
83void
Guido van Rossum373c8691997-04-29 18:22:47 +000084PyErr_SetString(exception, string)
85 PyObject *exception;
Guido van Rossum067998f1996-12-10 15:33:34 +000086 const char *string;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000087{
Guido van Rossum373c8691997-04-29 18:22:47 +000088 PyObject *value = PyString_FromString(string);
89 PyErr_SetObject(exception, value);
90 Py_XDECREF(value);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000091}
92
Guido van Rossum3a241811994-08-29 12:14:12 +000093
Guido van Rossum373c8691997-04-29 18:22:47 +000094PyObject *
95PyErr_Occurred()
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000096{
Guido van Rossuma027efa1997-05-05 20:56:21 +000097 PyThreadState *tstate = PyThreadState_Get();
98
99 return tstate->curexc_type;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000100}
101
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000102
103int
104PyErr_GivenExceptionMatches(err, exc)
105 PyObject *err, *exc;
106{
Barry Warsawfa5c3152000-05-02 19:27:51 +0000107 if (err == NULL || exc == NULL) {
108 /* maybe caused by "import exceptions" that failed early on */
109 return 0;
110 }
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000111 if (PyTuple_Check(exc)) {
112 int i, n;
113 n = PyTuple_Size(exc);
114 for (i = 0; i < n; i++) {
115 /* Test recursively */
116 if (PyErr_GivenExceptionMatches(
117 err, PyTuple_GET_ITEM(exc, i)))
118 {
119 return 1;
120 }
121 }
122 return 0;
123 }
124 /* err might be an instance, so check its class. */
125 if (PyInstance_Check(err))
126 err = (PyObject*)((PyInstanceObject*)err)->in_class;
127
128 if (PyClass_Check(err) && PyClass_Check(exc))
129 return PyClass_IsSubclass(err, exc);
130
131 return err == exc;
132}
Guido van Rossum743007d1999-04-21 15:27:31 +0000133
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000134
135int
136PyErr_ExceptionMatches(exc)
137 PyObject *exc;
138{
139 return PyErr_GivenExceptionMatches(PyErr_Occurred(), exc);
140}
141
142
143/* Used in many places to normalize a raised exception, including in
144 eval_code2(), do_raise(), and PyErr_Print()
145*/
146void
147PyErr_NormalizeException(exc, val, tb)
148 PyObject **exc;
149 PyObject **val;
150 PyObject **tb;
151{
152 PyObject *type = *exc;
153 PyObject *value = *val;
154 PyObject *inclass = NULL;
155
156 /* If PyErr_SetNone() was used, the value will have been actually
157 set to NULL.
158 */
159 if (!value) {
160 value = Py_None;
161 Py_INCREF(value);
162 }
163
164 if (PyInstance_Check(value))
165 inclass = (PyObject*)((PyInstanceObject*)value)->in_class;
166
167 /* Normalize the exception so that if the type is a class, the
168 value will be an instance.
169 */
170 if (PyClass_Check(type)) {
171 /* if the value was not an instance, or is not an instance
172 whose class is (or is derived from) type, then use the
173 value as an argument to instantiation of the type
174 class.
175 */
176 if (!inclass || !PyClass_IsSubclass(inclass, type)) {
177 PyObject *args, *res;
178
179 if (value == Py_None)
180 args = Py_BuildValue("()");
181 else if (PyTuple_Check(value)) {
182 Py_INCREF(value);
183 args = value;
184 }
185 else
186 args = Py_BuildValue("(O)", value);
187
188 if (args == NULL)
189 goto finally;
190 res = PyEval_CallObject(type, args);
191 Py_DECREF(args);
192 if (res == NULL)
193 goto finally;
194 Py_DECREF(value);
195 value = res;
196 }
Barry Warsaw3a749931997-09-30 15:00:18 +0000197 /* if the class of the instance doesn't exactly match the
198 class of the type, believe the instance
199 */
200 else if (inclass != type) {
201 Py_DECREF(type);
202 type = inclass;
203 Py_INCREF(type);
204 }
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000205 }
206 *exc = type;
207 *val = value;
208 return;
209finally:
Guido van Rossum19b55f21997-12-09 14:11:39 +0000210 Py_DECREF(type);
211 Py_DECREF(value);
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000212 Py_XDECREF(*tb);
213 PyErr_Fetch(exc, val, tb);
214 /* normalize recursively */
215 PyErr_NormalizeException(exc, val, tb);
216}
217
218
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000219void
Guido van Rossuma027efa1997-05-05 20:56:21 +0000220PyErr_Fetch(p_type, p_value, p_traceback)
221 PyObject **p_type;
222 PyObject **p_value;
223 PyObject **p_traceback;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000224{
Guido van Rossuma027efa1997-05-05 20:56:21 +0000225 PyThreadState *tstate = PyThreadState_Get();
226
227 *p_type = tstate->curexc_type;
228 *p_value = tstate->curexc_value;
229 *p_traceback = tstate->curexc_traceback;
230
231 tstate->curexc_type = NULL;
232 tstate->curexc_value = NULL;
233 tstate->curexc_traceback = NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000234}
235
236void
Guido van Rossum373c8691997-04-29 18:22:47 +0000237PyErr_Clear()
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000238{
Guido van Rossuma027efa1997-05-05 20:56:21 +0000239 PyErr_Restore(NULL, NULL, NULL);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000240}
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000241
242/* Convenience functions to set a type error exception and return 0 */
243
244int
Guido van Rossum373c8691997-04-29 18:22:47 +0000245PyErr_BadArgument()
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000246{
Guido van Rossuma027efa1997-05-05 20:56:21 +0000247 PyErr_SetString(PyExc_TypeError,
248 "illegal argument type for built-in operation");
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000249 return 0;
250}
251
Guido van Rossum373c8691997-04-29 18:22:47 +0000252PyObject *
253PyErr_NoMemory()
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000254{
Barry Warsaw2d8adff1997-08-29 21:54:35 +0000255 /* raise the pre-allocated instance if it still exists */
256 if (PyExc_MemoryErrorInst)
257 PyErr_SetObject(PyExc_MemoryError, PyExc_MemoryErrorInst);
258 else
259 /* this will probably fail since there's no memory and hee,
260 hee, we have to instantiate this class
261 */
262 PyErr_SetNone(PyExc_MemoryError);
263
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000264 return NULL;
265}
266
Guido van Rossum373c8691997-04-29 18:22:47 +0000267PyObject *
Barry Warsaw97d95151998-07-23 16:05:56 +0000268PyErr_SetFromErrnoWithFilename(exc, filename)
Guido van Rossum373c8691997-04-29 18:22:47 +0000269 PyObject *exc;
Barry Warsaw97d95151998-07-23 16:05:56 +0000270 char *filename;
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000271{
Guido van Rossum373c8691997-04-29 18:22:47 +0000272 PyObject *v;
Guido van Rossume0e59821998-10-14 20:38:13 +0000273 char *s;
Guido van Rossum3a241811994-08-29 12:14:12 +0000274 int i = errno;
Guido van Rossum795e1892000-02-17 15:19:15 +0000275#ifdef MS_WIN32
276 char *s_buf = NULL;
277#endif
Guido van Rossume9fbc091995-02-18 14:52:19 +0000278#ifdef EINTR
Guido van Rossum373c8691997-04-29 18:22:47 +0000279 if (i == EINTR && PyErr_CheckSignals())
Guido van Rossum5063bab1991-10-20 20:14:56 +0000280 return NULL;
Guido van Rossume9fbc091995-02-18 14:52:19 +0000281#endif
Guido van Rossume0e59821998-10-14 20:38:13 +0000282 if (i == 0)
283 s = "Error"; /* Sometimes errno didn't get set */
Barry Warsaw97d95151998-07-23 16:05:56 +0000284 else
Guido van Rossum743007d1999-04-21 15:27:31 +0000285#ifndef MS_WIN32
Guido van Rossume0e59821998-10-14 20:38:13 +0000286 s = strerror(i);
Guido van Rossum743007d1999-04-21 15:27:31 +0000287#else
288 {
Guido van Rossum795e1892000-02-17 15:19:15 +0000289 /* Note that the Win32 errors do not lineup with the
290 errno error. So if the error is in the MSVC error
291 table, we use it, otherwise we assume it really _is_
292 a Win32 error code
293 */
Guido van Rossum584b16a2000-02-21 16:50:31 +0000294 if (i > 0 && i < _sys_nerr) {
Guido van Rossum795e1892000-02-17 15:19:15 +0000295 s = _sys_errlist[i];
296 }
297 else {
298 int len = FormatMessage(
299 FORMAT_MESSAGE_ALLOCATE_BUFFER |
300 FORMAT_MESSAGE_FROM_SYSTEM |
301 FORMAT_MESSAGE_IGNORE_INSERTS,
302 NULL, /* no message source */
303 i,
304 MAKELANGID(LANG_NEUTRAL,
305 SUBLANG_DEFAULT),
306 /* Default language */
307 (LPTSTR) &s_buf,
308 0, /* size not used */
309 NULL); /* no args */
310 s = s_buf;
311 /* remove trailing cr/lf and dots */
312 while (len > 0 && (s[len-1] <= ' ' || s[len-1] == '.'))
313 s[--len] = '\0';
314 }
Guido van Rossum743007d1999-04-21 15:27:31 +0000315 }
316#endif
Barry Warsawfa5c3152000-05-02 19:27:51 +0000317 if (filename != NULL)
Guido van Rossume0e59821998-10-14 20:38:13 +0000318 v = Py_BuildValue("(iss)", i, s, filename);
319 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 }
Guido van Rossum743007d1999-04-21 15:27:31 +0000325#ifdef MS_WIN32
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 *
333PyErr_SetFromErrno(exc)
334 PyObject *exc;
335{
336 return PyErr_SetFromErrnoWithFilename(exc, NULL);
337}
Guido van Rossum683a0721990-10-21 22:09:12 +0000338
Guido van Rossum795e1892000-02-17 15:19:15 +0000339#ifdef MS_WINDOWS
340/* Windows specific error code handling */
341PyObject *PyErr_SetFromWindowsErrWithFilename(
342 int ierr,
343 const char *filename)
344{
345 int len;
346 char *s;
347 PyObject *v;
348 DWORD err = (DWORD)ierr;
349 if (err==0) err = GetLastError();
350 len = FormatMessage(
351 /* Error API error */
352 FORMAT_MESSAGE_ALLOCATE_BUFFER |
353 FORMAT_MESSAGE_FROM_SYSTEM |
354 FORMAT_MESSAGE_IGNORE_INSERTS,
355 NULL, /* no message source */
356 err,
357 MAKELANGID(LANG_NEUTRAL,
358 SUBLANG_DEFAULT), /* Default language */
359 (LPTSTR) &s,
360 0, /* size not used */
361 NULL); /* no args */
362 /* remove trailing cr/lf and dots */
363 while (len > 0 && (s[len-1] <= ' ' || s[len-1] == '.'))
364 s[--len] = '\0';
Barry Warsawfa5c3152000-05-02 19:27:51 +0000365 if (filename != NULL)
Guido van Rossum795e1892000-02-17 15:19:15 +0000366 v = Py_BuildValue("(iss)", err, s, filename);
367 else
368 v = Py_BuildValue("(is)", err, s);
369 if (v != NULL) {
Guido van Rossum0b556702000-03-02 13:55:01 +0000370 PyErr_SetObject(PyExc_WindowsError, v);
Guido van Rossum795e1892000-02-17 15:19:15 +0000371 Py_DECREF(v);
372 }
373 LocalFree(s);
374 return NULL;
375}
376
377PyObject *PyErr_SetFromWindowsErr(int ierr)
378{
379 return PyErr_SetFromWindowsErrWithFilename(ierr, NULL);
380
381}
382#endif /* MS_WINDOWS */
383
Guido van Rossum683a0721990-10-21 22:09:12 +0000384void
Guido van Rossum373c8691997-04-29 18:22:47 +0000385PyErr_BadInternalCall()
Guido van Rossum683a0721990-10-21 22:09:12 +0000386{
Guido van Rossuma027efa1997-05-05 20:56:21 +0000387 PyErr_SetString(PyExc_SystemError,
388 "bad argument to internal function");
Guido van Rossum683a0721990-10-21 22:09:12 +0000389}
Guido van Rossum1548bac1997-02-14 17:09:47 +0000390
391
392#ifdef HAVE_STDARG_PROTOTYPES
393PyObject *
394PyErr_Format(PyObject *exception, const char *format, ...)
395#else
396PyObject *
397PyErr_Format(exception, format, va_alist)
398 PyObject *exception;
399 const char *format;
400 va_dcl
401#endif
402{
403 va_list vargs;
404 char buffer[500]; /* Caller is responsible for limiting the format */
Guido van Rossum1548bac1997-02-14 17:09:47 +0000405
406#ifdef HAVE_STDARG_PROTOTYPES
407 va_start(vargs, format);
408#else
409 va_start(vargs);
410#endif
411
412 vsprintf(buffer, format, vargs);
413 PyErr_SetString(exception, buffer);
414 return NULL;
415}
Guido van Rossum7617e051997-09-16 18:43:50 +0000416
417
418PyObject *
419PyErr_NewException(name, base, dict)
Guido van Rossum2ac650f1997-10-03 19:50:55 +0000420 char *name; /* modulename.classname */
Guido van Rossum7617e051997-09-16 18:43:50 +0000421 PyObject *base;
422 PyObject *dict;
423{
Guido van Rossum2ac650f1997-10-03 19:50:55 +0000424 char *dot;
425 PyObject *modulename = NULL;
426 PyObject *classname = NULL;
427 PyObject *mydict = NULL;
428 PyObject *bases = NULL;
Guido van Rossum7617e051997-09-16 18:43:50 +0000429 PyObject *result = NULL;
Guido van Rossum2ac650f1997-10-03 19:50:55 +0000430 dot = strrchr(name, '.');
431 if (dot == NULL) {
432 PyErr_SetString(PyExc_SystemError,
433 "PyErr_NewException: name must be module.class");
Guido van Rossum7617e051997-09-16 18:43:50 +0000434 return NULL;
Guido van Rossum7617e051997-09-16 18:43:50 +0000435 }
436 if (base == NULL)
437 base = PyExc_Exception;
Guido van Rossum2ac650f1997-10-03 19:50:55 +0000438 if (!PyClass_Check(base)) {
439 /* Must be using string-based standard exceptions (-X) */
440 return PyString_FromString(name);
441 }
442 if (dict == NULL) {
443 dict = mydict = PyDict_New();
444 if (dict == NULL)
445 goto failure;
446 }
447 if (PyDict_GetItemString(dict, "__module__") == NULL) {
448 modulename = PyString_FromStringAndSize(name, (int)(dot-name));
449 if (modulename == NULL)
450 goto failure;
451 if (PyDict_SetItemString(dict, "__module__", modulename) != 0)
452 goto failure;
453 }
454 classname = PyString_FromString(dot+1);
455 if (classname == NULL)
Guido van Rossum7617e051997-09-16 18:43:50 +0000456 goto failure;
Guido van Rossum2ac650f1997-10-03 19:50:55 +0000457 bases = Py_BuildValue("(O)", base);
458 if (bases == NULL)
459 goto failure;
460 result = PyClass_New(bases, dict, classname);
Guido van Rossum7617e051997-09-16 18:43:50 +0000461 failure:
Guido van Rossum2ac650f1997-10-03 19:50:55 +0000462 Py_XDECREF(bases);
463 Py_XDECREF(mydict);
464 Py_XDECREF(classname);
465 Py_XDECREF(modulename);
Guido van Rossum7617e051997-09-16 18:43:50 +0000466 return result;
467}