blob: 615da191a1a0133841baa8c98b8b854fd243c6a9 [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)) {
Amaury Forgeot d'Arc246daed2008-07-31 00:42:16 +0000109 int res = 0;
110 PyObject *exception, *value, *tb;
111 PyErr_Fetch(&exception, &value, &tb);
112 res = PyObject_IsSubclass(err, exc);
113 /* This function must not fail, so print the error here */
114 if (res == -1) {
115 PyErr_WriteUnraisable(err);
Amaury Forgeot d'Arc246daed2008-07-31 00:42:16 +0000116 res = 0;
117 }
118 PyErr_Restore(exception, value, tb);
119 return res;
Brett Cannonbf364092006-03-01 04:25:17 +0000120 }
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000121
122 return err == exc;
123}
Guido van Rossum743007d1999-04-21 15:27:31 +0000124
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000125
126int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000127PyErr_ExceptionMatches(PyObject *exc)
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000128{
129 return PyErr_GivenExceptionMatches(PyErr_Occurred(), exc);
130}
131
132
133/* Used in many places to normalize a raised exception, including in
134 eval_code2(), do_raise(), and PyErr_Print()
135*/
136void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000137PyErr_NormalizeException(PyObject **exc, PyObject **val, PyObject **tb)
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000138{
139 PyObject *type = *exc;
140 PyObject *value = *val;
141 PyObject *inclass = NULL;
Jeremy Hyltone2e2c9f2001-09-26 19:58:38 +0000142 PyObject *initial_tb = NULL;
Brett Cannon1e534b52007-09-07 04:18:30 +0000143 PyThreadState *tstate = 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 */
Brett Cannon1e534b52007-09-07 04:18:30 +0000219 tstate = PyThreadState_GET();
220 if (++tstate->recursion_depth > Py_GetRecursionLimit()) {
221 --tstate->recursion_depth;
Georg Brandle9b91212009-04-05 21:26:31 +0000222 /* throw away the old exception... */
223 Py_DECREF(*exc);
224 Py_DECREF(*val);
225 /* ... and use the recursion error instead */
226 *exc = PyExc_RuntimeError;
227 *val = PyExc_RecursionErrorInst;
228 Py_INCREF(*exc);
229 Py_INCREF(*val);
230 /* just keeping the old traceback */
Brett Cannon1e534b52007-09-07 04:18:30 +0000231 return;
232 }
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000233 PyErr_NormalizeException(exc, val, tb);
Brett Cannon1e534b52007-09-07 04:18:30 +0000234 --tstate->recursion_depth;
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000235}
236
237
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000238void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000239PyErr_Fetch(PyObject **p_type, PyObject **p_value, PyObject **p_traceback)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000240{
Nicholas Bastine5662ae2004-03-24 22:22:12 +0000241 PyThreadState *tstate = PyThreadState_GET();
Guido van Rossuma027efa1997-05-05 20:56:21 +0000242
243 *p_type = tstate->curexc_type;
244 *p_value = tstate->curexc_value;
245 *p_traceback = tstate->curexc_traceback;
246
247 tstate->curexc_type = NULL;
248 tstate->curexc_value = NULL;
249 tstate->curexc_traceback = NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000250}
251
252void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000253PyErr_Clear(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000254{
Guido van Rossuma027efa1997-05-05 20:56:21 +0000255 PyErr_Restore(NULL, NULL, NULL);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000256}
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000257
258/* Convenience functions to set a type error exception and return 0 */
259
260int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000261PyErr_BadArgument(void)
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000262{
Guido van Rossuma027efa1997-05-05 20:56:21 +0000263 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +0000264 "bad argument type for built-in operation");
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000265 return 0;
266}
267
Guido van Rossum373c8691997-04-29 18:22:47 +0000268PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000269PyErr_NoMemory(void)
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000270{
Vladimir Marangozov0888ff12000-08-18 18:01:06 +0000271 if (PyErr_ExceptionMatches(PyExc_MemoryError))
272 /* already current */
273 return NULL;
274
Barry Warsaw2d8adff1997-08-29 21:54:35 +0000275 /* raise the pre-allocated instance if it still exists */
276 if (PyExc_MemoryErrorInst)
277 PyErr_SetObject(PyExc_MemoryError, PyExc_MemoryErrorInst);
278 else
279 /* this will probably fail since there's no memory and hee,
280 hee, we have to instantiate this class
281 */
282 PyErr_SetNone(PyExc_MemoryError);
283
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000284 return NULL;
285}
286
Guido van Rossum373c8691997-04-29 18:22:47 +0000287PyObject *
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000288PyErr_SetFromErrnoWithFilenameObject(PyObject *exc, PyObject *filenameObject)
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000289{
Guido van Rossum373c8691997-04-29 18:22:47 +0000290 PyObject *v;
Guido van Rossume0e59821998-10-14 20:38:13 +0000291 char *s;
Guido van Rossum3a241811994-08-29 12:14:12 +0000292 int i = errno;
Martin v. Löwis3484a182002-03-09 12:07:51 +0000293#ifdef PLAN9
294 char errbuf[ERRMAX];
295#endif
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000296#ifdef MS_WINDOWS
Guido van Rossum795e1892000-02-17 15:19:15 +0000297 char *s_buf = NULL;
Mark Hammond3d61a062002-10-04 00:13:02 +0000298 char s_small_buf[28]; /* Room for "Windows Error 0xFFFFFFFF" */
Guido van Rossum795e1892000-02-17 15:19:15 +0000299#endif
Guido van Rossume9fbc091995-02-18 14:52:19 +0000300#ifdef EINTR
Guido van Rossum373c8691997-04-29 18:22:47 +0000301 if (i == EINTR && PyErr_CheckSignals())
Guido van Rossum5063bab1991-10-20 20:14:56 +0000302 return NULL;
Guido van Rossume9fbc091995-02-18 14:52:19 +0000303#endif
Martin v. Löwis3484a182002-03-09 12:07:51 +0000304#ifdef PLAN9
305 rerrstr(errbuf, sizeof errbuf);
306 s = errbuf;
307#else
Guido van Rossume0e59821998-10-14 20:38:13 +0000308 if (i == 0)
309 s = "Error"; /* Sometimes errno didn't get set */
Barry Warsaw97d95151998-07-23 16:05:56 +0000310 else
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000311#ifndef MS_WINDOWS
Guido van Rossume0e59821998-10-14 20:38:13 +0000312 s = strerror(i);
Guido van Rossum743007d1999-04-21 15:27:31 +0000313#else
314 {
Guido van Rossum795e1892000-02-17 15:19:15 +0000315 /* Note that the Win32 errors do not lineup with the
316 errno error. So if the error is in the MSVC error
Brett Cannonbf364092006-03-01 04:25:17 +0000317 table, we use it, otherwise we assume it really _is_
Guido van Rossum795e1892000-02-17 15:19:15 +0000318 a Win32 error code
319 */
Guido van Rossum584b16a2000-02-21 16:50:31 +0000320 if (i > 0 && i < _sys_nerr) {
Guido van Rossum795e1892000-02-17 15:19:15 +0000321 s = _sys_errlist[i];
322 }
323 else {
324 int len = FormatMessage(
325 FORMAT_MESSAGE_ALLOCATE_BUFFER |
326 FORMAT_MESSAGE_FROM_SYSTEM |
327 FORMAT_MESSAGE_IGNORE_INSERTS,
328 NULL, /* no message source */
329 i,
330 MAKELANGID(LANG_NEUTRAL,
331 SUBLANG_DEFAULT),
332 /* Default language */
333 (LPTSTR) &s_buf,
334 0, /* size not used */
335 NULL); /* no args */
Mark Hammond3d61a062002-10-04 00:13:02 +0000336 if (len==0) {
Brett Cannonbf364092006-03-01 04:25:17 +0000337 /* Only ever seen this in out-of-mem
Mark Hammond3d61a062002-10-04 00:13:02 +0000338 situations */
339 sprintf(s_small_buf, "Windows Error 0x%X", i);
340 s = s_small_buf;
341 s_buf = NULL;
342 } else {
343 s = s_buf;
344 /* remove trailing cr/lf and dots */
345 while (len > 0 && (s[len-1] <= ' ' || s[len-1] == '.'))
346 s[--len] = '\0';
347 }
Guido van Rossum795e1892000-02-17 15:19:15 +0000348 }
Guido van Rossum743007d1999-04-21 15:27:31 +0000349 }
Martin v. Löwis3484a182002-03-09 12:07:51 +0000350#endif /* Unix/Windows */
351#endif /* PLAN 9*/
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000352 if (filenameObject != NULL)
353 v = Py_BuildValue("(isO)", i, s, filenameObject);
Guido van Rossume0e59821998-10-14 20:38:13 +0000354 else
355 v = Py_BuildValue("(is)", i, s);
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000356 if (v != NULL) {
Guido van Rossum373c8691997-04-29 18:22:47 +0000357 PyErr_SetObject(exc, v);
358 Py_DECREF(v);
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000359 }
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000360#ifdef MS_WINDOWS
Guido van Rossum795e1892000-02-17 15:19:15 +0000361 LocalFree(s_buf);
Guido van Rossum743007d1999-04-21 15:27:31 +0000362#endif
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000363 return NULL;
364}
Guido van Rossum743007d1999-04-21 15:27:31 +0000365
Barry Warsaw97d95151998-07-23 16:05:56 +0000366
367PyObject *
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000368PyErr_SetFromErrnoWithFilename(PyObject *exc, char *filename)
369{
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000370 PyObject *name = filename ? PyString_FromString(filename) : NULL;
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000371 PyObject *result = PyErr_SetFromErrnoWithFilenameObject(exc, name);
Mark Hammondda7efaa2002-10-04 00:09:38 +0000372 Py_XDECREF(name);
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000373 return result;
374}
375
376#ifdef Py_WIN_WIDE_FILENAMES
377PyObject *
378PyErr_SetFromErrnoWithUnicodeFilename(PyObject *exc, Py_UNICODE *filename)
379{
Brett Cannonbf364092006-03-01 04:25:17 +0000380 PyObject *name = filename ?
381 PyUnicode_FromUnicode(filename, wcslen(filename)) :
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000382 NULL;
383 PyObject *result = PyErr_SetFromErrnoWithFilenameObject(exc, name);
384 Py_XDECREF(name);
385 return result;
386}
387#endif /* Py_WIN_WIDE_FILENAMES */
388
389PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000390PyErr_SetFromErrno(PyObject *exc)
Barry Warsaw97d95151998-07-23 16:05:56 +0000391{
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000392 return PyErr_SetFromErrnoWithFilenameObject(exc, NULL);
Barry Warsaw97d95151998-07-23 16:05:56 +0000393}
Guido van Rossum683a0721990-10-21 22:09:12 +0000394
Brett Cannonbf364092006-03-01 04:25:17 +0000395#ifdef MS_WINDOWS
Guido van Rossum795e1892000-02-17 15:19:15 +0000396/* Windows specific error code handling */
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000397PyObject *PyErr_SetExcFromWindowsErrWithFilenameObject(
Thomas Heller085358a2002-07-29 14:27:41 +0000398 PyObject *exc,
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000399 int ierr,
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000400 PyObject *filenameObject)
Guido van Rossum795e1892000-02-17 15:19:15 +0000401{
402 int len;
403 char *s;
Mark Hammond3d61a062002-10-04 00:13:02 +0000404 char *s_buf = NULL; /* Free via LocalFree */
405 char s_small_buf[28]; /* Room for "Windows Error 0xFFFFFFFF" */
Guido van Rossum795e1892000-02-17 15:19:15 +0000406 PyObject *v;
407 DWORD err = (DWORD)ierr;
408 if (err==0) err = GetLastError();
409 len = FormatMessage(
410 /* Error API error */
411 FORMAT_MESSAGE_ALLOCATE_BUFFER |
412 FORMAT_MESSAGE_FROM_SYSTEM |
413 FORMAT_MESSAGE_IGNORE_INSERTS,
414 NULL, /* no message source */
415 err,
416 MAKELANGID(LANG_NEUTRAL,
417 SUBLANG_DEFAULT), /* Default language */
Mark Hammond3d61a062002-10-04 00:13:02 +0000418 (LPTSTR) &s_buf,
Guido van Rossum795e1892000-02-17 15:19:15 +0000419 0, /* size not used */
420 NULL); /* no args */
Mark Hammond3d61a062002-10-04 00:13:02 +0000421 if (len==0) {
422 /* Only seen this in out of mem situations */
423 sprintf(s_small_buf, "Windows Error 0x%X", err);
424 s = s_small_buf;
425 s_buf = NULL;
426 } else {
427 s = s_buf;
428 /* remove trailing cr/lf and dots */
429 while (len > 0 && (s[len-1] <= ' ' || s[len-1] == '.'))
430 s[--len] = '\0';
431 }
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000432 if (filenameObject != NULL)
433 v = Py_BuildValue("(isO)", err, s, filenameObject);
Guido van Rossum795e1892000-02-17 15:19:15 +0000434 else
435 v = Py_BuildValue("(is)", err, s);
436 if (v != NULL) {
Thomas Heller085358a2002-07-29 14:27:41 +0000437 PyErr_SetObject(exc, v);
Guido van Rossum795e1892000-02-17 15:19:15 +0000438 Py_DECREF(v);
439 }
Mark Hammond3d61a062002-10-04 00:13:02 +0000440 LocalFree(s_buf);
Guido van Rossum795e1892000-02-17 15:19:15 +0000441 return NULL;
442}
443
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000444PyObject *PyErr_SetExcFromWindowsErrWithFilename(
445 PyObject *exc,
446 int ierr,
447 const char *filename)
448{
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000449 PyObject *name = filename ? PyString_FromString(filename) : 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
457#ifdef Py_WIN_WIDE_FILENAMES
458PyObject *PyErr_SetExcFromWindowsErrWithUnicodeFilename(
459 PyObject *exc,
460 int ierr,
461 const Py_UNICODE *filename)
462{
Brett Cannonbf364092006-03-01 04:25:17 +0000463 PyObject *name = filename ?
464 PyUnicode_FromUnicode(filename, wcslen(filename)) :
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000465 NULL;
Brett Cannonbf364092006-03-01 04:25:17 +0000466 PyObject *ret = PyErr_SetExcFromWindowsErrWithFilenameObject(exc,
467 ierr,
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000468 name);
469 Py_XDECREF(name);
470 return ret;
471}
472#endif /* Py_WIN_WIDE_FILENAMES */
473
Thomas Heller085358a2002-07-29 14:27:41 +0000474PyObject *PyErr_SetExcFromWindowsErr(PyObject *exc, int ierr)
475{
476 return PyErr_SetExcFromWindowsErrWithFilename(exc, ierr, NULL);
477}
478
Guido van Rossum795e1892000-02-17 15:19:15 +0000479PyObject *PyErr_SetFromWindowsErr(int ierr)
480{
Thomas Heller085358a2002-07-29 14:27:41 +0000481 return PyErr_SetExcFromWindowsErrWithFilename(PyExc_WindowsError,
482 ierr, NULL);
483}
484PyObject *PyErr_SetFromWindowsErrWithFilename(
485 int ierr,
486 const char *filename)
487{
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000488 PyObject *name = filename ? PyString_FromString(filename) : NULL;
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000489 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;
Guido van Rossum795e1892000-02-17 15:19:15 +0000494}
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000495
496#ifdef Py_WIN_WIDE_FILENAMES
497PyObject *PyErr_SetFromWindowsErrWithUnicodeFilename(
498 int ierr,
499 const Py_UNICODE *filename)
500{
Brett Cannonbf364092006-03-01 04:25:17 +0000501 PyObject *name = filename ?
502 PyUnicode_FromUnicode(filename, wcslen(filename)) :
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000503 NULL;
504 PyObject *result = PyErr_SetExcFromWindowsErrWithFilenameObject(
505 PyExc_WindowsError,
506 ierr, name);
Mark Hammondda7efaa2002-10-04 00:09:38 +0000507 Py_XDECREF(name);
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000508 return result;
509}
510#endif /* Py_WIN_WIDE_FILENAMES */
Guido van Rossum795e1892000-02-17 15:19:15 +0000511#endif /* MS_WINDOWS */
512
Guido van Rossum683a0721990-10-21 22:09:12 +0000513void
Fred Drake6d63adf2000-08-24 22:38:39 +0000514_PyErr_BadInternalCall(char *filename, int lineno)
515{
516 PyErr_Format(PyExc_SystemError,
517 "%s:%d: bad argument to internal function",
518 filename, lineno);
519}
520
521/* Remove the preprocessor macro for PyErr_BadInternalCall() so that we can
522 export the entry point for existing object code: */
523#undef PyErr_BadInternalCall
524void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000525PyErr_BadInternalCall(void)
Guido van Rossum683a0721990-10-21 22:09:12 +0000526{
Fred Drake6d63adf2000-08-24 22:38:39 +0000527 PyErr_Format(PyExc_SystemError,
528 "bad argument to internal function");
Guido van Rossum683a0721990-10-21 22:09:12 +0000529}
Fred Drake6d63adf2000-08-24 22:38:39 +0000530#define PyErr_BadInternalCall() _PyErr_BadInternalCall(__FILE__, __LINE__)
531
Guido van Rossum1548bac1997-02-14 17:09:47 +0000532
533
Guido van Rossum1548bac1997-02-14 17:09:47 +0000534PyObject *
535PyErr_Format(PyObject *exception, const char *format, ...)
Guido van Rossum1548bac1997-02-14 17:09:47 +0000536{
537 va_list vargs;
Jeremy Hyltonb69a27e2000-09-01 03:49:47 +0000538 PyObject* string;
Guido van Rossum1548bac1997-02-14 17:09:47 +0000539
Jeremy Hyltonb69a27e2000-09-01 03:49:47 +0000540#ifdef HAVE_STDARG_PROTOTYPES
Guido van Rossum1548bac1997-02-14 17:09:47 +0000541 va_start(vargs, format);
Jeremy Hyltonb69a27e2000-09-01 03:49:47 +0000542#else
543 va_start(vargs);
544#endif
Guido van Rossum1548bac1997-02-14 17:09:47 +0000545
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000546 string = PyString_FromFormatV(format, vargs);
Jeremy Hyltonb69a27e2000-09-01 03:49:47 +0000547 PyErr_SetObject(exception, string);
548 Py_XDECREF(string);
Tim Petersc15c4f12001-10-02 21:32:07 +0000549 va_end(vargs);
Guido van Rossum1548bac1997-02-14 17:09:47 +0000550 return NULL;
551}
Guido van Rossum7617e051997-09-16 18:43:50 +0000552
553
Georg Brandl658d5132006-05-23 11:17:21 +0000554
Guido van Rossum7617e051997-09-16 18:43:50 +0000555PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000556PyErr_NewException(char *name, PyObject *base, PyObject *dict)
Guido van Rossum7617e051997-09-16 18:43:50 +0000557{
Guido van Rossum2ac650f1997-10-03 19:50:55 +0000558 char *dot;
559 PyObject *modulename = NULL;
560 PyObject *classname = NULL;
561 PyObject *mydict = NULL;
562 PyObject *bases = NULL;
Guido van Rossum7617e051997-09-16 18:43:50 +0000563 PyObject *result = NULL;
Guido van Rossum2ac650f1997-10-03 19:50:55 +0000564 dot = strrchr(name, '.');
565 if (dot == NULL) {
566 PyErr_SetString(PyExc_SystemError,
567 "PyErr_NewException: name must be module.class");
Guido van Rossum7617e051997-09-16 18:43:50 +0000568 return NULL;
Guido van Rossum7617e051997-09-16 18:43:50 +0000569 }
570 if (base == NULL)
571 base = PyExc_Exception;
Guido van Rossum2ac650f1997-10-03 19:50:55 +0000572 if (dict == NULL) {
573 dict = mydict = PyDict_New();
574 if (dict == NULL)
575 goto failure;
576 }
577 if (PyDict_GetItemString(dict, "__module__") == NULL) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000578 modulename = PyString_FromStringAndSize(name,
Armin Rigo7ccbca92006-10-04 12:17:45 +0000579 (Py_ssize_t)(dot-name));
Guido van Rossum2ac650f1997-10-03 19:50:55 +0000580 if (modulename == NULL)
581 goto failure;
582 if (PyDict_SetItemString(dict, "__module__", modulename) != 0)
583 goto failure;
584 }
Georg Brandl658d5132006-05-23 11:17:21 +0000585 if (PyTuple_Check(base)) {
586 bases = base;
587 /* INCREF as we create a new ref in the else branch */
588 Py_INCREF(bases);
589 } else {
590 bases = PyTuple_Pack(1, base);
591 if (bases == NULL)
592 goto failure;
593 }
Richard Jones7b9558d2006-05-27 12:29:24 +0000594 /* Create a real new-style class. */
595 result = PyObject_CallFunction((PyObject *)&PyType_Type, "sOO",
596 dot+1, bases, dict);
Guido van Rossum7617e051997-09-16 18:43:50 +0000597 failure:
Guido van Rossum2ac650f1997-10-03 19:50:55 +0000598 Py_XDECREF(bases);
599 Py_XDECREF(mydict);
600 Py_XDECREF(classname);
601 Py_XDECREF(modulename);
Guido van Rossum7617e051997-09-16 18:43:50 +0000602 return result;
603}
Jeremy Hyltonb709df32000-09-01 02:47:25 +0000604
605/* Call when an exception has occurred but there is no way for Python
606 to handle it. Examples: exception in __del__ or during GC. */
607void
608PyErr_WriteUnraisable(PyObject *obj)
609{
610 PyObject *f, *t, *v, *tb;
611 PyErr_Fetch(&t, &v, &tb);
612 f = PySys_GetObject("stderr");
613 if (f != NULL) {
614 PyFile_WriteString("Exception ", f);
615 if (t) {
Richard Jones7b9558d2006-05-27 12:29:24 +0000616 PyObject* moduleName;
Neal Norwitzf83b7512007-02-26 23:48:27 +0000617 char* className;
618 assert(PyExceptionClass_Check(t));
619 className = PyExceptionClass_Name(t);
Tim Peters5e9d6cf2006-05-28 10:41:29 +0000620 if (className != NULL) {
621 char *dot = strrchr(className, '.');
622 if (dot != NULL)
623 className = dot+1;
624 }
625
626 moduleName = PyObject_GetAttrString(t, "__module__");
Brett Cannonbf364092006-03-01 04:25:17 +0000627 if (moduleName == NULL)
628 PyFile_WriteString("<unknown>", f);
629 else {
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000630 char* modstr = PyString_AsString(moduleName);
Neal Norwitz88516a62007-02-26 22:41:45 +0000631 if (modstr &&
632 strcmp(modstr, "exceptions") != 0)
Brett Cannonbf364092006-03-01 04:25:17 +0000633 {
634 PyFile_WriteString(modstr, f);
635 PyFile_WriteString(".", f);
636 }
637 }
638 if (className == NULL)
639 PyFile_WriteString("<unknown>", f);
640 else
641 PyFile_WriteString(className, f);
Jeremy Hyltonb709df32000-09-01 02:47:25 +0000642 if (v && v != Py_None) {
643 PyFile_WriteString(": ", f);
644 PyFile_WriteObject(v, f, 0);
645 }
Neal Norwitz1a269202006-04-17 00:33:23 +0000646 Py_XDECREF(moduleName);
Jeremy Hyltonb709df32000-09-01 02:47:25 +0000647 }
648 PyFile_WriteString(" in ", f);
649 PyFile_WriteObject(obj, f, 0);
650 PyFile_WriteString(" ignored\n", f);
651 PyErr_Clear(); /* Just in case */
652 }
653 Py_XDECREF(t);
654 Py_XDECREF(v);
655 Py_XDECREF(tb);
656}
Guido van Rossumcfd42b52000-12-15 21:58:52 +0000657
Armin Rigo092381a2003-10-25 14:29:27 +0000658extern PyObject *PyModule_GetWarningsModule(void);
Guido van Rossumcfd42b52000-12-15 21:58:52 +0000659
Guido van Rossum2fd45652001-02-28 21:46:24 +0000660
Martin v. Löwiscfeb3b62002-03-03 21:30:27 +0000661/* Set file and line information for the current exception.
662 If the exception is not a SyntaxError, also sets additional attributes
663 to make printing of exceptions believe it is a syntax error. */
Guido van Rossum2fd45652001-02-28 21:46:24 +0000664
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000665void
Martin v. Löwis95292d62002-12-11 14:04:59 +0000666PyErr_SyntaxLocation(const char *filename, int lineno)
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000667{
668 PyObject *exc, *v, *tb, *tmp;
669
670 /* add attributes for the line number and filename for the error */
671 PyErr_Fetch(&exc, &v, &tb);
672 PyErr_NormalizeException(&exc, &v, &tb);
Richard Jones7b9558d2006-05-27 12:29:24 +0000673 /* XXX check that it is, indeed, a syntax error. It might not
674 * be, though. */
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000675 tmp = PyInt_FromLong(lineno);
676 if (tmp == NULL)
677 PyErr_Clear();
678 else {
679 if (PyObject_SetAttrString(v, "lineno", tmp))
680 PyErr_Clear();
681 Py_DECREF(tmp);
682 }
683 if (filename != NULL) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000684 tmp = PyString_FromString(filename);
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000685 if (tmp == NULL)
686 PyErr_Clear();
687 else {
688 if (PyObject_SetAttrString(v, "filename", tmp))
689 PyErr_Clear();
690 Py_DECREF(tmp);
691 }
692
693 tmp = PyErr_ProgramText(filename, lineno);
694 if (tmp) {
Georg Brandla1121fa2006-05-29 14:13:21 +0000695 if (PyObject_SetAttrString(v, "text", tmp))
696 PyErr_Clear();
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000697 Py_DECREF(tmp);
698 }
699 }
Martin v. Löwiscfeb3b62002-03-03 21:30:27 +0000700 if (PyObject_SetAttrString(v, "offset", Py_None)) {
701 PyErr_Clear();
702 }
703 if (exc != PyExc_SyntaxError) {
704 if (!PyObject_HasAttrString(v, "msg")) {
705 tmp = PyObject_Str(v);
706 if (tmp) {
707 if (PyObject_SetAttrString(v, "msg", tmp))
708 PyErr_Clear();
709 Py_DECREF(tmp);
710 } else {
711 PyErr_Clear();
712 }
713 }
714 if (!PyObject_HasAttrString(v, "print_file_and_line")) {
715 if (PyObject_SetAttrString(v, "print_file_and_line",
716 Py_None))
717 PyErr_Clear();
718 }
719 }
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000720 PyErr_Restore(exc, v, tb);
721}
722
723/* com_fetch_program_text will attempt to load the line of text that
724 the exception refers to. If it fails, it will return NULL but will
Brett Cannonbf364092006-03-01 04:25:17 +0000725 not set an exception.
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000726
727 XXX The functionality of this function is quite similar to the
728 functionality in tb_displayline() in traceback.c.
729*/
730
731PyObject *
Martin v. Löwis95292d62002-12-11 14:04:59 +0000732PyErr_ProgramText(const char *filename, int lineno)
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000733{
734 FILE *fp;
735 int i;
736 char linebuf[1000];
737
Tim Petersa7444f42006-02-27 23:29:46 +0000738 if (filename == NULL || *filename == '\0' || lineno <= 0)
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000739 return NULL;
Jack Jansen7b8c7542002-04-14 20:12:41 +0000740 fp = fopen(filename, "r" PY_STDIOTEXTMODE);
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000741 if (fp == NULL)
742 return NULL;
743 for (i = 0; i < lineno; i++) {
744 char *pLastChar = &linebuf[sizeof(linebuf) - 2];
745 do {
746 *pLastChar = '\0';
Jack Jansen7b8c7542002-04-14 20:12:41 +0000747 if (Py_UniversalNewlineFgets(linebuf, sizeof linebuf, fp, NULL) == NULL)
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000748 break;
749 /* fgets read *something*; if it didn't get as
750 far as pLastChar, it must have found a newline
Walter Dörwaldc611f172006-05-25 08:53:28 +0000751 or hit the end of the file; if pLastChar is \n,
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000752 it obviously found a newline; else we haven't
753 yet seen a newline, so must continue */
754 } while (*pLastChar != '\0' && *pLastChar != '\n');
755 }
756 fclose(fp);
757 if (i == lineno) {
758 char *p = linebuf;
759 while (*p == ' ' || *p == '\t' || *p == '\014')
760 p++;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000761 return PyString_FromString(p);
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000762 }
763 return NULL;
764}
Anthony Baxterac6bd462006-04-13 02:06:09 +0000765
766#ifdef __cplusplus
767}
768#endif
769