blob: 7fc4c97c825d106c2865a3e025f0dac61bbccfd3 [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
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000019void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000020PyErr_Restore(PyObject *type, PyObject *value, PyObject *traceback)
Guido van Rossum1ae940a1995-01-02 19:04:15 +000021{
Guido van Rossum885553e1998-12-21 18:33:30 +000022 PyThreadState *tstate = PyThreadState_GET();
Guido van Rossuma027efa1997-05-05 20:56:21 +000023 PyObject *oldtype, *oldvalue, *oldtraceback;
Guido van Rossum1ae940a1995-01-02 19:04:15 +000024
Guido van Rossuma027efa1997-05-05 20:56:21 +000025 if (traceback != NULL && !PyTraceBack_Check(traceback)) {
26 /* XXX Should never happen -- fatal error instead? */
Guido van Rossum1a5e21e2006-02-28 21:57:43 +000027 /* Well, it could be None. */
Guido van Rossuma027efa1997-05-05 20:56:21 +000028 Py_DECREF(traceback);
29 traceback = NULL;
30 }
31
32 /* Save these in locals to safeguard against recursive
33 invocation through Py_XDECREF */
34 oldtype = tstate->curexc_type;
35 oldvalue = tstate->curexc_value;
36 oldtraceback = tstate->curexc_traceback;
37
38 tstate->curexc_type = type;
39 tstate->curexc_value = value;
40 tstate->curexc_traceback = traceback;
41
42 Py_XDECREF(oldtype);
43 Py_XDECREF(oldvalue);
44 Py_XDECREF(oldtraceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +000045}
46
47void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000048PyErr_SetObject(PyObject *exception, PyObject *value)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000049{
Guido van Rossum373c8691997-04-29 18:22:47 +000050 Py_XINCREF(exception);
51 Py_XINCREF(value);
52 PyErr_Restore(exception, value, (PyObject *)NULL);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000053}
54
55void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000056PyErr_SetNone(PyObject *exception)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000057{
Guido van Rossum373c8691997-04-29 18:22:47 +000058 PyErr_SetObject(exception, (PyObject *)NULL);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000059}
60
61void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000062PyErr_SetString(PyObject *exception, const char *string)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000063{
Guido van Rossum373c8691997-04-29 18:22:47 +000064 PyObject *value = PyString_FromString(string);
65 PyErr_SetObject(exception, value);
66 Py_XDECREF(value);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000067}
68
Guido van Rossum3a241811994-08-29 12:14:12 +000069
Guido van Rossum373c8691997-04-29 18:22:47 +000070PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000071PyErr_Occurred(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000072{
Tim Peters024da352001-05-30 06:09:50 +000073 PyThreadState *tstate = PyThreadState_GET();
Guido van Rossuma027efa1997-05-05 20:56:21 +000074
75 return tstate->curexc_type;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000076}
77
Barry Warsawc0dc92a1997-08-22 21:22:58 +000078
79int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000080PyErr_GivenExceptionMatches(PyObject *err, PyObject *exc)
Barry Warsawc0dc92a1997-08-22 21:22:58 +000081{
Barry Warsawfa5c3152000-05-02 19:27:51 +000082 if (err == NULL || exc == NULL) {
83 /* maybe caused by "import exceptions" that failed early on */
84 return 0;
85 }
Barry Warsawc0dc92a1997-08-22 21:22:58 +000086 if (PyTuple_Check(exc)) {
Martin v. Löwisd96ee902006-02-16 14:37:16 +000087 Py_ssize_t i, n;
Barry Warsawc0dc92a1997-08-22 21:22:58 +000088 n = PyTuple_Size(exc);
89 for (i = 0; i < n; i++) {
90 /* Test recursively */
91 if (PyErr_GivenExceptionMatches(
92 err, PyTuple_GET_ITEM(exc, i)))
93 {
94 return 1;
95 }
96 }
97 return 0;
98 }
99 /* err might be an instance, so check its class. */
Brett Cannonbf364092006-03-01 04:25:17 +0000100 if (PyExceptionInstance_Check(err))
101 err = PyExceptionInstance_Class(err);
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000102
Brett Cannonbf364092006-03-01 04:25:17 +0000103 if (PyExceptionClass_Check(err) && PyExceptionClass_Check(exc)) {
104 /* problems here!? not sure PyObject_IsSubclass expects to
105 be called with an exception pending... */
106 return PyObject_IsSubclass(err, exc);
107 }
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000108
109 return err == exc;
110}
Guido van Rossum743007d1999-04-21 15:27:31 +0000111
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000112
113int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000114PyErr_ExceptionMatches(PyObject *exc)
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000115{
116 return PyErr_GivenExceptionMatches(PyErr_Occurred(), exc);
117}
118
119
120/* Used in many places to normalize a raised exception, including in
121 eval_code2(), do_raise(), and PyErr_Print()
122*/
123void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000124PyErr_NormalizeException(PyObject **exc, PyObject **val, PyObject **tb)
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000125{
126 PyObject *type = *exc;
127 PyObject *value = *val;
128 PyObject *inclass = NULL;
Jeremy Hyltone2e2c9f2001-09-26 19:58:38 +0000129 PyObject *initial_tb = NULL;
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000130
Guido van Rossumed473a42000-08-07 19:18:27 +0000131 if (type == NULL) {
Guido van Rossum6b3fffa2003-04-10 20:29:48 +0000132 /* There was no exception, so nothing to do. */
133 return;
Guido van Rossumed473a42000-08-07 19:18:27 +0000134 }
135
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000136 /* If PyErr_SetNone() was used, the value will have been actually
137 set to NULL.
138 */
139 if (!value) {
140 value = Py_None;
141 Py_INCREF(value);
142 }
143
Brett Cannonbf364092006-03-01 04:25:17 +0000144 if (PyExceptionInstance_Check(value))
145 inclass = PyExceptionInstance_Class(value);
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000146
147 /* Normalize the exception so that if the type is a class, the
148 value will be an instance.
149 */
Brett Cannonbf364092006-03-01 04:25:17 +0000150 if (PyExceptionClass_Check(type)) {
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000151 /* if the value was not an instance, or is not an instance
152 whose class is (or is derived from) type, then use the
153 value as an argument to instantiation of the type
154 class.
155 */
Brett Cannonbf364092006-03-01 04:25:17 +0000156 if (!inclass || !PyObject_IsSubclass(inclass, type)) {
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000157 PyObject *args, *res;
158
159 if (value == Py_None)
Raymond Hettinger8ae46892003-10-12 19:09:37 +0000160 args = PyTuple_New(0);
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000161 else if (PyTuple_Check(value)) {
162 Py_INCREF(value);
163 args = value;
164 }
165 else
Raymond Hettinger8ae46892003-10-12 19:09:37 +0000166 args = PyTuple_Pack(1, value);
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000167
168 if (args == NULL)
169 goto finally;
170 res = PyEval_CallObject(type, args);
171 Py_DECREF(args);
172 if (res == NULL)
173 goto finally;
174 Py_DECREF(value);
175 value = res;
176 }
Barry Warsaw3a749931997-09-30 15:00:18 +0000177 /* if the class of the instance doesn't exactly match the
178 class of the type, believe the instance
179 */
180 else if (inclass != type) {
181 Py_DECREF(type);
182 type = inclass;
183 Py_INCREF(type);
184 }
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000185 }
186 *exc = type;
187 *val = value;
188 return;
189finally:
Guido van Rossum19b55f21997-12-09 14:11:39 +0000190 Py_DECREF(type);
191 Py_DECREF(value);
Jeremy Hyltone2e2c9f2001-09-26 19:58:38 +0000192 /* If the new exception doesn't set a traceback and the old
193 exception had a traceback, use the old traceback for the
194 new exception. It's better than nothing.
195 */
196 initial_tb = *tb;
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000197 PyErr_Fetch(exc, val, tb);
Jeremy Hyltone2e2c9f2001-09-26 19:58:38 +0000198 if (initial_tb != NULL) {
199 if (*tb == NULL)
200 *tb = initial_tb;
201 else
202 Py_DECREF(initial_tb);
203 }
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000204 /* normalize recursively */
205 PyErr_NormalizeException(exc, val, tb);
206}
207
208
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000209void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000210PyErr_Fetch(PyObject **p_type, PyObject **p_value, PyObject **p_traceback)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000211{
Nicholas Bastine5662ae2004-03-24 22:22:12 +0000212 PyThreadState *tstate = PyThreadState_GET();
Guido van Rossuma027efa1997-05-05 20:56:21 +0000213
214 *p_type = tstate->curexc_type;
215 *p_value = tstate->curexc_value;
216 *p_traceback = tstate->curexc_traceback;
217
218 tstate->curexc_type = NULL;
219 tstate->curexc_value = NULL;
220 tstate->curexc_traceback = NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000221}
222
223void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000224PyErr_Clear(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000225{
Guido van Rossuma027efa1997-05-05 20:56:21 +0000226 PyErr_Restore(NULL, NULL, NULL);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000227}
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000228
229/* Convenience functions to set a type error exception and return 0 */
230
231int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000232PyErr_BadArgument(void)
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000233{
Guido van Rossuma027efa1997-05-05 20:56:21 +0000234 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +0000235 "bad argument type for built-in operation");
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000236 return 0;
237}
238
Guido van Rossum373c8691997-04-29 18:22:47 +0000239PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000240PyErr_NoMemory(void)
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000241{
Vladimir Marangozov0888ff12000-08-18 18:01:06 +0000242 if (PyErr_ExceptionMatches(PyExc_MemoryError))
243 /* already current */
244 return NULL;
245
Barry Warsaw2d8adff1997-08-29 21:54:35 +0000246 /* raise the pre-allocated instance if it still exists */
247 if (PyExc_MemoryErrorInst)
248 PyErr_SetObject(PyExc_MemoryError, PyExc_MemoryErrorInst);
249 else
250 /* this will probably fail since there's no memory and hee,
251 hee, we have to instantiate this class
252 */
253 PyErr_SetNone(PyExc_MemoryError);
254
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000255 return NULL;
256}
257
Guido van Rossum373c8691997-04-29 18:22:47 +0000258PyObject *
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000259PyErr_SetFromErrnoWithFilenameObject(PyObject *exc, PyObject *filenameObject)
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000260{
Guido van Rossum373c8691997-04-29 18:22:47 +0000261 PyObject *v;
Guido van Rossume0e59821998-10-14 20:38:13 +0000262 char *s;
Guido van Rossum3a241811994-08-29 12:14:12 +0000263 int i = errno;
Martin v. Löwis3484a182002-03-09 12:07:51 +0000264#ifdef PLAN9
265 char errbuf[ERRMAX];
266#endif
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000267#ifdef MS_WINDOWS
Guido van Rossum795e1892000-02-17 15:19:15 +0000268 char *s_buf = NULL;
Mark Hammond3d61a062002-10-04 00:13:02 +0000269 char s_small_buf[28]; /* Room for "Windows Error 0xFFFFFFFF" */
Guido van Rossum795e1892000-02-17 15:19:15 +0000270#endif
Guido van Rossume9fbc091995-02-18 14:52:19 +0000271#ifdef EINTR
Guido van Rossum373c8691997-04-29 18:22:47 +0000272 if (i == EINTR && PyErr_CheckSignals())
Guido van Rossum5063bab1991-10-20 20:14:56 +0000273 return NULL;
Guido van Rossume9fbc091995-02-18 14:52:19 +0000274#endif
Martin v. Löwis3484a182002-03-09 12:07:51 +0000275#ifdef PLAN9
276 rerrstr(errbuf, sizeof errbuf);
277 s = errbuf;
278#else
Guido van Rossume0e59821998-10-14 20:38:13 +0000279 if (i == 0)
280 s = "Error"; /* Sometimes errno didn't get set */
Barry Warsaw97d95151998-07-23 16:05:56 +0000281 else
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000282#ifndef MS_WINDOWS
Guido van Rossume0e59821998-10-14 20:38:13 +0000283 s = strerror(i);
Guido van Rossum743007d1999-04-21 15:27:31 +0000284#else
285 {
Guido van Rossum795e1892000-02-17 15:19:15 +0000286 /* Note that the Win32 errors do not lineup with the
287 errno error. So if the error is in the MSVC error
Brett Cannonbf364092006-03-01 04:25:17 +0000288 table, we use it, otherwise we assume it really _is_
Guido van Rossum795e1892000-02-17 15:19:15 +0000289 a Win32 error code
290 */
Guido van Rossum584b16a2000-02-21 16:50:31 +0000291 if (i > 0 && i < _sys_nerr) {
Guido van Rossum795e1892000-02-17 15:19:15 +0000292 s = _sys_errlist[i];
293 }
294 else {
295 int len = FormatMessage(
296 FORMAT_MESSAGE_ALLOCATE_BUFFER |
297 FORMAT_MESSAGE_FROM_SYSTEM |
298 FORMAT_MESSAGE_IGNORE_INSERTS,
299 NULL, /* no message source */
300 i,
301 MAKELANGID(LANG_NEUTRAL,
302 SUBLANG_DEFAULT),
303 /* Default language */
304 (LPTSTR) &s_buf,
305 0, /* size not used */
306 NULL); /* no args */
Mark Hammond3d61a062002-10-04 00:13:02 +0000307 if (len==0) {
Brett Cannonbf364092006-03-01 04:25:17 +0000308 /* Only ever seen this in out-of-mem
Mark Hammond3d61a062002-10-04 00:13:02 +0000309 situations */
310 sprintf(s_small_buf, "Windows Error 0x%X", i);
311 s = s_small_buf;
312 s_buf = NULL;
313 } else {
314 s = s_buf;
315 /* remove trailing cr/lf and dots */
316 while (len > 0 && (s[len-1] <= ' ' || s[len-1] == '.'))
317 s[--len] = '\0';
318 }
Guido van Rossum795e1892000-02-17 15:19:15 +0000319 }
Guido van Rossum743007d1999-04-21 15:27:31 +0000320 }
Martin v. Löwis3484a182002-03-09 12:07:51 +0000321#endif /* Unix/Windows */
322#endif /* PLAN 9*/
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000323 if (filenameObject != NULL)
324 v = Py_BuildValue("(isO)", i, s, filenameObject);
Guido van Rossume0e59821998-10-14 20:38:13 +0000325 else
326 v = Py_BuildValue("(is)", i, s);
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000327 if (v != NULL) {
Guido van Rossum373c8691997-04-29 18:22:47 +0000328 PyErr_SetObject(exc, v);
329 Py_DECREF(v);
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000330 }
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000331#ifdef MS_WINDOWS
Guido van Rossum795e1892000-02-17 15:19:15 +0000332 LocalFree(s_buf);
Guido van Rossum743007d1999-04-21 15:27:31 +0000333#endif
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000334 return NULL;
335}
Guido van Rossum743007d1999-04-21 15:27:31 +0000336
Barry Warsaw97d95151998-07-23 16:05:56 +0000337
338PyObject *
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000339PyErr_SetFromErrnoWithFilename(PyObject *exc, char *filename)
340{
341 PyObject *name = filename ? PyString_FromString(filename) : NULL;
342 PyObject *result = PyErr_SetFromErrnoWithFilenameObject(exc, name);
Mark Hammondda7efaa2002-10-04 00:09:38 +0000343 Py_XDECREF(name);
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000344 return result;
345}
346
347#ifdef Py_WIN_WIDE_FILENAMES
348PyObject *
349PyErr_SetFromErrnoWithUnicodeFilename(PyObject *exc, Py_UNICODE *filename)
350{
Brett Cannonbf364092006-03-01 04:25:17 +0000351 PyObject *name = filename ?
352 PyUnicode_FromUnicode(filename, wcslen(filename)) :
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000353 NULL;
354 PyObject *result = PyErr_SetFromErrnoWithFilenameObject(exc, name);
355 Py_XDECREF(name);
356 return result;
357}
358#endif /* Py_WIN_WIDE_FILENAMES */
359
360PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000361PyErr_SetFromErrno(PyObject *exc)
Barry Warsaw97d95151998-07-23 16:05:56 +0000362{
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000363 return PyErr_SetFromErrnoWithFilenameObject(exc, NULL);
Barry Warsaw97d95151998-07-23 16:05:56 +0000364}
Guido van Rossum683a0721990-10-21 22:09:12 +0000365
Brett Cannonbf364092006-03-01 04:25:17 +0000366#ifdef MS_WINDOWS
Guido van Rossum795e1892000-02-17 15:19:15 +0000367/* Windows specific error code handling */
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000368PyObject *PyErr_SetExcFromWindowsErrWithFilenameObject(
Thomas Heller085358a2002-07-29 14:27:41 +0000369 PyObject *exc,
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000370 int ierr,
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000371 PyObject *filenameObject)
Guido van Rossum795e1892000-02-17 15:19:15 +0000372{
373 int len;
374 char *s;
Mark Hammond3d61a062002-10-04 00:13:02 +0000375 char *s_buf = NULL; /* Free via LocalFree */
376 char s_small_buf[28]; /* Room for "Windows Error 0xFFFFFFFF" */
Guido van Rossum795e1892000-02-17 15:19:15 +0000377 PyObject *v;
378 DWORD err = (DWORD)ierr;
379 if (err==0) err = GetLastError();
380 len = FormatMessage(
381 /* Error API error */
382 FORMAT_MESSAGE_ALLOCATE_BUFFER |
383 FORMAT_MESSAGE_FROM_SYSTEM |
384 FORMAT_MESSAGE_IGNORE_INSERTS,
385 NULL, /* no message source */
386 err,
387 MAKELANGID(LANG_NEUTRAL,
388 SUBLANG_DEFAULT), /* Default language */
Mark Hammond3d61a062002-10-04 00:13:02 +0000389 (LPTSTR) &s_buf,
Guido van Rossum795e1892000-02-17 15:19:15 +0000390 0, /* size not used */
391 NULL); /* no args */
Mark Hammond3d61a062002-10-04 00:13:02 +0000392 if (len==0) {
393 /* Only seen this in out of mem situations */
394 sprintf(s_small_buf, "Windows Error 0x%X", err);
395 s = s_small_buf;
396 s_buf = NULL;
397 } else {
398 s = s_buf;
399 /* remove trailing cr/lf and dots */
400 while (len > 0 && (s[len-1] <= ' ' || s[len-1] == '.'))
401 s[--len] = '\0';
402 }
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000403 if (filenameObject != NULL)
404 v = Py_BuildValue("(isO)", err, s, filenameObject);
Guido van Rossum795e1892000-02-17 15:19:15 +0000405 else
406 v = Py_BuildValue("(is)", err, s);
407 if (v != NULL) {
Thomas Heller085358a2002-07-29 14:27:41 +0000408 PyErr_SetObject(exc, v);
Guido van Rossum795e1892000-02-17 15:19:15 +0000409 Py_DECREF(v);
410 }
Mark Hammond3d61a062002-10-04 00:13:02 +0000411 LocalFree(s_buf);
Guido van Rossum795e1892000-02-17 15:19:15 +0000412 return NULL;
413}
414
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000415PyObject *PyErr_SetExcFromWindowsErrWithFilename(
416 PyObject *exc,
417 int ierr,
418 const char *filename)
419{
420 PyObject *name = filename ? PyString_FromString(filename) : NULL;
Brett Cannonbf364092006-03-01 04:25:17 +0000421 PyObject *ret = PyErr_SetExcFromWindowsErrWithFilenameObject(exc,
422 ierr,
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000423 name);
424 Py_XDECREF(name);
425 return ret;
426}
427
428#ifdef Py_WIN_WIDE_FILENAMES
429PyObject *PyErr_SetExcFromWindowsErrWithUnicodeFilename(
430 PyObject *exc,
431 int ierr,
432 const Py_UNICODE *filename)
433{
Brett Cannonbf364092006-03-01 04:25:17 +0000434 PyObject *name = filename ?
435 PyUnicode_FromUnicode(filename, wcslen(filename)) :
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000436 NULL;
Brett Cannonbf364092006-03-01 04:25:17 +0000437 PyObject *ret = PyErr_SetExcFromWindowsErrWithFilenameObject(exc,
438 ierr,
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000439 name);
440 Py_XDECREF(name);
441 return ret;
442}
443#endif /* Py_WIN_WIDE_FILENAMES */
444
Thomas Heller085358a2002-07-29 14:27:41 +0000445PyObject *PyErr_SetExcFromWindowsErr(PyObject *exc, int ierr)
446{
447 return PyErr_SetExcFromWindowsErrWithFilename(exc, ierr, NULL);
448}
449
Guido van Rossum795e1892000-02-17 15:19:15 +0000450PyObject *PyErr_SetFromWindowsErr(int ierr)
451{
Thomas Heller085358a2002-07-29 14:27:41 +0000452 return PyErr_SetExcFromWindowsErrWithFilename(PyExc_WindowsError,
453 ierr, NULL);
454}
455PyObject *PyErr_SetFromWindowsErrWithFilename(
456 int ierr,
457 const char *filename)
458{
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000459 PyObject *name = filename ? PyString_FromString(filename) : NULL;
460 PyObject *result = PyErr_SetExcFromWindowsErrWithFilenameObject(
461 PyExc_WindowsError,
462 ierr, name);
Mark Hammondda7efaa2002-10-04 00:09:38 +0000463 Py_XDECREF(name);
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000464 return result;
Guido van Rossum795e1892000-02-17 15:19:15 +0000465}
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000466
467#ifdef Py_WIN_WIDE_FILENAMES
468PyObject *PyErr_SetFromWindowsErrWithUnicodeFilename(
469 int ierr,
470 const Py_UNICODE *filename)
471{
Brett Cannonbf364092006-03-01 04:25:17 +0000472 PyObject *name = filename ?
473 PyUnicode_FromUnicode(filename, wcslen(filename)) :
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000474 NULL;
475 PyObject *result = PyErr_SetExcFromWindowsErrWithFilenameObject(
476 PyExc_WindowsError,
477 ierr, name);
Mark Hammondda7efaa2002-10-04 00:09:38 +0000478 Py_XDECREF(name);
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000479 return result;
480}
481#endif /* Py_WIN_WIDE_FILENAMES */
Guido van Rossum795e1892000-02-17 15:19:15 +0000482#endif /* MS_WINDOWS */
483
Guido van Rossum683a0721990-10-21 22:09:12 +0000484void
Fred Drake6d63adf2000-08-24 22:38:39 +0000485_PyErr_BadInternalCall(char *filename, int lineno)
486{
487 PyErr_Format(PyExc_SystemError,
488 "%s:%d: bad argument to internal function",
489 filename, lineno);
490}
491
492/* Remove the preprocessor macro for PyErr_BadInternalCall() so that we can
493 export the entry point for existing object code: */
494#undef PyErr_BadInternalCall
495void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000496PyErr_BadInternalCall(void)
Guido van Rossum683a0721990-10-21 22:09:12 +0000497{
Fred Drake6d63adf2000-08-24 22:38:39 +0000498 PyErr_Format(PyExc_SystemError,
499 "bad argument to internal function");
Guido van Rossum683a0721990-10-21 22:09:12 +0000500}
Fred Drake6d63adf2000-08-24 22:38:39 +0000501#define PyErr_BadInternalCall() _PyErr_BadInternalCall(__FILE__, __LINE__)
502
Guido van Rossum1548bac1997-02-14 17:09:47 +0000503
504
Guido van Rossum1548bac1997-02-14 17:09:47 +0000505PyObject *
506PyErr_Format(PyObject *exception, const char *format, ...)
Guido van Rossum1548bac1997-02-14 17:09:47 +0000507{
508 va_list vargs;
Jeremy Hyltonb69a27e2000-09-01 03:49:47 +0000509 PyObject* string;
Guido van Rossum1548bac1997-02-14 17:09:47 +0000510
Jeremy Hyltonb69a27e2000-09-01 03:49:47 +0000511#ifdef HAVE_STDARG_PROTOTYPES
Guido van Rossum1548bac1997-02-14 17:09:47 +0000512 va_start(vargs, format);
Jeremy Hyltonb69a27e2000-09-01 03:49:47 +0000513#else
514 va_start(vargs);
515#endif
Guido van Rossum1548bac1997-02-14 17:09:47 +0000516
Barry Warsaw876c8cb2001-08-24 18:35:23 +0000517 string = PyString_FromFormatV(format, vargs);
Jeremy Hyltonb69a27e2000-09-01 03:49:47 +0000518 PyErr_SetObject(exception, string);
519 Py_XDECREF(string);
Tim Petersc15c4f12001-10-02 21:32:07 +0000520 va_end(vargs);
Guido van Rossum1548bac1997-02-14 17:09:47 +0000521 return NULL;
522}
Guido van Rossum7617e051997-09-16 18:43:50 +0000523
524
525PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000526PyErr_NewException(char *name, PyObject *base, PyObject *dict)
Guido van Rossum7617e051997-09-16 18:43:50 +0000527{
Guido van Rossum2ac650f1997-10-03 19:50:55 +0000528 char *dot;
529 PyObject *modulename = NULL;
530 PyObject *classname = NULL;
531 PyObject *mydict = NULL;
532 PyObject *bases = NULL;
Guido van Rossum7617e051997-09-16 18:43:50 +0000533 PyObject *result = NULL;
Guido van Rossum2ac650f1997-10-03 19:50:55 +0000534 dot = strrchr(name, '.');
535 if (dot == NULL) {
536 PyErr_SetString(PyExc_SystemError,
537 "PyErr_NewException: name must be module.class");
Guido van Rossum7617e051997-09-16 18:43:50 +0000538 return NULL;
Guido van Rossum7617e051997-09-16 18:43:50 +0000539 }
540 if (base == NULL)
541 base = PyExc_Exception;
Guido van Rossum2ac650f1997-10-03 19:50:55 +0000542 if (dict == NULL) {
543 dict = mydict = PyDict_New();
544 if (dict == NULL)
545 goto failure;
546 }
547 if (PyDict_GetItemString(dict, "__module__") == NULL) {
548 modulename = PyString_FromStringAndSize(name, (int)(dot-name));
549 if (modulename == NULL)
550 goto failure;
551 if (PyDict_SetItemString(dict, "__module__", modulename) != 0)
552 goto failure;
553 }
554 classname = PyString_FromString(dot+1);
555 if (classname == NULL)
Guido van Rossum7617e051997-09-16 18:43:50 +0000556 goto failure;
Raymond Hettinger8ae46892003-10-12 19:09:37 +0000557 bases = PyTuple_Pack(1, base);
Guido van Rossum2ac650f1997-10-03 19:50:55 +0000558 if (bases == NULL)
559 goto failure;
560 result = PyClass_New(bases, dict, classname);
Guido van Rossum7617e051997-09-16 18:43:50 +0000561 failure:
Guido van Rossum2ac650f1997-10-03 19:50:55 +0000562 Py_XDECREF(bases);
563 Py_XDECREF(mydict);
564 Py_XDECREF(classname);
565 Py_XDECREF(modulename);
Guido van Rossum7617e051997-09-16 18:43:50 +0000566 return result;
567}
Jeremy Hyltonb709df32000-09-01 02:47:25 +0000568
569/* Call when an exception has occurred but there is no way for Python
570 to handle it. Examples: exception in __del__ or during GC. */
571void
572PyErr_WriteUnraisable(PyObject *obj)
573{
574 PyObject *f, *t, *v, *tb;
575 PyErr_Fetch(&t, &v, &tb);
576 f = PySys_GetObject("stderr");
577 if (f != NULL) {
578 PyFile_WriteString("Exception ", f);
579 if (t) {
Brett Cannonbf364092006-03-01 04:25:17 +0000580 char* className = PyExceptionClass_Name(t);
581 PyObject* moduleName =
582 PyObject_GetAttrString(t, "__module__");
583
584 if (moduleName == NULL)
585 PyFile_WriteString("<unknown>", f);
586 else {
587 char* modstr = PyString_AsString(moduleName);
588 if (modstr)
589 {
590 PyFile_WriteString(modstr, f);
591 PyFile_WriteString(".", f);
592 }
593 }
594 if (className == NULL)
595 PyFile_WriteString("<unknown>", f);
596 else
597 PyFile_WriteString(className, f);
Jeremy Hyltonb709df32000-09-01 02:47:25 +0000598 if (v && v != Py_None) {
599 PyFile_WriteString(": ", f);
600 PyFile_WriteObject(v, f, 0);
601 }
602 }
603 PyFile_WriteString(" in ", f);
604 PyFile_WriteObject(obj, f, 0);
605 PyFile_WriteString(" ignored\n", f);
606 PyErr_Clear(); /* Just in case */
607 }
608 Py_XDECREF(t);
609 Py_XDECREF(v);
610 Py_XDECREF(tb);
611}
Guido van Rossumcfd42b52000-12-15 21:58:52 +0000612
Armin Rigo092381a2003-10-25 14:29:27 +0000613extern PyObject *PyModule_GetWarningsModule(void);
Guido van Rossumcfd42b52000-12-15 21:58:52 +0000614
615/* Function to issue a warning message; may raise an exception. */
616int
617PyErr_Warn(PyObject *category, char *message)
618{
Mark Hammonda43fd0c2003-02-19 00:33:33 +0000619 PyObject *dict, *func = NULL;
Mark Hammondedd07732003-07-15 23:03:55 +0000620 PyObject *warnings_module = PyModule_GetWarningsModule();
Guido van Rossumcfd42b52000-12-15 21:58:52 +0000621
Mark Hammondedd07732003-07-15 23:03:55 +0000622 if (warnings_module != NULL) {
623 dict = PyModule_GetDict(warnings_module);
Guido van Rossumcfd42b52000-12-15 21:58:52 +0000624 func = PyDict_GetItemString(dict, "warn");
Guido van Rossumcfd42b52000-12-15 21:58:52 +0000625 }
626 if (func == NULL) {
627 PySys_WriteStderr("warning: %s\n", message);
628 return 0;
629 }
630 else {
631 PyObject *args, *res;
632
633 if (category == NULL)
634 category = PyExc_RuntimeWarning;
635 args = Py_BuildValue("(sO)", message, category);
636 if (args == NULL)
637 return -1;
638 res = PyEval_CallObject(func, args);
639 Py_DECREF(args);
640 if (res == NULL)
641 return -1;
642 Py_DECREF(res);
643 return 0;
644 }
645}
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000646
Guido van Rossum2fd45652001-02-28 21:46:24 +0000647
648/* Warning with explicit origin */
649int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000650PyErr_WarnExplicit(PyObject *category, const char *message,
651 const char *filename, int lineno,
652 const char *module, PyObject *registry)
Guido van Rossum2fd45652001-02-28 21:46:24 +0000653{
654 PyObject *mod, *dict, *func = NULL;
655
656 mod = PyImport_ImportModule("warnings");
657 if (mod != NULL) {
658 dict = PyModule_GetDict(mod);
659 func = PyDict_GetItemString(dict, "warn_explicit");
660 Py_DECREF(mod);
661 }
662 if (func == NULL) {
663 PySys_WriteStderr("warning: %s\n", message);
664 return 0;
665 }
666 else {
667 PyObject *args, *res;
668
669 if (category == NULL)
670 category = PyExc_RuntimeWarning;
671 if (registry == NULL)
672 registry = Py_None;
673 args = Py_BuildValue("(sOsizO)", message, category,
674 filename, lineno, module, registry);
675 if (args == NULL)
676 return -1;
677 res = PyEval_CallObject(func, args);
678 Py_DECREF(args);
679 if (res == NULL)
680 return -1;
681 Py_DECREF(res);
682 return 0;
683 }
684}
685
686
Martin v. Löwiscfeb3b62002-03-03 21:30:27 +0000687/* Set file and line information for the current exception.
688 If the exception is not a SyntaxError, also sets additional attributes
689 to make printing of exceptions believe it is a syntax error. */
Guido van Rossum2fd45652001-02-28 21:46:24 +0000690
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000691void
Martin v. Löwis95292d62002-12-11 14:04:59 +0000692PyErr_SyntaxLocation(const char *filename, int lineno)
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000693{
694 PyObject *exc, *v, *tb, *tmp;
695
696 /* add attributes for the line number and filename for the error */
697 PyErr_Fetch(&exc, &v, &tb);
698 PyErr_NormalizeException(&exc, &v, &tb);
699 /* XXX check that it is, indeed, a syntax error */
700 tmp = PyInt_FromLong(lineno);
701 if (tmp == NULL)
702 PyErr_Clear();
703 else {
704 if (PyObject_SetAttrString(v, "lineno", tmp))
705 PyErr_Clear();
706 Py_DECREF(tmp);
707 }
708 if (filename != NULL) {
709 tmp = PyString_FromString(filename);
710 if (tmp == NULL)
711 PyErr_Clear();
712 else {
713 if (PyObject_SetAttrString(v, "filename", tmp))
714 PyErr_Clear();
715 Py_DECREF(tmp);
716 }
717
718 tmp = PyErr_ProgramText(filename, lineno);
719 if (tmp) {
720 PyObject_SetAttrString(v, "text", tmp);
721 Py_DECREF(tmp);
722 }
723 }
Martin v. Löwiscfeb3b62002-03-03 21:30:27 +0000724 if (PyObject_SetAttrString(v, "offset", Py_None)) {
725 PyErr_Clear();
726 }
727 if (exc != PyExc_SyntaxError) {
728 if (!PyObject_HasAttrString(v, "msg")) {
729 tmp = PyObject_Str(v);
730 if (tmp) {
731 if (PyObject_SetAttrString(v, "msg", tmp))
732 PyErr_Clear();
733 Py_DECREF(tmp);
734 } else {
735 PyErr_Clear();
736 }
737 }
738 if (!PyObject_HasAttrString(v, "print_file_and_line")) {
739 if (PyObject_SetAttrString(v, "print_file_and_line",
740 Py_None))
741 PyErr_Clear();
742 }
743 }
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000744 PyErr_Restore(exc, v, tb);
745}
746
747/* com_fetch_program_text will attempt to load the line of text that
748 the exception refers to. If it fails, it will return NULL but will
Brett Cannonbf364092006-03-01 04:25:17 +0000749 not set an exception.
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000750
751 XXX The functionality of this function is quite similar to the
752 functionality in tb_displayline() in traceback.c.
753*/
754
755PyObject *
Martin v. Löwis95292d62002-12-11 14:04:59 +0000756PyErr_ProgramText(const char *filename, int lineno)
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000757{
758 FILE *fp;
759 int i;
760 char linebuf[1000];
761
Tim Petersa7444f42006-02-27 23:29:46 +0000762 if (filename == NULL || *filename == '\0' || lineno <= 0)
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000763 return NULL;
Jack Jansen7b8c7542002-04-14 20:12:41 +0000764 fp = fopen(filename, "r" PY_STDIOTEXTMODE);
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000765 if (fp == NULL)
766 return NULL;
767 for (i = 0; i < lineno; i++) {
768 char *pLastChar = &linebuf[sizeof(linebuf) - 2];
769 do {
770 *pLastChar = '\0';
Jack Jansen7b8c7542002-04-14 20:12:41 +0000771 if (Py_UniversalNewlineFgets(linebuf, sizeof linebuf, fp, NULL) == NULL)
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000772 break;
773 /* fgets read *something*; if it didn't get as
774 far as pLastChar, it must have found a newline
775 or hit the end of the file; if pLastChar is \n,
776 it obviously found a newline; else we haven't
777 yet seen a newline, so must continue */
778 } while (*pLastChar != '\0' && *pLastChar != '\n');
779 }
780 fclose(fp);
781 if (i == lineno) {
782 char *p = linebuf;
783 while (*p == ' ' || *p == '\t' || *p == '\014')
784 p++;
785 return PyString_FromString(p);
786 }
787 return NULL;
788}