blob: 24eeead9cec552d6d7cf09afdd5744fb117c11cf [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
139 /* 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);
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000195 Py_XDECREF(*tb);
196 PyErr_Fetch(exc, val, tb);
197 /* normalize recursively */
198 PyErr_NormalizeException(exc, val, tb);
199}
200
201
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000202void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000203PyErr_Fetch(PyObject **p_type, PyObject **p_value, PyObject **p_traceback)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000204{
Guido van Rossuma027efa1997-05-05 20:56:21 +0000205 PyThreadState *tstate = PyThreadState_Get();
206
207 *p_type = tstate->curexc_type;
208 *p_value = tstate->curexc_value;
209 *p_traceback = tstate->curexc_traceback;
210
211 tstate->curexc_type = NULL;
212 tstate->curexc_value = NULL;
213 tstate->curexc_traceback = NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000214}
215
216void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000217PyErr_Clear(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000218{
Guido van Rossuma027efa1997-05-05 20:56:21 +0000219 PyErr_Restore(NULL, NULL, NULL);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000220}
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000221
222/* Convenience functions to set a type error exception and return 0 */
223
224int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000225PyErr_BadArgument(void)
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000226{
Guido van Rossuma027efa1997-05-05 20:56:21 +0000227 PyErr_SetString(PyExc_TypeError,
228 "illegal argument type for built-in operation");
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000229 return 0;
230}
231
Guido van Rossum373c8691997-04-29 18:22:47 +0000232PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000233PyErr_NoMemory(void)
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000234{
Barry Warsaw2d8adff1997-08-29 21:54:35 +0000235 /* raise the pre-allocated instance if it still exists */
236 if (PyExc_MemoryErrorInst)
237 PyErr_SetObject(PyExc_MemoryError, PyExc_MemoryErrorInst);
238 else
239 /* this will probably fail since there's no memory and hee,
240 hee, we have to instantiate this class
241 */
242 PyErr_SetNone(PyExc_MemoryError);
243
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000244 return NULL;
245}
246
Guido van Rossum373c8691997-04-29 18:22:47 +0000247PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000248PyErr_SetFromErrnoWithFilename(PyObject *exc, char *filename)
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000249{
Guido van Rossum373c8691997-04-29 18:22:47 +0000250 PyObject *v;
Guido van Rossume0e59821998-10-14 20:38:13 +0000251 char *s;
Guido van Rossum3a241811994-08-29 12:14:12 +0000252 int i = errno;
Guido van Rossum795e1892000-02-17 15:19:15 +0000253#ifdef MS_WIN32
254 char *s_buf = NULL;
255#endif
Guido van Rossume9fbc091995-02-18 14:52:19 +0000256#ifdef EINTR
Guido van Rossum373c8691997-04-29 18:22:47 +0000257 if (i == EINTR && PyErr_CheckSignals())
Guido van Rossum5063bab1991-10-20 20:14:56 +0000258 return NULL;
Guido van Rossume9fbc091995-02-18 14:52:19 +0000259#endif
Guido van Rossume0e59821998-10-14 20:38:13 +0000260 if (i == 0)
261 s = "Error"; /* Sometimes errno didn't get set */
Barry Warsaw97d95151998-07-23 16:05:56 +0000262 else
Guido van Rossum743007d1999-04-21 15:27:31 +0000263#ifndef MS_WIN32
Guido van Rossume0e59821998-10-14 20:38:13 +0000264 s = strerror(i);
Guido van Rossum743007d1999-04-21 15:27:31 +0000265#else
266 {
Guido van Rossum795e1892000-02-17 15:19:15 +0000267 /* Note that the Win32 errors do not lineup with the
268 errno error. So if the error is in the MSVC error
269 table, we use it, otherwise we assume it really _is_
270 a Win32 error code
271 */
Guido van Rossum584b16a2000-02-21 16:50:31 +0000272 if (i > 0 && i < _sys_nerr) {
Guido van Rossum795e1892000-02-17 15:19:15 +0000273 s = _sys_errlist[i];
274 }
275 else {
276 int len = FormatMessage(
277 FORMAT_MESSAGE_ALLOCATE_BUFFER |
278 FORMAT_MESSAGE_FROM_SYSTEM |
279 FORMAT_MESSAGE_IGNORE_INSERTS,
280 NULL, /* no message source */
281 i,
282 MAKELANGID(LANG_NEUTRAL,
283 SUBLANG_DEFAULT),
284 /* Default language */
285 (LPTSTR) &s_buf,
286 0, /* size not used */
287 NULL); /* no args */
288 s = s_buf;
289 /* remove trailing cr/lf and dots */
290 while (len > 0 && (s[len-1] <= ' ' || s[len-1] == '.'))
291 s[--len] = '\0';
292 }
Guido van Rossum743007d1999-04-21 15:27:31 +0000293 }
294#endif
Barry Warsawfa5c3152000-05-02 19:27:51 +0000295 if (filename != NULL)
Guido van Rossume0e59821998-10-14 20:38:13 +0000296 v = Py_BuildValue("(iss)", i, s, filename);
297 else
298 v = Py_BuildValue("(is)", i, s);
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000299 if (v != NULL) {
Guido van Rossum373c8691997-04-29 18:22:47 +0000300 PyErr_SetObject(exc, v);
301 Py_DECREF(v);
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000302 }
Guido van Rossum743007d1999-04-21 15:27:31 +0000303#ifdef MS_WIN32
Guido van Rossum795e1892000-02-17 15:19:15 +0000304 LocalFree(s_buf);
Guido van Rossum743007d1999-04-21 15:27:31 +0000305#endif
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000306 return NULL;
307}
Guido van Rossum743007d1999-04-21 15:27:31 +0000308
Barry Warsaw97d95151998-07-23 16:05:56 +0000309
310PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000311PyErr_SetFromErrno(PyObject *exc)
Barry Warsaw97d95151998-07-23 16:05:56 +0000312{
313 return PyErr_SetFromErrnoWithFilename(exc, NULL);
314}
Guido van Rossum683a0721990-10-21 22:09:12 +0000315
Guido van Rossum795e1892000-02-17 15:19:15 +0000316#ifdef MS_WINDOWS
317/* Windows specific error code handling */
318PyObject *PyErr_SetFromWindowsErrWithFilename(
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000319 int ierr,
Guido van Rossum795e1892000-02-17 15:19:15 +0000320 const char *filename)
321{
322 int len;
323 char *s;
324 PyObject *v;
325 DWORD err = (DWORD)ierr;
326 if (err==0) err = GetLastError();
327 len = FormatMessage(
328 /* Error API error */
329 FORMAT_MESSAGE_ALLOCATE_BUFFER |
330 FORMAT_MESSAGE_FROM_SYSTEM |
331 FORMAT_MESSAGE_IGNORE_INSERTS,
332 NULL, /* no message source */
333 err,
334 MAKELANGID(LANG_NEUTRAL,
335 SUBLANG_DEFAULT), /* Default language */
336 (LPTSTR) &s,
337 0, /* size not used */
338 NULL); /* no args */
339 /* remove trailing cr/lf and dots */
340 while (len > 0 && (s[len-1] <= ' ' || s[len-1] == '.'))
341 s[--len] = '\0';
Barry Warsawfa5c3152000-05-02 19:27:51 +0000342 if (filename != NULL)
Guido van Rossum795e1892000-02-17 15:19:15 +0000343 v = Py_BuildValue("(iss)", err, s, filename);
344 else
345 v = Py_BuildValue("(is)", err, s);
346 if (v != NULL) {
Guido van Rossum0b556702000-03-02 13:55:01 +0000347 PyErr_SetObject(PyExc_WindowsError, v);
Guido van Rossum795e1892000-02-17 15:19:15 +0000348 Py_DECREF(v);
349 }
350 LocalFree(s);
351 return NULL;
352}
353
354PyObject *PyErr_SetFromWindowsErr(int ierr)
355{
356 return PyErr_SetFromWindowsErrWithFilename(ierr, NULL);
357
358}
359#endif /* MS_WINDOWS */
360
Guido van Rossum683a0721990-10-21 22:09:12 +0000361void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000362PyErr_BadInternalCall(void)
Guido van Rossum683a0721990-10-21 22:09:12 +0000363{
Guido van Rossuma027efa1997-05-05 20:56:21 +0000364 PyErr_SetString(PyExc_SystemError,
365 "bad argument to internal function");
Guido van Rossum683a0721990-10-21 22:09:12 +0000366}
Guido van Rossum1548bac1997-02-14 17:09:47 +0000367
368
Guido van Rossum1548bac1997-02-14 17:09:47 +0000369PyObject *
370PyErr_Format(PyObject *exception, const char *format, ...)
Guido van Rossum1548bac1997-02-14 17:09:47 +0000371{
372 va_list vargs;
373 char buffer[500]; /* Caller is responsible for limiting the format */
Guido van Rossum1548bac1997-02-14 17:09:47 +0000374
Guido van Rossum1548bac1997-02-14 17:09:47 +0000375 va_start(vargs, format);
Guido van Rossum1548bac1997-02-14 17:09:47 +0000376
377 vsprintf(buffer, format, vargs);
378 PyErr_SetString(exception, buffer);
379 return NULL;
380}
Guido van Rossum7617e051997-09-16 18:43:50 +0000381
382
383PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000384PyErr_NewException(char *name, PyObject *base, PyObject *dict)
Guido van Rossum7617e051997-09-16 18:43:50 +0000385{
Guido van Rossum2ac650f1997-10-03 19:50:55 +0000386 char *dot;
387 PyObject *modulename = NULL;
388 PyObject *classname = NULL;
389 PyObject *mydict = NULL;
390 PyObject *bases = NULL;
Guido van Rossum7617e051997-09-16 18:43:50 +0000391 PyObject *result = NULL;
Guido van Rossum2ac650f1997-10-03 19:50:55 +0000392 dot = strrchr(name, '.');
393 if (dot == NULL) {
394 PyErr_SetString(PyExc_SystemError,
395 "PyErr_NewException: name must be module.class");
Guido van Rossum7617e051997-09-16 18:43:50 +0000396 return NULL;
Guido van Rossum7617e051997-09-16 18:43:50 +0000397 }
398 if (base == NULL)
399 base = PyExc_Exception;
Guido van Rossum2ac650f1997-10-03 19:50:55 +0000400 if (!PyClass_Check(base)) {
401 /* Must be using string-based standard exceptions (-X) */
402 return PyString_FromString(name);
403 }
404 if (dict == NULL) {
405 dict = mydict = PyDict_New();
406 if (dict == NULL)
407 goto failure;
408 }
409 if (PyDict_GetItemString(dict, "__module__") == NULL) {
410 modulename = PyString_FromStringAndSize(name, (int)(dot-name));
411 if (modulename == NULL)
412 goto failure;
413 if (PyDict_SetItemString(dict, "__module__", modulename) != 0)
414 goto failure;
415 }
416 classname = PyString_FromString(dot+1);
417 if (classname == NULL)
Guido van Rossum7617e051997-09-16 18:43:50 +0000418 goto failure;
Guido van Rossum2ac650f1997-10-03 19:50:55 +0000419 bases = Py_BuildValue("(O)", base);
420 if (bases == NULL)
421 goto failure;
422 result = PyClass_New(bases, dict, classname);
Guido van Rossum7617e051997-09-16 18:43:50 +0000423 failure:
Guido van Rossum2ac650f1997-10-03 19:50:55 +0000424 Py_XDECREF(bases);
425 Py_XDECREF(mydict);
426 Py_XDECREF(classname);
427 Py_XDECREF(modulename);
Guido van Rossum7617e051997-09-16 18:43:50 +0000428 return result;
429}