blob: d05a21d5ba389e1c8c58d2d9065738a09ac4aa3f [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001/***********************************************************
Guido van Rossum6d023c91995-01-04 19:12:13 +00002Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
3The Netherlands.
Guido van Rossumf70e43a1991-02-19 12:39:46 +00004
5 All Rights Reserved
6
Guido van Rossumd266eb41996-10-25 14:44:06 +00007Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
Guido van Rossumf70e43a1991-02-19 12:39:46 +00009provided that the above copyright notice appear in all copies and that
Guido van Rossumd266eb41996-10-25 14:44:06 +000010both that copyright notice and this permission notice appear in
Guido van Rossumf70e43a1991-02-19 12:39:46 +000011supporting documentation, and that the names of Stichting Mathematisch
Guido van Rossumd266eb41996-10-25 14:44:06 +000012Centrum or CWI or Corporation for National Research Initiatives or
13CNRI not be used in advertising or publicity pertaining to
14distribution of the software without specific, written prior
15permission.
Guido van Rossumf70e43a1991-02-19 12:39:46 +000016
Guido van Rossumd266eb41996-10-25 14:44:06 +000017While CWI is the initial source for this software, a modified version
18is made available by the Corporation for National Research Initiatives
19(CNRI) at the Internet address ftp://ftp.python.org.
20
21STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
22REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
23MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
24CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
25DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
26PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
27TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
28PERFORMANCE OF THIS SOFTWARE.
Guido van Rossumf70e43a1991-02-19 12:39:46 +000029
30******************************************************************/
31
Guido van Rossum373c8691997-04-29 18:22:47 +000032/* Error handling */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000033
Guido van Rossum373c8691997-04-29 18:22:47 +000034#include "Python.h"
Guido van Rossumf22120a1990-12-20 23:05:40 +000035
Jack Janseneceb3e31995-06-27 13:15:15 +000036#ifdef SYMANTEC__CFM68K__
Guido van Rossume9fbc091995-02-18 14:52:19 +000037#pragma lib_export on
38#endif
39
Jack Jansen8fd2d941994-12-14 12:54:54 +000040#ifdef macintosh
Guido van Rossum373c8691997-04-29 18:22:47 +000041extern char *PyMac_StrError Py_PROTO((int));
Jack Jansen5ef86d51995-01-19 12:16:44 +000042#undef strerror
Guido van Rossume9fbc091995-02-18 14:52:19 +000043#define strerror PyMac_StrError
44#endif /* macintosh */
45
Guido van Rossum53e8d441995-03-09 12:11:31 +000046#ifndef __STDC__
Guido van Rossum7844e381997-04-11 20:44:04 +000047#ifndef MS_WINDOWS
Guido van Rossum373c8691997-04-29 18:22:47 +000048extern char *strerror Py_PROTO((int));
Guido van Rossum53e8d441995-03-09 12:11:31 +000049#endif
Guido van Rossum7844e381997-04-11 20:44:04 +000050#endif
Guido van Rossumf5401bd1990-11-02 17:50:28 +000051
Guido van Rossum743007d1999-04-21 15:27:31 +000052#ifdef MS_WIN32
53#include "windows.h"
54#include "winbase.h"
55#endif
56
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000057void
Guido van Rossuma027efa1997-05-05 20:56:21 +000058PyErr_Restore(type, value, traceback)
59 PyObject *type;
Guido van Rossum373c8691997-04-29 18:22:47 +000060 PyObject *value;
61 PyObject *traceback;
Guido van Rossum1ae940a1995-01-02 19:04:15 +000062{
Guido van Rossum885553e1998-12-21 18:33:30 +000063 PyThreadState *tstate = PyThreadState_GET();
Guido van Rossuma027efa1997-05-05 20:56:21 +000064 PyObject *oldtype, *oldvalue, *oldtraceback;
Guido van Rossum1ae940a1995-01-02 19:04:15 +000065
Guido van Rossuma027efa1997-05-05 20:56:21 +000066 if (traceback != NULL && !PyTraceBack_Check(traceback)) {
67 /* XXX Should never happen -- fatal error instead? */
68 Py_DECREF(traceback);
69 traceback = NULL;
70 }
71
72 /* Save these in locals to safeguard against recursive
73 invocation through Py_XDECREF */
74 oldtype = tstate->curexc_type;
75 oldvalue = tstate->curexc_value;
76 oldtraceback = tstate->curexc_traceback;
77
78 tstate->curexc_type = type;
79 tstate->curexc_value = value;
80 tstate->curexc_traceback = traceback;
81
82 Py_XDECREF(oldtype);
83 Py_XDECREF(oldvalue);
84 Py_XDECREF(oldtraceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +000085}
86
87void
Guido van Rossum373c8691997-04-29 18:22:47 +000088PyErr_SetObject(exception, value)
89 PyObject *exception;
90 PyObject *value;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000091{
Guido van Rossum373c8691997-04-29 18:22:47 +000092 Py_XINCREF(exception);
93 Py_XINCREF(value);
94 PyErr_Restore(exception, value, (PyObject *)NULL);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000095}
96
97void
Guido van Rossum373c8691997-04-29 18:22:47 +000098PyErr_SetNone(exception)
99 PyObject *exception;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000100{
Guido van Rossum373c8691997-04-29 18:22:47 +0000101 PyErr_SetObject(exception, (PyObject *)NULL);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000102}
103
104void
Guido van Rossum373c8691997-04-29 18:22:47 +0000105PyErr_SetString(exception, string)
106 PyObject *exception;
Guido van Rossum067998f1996-12-10 15:33:34 +0000107 const char *string;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000108{
Guido van Rossum373c8691997-04-29 18:22:47 +0000109 PyObject *value = PyString_FromString(string);
110 PyErr_SetObject(exception, value);
111 Py_XDECREF(value);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000112}
113
Guido van Rossum3a241811994-08-29 12:14:12 +0000114
Guido van Rossum373c8691997-04-29 18:22:47 +0000115PyObject *
116PyErr_Occurred()
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000117{
Guido van Rossuma027efa1997-05-05 20:56:21 +0000118 PyThreadState *tstate = PyThreadState_Get();
119
120 return tstate->curexc_type;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000121}
122
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000123
124int
125PyErr_GivenExceptionMatches(err, exc)
126 PyObject *err, *exc;
127{
Barry Warsawfa5c3152000-05-02 19:27:51 +0000128 if (err == NULL || exc == NULL) {
129 /* maybe caused by "import exceptions" that failed early on */
130 return 0;
131 }
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000132 if (PyTuple_Check(exc)) {
133 int i, n;
134 n = PyTuple_Size(exc);
135 for (i = 0; i < n; i++) {
136 /* Test recursively */
137 if (PyErr_GivenExceptionMatches(
138 err, PyTuple_GET_ITEM(exc, i)))
139 {
140 return 1;
141 }
142 }
143 return 0;
144 }
145 /* err might be an instance, so check its class. */
146 if (PyInstance_Check(err))
147 err = (PyObject*)((PyInstanceObject*)err)->in_class;
148
149 if (PyClass_Check(err) && PyClass_Check(exc))
150 return PyClass_IsSubclass(err, exc);
151
152 return err == exc;
153}
Guido van Rossum743007d1999-04-21 15:27:31 +0000154
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000155
156int
157PyErr_ExceptionMatches(exc)
158 PyObject *exc;
159{
160 return PyErr_GivenExceptionMatches(PyErr_Occurred(), exc);
161}
162
163
164/* Used in many places to normalize a raised exception, including in
165 eval_code2(), do_raise(), and PyErr_Print()
166*/
167void
168PyErr_NormalizeException(exc, val, tb)
169 PyObject **exc;
170 PyObject **val;
171 PyObject **tb;
172{
173 PyObject *type = *exc;
174 PyObject *value = *val;
175 PyObject *inclass = NULL;
176
177 /* If PyErr_SetNone() was used, the value will have been actually
178 set to NULL.
179 */
180 if (!value) {
181 value = Py_None;
182 Py_INCREF(value);
183 }
184
185 if (PyInstance_Check(value))
186 inclass = (PyObject*)((PyInstanceObject*)value)->in_class;
187
188 /* Normalize the exception so that if the type is a class, the
189 value will be an instance.
190 */
191 if (PyClass_Check(type)) {
192 /* if the value was not an instance, or is not an instance
193 whose class is (or is derived from) type, then use the
194 value as an argument to instantiation of the type
195 class.
196 */
197 if (!inclass || !PyClass_IsSubclass(inclass, type)) {
198 PyObject *args, *res;
199
200 if (value == Py_None)
201 args = Py_BuildValue("()");
202 else if (PyTuple_Check(value)) {
203 Py_INCREF(value);
204 args = value;
205 }
206 else
207 args = Py_BuildValue("(O)", value);
208
209 if (args == NULL)
210 goto finally;
211 res = PyEval_CallObject(type, args);
212 Py_DECREF(args);
213 if (res == NULL)
214 goto finally;
215 Py_DECREF(value);
216 value = res;
217 }
Barry Warsaw3a749931997-09-30 15:00:18 +0000218 /* if the class of the instance doesn't exactly match the
219 class of the type, believe the instance
220 */
221 else if (inclass != type) {
222 Py_DECREF(type);
223 type = inclass;
224 Py_INCREF(type);
225 }
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000226 }
227 *exc = type;
228 *val = value;
229 return;
230finally:
Guido van Rossum19b55f21997-12-09 14:11:39 +0000231 Py_DECREF(type);
232 Py_DECREF(value);
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000233 Py_XDECREF(*tb);
234 PyErr_Fetch(exc, val, tb);
235 /* normalize recursively */
236 PyErr_NormalizeException(exc, val, tb);
237}
238
239
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000240void
Guido van Rossuma027efa1997-05-05 20:56:21 +0000241PyErr_Fetch(p_type, p_value, p_traceback)
242 PyObject **p_type;
243 PyObject **p_value;
244 PyObject **p_traceback;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000245{
Guido van Rossuma027efa1997-05-05 20:56:21 +0000246 PyThreadState *tstate = PyThreadState_Get();
247
248 *p_type = tstate->curexc_type;
249 *p_value = tstate->curexc_value;
250 *p_traceback = tstate->curexc_traceback;
251
252 tstate->curexc_type = NULL;
253 tstate->curexc_value = NULL;
254 tstate->curexc_traceback = NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000255}
256
257void
Guido van Rossum373c8691997-04-29 18:22:47 +0000258PyErr_Clear()
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000259{
Guido van Rossuma027efa1997-05-05 20:56:21 +0000260 PyErr_Restore(NULL, NULL, NULL);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000261}
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000262
263/* Convenience functions to set a type error exception and return 0 */
264
265int
Guido van Rossum373c8691997-04-29 18:22:47 +0000266PyErr_BadArgument()
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000267{
Guido van Rossuma027efa1997-05-05 20:56:21 +0000268 PyErr_SetString(PyExc_TypeError,
269 "illegal argument type for built-in operation");
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000270 return 0;
271}
272
Guido van Rossum373c8691997-04-29 18:22:47 +0000273PyObject *
274PyErr_NoMemory()
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000275{
Barry Warsaw2d8adff1997-08-29 21:54:35 +0000276 /* raise the pre-allocated instance if it still exists */
277 if (PyExc_MemoryErrorInst)
278 PyErr_SetObject(PyExc_MemoryError, PyExc_MemoryErrorInst);
279 else
280 /* this will probably fail since there's no memory and hee,
281 hee, we have to instantiate this class
282 */
283 PyErr_SetNone(PyExc_MemoryError);
284
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000285 return NULL;
286}
287
Guido van Rossum373c8691997-04-29 18:22:47 +0000288PyObject *
Barry Warsaw97d95151998-07-23 16:05:56 +0000289PyErr_SetFromErrnoWithFilename(exc, filename)
Guido van Rossum373c8691997-04-29 18:22:47 +0000290 PyObject *exc;
Barry Warsaw97d95151998-07-23 16:05:56 +0000291 char *filename;
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000292{
Guido van Rossum373c8691997-04-29 18:22:47 +0000293 PyObject *v;
Guido van Rossume0e59821998-10-14 20:38:13 +0000294 char *s;
Guido van Rossum3a241811994-08-29 12:14:12 +0000295 int i = errno;
Guido van Rossum795e1892000-02-17 15:19:15 +0000296#ifdef MS_WIN32
297 char *s_buf = NULL;
298#endif
Guido van Rossume9fbc091995-02-18 14:52:19 +0000299#ifdef EINTR
Guido van Rossum373c8691997-04-29 18:22:47 +0000300 if (i == EINTR && PyErr_CheckSignals())
Guido van Rossum5063bab1991-10-20 20:14:56 +0000301 return NULL;
Guido van Rossume9fbc091995-02-18 14:52:19 +0000302#endif
Guido van Rossume0e59821998-10-14 20:38:13 +0000303 if (i == 0)
304 s = "Error"; /* Sometimes errno didn't get set */
Barry Warsaw97d95151998-07-23 16:05:56 +0000305 else
Guido van Rossum743007d1999-04-21 15:27:31 +0000306#ifndef MS_WIN32
Guido van Rossume0e59821998-10-14 20:38:13 +0000307 s = strerror(i);
Guido van Rossum743007d1999-04-21 15:27:31 +0000308#else
309 {
Guido van Rossum795e1892000-02-17 15:19:15 +0000310 /* Note that the Win32 errors do not lineup with the
311 errno error. So if the error is in the MSVC error
312 table, we use it, otherwise we assume it really _is_
313 a Win32 error code
314 */
Guido van Rossum584b16a2000-02-21 16:50:31 +0000315 if (i > 0 && i < _sys_nerr) {
Guido van Rossum795e1892000-02-17 15:19:15 +0000316 s = _sys_errlist[i];
317 }
318 else {
319 int len = FormatMessage(
320 FORMAT_MESSAGE_ALLOCATE_BUFFER |
321 FORMAT_MESSAGE_FROM_SYSTEM |
322 FORMAT_MESSAGE_IGNORE_INSERTS,
323 NULL, /* no message source */
324 i,
325 MAKELANGID(LANG_NEUTRAL,
326 SUBLANG_DEFAULT),
327 /* Default language */
328 (LPTSTR) &s_buf,
329 0, /* size not used */
330 NULL); /* no args */
331 s = s_buf;
332 /* remove trailing cr/lf and dots */
333 while (len > 0 && (s[len-1] <= ' ' || s[len-1] == '.'))
334 s[--len] = '\0';
335 }
Guido van Rossum743007d1999-04-21 15:27:31 +0000336 }
337#endif
Barry Warsawfa5c3152000-05-02 19:27:51 +0000338 if (filename != NULL)
Guido van Rossume0e59821998-10-14 20:38:13 +0000339 v = Py_BuildValue("(iss)", i, s, filename);
340 else
341 v = Py_BuildValue("(is)", i, s);
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000342 if (v != NULL) {
Guido van Rossum373c8691997-04-29 18:22:47 +0000343 PyErr_SetObject(exc, v);
344 Py_DECREF(v);
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000345 }
Guido van Rossum743007d1999-04-21 15:27:31 +0000346#ifdef MS_WIN32
Guido van Rossum795e1892000-02-17 15:19:15 +0000347 LocalFree(s_buf);
Guido van Rossum743007d1999-04-21 15:27:31 +0000348#endif
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000349 return NULL;
350}
Guido van Rossum743007d1999-04-21 15:27:31 +0000351
Barry Warsaw97d95151998-07-23 16:05:56 +0000352
353PyObject *
354PyErr_SetFromErrno(exc)
355 PyObject *exc;
356{
357 return PyErr_SetFromErrnoWithFilename(exc, NULL);
358}
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 */
362PyObject *PyErr_SetFromWindowsErrWithFilename(
363 int ierr,
364 const char *filename)
365{
366 int len;
367 char *s;
368 PyObject *v;
369 DWORD err = (DWORD)ierr;
370 if (err==0) err = GetLastError();
371 len = FormatMessage(
372 /* Error API error */
373 FORMAT_MESSAGE_ALLOCATE_BUFFER |
374 FORMAT_MESSAGE_FROM_SYSTEM |
375 FORMAT_MESSAGE_IGNORE_INSERTS,
376 NULL, /* no message source */
377 err,
378 MAKELANGID(LANG_NEUTRAL,
379 SUBLANG_DEFAULT), /* Default language */
380 (LPTSTR) &s,
381 0, /* size not used */
382 NULL); /* no args */
383 /* remove trailing cr/lf and dots */
384 while (len > 0 && (s[len-1] <= ' ' || s[len-1] == '.'))
385 s[--len] = '\0';
Barry Warsawfa5c3152000-05-02 19:27:51 +0000386 if (filename != NULL)
Guido van Rossum795e1892000-02-17 15:19:15 +0000387 v = Py_BuildValue("(iss)", err, s, filename);
388 else
389 v = Py_BuildValue("(is)", err, s);
390 if (v != NULL) {
Guido van Rossum0b556702000-03-02 13:55:01 +0000391 PyErr_SetObject(PyExc_WindowsError, v);
Guido van Rossum795e1892000-02-17 15:19:15 +0000392 Py_DECREF(v);
393 }
394 LocalFree(s);
395 return NULL;
396}
397
398PyObject *PyErr_SetFromWindowsErr(int ierr)
399{
400 return PyErr_SetFromWindowsErrWithFilename(ierr, NULL);
401
402}
403#endif /* MS_WINDOWS */
404
Guido van Rossum683a0721990-10-21 22:09:12 +0000405void
Guido van Rossum373c8691997-04-29 18:22:47 +0000406PyErr_BadInternalCall()
Guido van Rossum683a0721990-10-21 22:09:12 +0000407{
Guido van Rossuma027efa1997-05-05 20:56:21 +0000408 PyErr_SetString(PyExc_SystemError,
409 "bad argument to internal function");
Guido van Rossum683a0721990-10-21 22:09:12 +0000410}
Guido van Rossum1548bac1997-02-14 17:09:47 +0000411
412
413#ifdef HAVE_STDARG_PROTOTYPES
414PyObject *
415PyErr_Format(PyObject *exception, const char *format, ...)
416#else
417PyObject *
418PyErr_Format(exception, format, va_alist)
419 PyObject *exception;
420 const char *format;
421 va_dcl
422#endif
423{
424 va_list vargs;
425 char buffer[500]; /* Caller is responsible for limiting the format */
Guido van Rossum1548bac1997-02-14 17:09:47 +0000426
427#ifdef HAVE_STDARG_PROTOTYPES
428 va_start(vargs, format);
429#else
430 va_start(vargs);
431#endif
432
433 vsprintf(buffer, format, vargs);
434 PyErr_SetString(exception, buffer);
435 return NULL;
436}
Guido van Rossum7617e051997-09-16 18:43:50 +0000437
438
439PyObject *
440PyErr_NewException(name, base, dict)
Guido van Rossum2ac650f1997-10-03 19:50:55 +0000441 char *name; /* modulename.classname */
Guido van Rossum7617e051997-09-16 18:43:50 +0000442 PyObject *base;
443 PyObject *dict;
444{
Guido van Rossum2ac650f1997-10-03 19:50:55 +0000445 char *dot;
446 PyObject *modulename = NULL;
447 PyObject *classname = NULL;
448 PyObject *mydict = NULL;
449 PyObject *bases = NULL;
Guido van Rossum7617e051997-09-16 18:43:50 +0000450 PyObject *result = NULL;
Guido van Rossum2ac650f1997-10-03 19:50:55 +0000451 dot = strrchr(name, '.');
452 if (dot == NULL) {
453 PyErr_SetString(PyExc_SystemError,
454 "PyErr_NewException: name must be module.class");
Guido van Rossum7617e051997-09-16 18:43:50 +0000455 return NULL;
Guido van Rossum7617e051997-09-16 18:43:50 +0000456 }
457 if (base == NULL)
458 base = PyExc_Exception;
Guido van Rossum2ac650f1997-10-03 19:50:55 +0000459 if (!PyClass_Check(base)) {
460 /* Must be using string-based standard exceptions (-X) */
461 return PyString_FromString(name);
462 }
463 if (dict == NULL) {
464 dict = mydict = PyDict_New();
465 if (dict == NULL)
466 goto failure;
467 }
468 if (PyDict_GetItemString(dict, "__module__") == NULL) {
469 modulename = PyString_FromStringAndSize(name, (int)(dot-name));
470 if (modulename == NULL)
471 goto failure;
472 if (PyDict_SetItemString(dict, "__module__", modulename) != 0)
473 goto failure;
474 }
475 classname = PyString_FromString(dot+1);
476 if (classname == NULL)
Guido van Rossum7617e051997-09-16 18:43:50 +0000477 goto failure;
Guido van Rossum2ac650f1997-10-03 19:50:55 +0000478 bases = Py_BuildValue("(O)", base);
479 if (bases == NULL)
480 goto failure;
481 result = PyClass_New(bases, dict, classname);
Guido van Rossum7617e051997-09-16 18:43:50 +0000482 failure:
Guido van Rossum2ac650f1997-10-03 19:50:55 +0000483 Py_XDECREF(bases);
484 Py_XDECREF(mydict);
485 Py_XDECREF(classname);
486 Py_XDECREF(modulename);
Guido van Rossum7617e051997-09-16 18:43:50 +0000487 return result;
488}