blob: 28c1ea55a3a067ae40dbdb06f1565d15cc9a8c77 [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
Guido van Rossum53e8d441995-03-09 12:11:31 +00006#ifndef __STDC__
Guido van Rossum7844e381997-04-11 20:44:04 +00007#ifndef MS_WINDOWS
Tim Petersdbd9ba62000-07-09 03:09:57 +00008extern char *strerror(int);
Guido van Rossum53e8d441995-03-09 12:11:31 +00009#endif
Guido van Rossum7844e381997-04-11 20:44:04 +000010#endif
Guido van Rossumf5401bd1990-11-02 17:50:28 +000011
Martin v. Löwis6238d2b2002-06-30 15:26:10 +000012#ifdef MS_WINDOWS
Guido van Rossum743007d1999-04-21 15:27:31 +000013#include "windows.h"
14#include "winbase.h"
15#endif
16
Jeremy Hyltonb69a27e2000-09-01 03:49:47 +000017#include <ctype.h>
18
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000019#ifdef __cplusplus
20extern "C" {
21#endif
22
23
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000024void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000025PyErr_Restore(PyObject *type, PyObject *value, PyObject *traceback)
Guido van Rossum1ae940a1995-01-02 19:04:15 +000026{
Guido van Rossum885553e1998-12-21 18:33:30 +000027 PyThreadState *tstate = PyThreadState_GET();
Guido van Rossuma027efa1997-05-05 20:56:21 +000028 PyObject *oldtype, *oldvalue, *oldtraceback;
Guido van Rossum1ae940a1995-01-02 19:04:15 +000029
Guido van Rossuma027efa1997-05-05 20:56:21 +000030 if (traceback != NULL && !PyTraceBack_Check(traceback)) {
31 /* XXX Should never happen -- fatal error instead? */
Guido van Rossum1a5e21e2006-02-28 21:57:43 +000032 /* Well, it could be None. */
Guido van Rossuma027efa1997-05-05 20:56:21 +000033 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{
Thomas Wouters303de6a2006-04-20 22:42:37 +000055 if (exception != NULL &&
56 !PyExceptionClass_Check(exception)) {
57 PyObject *excstr = PyObject_Repr(exception);
58 PyErr_Format(PyExc_SystemError,
59 "exception %s not a BaseException subclass",
60 PyString_AS_STRING(excstr));
61 Py_DECREF(excstr);
62 return;
63 }
Guido van Rossum373c8691997-04-29 18:22:47 +000064 Py_XINCREF(exception);
65 Py_XINCREF(value);
66 PyErr_Restore(exception, value, (PyObject *)NULL);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000067}
68
69void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000070PyErr_SetNone(PyObject *exception)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000071{
Guido van Rossum373c8691997-04-29 18:22:47 +000072 PyErr_SetObject(exception, (PyObject *)NULL);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000073}
74
75void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000076PyErr_SetString(PyObject *exception, const char *string)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000077{
Guido van Rossum373c8691997-04-29 18:22:47 +000078 PyObject *value = PyString_FromString(string);
79 PyErr_SetObject(exception, value);
80 Py_XDECREF(value);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000081}
82
Guido van Rossum3a241811994-08-29 12:14:12 +000083
Guido van Rossum373c8691997-04-29 18:22:47 +000084PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000085PyErr_Occurred(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000086{
Tim Peters024da352001-05-30 06:09:50 +000087 PyThreadState *tstate = PyThreadState_GET();
Guido van Rossuma027efa1997-05-05 20:56:21 +000088
89 return tstate->curexc_type;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000090}
91
Barry Warsawc0dc92a1997-08-22 21:22:58 +000092
93int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000094PyErr_GivenExceptionMatches(PyObject *err, PyObject *exc)
Barry Warsawc0dc92a1997-08-22 21:22:58 +000095{
Barry Warsawfa5c3152000-05-02 19:27:51 +000096 if (err == NULL || exc == NULL) {
97 /* maybe caused by "import exceptions" that failed early on */
98 return 0;
99 }
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000100 if (PyTuple_Check(exc)) {
Martin v. Löwisd96ee902006-02-16 14:37:16 +0000101 Py_ssize_t i, n;
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000102 n = PyTuple_Size(exc);
103 for (i = 0; i < n; i++) {
104 /* Test recursively */
105 if (PyErr_GivenExceptionMatches(
106 err, PyTuple_GET_ITEM(exc, i)))
107 {
108 return 1;
109 }
110 }
111 return 0;
112 }
113 /* err might be an instance, so check its class. */
Brett Cannonbf364092006-03-01 04:25:17 +0000114 if (PyExceptionInstance_Check(err))
115 err = PyExceptionInstance_Class(err);
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000116
Brett Cannonbf364092006-03-01 04:25:17 +0000117 if (PyExceptionClass_Check(err) && PyExceptionClass_Check(exc)) {
118 /* problems here!? not sure PyObject_IsSubclass expects to
119 be called with an exception pending... */
120 return PyObject_IsSubclass(err, exc);
121 }
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000122
123 return err == exc;
124}
Guido van Rossum743007d1999-04-21 15:27:31 +0000125
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000126
127int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000128PyErr_ExceptionMatches(PyObject *exc)
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000129{
130 return PyErr_GivenExceptionMatches(PyErr_Occurred(), exc);
131}
132
133
134/* Used in many places to normalize a raised exception, including in
135 eval_code2(), do_raise(), and PyErr_Print()
136*/
137void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000138PyErr_NormalizeException(PyObject **exc, PyObject **val, PyObject **tb)
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000139{
140 PyObject *type = *exc;
141 PyObject *value = *val;
142 PyObject *inclass = NULL;
Jeremy Hyltone2e2c9f2001-09-26 19:58:38 +0000143 PyObject *initial_tb = NULL;
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000144
Guido van Rossumed473a42000-08-07 19:18:27 +0000145 if (type == NULL) {
Guido van Rossum6b3fffa2003-04-10 20:29:48 +0000146 /* There was no exception, so nothing to do. */
147 return;
Guido van Rossumed473a42000-08-07 19:18:27 +0000148 }
149
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000150 /* If PyErr_SetNone() was used, the value will have been actually
151 set to NULL.
152 */
153 if (!value) {
154 value = Py_None;
155 Py_INCREF(value);
156 }
157
Brett Cannonbf364092006-03-01 04:25:17 +0000158 if (PyExceptionInstance_Check(value))
159 inclass = PyExceptionInstance_Class(value);
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000160
161 /* Normalize the exception so that if the type is a class, the
162 value will be an instance.
163 */
Brett Cannonbf364092006-03-01 04:25:17 +0000164 if (PyExceptionClass_Check(type)) {
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000165 /* if the value was not an instance, or is not an instance
166 whose class is (or is derived from) type, then use the
167 value as an argument to instantiation of the type
168 class.
169 */
Brett Cannonbf364092006-03-01 04:25:17 +0000170 if (!inclass || !PyObject_IsSubclass(inclass, type)) {
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000171 PyObject *args, *res;
172
173 if (value == Py_None)
Raymond Hettinger8ae46892003-10-12 19:09:37 +0000174 args = PyTuple_New(0);
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000175 else if (PyTuple_Check(value)) {
176 Py_INCREF(value);
177 args = value;
178 }
179 else
Raymond Hettinger8ae46892003-10-12 19:09:37 +0000180 args = PyTuple_Pack(1, value);
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000181
182 if (args == NULL)
183 goto finally;
184 res = PyEval_CallObject(type, args);
185 Py_DECREF(args);
186 if (res == NULL)
187 goto finally;
188 Py_DECREF(value);
189 value = res;
190 }
Barry Warsaw3a749931997-09-30 15:00:18 +0000191 /* if the class of the instance doesn't exactly match the
192 class of the type, believe the instance
193 */
194 else if (inclass != type) {
195 Py_DECREF(type);
196 type = inclass;
197 Py_INCREF(type);
198 }
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000199 }
200 *exc = type;
201 *val = value;
202 return;
203finally:
Guido van Rossum19b55f21997-12-09 14:11:39 +0000204 Py_DECREF(type);
205 Py_DECREF(value);
Jeremy Hyltone2e2c9f2001-09-26 19:58:38 +0000206 /* If the new exception doesn't set a traceback and the old
207 exception had a traceback, use the old traceback for the
208 new exception. It's better than nothing.
209 */
210 initial_tb = *tb;
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000211 PyErr_Fetch(exc, val, tb);
Jeremy Hyltone2e2c9f2001-09-26 19:58:38 +0000212 if (initial_tb != NULL) {
213 if (*tb == NULL)
214 *tb = initial_tb;
215 else
216 Py_DECREF(initial_tb);
217 }
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000218 /* normalize recursively */
219 PyErr_NormalizeException(exc, val, tb);
220}
221
222
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000223void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000224PyErr_Fetch(PyObject **p_type, PyObject **p_value, PyObject **p_traceback)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000225{
Nicholas Bastine5662ae2004-03-24 22:22:12 +0000226 PyThreadState *tstate = PyThreadState_GET();
Guido van Rossuma027efa1997-05-05 20:56:21 +0000227
228 *p_type = tstate->curexc_type;
229 *p_value = tstate->curexc_value;
230 *p_traceback = tstate->curexc_traceback;
231
232 tstate->curexc_type = NULL;
233 tstate->curexc_value = NULL;
234 tstate->curexc_traceback = NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000235}
236
237void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000238PyErr_Clear(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000239{
Guido van Rossuma027efa1997-05-05 20:56:21 +0000240 PyErr_Restore(NULL, NULL, NULL);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000241}
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000242
243/* Convenience functions to set a type error exception and return 0 */
244
245int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000246PyErr_BadArgument(void)
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000247{
Guido van Rossuma027efa1997-05-05 20:56:21 +0000248 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +0000249 "bad argument type for built-in operation");
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000250 return 0;
251}
252
Guido van Rossum373c8691997-04-29 18:22:47 +0000253PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000254PyErr_NoMemory(void)
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000255{
Vladimir Marangozov0888ff12000-08-18 18:01:06 +0000256 if (PyErr_ExceptionMatches(PyExc_MemoryError))
257 /* already current */
258 return NULL;
259
Barry Warsaw2d8adff1997-08-29 21:54:35 +0000260 /* raise the pre-allocated instance if it still exists */
261 if (PyExc_MemoryErrorInst)
262 PyErr_SetObject(PyExc_MemoryError, PyExc_MemoryErrorInst);
263 else
264 /* this will probably fail since there's no memory and hee,
265 hee, we have to instantiate this class
266 */
267 PyErr_SetNone(PyExc_MemoryError);
268
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000269 return NULL;
270}
271
Guido van Rossum373c8691997-04-29 18:22:47 +0000272PyObject *
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000273PyErr_SetFromErrnoWithFilenameObject(PyObject *exc, PyObject *filenameObject)
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000274{
Guido van Rossum373c8691997-04-29 18:22:47 +0000275 PyObject *v;
Guido van Rossume0e59821998-10-14 20:38:13 +0000276 char *s;
Guido van Rossum3a241811994-08-29 12:14:12 +0000277 int i = errno;
Martin v. Löwis3484a182002-03-09 12:07:51 +0000278#ifdef PLAN9
279 char errbuf[ERRMAX];
280#endif
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000281#ifdef MS_WINDOWS
Guido van Rossum795e1892000-02-17 15:19:15 +0000282 char *s_buf = NULL;
Mark Hammond3d61a062002-10-04 00:13:02 +0000283 char s_small_buf[28]; /* Room for "Windows Error 0xFFFFFFFF" */
Guido van Rossum795e1892000-02-17 15:19:15 +0000284#endif
Guido van Rossume9fbc091995-02-18 14:52:19 +0000285#ifdef EINTR
Guido van Rossum373c8691997-04-29 18:22:47 +0000286 if (i == EINTR && PyErr_CheckSignals())
Guido van Rossum5063bab1991-10-20 20:14:56 +0000287 return NULL;
Guido van Rossume9fbc091995-02-18 14:52:19 +0000288#endif
Martin v. Löwis3484a182002-03-09 12:07:51 +0000289#ifdef PLAN9
290 rerrstr(errbuf, sizeof errbuf);
291 s = errbuf;
292#else
Guido van Rossume0e59821998-10-14 20:38:13 +0000293 if (i == 0)
294 s = "Error"; /* Sometimes errno didn't get set */
Barry Warsaw97d95151998-07-23 16:05:56 +0000295 else
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000296#ifndef MS_WINDOWS
Guido van Rossume0e59821998-10-14 20:38:13 +0000297 s = strerror(i);
Guido van Rossum743007d1999-04-21 15:27:31 +0000298#else
299 {
Guido van Rossum795e1892000-02-17 15:19:15 +0000300 /* Note that the Win32 errors do not lineup with the
301 errno error. So if the error is in the MSVC error
Brett Cannonbf364092006-03-01 04:25:17 +0000302 table, we use it, otherwise we assume it really _is_
Guido van Rossum795e1892000-02-17 15:19:15 +0000303 a Win32 error code
304 */
Guido van Rossum584b16a2000-02-21 16:50:31 +0000305 if (i > 0 && i < _sys_nerr) {
Guido van Rossum795e1892000-02-17 15:19:15 +0000306 s = _sys_errlist[i];
307 }
308 else {
309 int len = FormatMessage(
310 FORMAT_MESSAGE_ALLOCATE_BUFFER |
311 FORMAT_MESSAGE_FROM_SYSTEM |
312 FORMAT_MESSAGE_IGNORE_INSERTS,
313 NULL, /* no message source */
314 i,
315 MAKELANGID(LANG_NEUTRAL,
316 SUBLANG_DEFAULT),
317 /* Default language */
318 (LPTSTR) &s_buf,
319 0, /* size not used */
320 NULL); /* no args */
Mark Hammond3d61a062002-10-04 00:13:02 +0000321 if (len==0) {
Brett Cannonbf364092006-03-01 04:25:17 +0000322 /* Only ever seen this in out-of-mem
Mark Hammond3d61a062002-10-04 00:13:02 +0000323 situations */
324 sprintf(s_small_buf, "Windows Error 0x%X", i);
325 s = s_small_buf;
326 s_buf = NULL;
327 } else {
328 s = s_buf;
329 /* remove trailing cr/lf and dots */
330 while (len > 0 && (s[len-1] <= ' ' || s[len-1] == '.'))
331 s[--len] = '\0';
332 }
Guido van Rossum795e1892000-02-17 15:19:15 +0000333 }
Guido van Rossum743007d1999-04-21 15:27:31 +0000334 }
Martin v. Löwis3484a182002-03-09 12:07:51 +0000335#endif /* Unix/Windows */
336#endif /* PLAN 9*/
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000337 if (filenameObject != NULL)
338 v = Py_BuildValue("(isO)", i, s, filenameObject);
Guido van Rossume0e59821998-10-14 20:38:13 +0000339 else
340 v = Py_BuildValue("(is)", i, s);
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000341 if (v != NULL) {
Guido van Rossum373c8691997-04-29 18:22:47 +0000342 PyErr_SetObject(exc, v);
343 Py_DECREF(v);
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000344 }
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000345#ifdef MS_WINDOWS
Guido van Rossum795e1892000-02-17 15:19:15 +0000346 LocalFree(s_buf);
Guido van Rossum743007d1999-04-21 15:27:31 +0000347#endif
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000348 return NULL;
349}
Guido van Rossum743007d1999-04-21 15:27:31 +0000350
Barry Warsaw97d95151998-07-23 16:05:56 +0000351
352PyObject *
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000353PyErr_SetFromErrnoWithFilename(PyObject *exc, char *filename)
354{
355 PyObject *name = filename ? PyString_FromString(filename) : NULL;
356 PyObject *result = PyErr_SetFromErrnoWithFilenameObject(exc, name);
Mark Hammondda7efaa2002-10-04 00:09:38 +0000357 Py_XDECREF(name);
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000358 return result;
359}
360
361#ifdef Py_WIN_WIDE_FILENAMES
362PyObject *
363PyErr_SetFromErrnoWithUnicodeFilename(PyObject *exc, Py_UNICODE *filename)
364{
Brett Cannonbf364092006-03-01 04:25:17 +0000365 PyObject *name = filename ?
366 PyUnicode_FromUnicode(filename, wcslen(filename)) :
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000367 NULL;
368 PyObject *result = PyErr_SetFromErrnoWithFilenameObject(exc, name);
369 Py_XDECREF(name);
370 return result;
371}
372#endif /* Py_WIN_WIDE_FILENAMES */
373
374PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000375PyErr_SetFromErrno(PyObject *exc)
Barry Warsaw97d95151998-07-23 16:05:56 +0000376{
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000377 return PyErr_SetFromErrnoWithFilenameObject(exc, NULL);
Barry Warsaw97d95151998-07-23 16:05:56 +0000378}
Guido van Rossum683a0721990-10-21 22:09:12 +0000379
Brett Cannonbf364092006-03-01 04:25:17 +0000380#ifdef MS_WINDOWS
Guido van Rossum795e1892000-02-17 15:19:15 +0000381/* Windows specific error code handling */
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000382PyObject *PyErr_SetExcFromWindowsErrWithFilenameObject(
Thomas Heller085358a2002-07-29 14:27:41 +0000383 PyObject *exc,
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000384 int ierr,
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000385 PyObject *filenameObject)
Guido van Rossum795e1892000-02-17 15:19:15 +0000386{
387 int len;
388 char *s;
Mark Hammond3d61a062002-10-04 00:13:02 +0000389 char *s_buf = NULL; /* Free via LocalFree */
390 char s_small_buf[28]; /* Room for "Windows Error 0xFFFFFFFF" */
Guido van Rossum795e1892000-02-17 15:19:15 +0000391 PyObject *v;
392 DWORD err = (DWORD)ierr;
393 if (err==0) err = GetLastError();
394 len = FormatMessage(
395 /* Error API error */
396 FORMAT_MESSAGE_ALLOCATE_BUFFER |
397 FORMAT_MESSAGE_FROM_SYSTEM |
398 FORMAT_MESSAGE_IGNORE_INSERTS,
399 NULL, /* no message source */
400 err,
401 MAKELANGID(LANG_NEUTRAL,
402 SUBLANG_DEFAULT), /* Default language */
Mark Hammond3d61a062002-10-04 00:13:02 +0000403 (LPTSTR) &s_buf,
Guido van Rossum795e1892000-02-17 15:19:15 +0000404 0, /* size not used */
405 NULL); /* no args */
Mark Hammond3d61a062002-10-04 00:13:02 +0000406 if (len==0) {
407 /* Only seen this in out of mem situations */
408 sprintf(s_small_buf, "Windows Error 0x%X", err);
409 s = s_small_buf;
410 s_buf = NULL;
411 } else {
412 s = s_buf;
413 /* remove trailing cr/lf and dots */
414 while (len > 0 && (s[len-1] <= ' ' || s[len-1] == '.'))
415 s[--len] = '\0';
416 }
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000417 if (filenameObject != NULL)
418 v = Py_BuildValue("(isO)", err, s, filenameObject);
Guido van Rossum795e1892000-02-17 15:19:15 +0000419 else
420 v = Py_BuildValue("(is)", err, s);
421 if (v != NULL) {
Thomas Heller085358a2002-07-29 14:27:41 +0000422 PyErr_SetObject(exc, v);
Guido van Rossum795e1892000-02-17 15:19:15 +0000423 Py_DECREF(v);
424 }
Mark Hammond3d61a062002-10-04 00:13:02 +0000425 LocalFree(s_buf);
Guido van Rossum795e1892000-02-17 15:19:15 +0000426 return NULL;
427}
428
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000429PyObject *PyErr_SetExcFromWindowsErrWithFilename(
430 PyObject *exc,
431 int ierr,
432 const char *filename)
433{
434 PyObject *name = filename ? PyString_FromString(filename) : NULL;
Brett Cannonbf364092006-03-01 04:25:17 +0000435 PyObject *ret = PyErr_SetExcFromWindowsErrWithFilenameObject(exc,
436 ierr,
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000437 name);
438 Py_XDECREF(name);
439 return ret;
440}
441
442#ifdef Py_WIN_WIDE_FILENAMES
443PyObject *PyErr_SetExcFromWindowsErrWithUnicodeFilename(
444 PyObject *exc,
445 int ierr,
446 const Py_UNICODE *filename)
447{
Brett Cannonbf364092006-03-01 04:25:17 +0000448 PyObject *name = filename ?
449 PyUnicode_FromUnicode(filename, wcslen(filename)) :
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000450 NULL;
Brett Cannonbf364092006-03-01 04:25:17 +0000451 PyObject *ret = PyErr_SetExcFromWindowsErrWithFilenameObject(exc,
452 ierr,
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000453 name);
454 Py_XDECREF(name);
455 return ret;
456}
457#endif /* Py_WIN_WIDE_FILENAMES */
458
Thomas Heller085358a2002-07-29 14:27:41 +0000459PyObject *PyErr_SetExcFromWindowsErr(PyObject *exc, int ierr)
460{
461 return PyErr_SetExcFromWindowsErrWithFilename(exc, ierr, NULL);
462}
463
Guido van Rossum795e1892000-02-17 15:19:15 +0000464PyObject *PyErr_SetFromWindowsErr(int ierr)
465{
Thomas Heller085358a2002-07-29 14:27:41 +0000466 return PyErr_SetExcFromWindowsErrWithFilename(PyExc_WindowsError,
467 ierr, NULL);
468}
469PyObject *PyErr_SetFromWindowsErrWithFilename(
470 int ierr,
471 const char *filename)
472{
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000473 PyObject *name = filename ? PyString_FromString(filename) : NULL;
474 PyObject *result = PyErr_SetExcFromWindowsErrWithFilenameObject(
475 PyExc_WindowsError,
476 ierr, name);
Mark Hammondda7efaa2002-10-04 00:09:38 +0000477 Py_XDECREF(name);
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000478 return result;
Guido van Rossum795e1892000-02-17 15:19:15 +0000479}
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000480
481#ifdef Py_WIN_WIDE_FILENAMES
482PyObject *PyErr_SetFromWindowsErrWithUnicodeFilename(
483 int ierr,
484 const Py_UNICODE *filename)
485{
Brett Cannonbf364092006-03-01 04:25:17 +0000486 PyObject *name = filename ?
487 PyUnicode_FromUnicode(filename, wcslen(filename)) :
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000488 NULL;
489 PyObject *result = PyErr_SetExcFromWindowsErrWithFilenameObject(
490 PyExc_WindowsError,
491 ierr, name);
Mark Hammondda7efaa2002-10-04 00:09:38 +0000492 Py_XDECREF(name);
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000493 return result;
494}
495#endif /* Py_WIN_WIDE_FILENAMES */
Guido van Rossum795e1892000-02-17 15:19:15 +0000496#endif /* MS_WINDOWS */
497
Guido van Rossum683a0721990-10-21 22:09:12 +0000498void
Fred Drake6d63adf2000-08-24 22:38:39 +0000499_PyErr_BadInternalCall(char *filename, int lineno)
500{
501 PyErr_Format(PyExc_SystemError,
502 "%s:%d: bad argument to internal function",
503 filename, lineno);
504}
505
506/* Remove the preprocessor macro for PyErr_BadInternalCall() so that we can
507 export the entry point for existing object code: */
508#undef PyErr_BadInternalCall
509void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000510PyErr_BadInternalCall(void)
Guido van Rossum683a0721990-10-21 22:09:12 +0000511{
Fred Drake6d63adf2000-08-24 22:38:39 +0000512 PyErr_Format(PyExc_SystemError,
513 "bad argument to internal function");
Guido van Rossum683a0721990-10-21 22:09:12 +0000514}
Fred Drake6d63adf2000-08-24 22:38:39 +0000515#define PyErr_BadInternalCall() _PyErr_BadInternalCall(__FILE__, __LINE__)
516
Guido van Rossum1548bac1997-02-14 17:09:47 +0000517
518
Guido van Rossum1548bac1997-02-14 17:09:47 +0000519PyObject *
520PyErr_Format(PyObject *exception, const char *format, ...)
Guido van Rossum1548bac1997-02-14 17:09:47 +0000521{
522 va_list vargs;
Jeremy Hyltonb69a27e2000-09-01 03:49:47 +0000523 PyObject* string;
Guido van Rossum1548bac1997-02-14 17:09:47 +0000524
Jeremy Hyltonb69a27e2000-09-01 03:49:47 +0000525#ifdef HAVE_STDARG_PROTOTYPES
Guido van Rossum1548bac1997-02-14 17:09:47 +0000526 va_start(vargs, format);
Jeremy Hyltonb69a27e2000-09-01 03:49:47 +0000527#else
528 va_start(vargs);
529#endif
Guido van Rossum1548bac1997-02-14 17:09:47 +0000530
Barry Warsaw876c8cb2001-08-24 18:35:23 +0000531 string = PyString_FromFormatV(format, vargs);
Jeremy Hyltonb69a27e2000-09-01 03:49:47 +0000532 PyErr_SetObject(exception, string);
533 Py_XDECREF(string);
Tim Petersc15c4f12001-10-02 21:32:07 +0000534 va_end(vargs);
Guido van Rossum1548bac1997-02-14 17:09:47 +0000535 return NULL;
536}
Guido van Rossum7617e051997-09-16 18:43:50 +0000537
538
Thomas Wouters477c8d52006-05-27 19:21:47 +0000539
Guido van Rossum7617e051997-09-16 18:43:50 +0000540PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000541PyErr_NewException(char *name, PyObject *base, PyObject *dict)
Guido van Rossum7617e051997-09-16 18:43:50 +0000542{
Guido van Rossum2ac650f1997-10-03 19:50:55 +0000543 char *dot;
544 PyObject *modulename = NULL;
545 PyObject *classname = NULL;
546 PyObject *mydict = NULL;
547 PyObject *bases = NULL;
Guido van Rossum7617e051997-09-16 18:43:50 +0000548 PyObject *result = NULL;
Guido van Rossum2ac650f1997-10-03 19:50:55 +0000549 dot = strrchr(name, '.');
550 if (dot == NULL) {
551 PyErr_SetString(PyExc_SystemError,
552 "PyErr_NewException: name must be module.class");
Guido van Rossum7617e051997-09-16 18:43:50 +0000553 return NULL;
Guido van Rossum7617e051997-09-16 18:43:50 +0000554 }
555 if (base == NULL)
556 base = PyExc_Exception;
Guido van Rossum2ac650f1997-10-03 19:50:55 +0000557 if (dict == NULL) {
558 dict = mydict = PyDict_New();
559 if (dict == NULL)
560 goto failure;
561 }
562 if (PyDict_GetItemString(dict, "__module__") == NULL) {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000563 modulename = PyString_FromStringAndSize(name,
564 (Py_ssize_t)(dot-name));
Guido van Rossum2ac650f1997-10-03 19:50:55 +0000565 if (modulename == NULL)
566 goto failure;
567 if (PyDict_SetItemString(dict, "__module__", modulename) != 0)
568 goto failure;
569 }
Thomas Wouters477c8d52006-05-27 19:21:47 +0000570 if (PyTuple_Check(base)) {
571 bases = base;
572 /* INCREF as we create a new ref in the else branch */
573 Py_INCREF(bases);
574 } else {
575 bases = PyTuple_Pack(1, base);
576 if (bases == NULL)
577 goto failure;
578 }
579 /* Create a real new-style class. */
580 result = PyObject_CallFunction((PyObject *)&PyType_Type, "sOO",
581 dot+1, bases, dict);
Guido van Rossum7617e051997-09-16 18:43:50 +0000582 failure:
Guido van Rossum2ac650f1997-10-03 19:50:55 +0000583 Py_XDECREF(bases);
584 Py_XDECREF(mydict);
585 Py_XDECREF(classname);
586 Py_XDECREF(modulename);
Guido van Rossum7617e051997-09-16 18:43:50 +0000587 return result;
588}
Jeremy Hyltonb709df32000-09-01 02:47:25 +0000589
590/* Call when an exception has occurred but there is no way for Python
591 to handle it. Examples: exception in __del__ or during GC. */
592void
593PyErr_WriteUnraisable(PyObject *obj)
594{
595 PyObject *f, *t, *v, *tb;
596 PyErr_Fetch(&t, &v, &tb);
597 f = PySys_GetObject("stderr");
598 if (f != NULL) {
599 PyFile_WriteString("Exception ", f);
600 if (t) {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000601 PyObject* moduleName;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000602 char* className = PyExceptionClass_Name(t);
Brett Cannonbf364092006-03-01 04:25:17 +0000603
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000604 if (className != NULL) {
605 char *dot = strrchr(className, '.');
606 if (dot != NULL)
607 className = dot+1;
608 }
609
610 moduleName = PyObject_GetAttrString(t, "__module__");
Brett Cannonbf364092006-03-01 04:25:17 +0000611 if (moduleName == NULL)
612 PyFile_WriteString("<unknown>", f);
613 else {
614 char* modstr = PyString_AsString(moduleName);
615 if (modstr)
616 {
617 PyFile_WriteString(modstr, f);
618 PyFile_WriteString(".", f);
619 }
620 }
621 if (className == NULL)
622 PyFile_WriteString("<unknown>", f);
623 else
624 PyFile_WriteString(className, f);
Jeremy Hyltonb709df32000-09-01 02:47:25 +0000625 if (v && v != Py_None) {
626 PyFile_WriteString(": ", f);
627 PyFile_WriteObject(v, f, 0);
628 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000629 Py_XDECREF(moduleName);
Jeremy Hyltonb709df32000-09-01 02:47:25 +0000630 }
631 PyFile_WriteString(" in ", f);
632 PyFile_WriteObject(obj, f, 0);
633 PyFile_WriteString(" ignored\n", f);
634 PyErr_Clear(); /* Just in case */
635 }
636 Py_XDECREF(t);
637 Py_XDECREF(v);
638 Py_XDECREF(tb);
639}
Guido van Rossumcfd42b52000-12-15 21:58:52 +0000640
Armin Rigo092381a2003-10-25 14:29:27 +0000641extern PyObject *PyModule_GetWarningsModule(void);
Guido van Rossumcfd42b52000-12-15 21:58:52 +0000642
643/* Function to issue a warning message; may raise an exception. */
644int
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000645PyErr_WarnEx(PyObject *category, const char *message, Py_ssize_t stack_level)
Guido van Rossumcfd42b52000-12-15 21:58:52 +0000646{
Mark Hammonda43fd0c2003-02-19 00:33:33 +0000647 PyObject *dict, *func = NULL;
Mark Hammondedd07732003-07-15 23:03:55 +0000648 PyObject *warnings_module = PyModule_GetWarningsModule();
Guido van Rossumcfd42b52000-12-15 21:58:52 +0000649
Mark Hammondedd07732003-07-15 23:03:55 +0000650 if (warnings_module != NULL) {
651 dict = PyModule_GetDict(warnings_module);
Guido van Rossumcfd42b52000-12-15 21:58:52 +0000652 func = PyDict_GetItemString(dict, "warn");
Guido van Rossumcfd42b52000-12-15 21:58:52 +0000653 }
654 if (func == NULL) {
655 PySys_WriteStderr("warning: %s\n", message);
656 return 0;
657 }
658 else {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000659 PyObject *res;
Guido van Rossumcfd42b52000-12-15 21:58:52 +0000660
661 if (category == NULL)
662 category = PyExc_RuntimeWarning;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000663 res = PyObject_CallFunction(func, "sOn",
664 message, category, stack_level);
Guido van Rossumcfd42b52000-12-15 21:58:52 +0000665 if (res == NULL)
666 return -1;
667 Py_DECREF(res);
668 return 0;
669 }
670}
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000671
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000672/* PyErr_Warn is only for backwards compatability and will be removed.
673 Use PyErr_WarnEx instead. */
674
675#undef PyErr_Warn
676
677PyAPI_FUNC(int)
678PyErr_Warn(PyObject *category, char *message)
679{
680 return PyErr_WarnEx(category, message, 1);
681}
Guido van Rossum2fd45652001-02-28 21:46:24 +0000682
683/* Warning with explicit origin */
684int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000685PyErr_WarnExplicit(PyObject *category, const char *message,
686 const char *filename, int lineno,
687 const char *module, PyObject *registry)
Guido van Rossum2fd45652001-02-28 21:46:24 +0000688{
689 PyObject *mod, *dict, *func = NULL;
690
691 mod = PyImport_ImportModule("warnings");
692 if (mod != NULL) {
693 dict = PyModule_GetDict(mod);
694 func = PyDict_GetItemString(dict, "warn_explicit");
695 Py_DECREF(mod);
696 }
697 if (func == NULL) {
698 PySys_WriteStderr("warning: %s\n", message);
699 return 0;
700 }
701 else {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000702 PyObject *res;
Guido van Rossum2fd45652001-02-28 21:46:24 +0000703
704 if (category == NULL)
705 category = PyExc_RuntimeWarning;
706 if (registry == NULL)
707 registry = Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000708 res = PyObject_CallFunction(func, "sOsizO", message, category,
709 filename, lineno, module, registry);
Guido van Rossum2fd45652001-02-28 21:46:24 +0000710 if (res == NULL)
711 return -1;
712 Py_DECREF(res);
713 return 0;
714 }
715}
716
717
Martin v. Löwiscfeb3b62002-03-03 21:30:27 +0000718/* Set file and line information for the current exception.
719 If the exception is not a SyntaxError, also sets additional attributes
720 to make printing of exceptions believe it is a syntax error. */
Guido van Rossum2fd45652001-02-28 21:46:24 +0000721
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000722void
Martin v. Löwis95292d62002-12-11 14:04:59 +0000723PyErr_SyntaxLocation(const char *filename, int lineno)
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000724{
725 PyObject *exc, *v, *tb, *tmp;
726
727 /* add attributes for the line number and filename for the error */
728 PyErr_Fetch(&exc, &v, &tb);
729 PyErr_NormalizeException(&exc, &v, &tb);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000730 /* XXX check that it is, indeed, a syntax error. It might not
731 * be, though. */
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000732 tmp = PyInt_FromLong(lineno);
733 if (tmp == NULL)
734 PyErr_Clear();
735 else {
736 if (PyObject_SetAttrString(v, "lineno", tmp))
737 PyErr_Clear();
738 Py_DECREF(tmp);
739 }
740 if (filename != NULL) {
741 tmp = PyString_FromString(filename);
742 if (tmp == NULL)
743 PyErr_Clear();
744 else {
745 if (PyObject_SetAttrString(v, "filename", tmp))
746 PyErr_Clear();
747 Py_DECREF(tmp);
748 }
749
750 tmp = PyErr_ProgramText(filename, lineno);
751 if (tmp) {
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000752 if (PyObject_SetAttrString(v, "text", tmp))
753 PyErr_Clear();
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000754 Py_DECREF(tmp);
755 }
756 }
Martin v. Löwiscfeb3b62002-03-03 21:30:27 +0000757 if (PyObject_SetAttrString(v, "offset", Py_None)) {
758 PyErr_Clear();
759 }
760 if (exc != PyExc_SyntaxError) {
761 if (!PyObject_HasAttrString(v, "msg")) {
762 tmp = PyObject_Str(v);
763 if (tmp) {
764 if (PyObject_SetAttrString(v, "msg", tmp))
765 PyErr_Clear();
766 Py_DECREF(tmp);
767 } else {
768 PyErr_Clear();
769 }
770 }
771 if (!PyObject_HasAttrString(v, "print_file_and_line")) {
772 if (PyObject_SetAttrString(v, "print_file_and_line",
773 Py_None))
774 PyErr_Clear();
775 }
776 }
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000777 PyErr_Restore(exc, v, tb);
778}
779
780/* com_fetch_program_text will attempt to load the line of text that
781 the exception refers to. If it fails, it will return NULL but will
Brett Cannonbf364092006-03-01 04:25:17 +0000782 not set an exception.
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000783
784 XXX The functionality of this function is quite similar to the
785 functionality in tb_displayline() in traceback.c.
786*/
787
788PyObject *
Martin v. Löwis95292d62002-12-11 14:04:59 +0000789PyErr_ProgramText(const char *filename, int lineno)
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000790{
791 FILE *fp;
792 int i;
793 char linebuf[1000];
794
Tim Petersa7444f42006-02-27 23:29:46 +0000795 if (filename == NULL || *filename == '\0' || lineno <= 0)
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000796 return NULL;
Jack Jansen7b8c7542002-04-14 20:12:41 +0000797 fp = fopen(filename, "r" PY_STDIOTEXTMODE);
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000798 if (fp == NULL)
799 return NULL;
800 for (i = 0; i < lineno; i++) {
801 char *pLastChar = &linebuf[sizeof(linebuf) - 2];
802 do {
803 *pLastChar = '\0';
Jack Jansen7b8c7542002-04-14 20:12:41 +0000804 if (Py_UniversalNewlineFgets(linebuf, sizeof linebuf, fp, NULL) == NULL)
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000805 break;
806 /* fgets read *something*; if it didn't get as
807 far as pLastChar, it must have found a newline
Thomas Wouters477c8d52006-05-27 19:21:47 +0000808 or hit the end of the file; if pLastChar is \n,
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000809 it obviously found a newline; else we haven't
810 yet seen a newline, so must continue */
811 } while (*pLastChar != '\0' && *pLastChar != '\n');
812 }
813 fclose(fp);
814 if (i == lineno) {
815 char *p = linebuf;
816 while (*p == ' ' || *p == '\t' || *p == '\014')
817 p++;
818 return PyString_FromString(p);
819 }
820 return NULL;
821}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000822
823#ifdef __cplusplus
824}
825#endif
826