blob: 63acf3306654581d53f1decb95c8c3edead1c5b2 [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
Anthony Baxterac6bd462006-04-13 02:06:09 +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{
Guido van Rossum373c8691997-04-29 18:22:47 +000055 Py_XINCREF(exception);
56 Py_XINCREF(value);
57 PyErr_Restore(exception, value, (PyObject *)NULL);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000058}
59
60void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000061PyErr_SetNone(PyObject *exception)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000062{
Guido van Rossum373c8691997-04-29 18:22:47 +000063 PyErr_SetObject(exception, (PyObject *)NULL);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000064}
65
66void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000067PyErr_SetString(PyObject *exception, const char *string)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000068{
Guido van Rossum373c8691997-04-29 18:22:47 +000069 PyObject *value = PyString_FromString(string);
70 PyErr_SetObject(exception, value);
71 Py_XDECREF(value);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000072}
73
Guido van Rossum3a241811994-08-29 12:14:12 +000074
Guido van Rossum373c8691997-04-29 18:22:47 +000075PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000076PyErr_Occurred(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000077{
Tim Peters024da352001-05-30 06:09:50 +000078 PyThreadState *tstate = PyThreadState_GET();
Guido van Rossuma027efa1997-05-05 20:56:21 +000079
80 return tstate->curexc_type;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000081}
82
Barry Warsawc0dc92a1997-08-22 21:22:58 +000083
84int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000085PyErr_GivenExceptionMatches(PyObject *err, PyObject *exc)
Barry Warsawc0dc92a1997-08-22 21:22:58 +000086{
Barry Warsawfa5c3152000-05-02 19:27:51 +000087 if (err == NULL || exc == NULL) {
88 /* maybe caused by "import exceptions" that failed early on */
89 return 0;
90 }
Barry Warsawc0dc92a1997-08-22 21:22:58 +000091 if (PyTuple_Check(exc)) {
Martin v. Löwisd96ee902006-02-16 14:37:16 +000092 Py_ssize_t i, n;
Barry Warsawc0dc92a1997-08-22 21:22:58 +000093 n = PyTuple_Size(exc);
94 for (i = 0; i < n; i++) {
95 /* Test recursively */
96 if (PyErr_GivenExceptionMatches(
97 err, PyTuple_GET_ITEM(exc, i)))
98 {
99 return 1;
100 }
101 }
102 return 0;
103 }
104 /* err might be an instance, so check its class. */
Brett Cannonbf364092006-03-01 04:25:17 +0000105 if (PyExceptionInstance_Check(err))
106 err = PyExceptionInstance_Class(err);
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000107
Brett Cannonbf364092006-03-01 04:25:17 +0000108 if (PyExceptionClass_Check(err) && PyExceptionClass_Check(exc)) {
109 /* problems here!? not sure PyObject_IsSubclass expects to
110 be called with an exception pending... */
111 return PyObject_IsSubclass(err, exc);
112 }
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000113
114 return err == exc;
115}
Guido van Rossum743007d1999-04-21 15:27:31 +0000116
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000117
118int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000119PyErr_ExceptionMatches(PyObject *exc)
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000120{
121 return PyErr_GivenExceptionMatches(PyErr_Occurred(), exc);
122}
123
124
125/* Used in many places to normalize a raised exception, including in
126 eval_code2(), do_raise(), and PyErr_Print()
127*/
128void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000129PyErr_NormalizeException(PyObject **exc, PyObject **val, PyObject **tb)
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000130{
131 PyObject *type = *exc;
132 PyObject *value = *val;
133 PyObject *inclass = NULL;
Jeremy Hyltone2e2c9f2001-09-26 19:58:38 +0000134 PyObject *initial_tb = NULL;
Brett Cannon1e534b52007-09-07 04:18:30 +0000135 PyThreadState *tstate = NULL;
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000136
Guido van Rossumed473a42000-08-07 19:18:27 +0000137 if (type == NULL) {
Guido van Rossum6b3fffa2003-04-10 20:29:48 +0000138 /* There was no exception, so nothing to do. */
139 return;
Guido van Rossumed473a42000-08-07 19:18:27 +0000140 }
141
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000142 /* If PyErr_SetNone() was used, the value will have been actually
143 set to NULL.
144 */
145 if (!value) {
146 value = Py_None;
147 Py_INCREF(value);
148 }
149
Brett Cannonbf364092006-03-01 04:25:17 +0000150 if (PyExceptionInstance_Check(value))
151 inclass = PyExceptionInstance_Class(value);
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000152
153 /* Normalize the exception so that if the type is a class, the
154 value will be an instance.
155 */
Brett Cannonbf364092006-03-01 04:25:17 +0000156 if (PyExceptionClass_Check(type)) {
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000157 /* if the value was not an instance, or is not an instance
158 whose class is (or is derived from) type, then use the
159 value as an argument to instantiation of the type
160 class.
161 */
Brett Cannonbf364092006-03-01 04:25:17 +0000162 if (!inclass || !PyObject_IsSubclass(inclass, type)) {
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000163 PyObject *args, *res;
164
165 if (value == Py_None)
Raymond Hettinger8ae46892003-10-12 19:09:37 +0000166 args = PyTuple_New(0);
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000167 else if (PyTuple_Check(value)) {
168 Py_INCREF(value);
169 args = value;
170 }
171 else
Raymond Hettinger8ae46892003-10-12 19:09:37 +0000172 args = PyTuple_Pack(1, value);
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000173
174 if (args == NULL)
175 goto finally;
176 res = PyEval_CallObject(type, args);
177 Py_DECREF(args);
178 if (res == NULL)
179 goto finally;
180 Py_DECREF(value);
181 value = res;
182 }
Barry Warsaw3a749931997-09-30 15:00:18 +0000183 /* if the class of the instance doesn't exactly match the
184 class of the type, believe the instance
185 */
186 else if (inclass != type) {
187 Py_DECREF(type);
188 type = inclass;
189 Py_INCREF(type);
190 }
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000191 }
192 *exc = type;
193 *val = value;
194 return;
195finally:
Guido van Rossum19b55f21997-12-09 14:11:39 +0000196 Py_DECREF(type);
197 Py_DECREF(value);
Jeremy Hyltone2e2c9f2001-09-26 19:58:38 +0000198 /* If the new exception doesn't set a traceback and the old
199 exception had a traceback, use the old traceback for the
200 new exception. It's better than nothing.
201 */
202 initial_tb = *tb;
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000203 PyErr_Fetch(exc, val, tb);
Jeremy Hyltone2e2c9f2001-09-26 19:58:38 +0000204 if (initial_tb != NULL) {
205 if (*tb == NULL)
206 *tb = initial_tb;
207 else
208 Py_DECREF(initial_tb);
209 }
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000210 /* normalize recursively */
Brett Cannon1e534b52007-09-07 04:18:30 +0000211 tstate = PyThreadState_GET();
212 if (++tstate->recursion_depth > Py_GetRecursionLimit()) {
213 --tstate->recursion_depth;
214 PyErr_SetObject(PyExc_RuntimeError, PyExc_RecursionErrorInst);
215 return;
216 }
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000217 PyErr_NormalizeException(exc, val, tb);
Brett Cannon1e534b52007-09-07 04:18:30 +0000218 --tstate->recursion_depth;
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000219}
220
221
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000222void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000223PyErr_Fetch(PyObject **p_type, PyObject **p_value, PyObject **p_traceback)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000224{
Nicholas Bastine5662ae2004-03-24 22:22:12 +0000225 PyThreadState *tstate = PyThreadState_GET();
Guido van Rossuma027efa1997-05-05 20:56:21 +0000226
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
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000237PyErr_Clear(void)
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
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000245PyErr_BadArgument(void)
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000246{
Guido van Rossuma027efa1997-05-05 20:56:21 +0000247 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +0000248 "bad 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 *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000253PyErr_NoMemory(void)
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000254{
Vladimir Marangozov0888ff12000-08-18 18:01:06 +0000255 if (PyErr_ExceptionMatches(PyExc_MemoryError))
256 /* already current */
257 return NULL;
258
Barry Warsaw2d8adff1997-08-29 21:54:35 +0000259 /* raise the pre-allocated instance if it still exists */
260 if (PyExc_MemoryErrorInst)
261 PyErr_SetObject(PyExc_MemoryError, PyExc_MemoryErrorInst);
262 else
263 /* this will probably fail since there's no memory and hee,
264 hee, we have to instantiate this class
265 */
266 PyErr_SetNone(PyExc_MemoryError);
267
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000268 return NULL;
269}
270
Guido van Rossum373c8691997-04-29 18:22:47 +0000271PyObject *
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000272PyErr_SetFromErrnoWithFilenameObject(PyObject *exc, PyObject *filenameObject)
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000273{
Guido van Rossum373c8691997-04-29 18:22:47 +0000274 PyObject *v;
Guido van Rossume0e59821998-10-14 20:38:13 +0000275 char *s;
Guido van Rossum3a241811994-08-29 12:14:12 +0000276 int i = errno;
Martin v. Löwis3484a182002-03-09 12:07:51 +0000277#ifdef PLAN9
278 char errbuf[ERRMAX];
279#endif
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000280#ifdef MS_WINDOWS
Guido van Rossum795e1892000-02-17 15:19:15 +0000281 char *s_buf = NULL;
Mark Hammond3d61a062002-10-04 00:13:02 +0000282 char s_small_buf[28]; /* Room for "Windows Error 0xFFFFFFFF" */
Guido van Rossum795e1892000-02-17 15:19:15 +0000283#endif
Guido van Rossume9fbc091995-02-18 14:52:19 +0000284#ifdef EINTR
Guido van Rossum373c8691997-04-29 18:22:47 +0000285 if (i == EINTR && PyErr_CheckSignals())
Guido van Rossum5063bab1991-10-20 20:14:56 +0000286 return NULL;
Guido van Rossume9fbc091995-02-18 14:52:19 +0000287#endif
Martin v. Löwis3484a182002-03-09 12:07:51 +0000288#ifdef PLAN9
289 rerrstr(errbuf, sizeof errbuf);
290 s = errbuf;
291#else
Guido van Rossume0e59821998-10-14 20:38:13 +0000292 if (i == 0)
293 s = "Error"; /* Sometimes errno didn't get set */
Barry Warsaw97d95151998-07-23 16:05:56 +0000294 else
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000295#ifndef MS_WINDOWS
Guido van Rossume0e59821998-10-14 20:38:13 +0000296 s = strerror(i);
Guido van Rossum743007d1999-04-21 15:27:31 +0000297#else
298 {
Guido van Rossum795e1892000-02-17 15:19:15 +0000299 /* Note that the Win32 errors do not lineup with the
300 errno error. So if the error is in the MSVC error
Brett Cannonbf364092006-03-01 04:25:17 +0000301 table, we use it, otherwise we assume it really _is_
Guido van Rossum795e1892000-02-17 15:19:15 +0000302 a Win32 error code
303 */
Guido van Rossum584b16a2000-02-21 16:50:31 +0000304 if (i > 0 && i < _sys_nerr) {
Guido van Rossum795e1892000-02-17 15:19:15 +0000305 s = _sys_errlist[i];
306 }
307 else {
308 int len = FormatMessage(
309 FORMAT_MESSAGE_ALLOCATE_BUFFER |
310 FORMAT_MESSAGE_FROM_SYSTEM |
311 FORMAT_MESSAGE_IGNORE_INSERTS,
312 NULL, /* no message source */
313 i,
314 MAKELANGID(LANG_NEUTRAL,
315 SUBLANG_DEFAULT),
316 /* Default language */
317 (LPTSTR) &s_buf,
318 0, /* size not used */
319 NULL); /* no args */
Mark Hammond3d61a062002-10-04 00:13:02 +0000320 if (len==0) {
Brett Cannonbf364092006-03-01 04:25:17 +0000321 /* Only ever seen this in out-of-mem
Mark Hammond3d61a062002-10-04 00:13:02 +0000322 situations */
323 sprintf(s_small_buf, "Windows Error 0x%X", i);
324 s = s_small_buf;
325 s_buf = NULL;
326 } else {
327 s = s_buf;
328 /* remove trailing cr/lf and dots */
329 while (len > 0 && (s[len-1] <= ' ' || s[len-1] == '.'))
330 s[--len] = '\0';
331 }
Guido van Rossum795e1892000-02-17 15:19:15 +0000332 }
Guido van Rossum743007d1999-04-21 15:27:31 +0000333 }
Martin v. Löwis3484a182002-03-09 12:07:51 +0000334#endif /* Unix/Windows */
335#endif /* PLAN 9*/
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000336 if (filenameObject != NULL)
337 v = Py_BuildValue("(isO)", i, s, filenameObject);
Guido van Rossume0e59821998-10-14 20:38:13 +0000338 else
339 v = Py_BuildValue("(is)", i, s);
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000340 if (v != NULL) {
Guido van Rossum373c8691997-04-29 18:22:47 +0000341 PyErr_SetObject(exc, v);
342 Py_DECREF(v);
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000343 }
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000344#ifdef MS_WINDOWS
Guido van Rossum795e1892000-02-17 15:19:15 +0000345 LocalFree(s_buf);
Guido van Rossum743007d1999-04-21 15:27:31 +0000346#endif
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000347 return NULL;
348}
Guido van Rossum743007d1999-04-21 15:27:31 +0000349
Barry Warsaw97d95151998-07-23 16:05:56 +0000350
351PyObject *
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000352PyErr_SetFromErrnoWithFilename(PyObject *exc, char *filename)
353{
354 PyObject *name = filename ? PyString_FromString(filename) : NULL;
355 PyObject *result = PyErr_SetFromErrnoWithFilenameObject(exc, name);
Mark Hammondda7efaa2002-10-04 00:09:38 +0000356 Py_XDECREF(name);
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000357 return result;
358}
359
360#ifdef Py_WIN_WIDE_FILENAMES
361PyObject *
362PyErr_SetFromErrnoWithUnicodeFilename(PyObject *exc, Py_UNICODE *filename)
363{
Brett Cannonbf364092006-03-01 04:25:17 +0000364 PyObject *name = filename ?
365 PyUnicode_FromUnicode(filename, wcslen(filename)) :
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000366 NULL;
367 PyObject *result = PyErr_SetFromErrnoWithFilenameObject(exc, name);
368 Py_XDECREF(name);
369 return result;
370}
371#endif /* Py_WIN_WIDE_FILENAMES */
372
373PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000374PyErr_SetFromErrno(PyObject *exc)
Barry Warsaw97d95151998-07-23 16:05:56 +0000375{
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000376 return PyErr_SetFromErrnoWithFilenameObject(exc, NULL);
Barry Warsaw97d95151998-07-23 16:05:56 +0000377}
Guido van Rossum683a0721990-10-21 22:09:12 +0000378
Brett Cannonbf364092006-03-01 04:25:17 +0000379#ifdef MS_WINDOWS
Guido van Rossum795e1892000-02-17 15:19:15 +0000380/* Windows specific error code handling */
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000381PyObject *PyErr_SetExcFromWindowsErrWithFilenameObject(
Thomas Heller085358a2002-07-29 14:27:41 +0000382 PyObject *exc,
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000383 int ierr,
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000384 PyObject *filenameObject)
Guido van Rossum795e1892000-02-17 15:19:15 +0000385{
386 int len;
387 char *s;
Mark Hammond3d61a062002-10-04 00:13:02 +0000388 char *s_buf = NULL; /* Free via LocalFree */
389 char s_small_buf[28]; /* Room for "Windows Error 0xFFFFFFFF" */
Guido van Rossum795e1892000-02-17 15:19:15 +0000390 PyObject *v;
391 DWORD err = (DWORD)ierr;
392 if (err==0) err = GetLastError();
393 len = FormatMessage(
394 /* Error API error */
395 FORMAT_MESSAGE_ALLOCATE_BUFFER |
396 FORMAT_MESSAGE_FROM_SYSTEM |
397 FORMAT_MESSAGE_IGNORE_INSERTS,
398 NULL, /* no message source */
399 err,
400 MAKELANGID(LANG_NEUTRAL,
401 SUBLANG_DEFAULT), /* Default language */
Mark Hammond3d61a062002-10-04 00:13:02 +0000402 (LPTSTR) &s_buf,
Guido van Rossum795e1892000-02-17 15:19:15 +0000403 0, /* size not used */
404 NULL); /* no args */
Mark Hammond3d61a062002-10-04 00:13:02 +0000405 if (len==0) {
406 /* Only seen this in out of mem situations */
407 sprintf(s_small_buf, "Windows Error 0x%X", err);
408 s = s_small_buf;
409 s_buf = NULL;
410 } else {
411 s = s_buf;
412 /* remove trailing cr/lf and dots */
413 while (len > 0 && (s[len-1] <= ' ' || s[len-1] == '.'))
414 s[--len] = '\0';
415 }
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000416 if (filenameObject != NULL)
417 v = Py_BuildValue("(isO)", err, s, filenameObject);
Guido van Rossum795e1892000-02-17 15:19:15 +0000418 else
419 v = Py_BuildValue("(is)", err, s);
420 if (v != NULL) {
Thomas Heller085358a2002-07-29 14:27:41 +0000421 PyErr_SetObject(exc, v);
Guido van Rossum795e1892000-02-17 15:19:15 +0000422 Py_DECREF(v);
423 }
Mark Hammond3d61a062002-10-04 00:13:02 +0000424 LocalFree(s_buf);
Guido van Rossum795e1892000-02-17 15:19:15 +0000425 return NULL;
426}
427
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000428PyObject *PyErr_SetExcFromWindowsErrWithFilename(
429 PyObject *exc,
430 int ierr,
431 const char *filename)
432{
433 PyObject *name = filename ? PyString_FromString(filename) : NULL;
Brett Cannonbf364092006-03-01 04:25:17 +0000434 PyObject *ret = PyErr_SetExcFromWindowsErrWithFilenameObject(exc,
435 ierr,
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000436 name);
437 Py_XDECREF(name);
438 return ret;
439}
440
441#ifdef Py_WIN_WIDE_FILENAMES
442PyObject *PyErr_SetExcFromWindowsErrWithUnicodeFilename(
443 PyObject *exc,
444 int ierr,
445 const Py_UNICODE *filename)
446{
Brett Cannonbf364092006-03-01 04:25:17 +0000447 PyObject *name = filename ?
448 PyUnicode_FromUnicode(filename, wcslen(filename)) :
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000449 NULL;
Brett Cannonbf364092006-03-01 04:25:17 +0000450 PyObject *ret = PyErr_SetExcFromWindowsErrWithFilenameObject(exc,
451 ierr,
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000452 name);
453 Py_XDECREF(name);
454 return ret;
455}
456#endif /* Py_WIN_WIDE_FILENAMES */
457
Thomas Heller085358a2002-07-29 14:27:41 +0000458PyObject *PyErr_SetExcFromWindowsErr(PyObject *exc, int ierr)
459{
460 return PyErr_SetExcFromWindowsErrWithFilename(exc, ierr, NULL);
461}
462
Guido van Rossum795e1892000-02-17 15:19:15 +0000463PyObject *PyErr_SetFromWindowsErr(int ierr)
464{
Thomas Heller085358a2002-07-29 14:27:41 +0000465 return PyErr_SetExcFromWindowsErrWithFilename(PyExc_WindowsError,
466 ierr, NULL);
467}
468PyObject *PyErr_SetFromWindowsErrWithFilename(
469 int ierr,
470 const char *filename)
471{
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000472 PyObject *name = filename ? PyString_FromString(filename) : NULL;
473 PyObject *result = PyErr_SetExcFromWindowsErrWithFilenameObject(
474 PyExc_WindowsError,
475 ierr, name);
Mark Hammondda7efaa2002-10-04 00:09:38 +0000476 Py_XDECREF(name);
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000477 return result;
Guido van Rossum795e1892000-02-17 15:19:15 +0000478}
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000479
480#ifdef Py_WIN_WIDE_FILENAMES
481PyObject *PyErr_SetFromWindowsErrWithUnicodeFilename(
482 int ierr,
483 const Py_UNICODE *filename)
484{
Brett Cannonbf364092006-03-01 04:25:17 +0000485 PyObject *name = filename ?
486 PyUnicode_FromUnicode(filename, wcslen(filename)) :
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000487 NULL;
488 PyObject *result = PyErr_SetExcFromWindowsErrWithFilenameObject(
489 PyExc_WindowsError,
490 ierr, name);
Mark Hammondda7efaa2002-10-04 00:09:38 +0000491 Py_XDECREF(name);
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000492 return result;
493}
494#endif /* Py_WIN_WIDE_FILENAMES */
Guido van Rossum795e1892000-02-17 15:19:15 +0000495#endif /* MS_WINDOWS */
496
Guido van Rossum683a0721990-10-21 22:09:12 +0000497void
Fred Drake6d63adf2000-08-24 22:38:39 +0000498_PyErr_BadInternalCall(char *filename, int lineno)
499{
500 PyErr_Format(PyExc_SystemError,
501 "%s:%d: bad argument to internal function",
502 filename, lineno);
503}
504
505/* Remove the preprocessor macro for PyErr_BadInternalCall() so that we can
506 export the entry point for existing object code: */
507#undef PyErr_BadInternalCall
508void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000509PyErr_BadInternalCall(void)
Guido van Rossum683a0721990-10-21 22:09:12 +0000510{
Fred Drake6d63adf2000-08-24 22:38:39 +0000511 PyErr_Format(PyExc_SystemError,
512 "bad argument to internal function");
Guido van Rossum683a0721990-10-21 22:09:12 +0000513}
Fred Drake6d63adf2000-08-24 22:38:39 +0000514#define PyErr_BadInternalCall() _PyErr_BadInternalCall(__FILE__, __LINE__)
515
Guido van Rossum1548bac1997-02-14 17:09:47 +0000516
517
Guido van Rossum1548bac1997-02-14 17:09:47 +0000518PyObject *
519PyErr_Format(PyObject *exception, const char *format, ...)
Guido van Rossum1548bac1997-02-14 17:09:47 +0000520{
521 va_list vargs;
Jeremy Hyltonb69a27e2000-09-01 03:49:47 +0000522 PyObject* string;
Guido van Rossum1548bac1997-02-14 17:09:47 +0000523
Jeremy Hyltonb69a27e2000-09-01 03:49:47 +0000524#ifdef HAVE_STDARG_PROTOTYPES
Guido van Rossum1548bac1997-02-14 17:09:47 +0000525 va_start(vargs, format);
Jeremy Hyltonb69a27e2000-09-01 03:49:47 +0000526#else
527 va_start(vargs);
528#endif
Guido van Rossum1548bac1997-02-14 17:09:47 +0000529
Barry Warsaw876c8cb2001-08-24 18:35:23 +0000530 string = PyString_FromFormatV(format, vargs);
Jeremy Hyltonb69a27e2000-09-01 03:49:47 +0000531 PyErr_SetObject(exception, string);
532 Py_XDECREF(string);
Tim Petersc15c4f12001-10-02 21:32:07 +0000533 va_end(vargs);
Guido van Rossum1548bac1997-02-14 17:09:47 +0000534 return NULL;
535}
Guido van Rossum7617e051997-09-16 18:43:50 +0000536
537
Georg Brandl658d5132006-05-23 11:17:21 +0000538
Guido van Rossum7617e051997-09-16 18:43:50 +0000539PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000540PyErr_NewException(char *name, PyObject *base, PyObject *dict)
Guido van Rossum7617e051997-09-16 18:43:50 +0000541{
Guido van Rossum2ac650f1997-10-03 19:50:55 +0000542 char *dot;
543 PyObject *modulename = NULL;
544 PyObject *classname = NULL;
545 PyObject *mydict = NULL;
546 PyObject *bases = NULL;
Guido van Rossum7617e051997-09-16 18:43:50 +0000547 PyObject *result = NULL;
Guido van Rossum2ac650f1997-10-03 19:50:55 +0000548 dot = strrchr(name, '.');
549 if (dot == NULL) {
550 PyErr_SetString(PyExc_SystemError,
551 "PyErr_NewException: name must be module.class");
Guido van Rossum7617e051997-09-16 18:43:50 +0000552 return NULL;
Guido van Rossum7617e051997-09-16 18:43:50 +0000553 }
554 if (base == NULL)
555 base = PyExc_Exception;
Guido van Rossum2ac650f1997-10-03 19:50:55 +0000556 if (dict == NULL) {
557 dict = mydict = PyDict_New();
558 if (dict == NULL)
559 goto failure;
560 }
561 if (PyDict_GetItemString(dict, "__module__") == NULL) {
Armin Rigo7ccbca92006-10-04 12:17:45 +0000562 modulename = PyString_FromStringAndSize(name,
563 (Py_ssize_t)(dot-name));
Guido van Rossum2ac650f1997-10-03 19:50:55 +0000564 if (modulename == NULL)
565 goto failure;
566 if (PyDict_SetItemString(dict, "__module__", modulename) != 0)
567 goto failure;
568 }
Georg Brandl658d5132006-05-23 11:17:21 +0000569 if (PyTuple_Check(base)) {
570 bases = base;
571 /* INCREF as we create a new ref in the else branch */
572 Py_INCREF(bases);
573 } else {
574 bases = PyTuple_Pack(1, base);
575 if (bases == NULL)
576 goto failure;
577 }
Richard Jones7b9558d2006-05-27 12:29:24 +0000578 /* Create a real new-style class. */
579 result = PyObject_CallFunction((PyObject *)&PyType_Type, "sOO",
580 dot+1, bases, dict);
Guido van Rossum7617e051997-09-16 18:43:50 +0000581 failure:
Guido van Rossum2ac650f1997-10-03 19:50:55 +0000582 Py_XDECREF(bases);
583 Py_XDECREF(mydict);
584 Py_XDECREF(classname);
585 Py_XDECREF(modulename);
Guido van Rossum7617e051997-09-16 18:43:50 +0000586 return result;
587}
Jeremy Hyltonb709df32000-09-01 02:47:25 +0000588
589/* Call when an exception has occurred but there is no way for Python
590 to handle it. Examples: exception in __del__ or during GC. */
591void
592PyErr_WriteUnraisable(PyObject *obj)
593{
594 PyObject *f, *t, *v, *tb;
595 PyErr_Fetch(&t, &v, &tb);
596 f = PySys_GetObject("stderr");
597 if (f != NULL) {
598 PyFile_WriteString("Exception ", f);
599 if (t) {
Richard Jones7b9558d2006-05-27 12:29:24 +0000600 PyObject* moduleName;
Neal Norwitzf83b7512007-02-26 23:48:27 +0000601 char* className;
602 assert(PyExceptionClass_Check(t));
603 className = PyExceptionClass_Name(t);
Tim Peters5e9d6cf2006-05-28 10:41:29 +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);
Neal Norwitz88516a62007-02-26 22:41:45 +0000615 if (modstr &&
616 strcmp(modstr, "exceptions") != 0)
Brett Cannonbf364092006-03-01 04:25:17 +0000617 {
618 PyFile_WriteString(modstr, f);
619 PyFile_WriteString(".", f);
620 }
621 }
622 if (className == NULL)
623 PyFile_WriteString("<unknown>", f);
624 else
625 PyFile_WriteString(className, f);
Jeremy Hyltonb709df32000-09-01 02:47:25 +0000626 if (v && v != Py_None) {
627 PyFile_WriteString(": ", f);
628 PyFile_WriteObject(v, f, 0);
629 }
Neal Norwitz1a269202006-04-17 00:33:23 +0000630 Py_XDECREF(moduleName);
Jeremy Hyltonb709df32000-09-01 02:47:25 +0000631 }
632 PyFile_WriteString(" in ", f);
633 PyFile_WriteObject(obj, f, 0);
634 PyFile_WriteString(" ignored\n", f);
635 PyErr_Clear(); /* Just in case */
636 }
637 Py_XDECREF(t);
638 Py_XDECREF(v);
639 Py_XDECREF(tb);
640}
Guido van Rossumcfd42b52000-12-15 21:58:52 +0000641
Armin Rigo092381a2003-10-25 14:29:27 +0000642extern PyObject *PyModule_GetWarningsModule(void);
Guido van Rossumcfd42b52000-12-15 21:58:52 +0000643
644/* Function to issue a warning message; may raise an exception. */
645int
Neal Norwitz07aadb12006-07-30 06:55:48 +0000646PyErr_WarnEx(PyObject *category, const char *message, Py_ssize_t stack_level)
Guido van Rossumcfd42b52000-12-15 21:58:52 +0000647{
Mark Hammonda43fd0c2003-02-19 00:33:33 +0000648 PyObject *dict, *func = NULL;
Mark Hammondedd07732003-07-15 23:03:55 +0000649 PyObject *warnings_module = PyModule_GetWarningsModule();
Guido van Rossumcfd42b52000-12-15 21:58:52 +0000650
Mark Hammondedd07732003-07-15 23:03:55 +0000651 if (warnings_module != NULL) {
652 dict = PyModule_GetDict(warnings_module);
Neal Norwitz72cd02c2007-01-05 05:25:22 +0000653 if (dict != NULL)
654 func = PyDict_GetItemString(dict, "warn");
Guido van Rossumcfd42b52000-12-15 21:58:52 +0000655 }
656 if (func == NULL) {
657 PySys_WriteStderr("warning: %s\n", message);
658 return 0;
659 }
660 else {
Richard Jones7b9558d2006-05-27 12:29:24 +0000661 PyObject *res;
Guido van Rossumcfd42b52000-12-15 21:58:52 +0000662
663 if (category == NULL)
664 category = PyExc_RuntimeWarning;
Neal Norwitz07aadb12006-07-30 06:55:48 +0000665 res = PyObject_CallFunction(func, "sOn",
666 message, category, stack_level);
Guido van Rossumcfd42b52000-12-15 21:58:52 +0000667 if (res == NULL)
668 return -1;
669 Py_DECREF(res);
670 return 0;
671 }
672}
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000673
Neal Norwitz07aadb12006-07-30 06:55:48 +0000674/* PyErr_Warn is only for backwards compatability and will be removed.
675 Use PyErr_WarnEx instead. */
676
677#undef PyErr_Warn
678
Thomas Heller74d36f02006-08-04 18:17:40 +0000679PyAPI_FUNC(int)
Neal Norwitz07aadb12006-07-30 06:55:48 +0000680PyErr_Warn(PyObject *category, char *message)
681{
682 return PyErr_WarnEx(category, message, 1);
683}
Guido van Rossum2fd45652001-02-28 21:46:24 +0000684
685/* Warning with explicit origin */
686int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000687PyErr_WarnExplicit(PyObject *category, const char *message,
688 const char *filename, int lineno,
689 const char *module, PyObject *registry)
Guido van Rossum2fd45652001-02-28 21:46:24 +0000690{
691 PyObject *mod, *dict, *func = NULL;
692
693 mod = PyImport_ImportModule("warnings");
694 if (mod != NULL) {
695 dict = PyModule_GetDict(mod);
696 func = PyDict_GetItemString(dict, "warn_explicit");
697 Py_DECREF(mod);
698 }
699 if (func == NULL) {
700 PySys_WriteStderr("warning: %s\n", message);
701 return 0;
702 }
703 else {
Richard Jones7b9558d2006-05-27 12:29:24 +0000704 PyObject *res;
Guido van Rossum2fd45652001-02-28 21:46:24 +0000705
706 if (category == NULL)
707 category = PyExc_RuntimeWarning;
708 if (registry == NULL)
709 registry = Py_None;
Richard Jones7b9558d2006-05-27 12:29:24 +0000710 res = PyObject_CallFunction(func, "sOsizO", message, category,
711 filename, lineno, module, registry);
Guido van Rossum2fd45652001-02-28 21:46:24 +0000712 if (res == NULL)
713 return -1;
714 Py_DECREF(res);
715 return 0;
716 }
717}
718
719
Martin v. Löwiscfeb3b62002-03-03 21:30:27 +0000720/* Set file and line information for the current exception.
721 If the exception is not a SyntaxError, also sets additional attributes
722 to make printing of exceptions believe it is a syntax error. */
Guido van Rossum2fd45652001-02-28 21:46:24 +0000723
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000724void
Martin v. Löwis95292d62002-12-11 14:04:59 +0000725PyErr_SyntaxLocation(const char *filename, int lineno)
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000726{
727 PyObject *exc, *v, *tb, *tmp;
728
729 /* add attributes for the line number and filename for the error */
730 PyErr_Fetch(&exc, &v, &tb);
731 PyErr_NormalizeException(&exc, &v, &tb);
Richard Jones7b9558d2006-05-27 12:29:24 +0000732 /* XXX check that it is, indeed, a syntax error. It might not
733 * be, though. */
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000734 tmp = PyInt_FromLong(lineno);
735 if (tmp == NULL)
736 PyErr_Clear();
737 else {
738 if (PyObject_SetAttrString(v, "lineno", tmp))
739 PyErr_Clear();
740 Py_DECREF(tmp);
741 }
742 if (filename != NULL) {
743 tmp = PyString_FromString(filename);
744 if (tmp == NULL)
745 PyErr_Clear();
746 else {
747 if (PyObject_SetAttrString(v, "filename", tmp))
748 PyErr_Clear();
749 Py_DECREF(tmp);
750 }
751
752 tmp = PyErr_ProgramText(filename, lineno);
753 if (tmp) {
Georg Brandla1121fa2006-05-29 14:13:21 +0000754 if (PyObject_SetAttrString(v, "text", tmp))
755 PyErr_Clear();
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000756 Py_DECREF(tmp);
757 }
758 }
Martin v. Löwiscfeb3b62002-03-03 21:30:27 +0000759 if (PyObject_SetAttrString(v, "offset", Py_None)) {
760 PyErr_Clear();
761 }
762 if (exc != PyExc_SyntaxError) {
763 if (!PyObject_HasAttrString(v, "msg")) {
764 tmp = PyObject_Str(v);
765 if (tmp) {
766 if (PyObject_SetAttrString(v, "msg", tmp))
767 PyErr_Clear();
768 Py_DECREF(tmp);
769 } else {
770 PyErr_Clear();
771 }
772 }
773 if (!PyObject_HasAttrString(v, "print_file_and_line")) {
774 if (PyObject_SetAttrString(v, "print_file_and_line",
775 Py_None))
776 PyErr_Clear();
777 }
778 }
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000779 PyErr_Restore(exc, v, tb);
780}
781
782/* com_fetch_program_text will attempt to load the line of text that
783 the exception refers to. If it fails, it will return NULL but will
Brett Cannonbf364092006-03-01 04:25:17 +0000784 not set an exception.
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000785
786 XXX The functionality of this function is quite similar to the
787 functionality in tb_displayline() in traceback.c.
788*/
789
790PyObject *
Martin v. Löwis95292d62002-12-11 14:04:59 +0000791PyErr_ProgramText(const char *filename, int lineno)
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000792{
793 FILE *fp;
794 int i;
795 char linebuf[1000];
796
Tim Petersa7444f42006-02-27 23:29:46 +0000797 if (filename == NULL || *filename == '\0' || lineno <= 0)
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000798 return NULL;
Jack Jansen7b8c7542002-04-14 20:12:41 +0000799 fp = fopen(filename, "r" PY_STDIOTEXTMODE);
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000800 if (fp == NULL)
801 return NULL;
802 for (i = 0; i < lineno; i++) {
803 char *pLastChar = &linebuf[sizeof(linebuf) - 2];
804 do {
805 *pLastChar = '\0';
Jack Jansen7b8c7542002-04-14 20:12:41 +0000806 if (Py_UniversalNewlineFgets(linebuf, sizeof linebuf, fp, NULL) == NULL)
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000807 break;
808 /* fgets read *something*; if it didn't get as
809 far as pLastChar, it must have found a newline
Walter Dörwaldc611f172006-05-25 08:53:28 +0000810 or hit the end of the file; if pLastChar is \n,
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000811 it obviously found a newline; else we haven't
812 yet seen a newline, so must continue */
813 } while (*pLastChar != '\0' && *pLastChar != '\n');
814 }
815 fclose(fp);
816 if (i == lineno) {
817 char *p = linebuf;
818 while (*p == ' ' || *p == '\t' || *p == '\014')
819 p++;
820 return PyString_FromString(p);
821 }
822 return NULL;
823}
Anthony Baxterac6bd462006-04-13 02:06:09 +0000824
825#ifdef __cplusplus
826}
827#endif
828