blob: 64ba05dd6cd72190ff4f14914f5287ec8d17ebe8 [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{
Antoine Pitrouc83ea132010-05-09 14:46:46 +000027 PyThreadState *tstate = PyThreadState_GET();
28 PyObject *oldtype, *oldvalue, *oldtraceback;
Guido van Rossum1ae940a1995-01-02 19:04:15 +000029
Antoine Pitrouc83ea132010-05-09 14:46:46 +000030 if (traceback != NULL && !PyTraceBack_Check(traceback)) {
31 /* XXX Should never happen -- fatal error instead? */
32 /* Well, it could be None. */
33 Py_DECREF(traceback);
34 traceback = NULL;
35 }
Guido van Rossuma027efa1997-05-05 20:56:21 +000036
Antoine Pitrouc83ea132010-05-09 14:46:46 +000037 /* 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;
Guido van Rossuma027efa1997-05-05 20:56:21 +000042
Antoine Pitrouc83ea132010-05-09 14:46:46 +000043 tstate->curexc_type = type;
44 tstate->curexc_value = value;
45 tstate->curexc_traceback = traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +000046
Antoine Pitrouc83ea132010-05-09 14:46:46 +000047 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{
Antoine Pitrouc83ea132010-05-09 14:46:46 +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{
Antoine Pitrouc83ea132010-05-09 14:46:46 +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{
Antoine Pitrouc83ea132010-05-09 14:46:46 +000069 PyObject *value = PyString_FromString(string);
70 PyErr_SetObject(exception, value);
71 Py_XDECREF(value);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000072}
73
Guido van Rossum3a241811994-08-29 12:14:12 +000074
Guido van Rossum373c8691997-04-29 18:22:47 +000075PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000076PyErr_Occurred(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000077{
Antoine Pitrouc83ea132010-05-09 14:46:46 +000078 PyThreadState *tstate = PyThreadState_GET();
Guido van Rossuma027efa1997-05-05 20:56:21 +000079
Antoine Pitrouc83ea132010-05-09 14:46:46 +000080 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{
Antoine Pitrouc83ea132010-05-09 14:46:46 +000087 if (err == NULL || exc == NULL) {
88 /* maybe caused by "import exceptions" that failed early on */
89 return 0;
90 }
91 if (PyTuple_Check(exc)) {
92 Py_ssize_t i, n;
93 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. */
105 if (PyExceptionInstance_Check(err))
106 err = PyExceptionInstance_Class(err);
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000107
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000108 if (PyExceptionClass_Check(err) && PyExceptionClass_Check(exc)) {
109 int res = 0, reclimit;
110 PyObject *exception, *value, *tb;
111 PyErr_Fetch(&exception, &value, &tb);
112 /* Temporarily bump the recursion limit, so that in the most
113 common case PyObject_IsSubclass will not raise a recursion
Amaury Forgeot d'Arc4bf21e22011-12-07 21:46:48 +0100114 error we have to ignore anyway. Don't do it when the limit
115 is already insanely high, to avoid overflow */
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000116 reclimit = Py_GetRecursionLimit();
Amaury Forgeot d'Arc4bf21e22011-12-07 21:46:48 +0100117 if (reclimit < (1 << 30))
118 Py_SetRecursionLimit(reclimit + 5);
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000119 res = PyObject_IsSubclass(err, exc);
120 Py_SetRecursionLimit(reclimit);
121 /* This function must not fail, so print the error here */
122 if (res == -1) {
123 PyErr_WriteUnraisable(err);
124 res = 0;
125 }
126 PyErr_Restore(exception, value, tb);
127 return res;
128 }
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000129
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000130 return err == exc;
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000131}
Guido van Rossum743007d1999-04-21 15:27:31 +0000132
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000133
134int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000135PyErr_ExceptionMatches(PyObject *exc)
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000136{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000137 return PyErr_GivenExceptionMatches(PyErr_Occurred(), exc);
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000138}
139
140
141/* Used in many places to normalize a raised exception, including in
142 eval_code2(), do_raise(), and PyErr_Print()
143*/
144void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000145PyErr_NormalizeException(PyObject **exc, PyObject **val, PyObject **tb)
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000146{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000147 PyObject *type = *exc;
148 PyObject *value = *val;
149 PyObject *inclass = NULL;
150 PyObject *initial_tb = NULL;
151 PyThreadState *tstate = NULL;
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000152
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000153 if (type == NULL) {
154 /* There was no exception, so nothing to do. */
155 return;
156 }
Guido van Rossumed473a42000-08-07 19:18:27 +0000157
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000158 /* If PyErr_SetNone() was used, the value will have been actually
159 set to NULL.
160 */
161 if (!value) {
162 value = Py_None;
163 Py_INCREF(value);
164 }
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000165
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000166 if (PyExceptionInstance_Check(value))
167 inclass = PyExceptionInstance_Class(value);
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000168
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000169 /* Normalize the exception so that if the type is a class, the
170 value will be an instance.
171 */
172 if (PyExceptionClass_Check(type)) {
173 /* if the value was not an instance, or is not an instance
174 whose class is (or is derived from) type, then use the
175 value as an argument to instantiation of the type
176 class.
177 */
178 if (!inclass || !PyObject_IsSubclass(inclass, type)) {
179 PyObject *args, *res;
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000180
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000181 if (value == Py_None)
182 args = PyTuple_New(0);
183 else if (PyTuple_Check(value)) {
184 Py_INCREF(value);
185 args = value;
186 }
187 else
188 args = PyTuple_Pack(1, value);
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000189
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000190 if (args == NULL)
191 goto finally;
192 res = PyEval_CallObject(type, args);
193 Py_DECREF(args);
194 if (res == NULL)
195 goto finally;
196 Py_DECREF(value);
197 value = res;
198 }
199 /* if the class of the instance doesn't exactly match the
200 class of the type, believe the instance
201 */
202 else if (inclass != type) {
203 Py_DECREF(type);
204 type = inclass;
205 Py_INCREF(type);
206 }
207 }
208 *exc = type;
209 *val = value;
210 return;
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000211finally:
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000212 Py_DECREF(type);
213 Py_DECREF(value);
214 /* If the new exception doesn't set a traceback and the old
215 exception had a traceback, use the old traceback for the
216 new exception. It's better than nothing.
217 */
218 initial_tb = *tb;
219 PyErr_Fetch(exc, val, tb);
220 if (initial_tb != NULL) {
221 if (*tb == NULL)
222 *tb = initial_tb;
223 else
224 Py_DECREF(initial_tb);
225 }
226 /* normalize recursively */
227 tstate = PyThreadState_GET();
228 if (++tstate->recursion_depth > Py_GetRecursionLimit()) {
229 --tstate->recursion_depth;
230 /* throw away the old exception... */
231 Py_DECREF(*exc);
232 Py_DECREF(*val);
233 /* ... and use the recursion error instead */
234 *exc = PyExc_RuntimeError;
235 *val = PyExc_RecursionErrorInst;
236 Py_INCREF(*exc);
237 Py_INCREF(*val);
238 /* just keeping the old traceback */
239 return;
240 }
241 PyErr_NormalizeException(exc, val, tb);
242 --tstate->recursion_depth;
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000243}
244
245
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000246void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000247PyErr_Fetch(PyObject **p_type, PyObject **p_value, PyObject **p_traceback)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000248{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000249 PyThreadState *tstate = PyThreadState_GET();
Guido van Rossuma027efa1997-05-05 20:56:21 +0000250
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000251 *p_type = tstate->curexc_type;
252 *p_value = tstate->curexc_value;
253 *p_traceback = tstate->curexc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000254
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000255 tstate->curexc_type = NULL;
256 tstate->curexc_value = NULL;
257 tstate->curexc_traceback = NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000258}
259
260void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000261PyErr_Clear(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000262{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000263 PyErr_Restore(NULL, NULL, NULL);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000264}
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000265
266/* Convenience functions to set a type error exception and return 0 */
267
268int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000269PyErr_BadArgument(void)
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000270{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000271 PyErr_SetString(PyExc_TypeError,
272 "bad argument type for built-in operation");
273 return 0;
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000274}
275
Guido van Rossum373c8691997-04-29 18:22:47 +0000276PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000277PyErr_NoMemory(void)
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000278{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000279 if (PyErr_ExceptionMatches(PyExc_MemoryError))
280 /* already current */
281 return NULL;
Vladimir Marangozov0888ff12000-08-18 18:01:06 +0000282
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000283 /* raise the pre-allocated instance if it still exists */
284 if (PyExc_MemoryErrorInst)
285 PyErr_SetObject(PyExc_MemoryError, PyExc_MemoryErrorInst);
286 else
287 /* this will probably fail since there's no memory and hee,
288 hee, we have to instantiate this class
289 */
290 PyErr_SetNone(PyExc_MemoryError);
Barry Warsaw2d8adff1997-08-29 21:54:35 +0000291
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000292 return NULL;
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000293}
294
Guido van Rossum373c8691997-04-29 18:22:47 +0000295PyObject *
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000296PyErr_SetFromErrnoWithFilenameObject(PyObject *exc, PyObject *filenameObject)
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000297{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000298 PyObject *v;
299 char *s;
300 int i = errno;
Martin v. Löwis3484a182002-03-09 12:07:51 +0000301#ifdef PLAN9
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000302 char errbuf[ERRMAX];
Martin v. Löwis3484a182002-03-09 12:07:51 +0000303#endif
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000304#ifdef MS_WINDOWS
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000305 char *s_buf = NULL;
306 char s_small_buf[28]; /* Room for "Windows Error 0xFFFFFFFF" */
Guido van Rossum795e1892000-02-17 15:19:15 +0000307#endif
Guido van Rossume9fbc091995-02-18 14:52:19 +0000308#ifdef EINTR
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000309 if (i == EINTR && PyErr_CheckSignals())
310 return NULL;
Guido van Rossume9fbc091995-02-18 14:52:19 +0000311#endif
Martin v. Löwis3484a182002-03-09 12:07:51 +0000312#ifdef PLAN9
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000313 rerrstr(errbuf, sizeof errbuf);
314 s = errbuf;
Martin v. Löwis3484a182002-03-09 12:07:51 +0000315#else
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000316 if (i == 0)
317 s = "Error"; /* Sometimes errno didn't get set */
318 else
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000319#ifndef MS_WINDOWS
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000320 s = strerror(i);
Guido van Rossum743007d1999-04-21 15:27:31 +0000321#else
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000322 {
323 /* Note that the Win32 errors do not lineup with the
324 errno error. So if the error is in the MSVC error
325 table, we use it, otherwise we assume it really _is_
326 a Win32 error code
327 */
328 if (i > 0 && i < _sys_nerr) {
329 s = _sys_errlist[i];
330 }
331 else {
332 int len = FormatMessage(
333 FORMAT_MESSAGE_ALLOCATE_BUFFER |
334 FORMAT_MESSAGE_FROM_SYSTEM |
335 FORMAT_MESSAGE_IGNORE_INSERTS,
336 NULL, /* no message source */
337 i,
338 MAKELANGID(LANG_NEUTRAL,
339 SUBLANG_DEFAULT),
340 /* Default language */
341 (LPTSTR) &s_buf,
342 0, /* size not used */
343 NULL); /* no args */
344 if (len==0) {
345 /* Only ever seen this in out-of-mem
346 situations */
347 sprintf(s_small_buf, "Windows Error 0x%X", i);
348 s = s_small_buf;
349 s_buf = NULL;
350 } else {
351 s = s_buf;
352 /* remove trailing cr/lf and dots */
353 while (len > 0 && (s[len-1] <= ' ' || s[len-1] == '.'))
354 s[--len] = '\0';
355 }
356 }
357 }
Martin v. Löwis3484a182002-03-09 12:07:51 +0000358#endif /* Unix/Windows */
359#endif /* PLAN 9*/
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000360 if (filenameObject != NULL)
361 v = Py_BuildValue("(isO)", i, s, filenameObject);
362 else
363 v = Py_BuildValue("(is)", i, s);
364 if (v != NULL) {
365 PyErr_SetObject(exc, v);
366 Py_DECREF(v);
367 }
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000368#ifdef MS_WINDOWS
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000369 LocalFree(s_buf);
Guido van Rossum743007d1999-04-21 15:27:31 +0000370#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000371 return NULL;
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000372}
Guido van Rossum743007d1999-04-21 15:27:31 +0000373
Barry Warsaw97d95151998-07-23 16:05:56 +0000374
375PyObject *
Alexandre Vassalottia6e34742009-06-12 20:57:12 +0000376PyErr_SetFromErrnoWithFilename(PyObject *exc, const char *filename)
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000377{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000378 PyObject *name = filename ? PyString_FromString(filename) : NULL;
379 PyObject *result = PyErr_SetFromErrnoWithFilenameObject(exc, name);
380 Py_XDECREF(name);
381 return result;
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000382}
383
Hirokazu Yamamotob24bb272009-05-17 02:52:09 +0000384#ifdef MS_WINDOWS
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000385PyObject *
Alexandre Vassalottia6e34742009-06-12 20:57:12 +0000386PyErr_SetFromErrnoWithUnicodeFilename(PyObject *exc, const Py_UNICODE *filename)
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000387{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000388 PyObject *name = filename ?
389 PyUnicode_FromUnicode(filename, wcslen(filename)) :
390 NULL;
391 PyObject *result = PyErr_SetFromErrnoWithFilenameObject(exc, name);
392 Py_XDECREF(name);
393 return result;
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000394}
Hirokazu Yamamotob24bb272009-05-17 02:52:09 +0000395#endif /* MS_WINDOWS */
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000396
397PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000398PyErr_SetFromErrno(PyObject *exc)
Barry Warsaw97d95151998-07-23 16:05:56 +0000399{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000400 return PyErr_SetFromErrnoWithFilenameObject(exc, NULL);
Barry Warsaw97d95151998-07-23 16:05:56 +0000401}
Guido van Rossum683a0721990-10-21 22:09:12 +0000402
Brett Cannonbf364092006-03-01 04:25:17 +0000403#ifdef MS_WINDOWS
Guido van Rossum795e1892000-02-17 15:19:15 +0000404/* Windows specific error code handling */
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000405PyObject *PyErr_SetExcFromWindowsErrWithFilenameObject(
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000406 PyObject *exc,
407 int ierr,
408 PyObject *filenameObject)
Guido van Rossum795e1892000-02-17 15:19:15 +0000409{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000410 int len;
411 char *s;
412 char *s_buf = NULL; /* Free via LocalFree */
413 char s_small_buf[28]; /* Room for "Windows Error 0xFFFFFFFF" */
414 PyObject *v;
415 DWORD err = (DWORD)ierr;
416 if (err==0) err = GetLastError();
417 len = FormatMessage(
418 /* Error API error */
419 FORMAT_MESSAGE_ALLOCATE_BUFFER |
420 FORMAT_MESSAGE_FROM_SYSTEM |
421 FORMAT_MESSAGE_IGNORE_INSERTS,
422 NULL, /* no message source */
423 err,
424 MAKELANGID(LANG_NEUTRAL,
425 SUBLANG_DEFAULT), /* Default language */
426 (LPTSTR) &s_buf,
427 0, /* size not used */
428 NULL); /* no args */
429 if (len==0) {
430 /* Only seen this in out of mem situations */
431 sprintf(s_small_buf, "Windows Error 0x%X", err);
432 s = s_small_buf;
433 s_buf = NULL;
434 } else {
435 s = s_buf;
436 /* remove trailing cr/lf and dots */
437 while (len > 0 && (s[len-1] <= ' ' || s[len-1] == '.'))
438 s[--len] = '\0';
439 }
440 if (filenameObject != NULL)
441 v = Py_BuildValue("(isO)", err, s, filenameObject);
442 else
443 v = Py_BuildValue("(is)", err, s);
444 if (v != NULL) {
445 PyErr_SetObject(exc, v);
446 Py_DECREF(v);
447 }
448 LocalFree(s_buf);
449 return NULL;
Guido van Rossum795e1892000-02-17 15:19:15 +0000450}
451
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000452PyObject *PyErr_SetExcFromWindowsErrWithFilename(
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000453 PyObject *exc,
454 int ierr,
455 const char *filename)
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000456{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000457 PyObject *name = filename ? PyString_FromString(filename) : NULL;
458 PyObject *ret = PyErr_SetExcFromWindowsErrWithFilenameObject(exc,
459 ierr,
460 name);
461 Py_XDECREF(name);
462 return ret;
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000463}
464
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000465PyObject *PyErr_SetExcFromWindowsErrWithUnicodeFilename(
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000466 PyObject *exc,
467 int ierr,
468 const Py_UNICODE *filename)
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000469{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000470 PyObject *name = filename ?
471 PyUnicode_FromUnicode(filename, wcslen(filename)) :
472 NULL;
473 PyObject *ret = PyErr_SetExcFromWindowsErrWithFilenameObject(exc,
474 ierr,
475 name);
476 Py_XDECREF(name);
477 return ret;
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000478}
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000479
Thomas Heller085358a2002-07-29 14:27:41 +0000480PyObject *PyErr_SetExcFromWindowsErr(PyObject *exc, int ierr)
481{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000482 return PyErr_SetExcFromWindowsErrWithFilename(exc, ierr, NULL);
Thomas Heller085358a2002-07-29 14:27:41 +0000483}
484
Guido van Rossum795e1892000-02-17 15:19:15 +0000485PyObject *PyErr_SetFromWindowsErr(int ierr)
486{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000487 return PyErr_SetExcFromWindowsErrWithFilename(PyExc_WindowsError,
488 ierr, NULL);
Thomas Heller085358a2002-07-29 14:27:41 +0000489}
490PyObject *PyErr_SetFromWindowsErrWithFilename(
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000491 int ierr,
492 const char *filename)
Thomas Heller085358a2002-07-29 14:27:41 +0000493{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000494 PyObject *name = filename ? PyString_FromString(filename) : NULL;
495 PyObject *result = PyErr_SetExcFromWindowsErrWithFilenameObject(
496 PyExc_WindowsError,
497 ierr, name);
498 Py_XDECREF(name);
499 return result;
Guido van Rossum795e1892000-02-17 15:19:15 +0000500}
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000501
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000502PyObject *PyErr_SetFromWindowsErrWithUnicodeFilename(
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000503 int ierr,
504 const Py_UNICODE *filename)
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000505{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000506 PyObject *name = filename ?
507 PyUnicode_FromUnicode(filename, wcslen(filename)) :
508 NULL;
509 PyObject *result = PyErr_SetExcFromWindowsErrWithFilenameObject(
510 PyExc_WindowsError,
511 ierr, name);
512 Py_XDECREF(name);
513 return result;
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000514}
Guido van Rossum795e1892000-02-17 15:19:15 +0000515#endif /* MS_WINDOWS */
516
Guido van Rossum683a0721990-10-21 22:09:12 +0000517void
Fred Drake6d63adf2000-08-24 22:38:39 +0000518_PyErr_BadInternalCall(char *filename, int lineno)
519{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000520 PyErr_Format(PyExc_SystemError,
521 "%s:%d: bad argument to internal function",
522 filename, lineno);
Fred Drake6d63adf2000-08-24 22:38:39 +0000523}
524
525/* Remove the preprocessor macro for PyErr_BadInternalCall() so that we can
526 export the entry point for existing object code: */
527#undef PyErr_BadInternalCall
528void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000529PyErr_BadInternalCall(void)
Guido van Rossum683a0721990-10-21 22:09:12 +0000530{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000531 PyErr_Format(PyExc_SystemError,
532 "bad argument to internal function");
Guido van Rossum683a0721990-10-21 22:09:12 +0000533}
Fred Drake6d63adf2000-08-24 22:38:39 +0000534#define PyErr_BadInternalCall() _PyErr_BadInternalCall(__FILE__, __LINE__)
535
Guido van Rossum1548bac1997-02-14 17:09:47 +0000536
537
Guido van Rossum1548bac1997-02-14 17:09:47 +0000538PyObject *
539PyErr_Format(PyObject *exception, const char *format, ...)
Guido van Rossum1548bac1997-02-14 17:09:47 +0000540{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000541 va_list vargs;
542 PyObject* string;
Guido van Rossum1548bac1997-02-14 17:09:47 +0000543
Jeremy Hyltonb69a27e2000-09-01 03:49:47 +0000544#ifdef HAVE_STDARG_PROTOTYPES
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000545 va_start(vargs, format);
Jeremy Hyltonb69a27e2000-09-01 03:49:47 +0000546#else
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000547 va_start(vargs);
Jeremy Hyltonb69a27e2000-09-01 03:49:47 +0000548#endif
Guido van Rossum1548bac1997-02-14 17:09:47 +0000549
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000550 string = PyString_FromFormatV(format, vargs);
551 PyErr_SetObject(exception, string);
552 Py_XDECREF(string);
553 va_end(vargs);
554 return NULL;
Guido van Rossum1548bac1997-02-14 17:09:47 +0000555}
Guido van Rossum7617e051997-09-16 18:43:50 +0000556
557
Georg Brandl658d5132006-05-23 11:17:21 +0000558
Guido van Rossum7617e051997-09-16 18:43:50 +0000559PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000560PyErr_NewException(char *name, PyObject *base, PyObject *dict)
Guido van Rossum7617e051997-09-16 18:43:50 +0000561{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000562 char *dot;
563 PyObject *modulename = NULL;
564 PyObject *classname = NULL;
565 PyObject *mydict = NULL;
566 PyObject *bases = NULL;
567 PyObject *result = NULL;
568 dot = strrchr(name, '.');
569 if (dot == NULL) {
570 PyErr_SetString(PyExc_SystemError,
571 "PyErr_NewException: name must be module.class");
572 return NULL;
573 }
574 if (base == NULL)
575 base = PyExc_Exception;
576 if (dict == NULL) {
577 dict = mydict = PyDict_New();
578 if (dict == NULL)
579 goto failure;
580 }
581 if (PyDict_GetItemString(dict, "__module__") == NULL) {
582 modulename = PyString_FromStringAndSize(name,
583 (Py_ssize_t)(dot-name));
584 if (modulename == NULL)
585 goto failure;
586 if (PyDict_SetItemString(dict, "__module__", modulename) != 0)
587 goto failure;
588 }
589 if (PyTuple_Check(base)) {
590 bases = base;
591 /* INCREF as we create a new ref in the else branch */
592 Py_INCREF(bases);
593 } else {
594 bases = PyTuple_Pack(1, base);
595 if (bases == NULL)
596 goto failure;
597 }
598 /* Create a real new-style class. */
599 result = PyObject_CallFunction((PyObject *)&PyType_Type, "sOO",
600 dot+1, bases, dict);
Guido van Rossum7617e051997-09-16 18:43:50 +0000601 failure:
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000602 Py_XDECREF(bases);
603 Py_XDECREF(mydict);
604 Py_XDECREF(classname);
605 Py_XDECREF(modulename);
606 return result;
Guido van Rossum7617e051997-09-16 18:43:50 +0000607}
Jeremy Hyltonb709df32000-09-01 02:47:25 +0000608
Georg Brandl740cdc32009-12-28 08:34:58 +0000609
610/* Create an exception with docstring */
611PyObject *
612PyErr_NewExceptionWithDoc(char *name, char *doc, PyObject *base, PyObject *dict)
613{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000614 int result;
615 PyObject *ret = NULL;
616 PyObject *mydict = NULL; /* points to the dict only if we create it */
617 PyObject *docobj;
Georg Brandl740cdc32009-12-28 08:34:58 +0000618
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000619 if (dict == NULL) {
620 dict = mydict = PyDict_New();
621 if (dict == NULL) {
622 return NULL;
623 }
624 }
Georg Brandl740cdc32009-12-28 08:34:58 +0000625
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000626 if (doc != NULL) {
627 docobj = PyString_FromString(doc);
628 if (docobj == NULL)
629 goto failure;
630 result = PyDict_SetItemString(dict, "__doc__", docobj);
631 Py_DECREF(docobj);
632 if (result < 0)
633 goto failure;
634 }
Georg Brandl740cdc32009-12-28 08:34:58 +0000635
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000636 ret = PyErr_NewException(name, base, dict);
Georg Brandl740cdc32009-12-28 08:34:58 +0000637 failure:
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000638 Py_XDECREF(mydict);
639 return ret;
Georg Brandl740cdc32009-12-28 08:34:58 +0000640}
641
642
Jeremy Hyltonb709df32000-09-01 02:47:25 +0000643/* Call when an exception has occurred but there is no way for Python
644 to handle it. Examples: exception in __del__ or during GC. */
645void
646PyErr_WriteUnraisable(PyObject *obj)
647{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000648 PyObject *f, *t, *v, *tb;
649 PyErr_Fetch(&t, &v, &tb);
650 f = PySys_GetObject("stderr");
651 if (f != NULL) {
652 PyFile_WriteString("Exception ", f);
653 if (t) {
654 PyObject* moduleName;
655 char* className;
656 assert(PyExceptionClass_Check(t));
657 className = PyExceptionClass_Name(t);
658 if (className != NULL) {
659 char *dot = strrchr(className, '.');
660 if (dot != NULL)
661 className = dot+1;
662 }
Tim Peters5e9d6cf2006-05-28 10:41:29 +0000663
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000664 moduleName = PyObject_GetAttrString(t, "__module__");
665 if (moduleName == NULL)
666 PyFile_WriteString("<unknown>", f);
667 else {
668 char* modstr = PyString_AsString(moduleName);
669 if (modstr &&
670 strcmp(modstr, "exceptions") != 0)
671 {
672 PyFile_WriteString(modstr, f);
673 PyFile_WriteString(".", f);
674 }
675 }
676 if (className == NULL)
677 PyFile_WriteString("<unknown>", f);
678 else
679 PyFile_WriteString(className, f);
680 if (v && v != Py_None) {
681 PyFile_WriteString(": ", f);
682 PyFile_WriteObject(v, f, 0);
683 }
684 Py_XDECREF(moduleName);
685 }
686 PyFile_WriteString(" in ", f);
687 PyFile_WriteObject(obj, f, 0);
688 PyFile_WriteString(" ignored\n", f);
689 PyErr_Clear(); /* Just in case */
690 }
691 Py_XDECREF(t);
692 Py_XDECREF(v);
693 Py_XDECREF(tb);
Jeremy Hyltonb709df32000-09-01 02:47:25 +0000694}
Guido van Rossumcfd42b52000-12-15 21:58:52 +0000695
Armin Rigo092381a2003-10-25 14:29:27 +0000696extern PyObject *PyModule_GetWarningsModule(void);
Guido van Rossumcfd42b52000-12-15 21:58:52 +0000697
Guido van Rossum2fd45652001-02-28 21:46:24 +0000698
Martin v. Löwiscfeb3b62002-03-03 21:30:27 +0000699/* Set file and line information for the current exception.
700 If the exception is not a SyntaxError, also sets additional attributes
701 to make printing of exceptions believe it is a syntax error. */
Guido van Rossum2fd45652001-02-28 21:46:24 +0000702
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000703void
Martin v. Löwis95292d62002-12-11 14:04:59 +0000704PyErr_SyntaxLocation(const char *filename, int lineno)
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000705{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000706 PyObject *exc, *v, *tb, *tmp;
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000707
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000708 /* add attributes for the line number and filename for the error */
709 PyErr_Fetch(&exc, &v, &tb);
710 PyErr_NormalizeException(&exc, &v, &tb);
711 /* XXX check that it is, indeed, a syntax error. It might not
712 * be, though. */
713 tmp = PyInt_FromLong(lineno);
714 if (tmp == NULL)
715 PyErr_Clear();
716 else {
717 if (PyObject_SetAttrString(v, "lineno", tmp))
718 PyErr_Clear();
719 Py_DECREF(tmp);
720 }
721 if (filename != NULL) {
722 tmp = PyString_FromString(filename);
723 if (tmp == NULL)
724 PyErr_Clear();
725 else {
726 if (PyObject_SetAttrString(v, "filename", tmp))
727 PyErr_Clear();
728 Py_DECREF(tmp);
729 }
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000730
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000731 tmp = PyErr_ProgramText(filename, lineno);
732 if (tmp) {
733 if (PyObject_SetAttrString(v, "text", tmp))
734 PyErr_Clear();
735 Py_DECREF(tmp);
736 }
737 }
738 if (PyObject_SetAttrString(v, "offset", Py_None)) {
739 PyErr_Clear();
740 }
741 if (exc != PyExc_SyntaxError) {
742 if (!PyObject_HasAttrString(v, "msg")) {
743 tmp = PyObject_Str(v);
744 if (tmp) {
745 if (PyObject_SetAttrString(v, "msg", tmp))
746 PyErr_Clear();
747 Py_DECREF(tmp);
748 } else {
749 PyErr_Clear();
750 }
751 }
752 if (!PyObject_HasAttrString(v, "print_file_and_line")) {
753 if (PyObject_SetAttrString(v, "print_file_and_line",
754 Py_None))
755 PyErr_Clear();
756 }
757 }
758 PyErr_Restore(exc, v, tb);
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000759}
760
761/* com_fetch_program_text will attempt to load the line of text that
762 the exception refers to. If it fails, it will return NULL but will
Brett Cannonbf364092006-03-01 04:25:17 +0000763 not set an exception.
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000764
765 XXX The functionality of this function is quite similar to the
766 functionality in tb_displayline() in traceback.c.
767*/
768
769PyObject *
Martin v. Löwis95292d62002-12-11 14:04:59 +0000770PyErr_ProgramText(const char *filename, int lineno)
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000771{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000772 FILE *fp;
773 int i;
774 char linebuf[1000];
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000775
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000776 if (filename == NULL || *filename == '\0' || lineno <= 0)
777 return NULL;
778 fp = fopen(filename, "r" PY_STDIOTEXTMODE);
779 if (fp == NULL)
780 return NULL;
781 for (i = 0; i < lineno; i++) {
782 char *pLastChar = &linebuf[sizeof(linebuf) - 2];
783 do {
784 *pLastChar = '\0';
785 if (Py_UniversalNewlineFgets(linebuf, sizeof linebuf, fp, NULL) == NULL)
786 break;
787 /* fgets read *something*; if it didn't get as
788 far as pLastChar, it must have found a newline
789 or hit the end of the file; if pLastChar is \n,
790 it obviously found a newline; else we haven't
791 yet seen a newline, so must continue */
792 } while (*pLastChar != '\0' && *pLastChar != '\n');
793 }
794 fclose(fp);
795 if (i == lineno) {
796 char *p = linebuf;
797 while (*p == ' ' || *p == '\t' || *p == '\014')
798 p++;
799 return PyString_FromString(p);
800 }
801 return NULL;
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000802}
Anthony Baxterac6bd462006-04-13 02:06:09 +0000803
804#ifdef __cplusplus
805}
806#endif
807