blob: 24dafca921a885cd1d5d017434feccdd18bb43ac [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{
Gregory P. Smithdd96db62008-06-09 04:58:54 +000069 PyObject *value = PyString_FromString(string);
Guido van Rossum373c8691997-04-29 18:22:47 +000070 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)) {
Georg Brandl4b5c53a2009-04-02 02:47:44 +0000109 int res = 0, reclimit;
Amaury Forgeot d'Arc246daed2008-07-31 00:42:16 +0000110 PyObject *exception, *value, *tb;
111 PyErr_Fetch(&exception, &value, &tb);
Georg Brandl4b5c53a2009-04-02 02:47:44 +0000112 /* Temporarily bump the recursion limit, so that in the most
113 common case PyObject_IsSubclass will not raise a recursion
114 error we have to ignore anyway. */
115 reclimit = Py_GetRecursionLimit();
116 Py_SetRecursionLimit(reclimit + 5);
Amaury Forgeot d'Arc246daed2008-07-31 00:42:16 +0000117 res = PyObject_IsSubclass(err, exc);
Georg Brandl4b5c53a2009-04-02 02:47:44 +0000118 Py_SetRecursionLimit(reclimit);
Amaury Forgeot d'Arc246daed2008-07-31 00:42:16 +0000119 /* This function must not fail, so print the error here */
120 if (res == -1) {
121 PyErr_WriteUnraisable(err);
Amaury Forgeot d'Arc246daed2008-07-31 00:42:16 +0000122 res = 0;
123 }
124 PyErr_Restore(exception, value, tb);
125 return res;
Brett Cannonbf364092006-03-01 04:25:17 +0000126 }
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000127
128 return err == exc;
129}
Guido van Rossum743007d1999-04-21 15:27:31 +0000130
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000131
132int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000133PyErr_ExceptionMatches(PyObject *exc)
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000134{
135 return PyErr_GivenExceptionMatches(PyErr_Occurred(), exc);
136}
137
138
139/* Used in many places to normalize a raised exception, including in
140 eval_code2(), do_raise(), and PyErr_Print()
141*/
142void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000143PyErr_NormalizeException(PyObject **exc, PyObject **val, PyObject **tb)
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000144{
145 PyObject *type = *exc;
146 PyObject *value = *val;
147 PyObject *inclass = NULL;
Jeremy Hyltone2e2c9f2001-09-26 19:58:38 +0000148 PyObject *initial_tb = NULL;
Brett Cannon1e534b52007-09-07 04:18:30 +0000149 PyThreadState *tstate = NULL;
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000150
Guido van Rossumed473a42000-08-07 19:18:27 +0000151 if (type == NULL) {
Guido van Rossum6b3fffa2003-04-10 20:29:48 +0000152 /* There was no exception, so nothing to do. */
153 return;
Guido van Rossumed473a42000-08-07 19:18:27 +0000154 }
155
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000156 /* If PyErr_SetNone() was used, the value will have been actually
157 set to NULL.
158 */
159 if (!value) {
160 value = Py_None;
161 Py_INCREF(value);
162 }
163
Brett Cannonbf364092006-03-01 04:25:17 +0000164 if (PyExceptionInstance_Check(value))
165 inclass = PyExceptionInstance_Class(value);
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000166
167 /* Normalize the exception so that if the type is a class, the
168 value will be an instance.
169 */
Brett Cannonbf364092006-03-01 04:25:17 +0000170 if (PyExceptionClass_Check(type)) {
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000171 /* if the value was not an instance, or is not an instance
172 whose class is (or is derived from) type, then use the
173 value as an argument to instantiation of the type
174 class.
175 */
Brett Cannonbf364092006-03-01 04:25:17 +0000176 if (!inclass || !PyObject_IsSubclass(inclass, type)) {
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000177 PyObject *args, *res;
178
179 if (value == Py_None)
Raymond Hettinger8ae46892003-10-12 19:09:37 +0000180 args = PyTuple_New(0);
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000181 else if (PyTuple_Check(value)) {
182 Py_INCREF(value);
183 args = value;
184 }
185 else
Raymond Hettinger8ae46892003-10-12 19:09:37 +0000186 args = PyTuple_Pack(1, value);
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000187
188 if (args == NULL)
189 goto finally;
190 res = PyEval_CallObject(type, args);
191 Py_DECREF(args);
192 if (res == NULL)
193 goto finally;
194 Py_DECREF(value);
195 value = res;
196 }
Barry Warsaw3a749931997-09-30 15:00:18 +0000197 /* if the class of the instance doesn't exactly match the
198 class of the type, believe the instance
199 */
200 else if (inclass != type) {
201 Py_DECREF(type);
202 type = inclass;
203 Py_INCREF(type);
204 }
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000205 }
206 *exc = type;
207 *val = value;
208 return;
209finally:
Guido van Rossum19b55f21997-12-09 14:11:39 +0000210 Py_DECREF(type);
211 Py_DECREF(value);
Jeremy Hyltone2e2c9f2001-09-26 19:58:38 +0000212 /* If the new exception doesn't set a traceback and the old
213 exception had a traceback, use the old traceback for the
214 new exception. It's better than nothing.
215 */
216 initial_tb = *tb;
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000217 PyErr_Fetch(exc, val, tb);
Jeremy Hyltone2e2c9f2001-09-26 19:58:38 +0000218 if (initial_tb != NULL) {
219 if (*tb == NULL)
220 *tb = initial_tb;
221 else
222 Py_DECREF(initial_tb);
223 }
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000224 /* normalize recursively */
Brett Cannon1e534b52007-09-07 04:18:30 +0000225 tstate = PyThreadState_GET();
226 if (++tstate->recursion_depth > Py_GetRecursionLimit()) {
227 --tstate->recursion_depth;
Georg Brandld3f03fa2009-04-02 18:09:04 +0000228 /* throw away the old exception... */
229 Py_DECREF(*exc);
230 Py_DECREF(*val);
231 /* ... and use the recursion error instead */
232 *exc = PyExc_RuntimeError;
233 *val = PyExc_RecursionErrorInst;
234 Py_INCREF(*exc);
235 Py_INCREF(*val);
236 /* just keeping the old traceback */
Brett Cannon1e534b52007-09-07 04:18:30 +0000237 return;
238 }
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000239 PyErr_NormalizeException(exc, val, tb);
Brett Cannon1e534b52007-09-07 04:18:30 +0000240 --tstate->recursion_depth;
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000241}
242
243
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000244void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000245PyErr_Fetch(PyObject **p_type, PyObject **p_value, PyObject **p_traceback)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000246{
Nicholas Bastine5662ae2004-03-24 22:22:12 +0000247 PyThreadState *tstate = PyThreadState_GET();
Guido van Rossuma027efa1997-05-05 20:56:21 +0000248
249 *p_type = tstate->curexc_type;
250 *p_value = tstate->curexc_value;
251 *p_traceback = tstate->curexc_traceback;
252
253 tstate->curexc_type = NULL;
254 tstate->curexc_value = NULL;
255 tstate->curexc_traceback = NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000256}
257
258void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000259PyErr_Clear(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000260{
Guido van Rossuma027efa1997-05-05 20:56:21 +0000261 PyErr_Restore(NULL, NULL, NULL);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000262}
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000263
264/* Convenience functions to set a type error exception and return 0 */
265
266int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000267PyErr_BadArgument(void)
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000268{
Guido van Rossuma027efa1997-05-05 20:56:21 +0000269 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +0000270 "bad argument type for built-in operation");
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000271 return 0;
272}
273
Guido van Rossum373c8691997-04-29 18:22:47 +0000274PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000275PyErr_NoMemory(void)
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000276{
Vladimir Marangozov0888ff12000-08-18 18:01:06 +0000277 if (PyErr_ExceptionMatches(PyExc_MemoryError))
278 /* already current */
279 return NULL;
280
Barry Warsaw2d8adff1997-08-29 21:54:35 +0000281 /* raise the pre-allocated instance if it still exists */
282 if (PyExc_MemoryErrorInst)
283 PyErr_SetObject(PyExc_MemoryError, PyExc_MemoryErrorInst);
284 else
285 /* this will probably fail since there's no memory and hee,
286 hee, we have to instantiate this class
287 */
288 PyErr_SetNone(PyExc_MemoryError);
289
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000290 return NULL;
291}
292
Guido van Rossum373c8691997-04-29 18:22:47 +0000293PyObject *
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000294PyErr_SetFromErrnoWithFilenameObject(PyObject *exc, PyObject *filenameObject)
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000295{
Guido van Rossum373c8691997-04-29 18:22:47 +0000296 PyObject *v;
Guido van Rossume0e59821998-10-14 20:38:13 +0000297 char *s;
Guido van Rossum3a241811994-08-29 12:14:12 +0000298 int i = errno;
Martin v. Löwis3484a182002-03-09 12:07:51 +0000299#ifdef PLAN9
300 char errbuf[ERRMAX];
301#endif
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000302#ifdef MS_WINDOWS
Guido van Rossum795e1892000-02-17 15:19:15 +0000303 char *s_buf = NULL;
Mark Hammond3d61a062002-10-04 00:13:02 +0000304 char s_small_buf[28]; /* Room for "Windows Error 0xFFFFFFFF" */
Guido van Rossum795e1892000-02-17 15:19:15 +0000305#endif
Guido van Rossume9fbc091995-02-18 14:52:19 +0000306#ifdef EINTR
Guido van Rossum373c8691997-04-29 18:22:47 +0000307 if (i == EINTR && PyErr_CheckSignals())
Guido van Rossum5063bab1991-10-20 20:14:56 +0000308 return NULL;
Guido van Rossume9fbc091995-02-18 14:52:19 +0000309#endif
Martin v. Löwis3484a182002-03-09 12:07:51 +0000310#ifdef PLAN9
311 rerrstr(errbuf, sizeof errbuf);
312 s = errbuf;
313#else
Guido van Rossume0e59821998-10-14 20:38:13 +0000314 if (i == 0)
315 s = "Error"; /* Sometimes errno didn't get set */
Barry Warsaw97d95151998-07-23 16:05:56 +0000316 else
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000317#ifndef MS_WINDOWS
Guido van Rossume0e59821998-10-14 20:38:13 +0000318 s = strerror(i);
Guido van Rossum743007d1999-04-21 15:27:31 +0000319#else
320 {
Guido van Rossum795e1892000-02-17 15:19:15 +0000321 /* Note that the Win32 errors do not lineup with the
322 errno error. So if the error is in the MSVC error
Brett Cannonbf364092006-03-01 04:25:17 +0000323 table, we use it, otherwise we assume it really _is_
Guido van Rossum795e1892000-02-17 15:19:15 +0000324 a Win32 error code
325 */
Guido van Rossum584b16a2000-02-21 16:50:31 +0000326 if (i > 0 && i < _sys_nerr) {
Guido van Rossum795e1892000-02-17 15:19:15 +0000327 s = _sys_errlist[i];
328 }
329 else {
330 int len = FormatMessage(
331 FORMAT_MESSAGE_ALLOCATE_BUFFER |
332 FORMAT_MESSAGE_FROM_SYSTEM |
333 FORMAT_MESSAGE_IGNORE_INSERTS,
334 NULL, /* no message source */
335 i,
336 MAKELANGID(LANG_NEUTRAL,
337 SUBLANG_DEFAULT),
338 /* Default language */
339 (LPTSTR) &s_buf,
340 0, /* size not used */
341 NULL); /* no args */
Mark Hammond3d61a062002-10-04 00:13:02 +0000342 if (len==0) {
Brett Cannonbf364092006-03-01 04:25:17 +0000343 /* Only ever seen this in out-of-mem
Mark Hammond3d61a062002-10-04 00:13:02 +0000344 situations */
345 sprintf(s_small_buf, "Windows Error 0x%X", i);
346 s = s_small_buf;
347 s_buf = NULL;
348 } else {
349 s = s_buf;
350 /* remove trailing cr/lf and dots */
351 while (len > 0 && (s[len-1] <= ' ' || s[len-1] == '.'))
352 s[--len] = '\0';
353 }
Guido van Rossum795e1892000-02-17 15:19:15 +0000354 }
Guido van Rossum743007d1999-04-21 15:27:31 +0000355 }
Martin v. Löwis3484a182002-03-09 12:07:51 +0000356#endif /* Unix/Windows */
357#endif /* PLAN 9*/
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000358 if (filenameObject != NULL)
359 v = Py_BuildValue("(isO)", i, s, filenameObject);
Guido van Rossume0e59821998-10-14 20:38:13 +0000360 else
361 v = Py_BuildValue("(is)", i, s);
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000362 if (v != NULL) {
Guido van Rossum373c8691997-04-29 18:22:47 +0000363 PyErr_SetObject(exc, v);
364 Py_DECREF(v);
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000365 }
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000366#ifdef MS_WINDOWS
Guido van Rossum795e1892000-02-17 15:19:15 +0000367 LocalFree(s_buf);
Guido van Rossum743007d1999-04-21 15:27:31 +0000368#endif
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000369 return NULL;
370}
Guido van Rossum743007d1999-04-21 15:27:31 +0000371
Barry Warsaw97d95151998-07-23 16:05:56 +0000372
373PyObject *
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000374PyErr_SetFromErrnoWithFilename(PyObject *exc, char *filename)
375{
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000376 PyObject *name = filename ? PyString_FromString(filename) : NULL;
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000377 PyObject *result = PyErr_SetFromErrnoWithFilenameObject(exc, name);
Mark Hammondda7efaa2002-10-04 00:09:38 +0000378 Py_XDECREF(name);
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000379 return result;
380}
381
Hirokazu Yamamotob24bb272009-05-17 02:52:09 +0000382#ifdef MS_WINDOWS
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000383PyObject *
384PyErr_SetFromErrnoWithUnicodeFilename(PyObject *exc, Py_UNICODE *filename)
385{
Brett Cannonbf364092006-03-01 04:25:17 +0000386 PyObject *name = filename ?
387 PyUnicode_FromUnicode(filename, wcslen(filename)) :
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000388 NULL;
389 PyObject *result = PyErr_SetFromErrnoWithFilenameObject(exc, name);
390 Py_XDECREF(name);
391 return result;
392}
Hirokazu Yamamotob24bb272009-05-17 02:52:09 +0000393#endif /* MS_WINDOWS */
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000394
395PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000396PyErr_SetFromErrno(PyObject *exc)
Barry Warsaw97d95151998-07-23 16:05:56 +0000397{
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000398 return PyErr_SetFromErrnoWithFilenameObject(exc, NULL);
Barry Warsaw97d95151998-07-23 16:05:56 +0000399}
Guido van Rossum683a0721990-10-21 22:09:12 +0000400
Brett Cannonbf364092006-03-01 04:25:17 +0000401#ifdef MS_WINDOWS
Guido van Rossum795e1892000-02-17 15:19:15 +0000402/* Windows specific error code handling */
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000403PyObject *PyErr_SetExcFromWindowsErrWithFilenameObject(
Thomas Heller085358a2002-07-29 14:27:41 +0000404 PyObject *exc,
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000405 int ierr,
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000406 PyObject *filenameObject)
Guido van Rossum795e1892000-02-17 15:19:15 +0000407{
408 int len;
409 char *s;
Mark Hammond3d61a062002-10-04 00:13:02 +0000410 char *s_buf = NULL; /* Free via LocalFree */
411 char s_small_buf[28]; /* Room for "Windows Error 0xFFFFFFFF" */
Guido van Rossum795e1892000-02-17 15:19:15 +0000412 PyObject *v;
413 DWORD err = (DWORD)ierr;
414 if (err==0) err = GetLastError();
415 len = FormatMessage(
416 /* Error API error */
417 FORMAT_MESSAGE_ALLOCATE_BUFFER |
418 FORMAT_MESSAGE_FROM_SYSTEM |
419 FORMAT_MESSAGE_IGNORE_INSERTS,
420 NULL, /* no message source */
421 err,
422 MAKELANGID(LANG_NEUTRAL,
423 SUBLANG_DEFAULT), /* Default language */
Mark Hammond3d61a062002-10-04 00:13:02 +0000424 (LPTSTR) &s_buf,
Guido van Rossum795e1892000-02-17 15:19:15 +0000425 0, /* size not used */
426 NULL); /* no args */
Mark Hammond3d61a062002-10-04 00:13:02 +0000427 if (len==0) {
428 /* Only seen this in out of mem situations */
429 sprintf(s_small_buf, "Windows Error 0x%X", err);
430 s = s_small_buf;
431 s_buf = NULL;
432 } else {
433 s = s_buf;
434 /* remove trailing cr/lf and dots */
435 while (len > 0 && (s[len-1] <= ' ' || s[len-1] == '.'))
436 s[--len] = '\0';
437 }
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000438 if (filenameObject != NULL)
439 v = Py_BuildValue("(isO)", err, s, filenameObject);
Guido van Rossum795e1892000-02-17 15:19:15 +0000440 else
441 v = Py_BuildValue("(is)", err, s);
442 if (v != NULL) {
Thomas Heller085358a2002-07-29 14:27:41 +0000443 PyErr_SetObject(exc, v);
Guido van Rossum795e1892000-02-17 15:19:15 +0000444 Py_DECREF(v);
445 }
Mark Hammond3d61a062002-10-04 00:13:02 +0000446 LocalFree(s_buf);
Guido van Rossum795e1892000-02-17 15:19:15 +0000447 return NULL;
448}
449
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000450PyObject *PyErr_SetExcFromWindowsErrWithFilename(
451 PyObject *exc,
452 int ierr,
453 const char *filename)
454{
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000455 PyObject *name = filename ? PyString_FromString(filename) : NULL;
Brett Cannonbf364092006-03-01 04:25:17 +0000456 PyObject *ret = PyErr_SetExcFromWindowsErrWithFilenameObject(exc,
457 ierr,
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000458 name);
459 Py_XDECREF(name);
460 return ret;
461}
462
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000463PyObject *PyErr_SetExcFromWindowsErrWithUnicodeFilename(
464 PyObject *exc,
465 int ierr,
466 const Py_UNICODE *filename)
467{
Brett Cannonbf364092006-03-01 04:25:17 +0000468 PyObject *name = filename ?
469 PyUnicode_FromUnicode(filename, wcslen(filename)) :
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000470 NULL;
Brett Cannonbf364092006-03-01 04:25:17 +0000471 PyObject *ret = PyErr_SetExcFromWindowsErrWithFilenameObject(exc,
472 ierr,
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000473 name);
474 Py_XDECREF(name);
475 return ret;
476}
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000477
Thomas Heller085358a2002-07-29 14:27:41 +0000478PyObject *PyErr_SetExcFromWindowsErr(PyObject *exc, int ierr)
479{
480 return PyErr_SetExcFromWindowsErrWithFilename(exc, ierr, NULL);
481}
482
Guido van Rossum795e1892000-02-17 15:19:15 +0000483PyObject *PyErr_SetFromWindowsErr(int ierr)
484{
Thomas Heller085358a2002-07-29 14:27:41 +0000485 return PyErr_SetExcFromWindowsErrWithFilename(PyExc_WindowsError,
486 ierr, NULL);
487}
488PyObject *PyErr_SetFromWindowsErrWithFilename(
489 int ierr,
490 const char *filename)
491{
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000492 PyObject *name = filename ? PyString_FromString(filename) : NULL;
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000493 PyObject *result = PyErr_SetExcFromWindowsErrWithFilenameObject(
494 PyExc_WindowsError,
495 ierr, name);
Mark Hammondda7efaa2002-10-04 00:09:38 +0000496 Py_XDECREF(name);
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000497 return result;
Guido van Rossum795e1892000-02-17 15:19:15 +0000498}
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000499
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000500PyObject *PyErr_SetFromWindowsErrWithUnicodeFilename(
501 int ierr,
502 const Py_UNICODE *filename)
503{
Brett Cannonbf364092006-03-01 04:25:17 +0000504 PyObject *name = filename ?
505 PyUnicode_FromUnicode(filename, wcslen(filename)) :
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000506 NULL;
507 PyObject *result = PyErr_SetExcFromWindowsErrWithFilenameObject(
508 PyExc_WindowsError,
509 ierr, name);
Mark Hammondda7efaa2002-10-04 00:09:38 +0000510 Py_XDECREF(name);
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000511 return result;
512}
Guido van Rossum795e1892000-02-17 15:19:15 +0000513#endif /* MS_WINDOWS */
514
Guido van Rossum683a0721990-10-21 22:09:12 +0000515void
Fred Drake6d63adf2000-08-24 22:38:39 +0000516_PyErr_BadInternalCall(char *filename, int lineno)
517{
518 PyErr_Format(PyExc_SystemError,
519 "%s:%d: bad argument to internal function",
520 filename, lineno);
521}
522
523/* Remove the preprocessor macro for PyErr_BadInternalCall() so that we can
524 export the entry point for existing object code: */
525#undef PyErr_BadInternalCall
526void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000527PyErr_BadInternalCall(void)
Guido van Rossum683a0721990-10-21 22:09:12 +0000528{
Fred Drake6d63adf2000-08-24 22:38:39 +0000529 PyErr_Format(PyExc_SystemError,
530 "bad argument to internal function");
Guido van Rossum683a0721990-10-21 22:09:12 +0000531}
Fred Drake6d63adf2000-08-24 22:38:39 +0000532#define PyErr_BadInternalCall() _PyErr_BadInternalCall(__FILE__, __LINE__)
533
Guido van Rossum1548bac1997-02-14 17:09:47 +0000534
535
Guido van Rossum1548bac1997-02-14 17:09:47 +0000536PyObject *
537PyErr_Format(PyObject *exception, const char *format, ...)
Guido van Rossum1548bac1997-02-14 17:09:47 +0000538{
539 va_list vargs;
Jeremy Hyltonb69a27e2000-09-01 03:49:47 +0000540 PyObject* string;
Guido van Rossum1548bac1997-02-14 17:09:47 +0000541
Jeremy Hyltonb69a27e2000-09-01 03:49:47 +0000542#ifdef HAVE_STDARG_PROTOTYPES
Guido van Rossum1548bac1997-02-14 17:09:47 +0000543 va_start(vargs, format);
Jeremy Hyltonb69a27e2000-09-01 03:49:47 +0000544#else
545 va_start(vargs);
546#endif
Guido van Rossum1548bac1997-02-14 17:09:47 +0000547
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000548 string = PyString_FromFormatV(format, vargs);
Jeremy Hyltonb69a27e2000-09-01 03:49:47 +0000549 PyErr_SetObject(exception, string);
550 Py_XDECREF(string);
Tim Petersc15c4f12001-10-02 21:32:07 +0000551 va_end(vargs);
Guido van Rossum1548bac1997-02-14 17:09:47 +0000552 return NULL;
553}
Guido van Rossum7617e051997-09-16 18:43:50 +0000554
555
Georg Brandl658d5132006-05-23 11:17:21 +0000556
Guido van Rossum7617e051997-09-16 18:43:50 +0000557PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000558PyErr_NewException(char *name, PyObject *base, PyObject *dict)
Guido van Rossum7617e051997-09-16 18:43:50 +0000559{
Guido van Rossum2ac650f1997-10-03 19:50:55 +0000560 char *dot;
561 PyObject *modulename = NULL;
562 PyObject *classname = NULL;
563 PyObject *mydict = NULL;
564 PyObject *bases = NULL;
Guido van Rossum7617e051997-09-16 18:43:50 +0000565 PyObject *result = NULL;
Guido van Rossum2ac650f1997-10-03 19:50:55 +0000566 dot = strrchr(name, '.');
567 if (dot == NULL) {
568 PyErr_SetString(PyExc_SystemError,
569 "PyErr_NewException: name must be module.class");
Guido van Rossum7617e051997-09-16 18:43:50 +0000570 return NULL;
Guido van Rossum7617e051997-09-16 18:43:50 +0000571 }
572 if (base == NULL)
573 base = PyExc_Exception;
Guido van Rossum2ac650f1997-10-03 19:50:55 +0000574 if (dict == NULL) {
575 dict = mydict = PyDict_New();
576 if (dict == NULL)
577 goto failure;
578 }
579 if (PyDict_GetItemString(dict, "__module__") == NULL) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000580 modulename = PyString_FromStringAndSize(name,
Armin Rigo7ccbca92006-10-04 12:17:45 +0000581 (Py_ssize_t)(dot-name));
Guido van Rossum2ac650f1997-10-03 19:50:55 +0000582 if (modulename == NULL)
583 goto failure;
584 if (PyDict_SetItemString(dict, "__module__", modulename) != 0)
585 goto failure;
586 }
Georg Brandl658d5132006-05-23 11:17:21 +0000587 if (PyTuple_Check(base)) {
588 bases = base;
589 /* INCREF as we create a new ref in the else branch */
590 Py_INCREF(bases);
591 } else {
592 bases = PyTuple_Pack(1, base);
593 if (bases == NULL)
594 goto failure;
595 }
Richard Jones7b9558d2006-05-27 12:29:24 +0000596 /* Create a real new-style class. */
597 result = PyObject_CallFunction((PyObject *)&PyType_Type, "sOO",
598 dot+1, bases, dict);
Guido van Rossum7617e051997-09-16 18:43:50 +0000599 failure:
Guido van Rossum2ac650f1997-10-03 19:50:55 +0000600 Py_XDECREF(bases);
601 Py_XDECREF(mydict);
602 Py_XDECREF(classname);
603 Py_XDECREF(modulename);
Guido van Rossum7617e051997-09-16 18:43:50 +0000604 return result;
605}
Jeremy Hyltonb709df32000-09-01 02:47:25 +0000606
607/* Call when an exception has occurred but there is no way for Python
608 to handle it. Examples: exception in __del__ or during GC. */
609void
610PyErr_WriteUnraisable(PyObject *obj)
611{
612 PyObject *f, *t, *v, *tb;
613 PyErr_Fetch(&t, &v, &tb);
614 f = PySys_GetObject("stderr");
615 if (f != NULL) {
616 PyFile_WriteString("Exception ", f);
617 if (t) {
Richard Jones7b9558d2006-05-27 12:29:24 +0000618 PyObject* moduleName;
Neal Norwitzf83b7512007-02-26 23:48:27 +0000619 char* className;
620 assert(PyExceptionClass_Check(t));
621 className = PyExceptionClass_Name(t);
Tim Peters5e9d6cf2006-05-28 10:41:29 +0000622 if (className != NULL) {
623 char *dot = strrchr(className, '.');
624 if (dot != NULL)
625 className = dot+1;
626 }
627
628 moduleName = PyObject_GetAttrString(t, "__module__");
Brett Cannonbf364092006-03-01 04:25:17 +0000629 if (moduleName == NULL)
630 PyFile_WriteString("<unknown>", f);
631 else {
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000632 char* modstr = PyString_AsString(moduleName);
Neal Norwitz88516a62007-02-26 22:41:45 +0000633 if (modstr &&
634 strcmp(modstr, "exceptions") != 0)
Brett Cannonbf364092006-03-01 04:25:17 +0000635 {
636 PyFile_WriteString(modstr, f);
637 PyFile_WriteString(".", f);
638 }
639 }
640 if (className == NULL)
641 PyFile_WriteString("<unknown>", f);
642 else
643 PyFile_WriteString(className, f);
Jeremy Hyltonb709df32000-09-01 02:47:25 +0000644 if (v && v != Py_None) {
645 PyFile_WriteString(": ", f);
646 PyFile_WriteObject(v, f, 0);
647 }
Neal Norwitz1a269202006-04-17 00:33:23 +0000648 Py_XDECREF(moduleName);
Jeremy Hyltonb709df32000-09-01 02:47:25 +0000649 }
650 PyFile_WriteString(" in ", f);
651 PyFile_WriteObject(obj, f, 0);
652 PyFile_WriteString(" ignored\n", f);
653 PyErr_Clear(); /* Just in case */
654 }
655 Py_XDECREF(t);
656 Py_XDECREF(v);
657 Py_XDECREF(tb);
658}
Guido van Rossumcfd42b52000-12-15 21:58:52 +0000659
Armin Rigo092381a2003-10-25 14:29:27 +0000660extern PyObject *PyModule_GetWarningsModule(void);
Guido van Rossumcfd42b52000-12-15 21:58:52 +0000661
Guido van Rossum2fd45652001-02-28 21:46:24 +0000662
Martin v. Löwiscfeb3b62002-03-03 21:30:27 +0000663/* Set file and line information for the current exception.
664 If the exception is not a SyntaxError, also sets additional attributes
665 to make printing of exceptions believe it is a syntax error. */
Guido van Rossum2fd45652001-02-28 21:46:24 +0000666
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000667void
Martin v. Löwis95292d62002-12-11 14:04:59 +0000668PyErr_SyntaxLocation(const char *filename, int lineno)
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000669{
670 PyObject *exc, *v, *tb, *tmp;
671
672 /* add attributes for the line number and filename for the error */
673 PyErr_Fetch(&exc, &v, &tb);
674 PyErr_NormalizeException(&exc, &v, &tb);
Richard Jones7b9558d2006-05-27 12:29:24 +0000675 /* XXX check that it is, indeed, a syntax error. It might not
676 * be, though. */
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000677 tmp = PyInt_FromLong(lineno);
678 if (tmp == NULL)
679 PyErr_Clear();
680 else {
681 if (PyObject_SetAttrString(v, "lineno", tmp))
682 PyErr_Clear();
683 Py_DECREF(tmp);
684 }
685 if (filename != NULL) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000686 tmp = PyString_FromString(filename);
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000687 if (tmp == NULL)
688 PyErr_Clear();
689 else {
690 if (PyObject_SetAttrString(v, "filename", tmp))
691 PyErr_Clear();
692 Py_DECREF(tmp);
693 }
694
695 tmp = PyErr_ProgramText(filename, lineno);
696 if (tmp) {
Georg Brandla1121fa2006-05-29 14:13:21 +0000697 if (PyObject_SetAttrString(v, "text", tmp))
698 PyErr_Clear();
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000699 Py_DECREF(tmp);
700 }
701 }
Martin v. Löwiscfeb3b62002-03-03 21:30:27 +0000702 if (PyObject_SetAttrString(v, "offset", Py_None)) {
703 PyErr_Clear();
704 }
705 if (exc != PyExc_SyntaxError) {
706 if (!PyObject_HasAttrString(v, "msg")) {
707 tmp = PyObject_Str(v);
708 if (tmp) {
709 if (PyObject_SetAttrString(v, "msg", tmp))
710 PyErr_Clear();
711 Py_DECREF(tmp);
712 } else {
713 PyErr_Clear();
714 }
715 }
716 if (!PyObject_HasAttrString(v, "print_file_and_line")) {
717 if (PyObject_SetAttrString(v, "print_file_and_line",
718 Py_None))
719 PyErr_Clear();
720 }
721 }
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000722 PyErr_Restore(exc, v, tb);
723}
724
725/* com_fetch_program_text will attempt to load the line of text that
726 the exception refers to. If it fails, it will return NULL but will
Brett Cannonbf364092006-03-01 04:25:17 +0000727 not set an exception.
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000728
729 XXX The functionality of this function is quite similar to the
730 functionality in tb_displayline() in traceback.c.
731*/
732
733PyObject *
Martin v. Löwis95292d62002-12-11 14:04:59 +0000734PyErr_ProgramText(const char *filename, int lineno)
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000735{
736 FILE *fp;
737 int i;
738 char linebuf[1000];
739
Tim Petersa7444f42006-02-27 23:29:46 +0000740 if (filename == NULL || *filename == '\0' || lineno <= 0)
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000741 return NULL;
Jack Jansen7b8c7542002-04-14 20:12:41 +0000742 fp = fopen(filename, "r" PY_STDIOTEXTMODE);
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000743 if (fp == NULL)
744 return NULL;
745 for (i = 0; i < lineno; i++) {
746 char *pLastChar = &linebuf[sizeof(linebuf) - 2];
747 do {
748 *pLastChar = '\0';
Jack Jansen7b8c7542002-04-14 20:12:41 +0000749 if (Py_UniversalNewlineFgets(linebuf, sizeof linebuf, fp, NULL) == NULL)
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000750 break;
751 /* fgets read *something*; if it didn't get as
752 far as pLastChar, it must have found a newline
Walter Dörwaldc611f172006-05-25 08:53:28 +0000753 or hit the end of the file; if pLastChar is \n,
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000754 it obviously found a newline; else we haven't
755 yet seen a newline, so must continue */
756 } while (*pLastChar != '\0' && *pLastChar != '\n');
757 }
758 fclose(fp);
759 if (i == lineno) {
760 char *p = linebuf;
761 while (*p == ' ' || *p == '\t' || *p == '\014')
762 p++;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000763 return PyString_FromString(p);
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000764 }
765 return NULL;
766}
Anthony Baxterac6bd462006-04-13 02:06:09 +0000767
768#ifdef __cplusplus
769}
770#endif
771