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