blob: ce36fc1dee2671431cf4da37e0a81134b3615d44 [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? */
27 Py_DECREF(traceback);
28 traceback = NULL;
29 }
30
31 /* Save these in locals to safeguard against recursive
32 invocation through Py_XDECREF */
33 oldtype = tstate->curexc_type;
34 oldvalue = tstate->curexc_value;
35 oldtraceback = tstate->curexc_traceback;
36
37 tstate->curexc_type = type;
38 tstate->curexc_value = value;
39 tstate->curexc_traceback = traceback;
40
41 Py_XDECREF(oldtype);
42 Py_XDECREF(oldvalue);
43 Py_XDECREF(oldtraceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +000044}
45
46void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000047PyErr_SetObject(PyObject *exception, PyObject *value)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000048{
Guido van Rossum373c8691997-04-29 18:22:47 +000049 Py_XINCREF(exception);
50 Py_XINCREF(value);
51 PyErr_Restore(exception, value, (PyObject *)NULL);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000052}
53
54void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000055PyErr_SetNone(PyObject *exception)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000056{
Guido van Rossum373c8691997-04-29 18:22:47 +000057 PyErr_SetObject(exception, (PyObject *)NULL);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000058}
59
60void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000061PyErr_SetString(PyObject *exception, const char *string)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000062{
Guido van Rossum373c8691997-04-29 18:22:47 +000063 PyObject *value = PyString_FromString(string);
64 PyErr_SetObject(exception, value);
65 Py_XDECREF(value);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000066}
67
Guido van Rossum3a241811994-08-29 12:14:12 +000068
Guido van Rossum373c8691997-04-29 18:22:47 +000069PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000070PyErr_Occurred(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000071{
Tim Peters024da352001-05-30 06:09:50 +000072 PyThreadState *tstate = PyThreadState_GET();
Guido van Rossuma027efa1997-05-05 20:56:21 +000073
74 return tstate->curexc_type;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000075}
76
Barry Warsawc0dc92a1997-08-22 21:22:58 +000077
78int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000079PyErr_GivenExceptionMatches(PyObject *err, PyObject *exc)
Barry Warsawc0dc92a1997-08-22 21:22:58 +000080{
Barry Warsawfa5c3152000-05-02 19:27:51 +000081 if (err == NULL || exc == NULL) {
82 /* maybe caused by "import exceptions" that failed early on */
83 return 0;
84 }
Barry Warsawc0dc92a1997-08-22 21:22:58 +000085 if (PyTuple_Check(exc)) {
86 int i, n;
87 n = PyTuple_Size(exc);
88 for (i = 0; i < n; i++) {
89 /* Test recursively */
90 if (PyErr_GivenExceptionMatches(
91 err, PyTuple_GET_ITEM(exc, i)))
92 {
93 return 1;
94 }
95 }
96 return 0;
97 }
98 /* err might be an instance, so check its class. */
99 if (PyInstance_Check(err))
100 err = (PyObject*)((PyInstanceObject*)err)->in_class;
101
102 if (PyClass_Check(err) && PyClass_Check(exc))
103 return PyClass_IsSubclass(err, exc);
104
105 return err == exc;
106}
Guido van Rossum743007d1999-04-21 15:27:31 +0000107
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000108
109int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000110PyErr_ExceptionMatches(PyObject *exc)
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000111{
112 return PyErr_GivenExceptionMatches(PyErr_Occurred(), exc);
113}
114
115
116/* Used in many places to normalize a raised exception, including in
117 eval_code2(), do_raise(), and PyErr_Print()
118*/
119void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000120PyErr_NormalizeException(PyObject **exc, PyObject **val, PyObject **tb)
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000121{
122 PyObject *type = *exc;
123 PyObject *value = *val;
124 PyObject *inclass = NULL;
Jeremy Hyltone2e2c9f2001-09-26 19:58:38 +0000125 PyObject *initial_tb = NULL;
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000126
Guido van Rossumed473a42000-08-07 19:18:27 +0000127 if (type == NULL) {
Guido van Rossum6b3fffa2003-04-10 20:29:48 +0000128 /* There was no exception, so nothing to do. */
129 return;
Guido van Rossumed473a42000-08-07 19:18:27 +0000130 }
131
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000132 /* If PyErr_SetNone() was used, the value will have been actually
133 set to NULL.
134 */
135 if (!value) {
136 value = Py_None;
137 Py_INCREF(value);
138 }
139
140 if (PyInstance_Check(value))
141 inclass = (PyObject*)((PyInstanceObject*)value)->in_class;
142
143 /* Normalize the exception so that if the type is a class, the
144 value will be an instance.
145 */
146 if (PyClass_Check(type)) {
147 /* if the value was not an instance, or is not an instance
148 whose class is (or is derived from) type, then use the
149 value as an argument to instantiation of the type
150 class.
151 */
152 if (!inclass || !PyClass_IsSubclass(inclass, type)) {
153 PyObject *args, *res;
154
155 if (value == Py_None)
Raymond Hettinger8ae46892003-10-12 19:09:37 +0000156 args = PyTuple_New(0);
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000157 else if (PyTuple_Check(value)) {
158 Py_INCREF(value);
159 args = value;
160 }
161 else
Raymond Hettinger8ae46892003-10-12 19:09:37 +0000162 args = PyTuple_Pack(1, value);
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000163
164 if (args == NULL)
165 goto finally;
166 res = PyEval_CallObject(type, args);
167 Py_DECREF(args);
168 if (res == NULL)
169 goto finally;
170 Py_DECREF(value);
171 value = res;
172 }
Barry Warsaw3a749931997-09-30 15:00:18 +0000173 /* if the class of the instance doesn't exactly match the
174 class of the type, believe the instance
175 */
176 else if (inclass != type) {
177 Py_DECREF(type);
178 type = inclass;
179 Py_INCREF(type);
180 }
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000181 }
182 *exc = type;
183 *val = value;
184 return;
185finally:
Guido van Rossum19b55f21997-12-09 14:11:39 +0000186 Py_DECREF(type);
187 Py_DECREF(value);
Jeremy Hyltone2e2c9f2001-09-26 19:58:38 +0000188 /* If the new exception doesn't set a traceback and the old
189 exception had a traceback, use the old traceback for the
190 new exception. It's better than nothing.
191 */
192 initial_tb = *tb;
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000193 PyErr_Fetch(exc, val, tb);
Jeremy Hyltone2e2c9f2001-09-26 19:58:38 +0000194 if (initial_tb != NULL) {
195 if (*tb == NULL)
196 *tb = initial_tb;
197 else
198 Py_DECREF(initial_tb);
199 }
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000200 /* normalize recursively */
201 PyErr_NormalizeException(exc, val, tb);
202}
203
204
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000205void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000206PyErr_Fetch(PyObject **p_type, PyObject **p_value, PyObject **p_traceback)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000207{
Nicholas Bastine5662ae2004-03-24 22:22:12 +0000208 PyThreadState *tstate = PyThreadState_GET();
Guido van Rossuma027efa1997-05-05 20:56:21 +0000209
210 *p_type = tstate->curexc_type;
211 *p_value = tstate->curexc_value;
212 *p_traceback = tstate->curexc_traceback;
213
214 tstate->curexc_type = NULL;
215 tstate->curexc_value = NULL;
216 tstate->curexc_traceback = NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000217}
218
219void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000220PyErr_Clear(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000221{
Guido van Rossuma027efa1997-05-05 20:56:21 +0000222 PyErr_Restore(NULL, NULL, NULL);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000223}
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000224
225/* Convenience functions to set a type error exception and return 0 */
226
227int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000228PyErr_BadArgument(void)
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000229{
Guido van Rossuma027efa1997-05-05 20:56:21 +0000230 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +0000231 "bad argument type for built-in operation");
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000232 return 0;
233}
234
Guido van Rossum373c8691997-04-29 18:22:47 +0000235PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000236PyErr_NoMemory(void)
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000237{
Vladimir Marangozov0888ff12000-08-18 18:01:06 +0000238 if (PyErr_ExceptionMatches(PyExc_MemoryError))
239 /* already current */
240 return NULL;
241
Barry Warsaw2d8adff1997-08-29 21:54:35 +0000242 /* raise the pre-allocated instance if it still exists */
243 if (PyExc_MemoryErrorInst)
244 PyErr_SetObject(PyExc_MemoryError, PyExc_MemoryErrorInst);
245 else
246 /* this will probably fail since there's no memory and hee,
247 hee, we have to instantiate this class
248 */
249 PyErr_SetNone(PyExc_MemoryError);
250
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000251 return NULL;
252}
253
Guido van Rossum373c8691997-04-29 18:22:47 +0000254PyObject *
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000255PyErr_SetFromErrnoWithFilenameObject(PyObject *exc, PyObject *filenameObject)
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000256{
Guido van Rossum373c8691997-04-29 18:22:47 +0000257 PyObject *v;
Guido van Rossume0e59821998-10-14 20:38:13 +0000258 char *s;
Guido van Rossum3a241811994-08-29 12:14:12 +0000259 int i = errno;
Martin v. Löwis3484a182002-03-09 12:07:51 +0000260#ifdef PLAN9
261 char errbuf[ERRMAX];
262#endif
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000263#ifdef MS_WINDOWS
Guido van Rossum795e1892000-02-17 15:19:15 +0000264 char *s_buf = NULL;
Mark Hammond3d61a062002-10-04 00:13:02 +0000265 char s_small_buf[28]; /* Room for "Windows Error 0xFFFFFFFF" */
Guido van Rossum795e1892000-02-17 15:19:15 +0000266#endif
Guido van Rossume9fbc091995-02-18 14:52:19 +0000267#ifdef EINTR
Guido van Rossum373c8691997-04-29 18:22:47 +0000268 if (i == EINTR && PyErr_CheckSignals())
Guido van Rossum5063bab1991-10-20 20:14:56 +0000269 return NULL;
Guido van Rossume9fbc091995-02-18 14:52:19 +0000270#endif
Martin v. Löwis3484a182002-03-09 12:07:51 +0000271#ifdef PLAN9
272 rerrstr(errbuf, sizeof errbuf);
273 s = errbuf;
274#else
Guido van Rossume0e59821998-10-14 20:38:13 +0000275 if (i == 0)
276 s = "Error"; /* Sometimes errno didn't get set */
Barry Warsaw97d95151998-07-23 16:05:56 +0000277 else
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000278#ifndef MS_WINDOWS
Guido van Rossume0e59821998-10-14 20:38:13 +0000279 s = strerror(i);
Guido van Rossum743007d1999-04-21 15:27:31 +0000280#else
281 {
Guido van Rossum795e1892000-02-17 15:19:15 +0000282 /* Note that the Win32 errors do not lineup with the
283 errno error. So if the error is in the MSVC error
284 table, we use it, otherwise we assume it really _is_
285 a Win32 error code
286 */
Guido van Rossum584b16a2000-02-21 16:50:31 +0000287 if (i > 0 && i < _sys_nerr) {
Guido van Rossum795e1892000-02-17 15:19:15 +0000288 s = _sys_errlist[i];
289 }
290 else {
291 int len = FormatMessage(
292 FORMAT_MESSAGE_ALLOCATE_BUFFER |
293 FORMAT_MESSAGE_FROM_SYSTEM |
294 FORMAT_MESSAGE_IGNORE_INSERTS,
295 NULL, /* no message source */
296 i,
297 MAKELANGID(LANG_NEUTRAL,
298 SUBLANG_DEFAULT),
299 /* Default language */
300 (LPTSTR) &s_buf,
301 0, /* size not used */
302 NULL); /* no args */
Mark Hammond3d61a062002-10-04 00:13:02 +0000303 if (len==0) {
304 /* Only ever seen this in out-of-mem
305 situations */
306 sprintf(s_small_buf, "Windows Error 0x%X", i);
307 s = s_small_buf;
308 s_buf = NULL;
309 } else {
310 s = s_buf;
311 /* remove trailing cr/lf and dots */
312 while (len > 0 && (s[len-1] <= ' ' || s[len-1] == '.'))
313 s[--len] = '\0';
314 }
Guido van Rossum795e1892000-02-17 15:19:15 +0000315 }
Guido van Rossum743007d1999-04-21 15:27:31 +0000316 }
Martin v. Löwis3484a182002-03-09 12:07:51 +0000317#endif /* Unix/Windows */
318#endif /* PLAN 9*/
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000319 if (filenameObject != NULL)
320 v = Py_BuildValue("(isO)", i, s, filenameObject);
Guido van Rossume0e59821998-10-14 20:38:13 +0000321 else
322 v = Py_BuildValue("(is)", i, s);
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000323 if (v != NULL) {
Guido van Rossum373c8691997-04-29 18:22:47 +0000324 PyErr_SetObject(exc, v);
325 Py_DECREF(v);
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000326 }
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000327#ifdef MS_WINDOWS
Guido van Rossum795e1892000-02-17 15:19:15 +0000328 LocalFree(s_buf);
Guido van Rossum743007d1999-04-21 15:27:31 +0000329#endif
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000330 return NULL;
331}
Guido van Rossum743007d1999-04-21 15:27:31 +0000332
Barry Warsaw97d95151998-07-23 16:05:56 +0000333
334PyObject *
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000335PyErr_SetFromErrnoWithFilename(PyObject *exc, char *filename)
336{
337 PyObject *name = filename ? PyString_FromString(filename) : NULL;
338 PyObject *result = PyErr_SetFromErrnoWithFilenameObject(exc, name);
Mark Hammondda7efaa2002-10-04 00:09:38 +0000339 Py_XDECREF(name);
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000340 return result;
341}
342
343#ifdef Py_WIN_WIDE_FILENAMES
344PyObject *
345PyErr_SetFromErrnoWithUnicodeFilename(PyObject *exc, Py_UNICODE *filename)
346{
347 PyObject *name = filename ?
348 PyUnicode_FromUnicode(filename, wcslen(filename)) :
349 NULL;
350 PyObject *result = PyErr_SetFromErrnoWithFilenameObject(exc, name);
351 Py_XDECREF(name);
352 return result;
353}
354#endif /* Py_WIN_WIDE_FILENAMES */
355
356PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000357PyErr_SetFromErrno(PyObject *exc)
Barry Warsaw97d95151998-07-23 16:05:56 +0000358{
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000359 return PyErr_SetFromErrnoWithFilenameObject(exc, NULL);
Barry Warsaw97d95151998-07-23 16:05:56 +0000360}
Guido van Rossum683a0721990-10-21 22:09:12 +0000361
Guido van Rossum795e1892000-02-17 15:19:15 +0000362#ifdef MS_WINDOWS
363/* Windows specific error code handling */
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000364PyObject *PyErr_SetExcFromWindowsErrWithFilenameObject(
Thomas Heller085358a2002-07-29 14:27:41 +0000365 PyObject *exc,
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000366 int ierr,
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000367 PyObject *filenameObject)
Guido van Rossum795e1892000-02-17 15:19:15 +0000368{
369 int len;
370 char *s;
Mark Hammond3d61a062002-10-04 00:13:02 +0000371 char *s_buf = NULL; /* Free via LocalFree */
372 char s_small_buf[28]; /* Room for "Windows Error 0xFFFFFFFF" */
Guido van Rossum795e1892000-02-17 15:19:15 +0000373 PyObject *v;
374 DWORD err = (DWORD)ierr;
375 if (err==0) err = GetLastError();
376 len = FormatMessage(
377 /* Error API error */
378 FORMAT_MESSAGE_ALLOCATE_BUFFER |
379 FORMAT_MESSAGE_FROM_SYSTEM |
380 FORMAT_MESSAGE_IGNORE_INSERTS,
381 NULL, /* no message source */
382 err,
383 MAKELANGID(LANG_NEUTRAL,
384 SUBLANG_DEFAULT), /* Default language */
Mark Hammond3d61a062002-10-04 00:13:02 +0000385 (LPTSTR) &s_buf,
Guido van Rossum795e1892000-02-17 15:19:15 +0000386 0, /* size not used */
387 NULL); /* no args */
Mark Hammond3d61a062002-10-04 00:13:02 +0000388 if (len==0) {
389 /* Only seen this in out of mem situations */
390 sprintf(s_small_buf, "Windows Error 0x%X", err);
391 s = s_small_buf;
392 s_buf = NULL;
393 } else {
394 s = s_buf;
395 /* remove trailing cr/lf and dots */
396 while (len > 0 && (s[len-1] <= ' ' || s[len-1] == '.'))
397 s[--len] = '\0';
398 }
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000399 if (filenameObject != NULL)
400 v = Py_BuildValue("(isO)", err, s, filenameObject);
Guido van Rossum795e1892000-02-17 15:19:15 +0000401 else
402 v = Py_BuildValue("(is)", err, s);
403 if (v != NULL) {
Thomas Heller085358a2002-07-29 14:27:41 +0000404 PyErr_SetObject(exc, v);
Guido van Rossum795e1892000-02-17 15:19:15 +0000405 Py_DECREF(v);
406 }
Mark Hammond3d61a062002-10-04 00:13:02 +0000407 LocalFree(s_buf);
Guido van Rossum795e1892000-02-17 15:19:15 +0000408 return NULL;
409}
410
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000411PyObject *PyErr_SetExcFromWindowsErrWithFilename(
412 PyObject *exc,
413 int ierr,
414 const char *filename)
415{
416 PyObject *name = filename ? PyString_FromString(filename) : NULL;
417 PyObject *ret = PyErr_SetExcFromWindowsErrWithFilenameObject(exc,
418 ierr,
419 name);
420 Py_XDECREF(name);
421 return ret;
422}
423
424#ifdef Py_WIN_WIDE_FILENAMES
425PyObject *PyErr_SetExcFromWindowsErrWithUnicodeFilename(
426 PyObject *exc,
427 int ierr,
428 const Py_UNICODE *filename)
429{
430 PyObject *name = filename ?
431 PyUnicode_FromUnicode(filename, wcslen(filename)) :
432 NULL;
433 PyObject *ret = PyErr_SetExcFromWindowsErrWithFilenameObject(exc,
434 ierr,
435 name);
436 Py_XDECREF(name);
437 return ret;
438}
439#endif /* Py_WIN_WIDE_FILENAMES */
440
Thomas Heller085358a2002-07-29 14:27:41 +0000441PyObject *PyErr_SetExcFromWindowsErr(PyObject *exc, int ierr)
442{
443 return PyErr_SetExcFromWindowsErrWithFilename(exc, ierr, NULL);
444}
445
Guido van Rossum795e1892000-02-17 15:19:15 +0000446PyObject *PyErr_SetFromWindowsErr(int ierr)
447{
Thomas Heller085358a2002-07-29 14:27:41 +0000448 return PyErr_SetExcFromWindowsErrWithFilename(PyExc_WindowsError,
449 ierr, NULL);
450}
451PyObject *PyErr_SetFromWindowsErrWithFilename(
452 int ierr,
453 const char *filename)
454{
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000455 PyObject *name = filename ? PyString_FromString(filename) : NULL;
456 PyObject *result = PyErr_SetExcFromWindowsErrWithFilenameObject(
457 PyExc_WindowsError,
458 ierr, name);
Mark Hammondda7efaa2002-10-04 00:09:38 +0000459 Py_XDECREF(name);
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000460 return result;
Guido van Rossum795e1892000-02-17 15:19:15 +0000461}
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000462
463#ifdef Py_WIN_WIDE_FILENAMES
464PyObject *PyErr_SetFromWindowsErrWithUnicodeFilename(
465 int ierr,
466 const Py_UNICODE *filename)
467{
468 PyObject *name = filename ?
469 PyUnicode_FromUnicode(filename, wcslen(filename)) :
470 NULL;
471 PyObject *result = PyErr_SetExcFromWindowsErrWithFilenameObject(
472 PyExc_WindowsError,
473 ierr, name);
Mark Hammondda7efaa2002-10-04 00:09:38 +0000474 Py_XDECREF(name);
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000475 return result;
476}
477#endif /* Py_WIN_WIDE_FILENAMES */
Guido van Rossum795e1892000-02-17 15:19:15 +0000478#endif /* MS_WINDOWS */
479
Guido van Rossum683a0721990-10-21 22:09:12 +0000480void
Fred Drake6d63adf2000-08-24 22:38:39 +0000481_PyErr_BadInternalCall(char *filename, int lineno)
482{
483 PyErr_Format(PyExc_SystemError,
484 "%s:%d: bad argument to internal function",
485 filename, lineno);
486}
487
488/* Remove the preprocessor macro for PyErr_BadInternalCall() so that we can
489 export the entry point for existing object code: */
490#undef PyErr_BadInternalCall
491void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000492PyErr_BadInternalCall(void)
Guido van Rossum683a0721990-10-21 22:09:12 +0000493{
Fred Drake6d63adf2000-08-24 22:38:39 +0000494 PyErr_Format(PyExc_SystemError,
495 "bad argument to internal function");
Guido van Rossum683a0721990-10-21 22:09:12 +0000496}
Fred Drake6d63adf2000-08-24 22:38:39 +0000497#define PyErr_BadInternalCall() _PyErr_BadInternalCall(__FILE__, __LINE__)
498
Guido van Rossum1548bac1997-02-14 17:09:47 +0000499
500
Guido van Rossum1548bac1997-02-14 17:09:47 +0000501PyObject *
502PyErr_Format(PyObject *exception, const char *format, ...)
Guido van Rossum1548bac1997-02-14 17:09:47 +0000503{
504 va_list vargs;
Jeremy Hyltonb69a27e2000-09-01 03:49:47 +0000505 PyObject* string;
Guido van Rossum1548bac1997-02-14 17:09:47 +0000506
Jeremy Hyltonb69a27e2000-09-01 03:49:47 +0000507#ifdef HAVE_STDARG_PROTOTYPES
Guido van Rossum1548bac1997-02-14 17:09:47 +0000508 va_start(vargs, format);
Jeremy Hyltonb69a27e2000-09-01 03:49:47 +0000509#else
510 va_start(vargs);
511#endif
Guido van Rossum1548bac1997-02-14 17:09:47 +0000512
Barry Warsaw876c8cb2001-08-24 18:35:23 +0000513 string = PyString_FromFormatV(format, vargs);
Jeremy Hyltonb69a27e2000-09-01 03:49:47 +0000514 PyErr_SetObject(exception, string);
515 Py_XDECREF(string);
Tim Petersc15c4f12001-10-02 21:32:07 +0000516 va_end(vargs);
Guido van Rossum1548bac1997-02-14 17:09:47 +0000517 return NULL;
518}
Guido van Rossum7617e051997-09-16 18:43:50 +0000519
520
521PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000522PyErr_NewException(char *name, PyObject *base, PyObject *dict)
Guido van Rossum7617e051997-09-16 18:43:50 +0000523{
Guido van Rossum2ac650f1997-10-03 19:50:55 +0000524 char *dot;
525 PyObject *modulename = NULL;
526 PyObject *classname = NULL;
527 PyObject *mydict = NULL;
528 PyObject *bases = NULL;
Guido van Rossum7617e051997-09-16 18:43:50 +0000529 PyObject *result = NULL;
Guido van Rossum2ac650f1997-10-03 19:50:55 +0000530 dot = strrchr(name, '.');
531 if (dot == NULL) {
532 PyErr_SetString(PyExc_SystemError,
533 "PyErr_NewException: name must be module.class");
Guido van Rossum7617e051997-09-16 18:43:50 +0000534 return NULL;
Guido van Rossum7617e051997-09-16 18:43:50 +0000535 }
536 if (base == NULL)
537 base = PyExc_Exception;
Guido van Rossum2ac650f1997-10-03 19:50:55 +0000538 if (dict == NULL) {
539 dict = mydict = PyDict_New();
540 if (dict == NULL)
541 goto failure;
542 }
543 if (PyDict_GetItemString(dict, "__module__") == NULL) {
544 modulename = PyString_FromStringAndSize(name, (int)(dot-name));
545 if (modulename == NULL)
546 goto failure;
547 if (PyDict_SetItemString(dict, "__module__", modulename) != 0)
548 goto failure;
549 }
550 classname = PyString_FromString(dot+1);
551 if (classname == NULL)
Guido van Rossum7617e051997-09-16 18:43:50 +0000552 goto failure;
Raymond Hettinger8ae46892003-10-12 19:09:37 +0000553 bases = PyTuple_Pack(1, base);
Guido van Rossum2ac650f1997-10-03 19:50:55 +0000554 if (bases == NULL)
555 goto failure;
556 result = PyClass_New(bases, dict, classname);
Guido van Rossum7617e051997-09-16 18:43:50 +0000557 failure:
Guido van Rossum2ac650f1997-10-03 19:50:55 +0000558 Py_XDECREF(bases);
559 Py_XDECREF(mydict);
560 Py_XDECREF(classname);
561 Py_XDECREF(modulename);
Guido van Rossum7617e051997-09-16 18:43:50 +0000562 return result;
563}
Jeremy Hyltonb709df32000-09-01 02:47:25 +0000564
565/* Call when an exception has occurred but there is no way for Python
566 to handle it. Examples: exception in __del__ or during GC. */
567void
568PyErr_WriteUnraisable(PyObject *obj)
569{
570 PyObject *f, *t, *v, *tb;
571 PyErr_Fetch(&t, &v, &tb);
572 f = PySys_GetObject("stderr");
573 if (f != NULL) {
574 PyFile_WriteString("Exception ", f);
575 if (t) {
576 PyFile_WriteObject(t, f, Py_PRINT_RAW);
577 if (v && v != Py_None) {
578 PyFile_WriteString(": ", f);
579 PyFile_WriteObject(v, f, 0);
580 }
581 }
582 PyFile_WriteString(" in ", f);
583 PyFile_WriteObject(obj, f, 0);
584 PyFile_WriteString(" ignored\n", f);
585 PyErr_Clear(); /* Just in case */
586 }
587 Py_XDECREF(t);
588 Py_XDECREF(v);
589 Py_XDECREF(tb);
590}
Guido van Rossumcfd42b52000-12-15 21:58:52 +0000591
Armin Rigo092381a2003-10-25 14:29:27 +0000592extern PyObject *PyModule_GetWarningsModule(void);
Guido van Rossumcfd42b52000-12-15 21:58:52 +0000593
594/* Function to issue a warning message; may raise an exception. */
595int
596PyErr_Warn(PyObject *category, char *message)
597{
Mark Hammonda43fd0c2003-02-19 00:33:33 +0000598 PyObject *dict, *func = NULL;
Mark Hammondedd07732003-07-15 23:03:55 +0000599 PyObject *warnings_module = PyModule_GetWarningsModule();
Guido van Rossumcfd42b52000-12-15 21:58:52 +0000600
Mark Hammondedd07732003-07-15 23:03:55 +0000601 if (warnings_module != NULL) {
602 dict = PyModule_GetDict(warnings_module);
Guido van Rossumcfd42b52000-12-15 21:58:52 +0000603 func = PyDict_GetItemString(dict, "warn");
Guido van Rossumcfd42b52000-12-15 21:58:52 +0000604 }
605 if (func == NULL) {
606 PySys_WriteStderr("warning: %s\n", message);
607 return 0;
608 }
609 else {
610 PyObject *args, *res;
611
612 if (category == NULL)
613 category = PyExc_RuntimeWarning;
614 args = Py_BuildValue("(sO)", message, category);
615 if (args == NULL)
616 return -1;
617 res = PyEval_CallObject(func, args);
618 Py_DECREF(args);
619 if (res == NULL)
620 return -1;
621 Py_DECREF(res);
622 return 0;
623 }
624}
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000625
Guido van Rossum2fd45652001-02-28 21:46:24 +0000626
627/* Warning with explicit origin */
628int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000629PyErr_WarnExplicit(PyObject *category, const char *message,
630 const char *filename, int lineno,
631 const char *module, PyObject *registry)
Guido van Rossum2fd45652001-02-28 21:46:24 +0000632{
633 PyObject *mod, *dict, *func = NULL;
634
635 mod = PyImport_ImportModule("warnings");
636 if (mod != NULL) {
637 dict = PyModule_GetDict(mod);
638 func = PyDict_GetItemString(dict, "warn_explicit");
639 Py_DECREF(mod);
640 }
641 if (func == NULL) {
642 PySys_WriteStderr("warning: %s\n", message);
643 return 0;
644 }
645 else {
646 PyObject *args, *res;
647
648 if (category == NULL)
649 category = PyExc_RuntimeWarning;
650 if (registry == NULL)
651 registry = Py_None;
652 args = Py_BuildValue("(sOsizO)", message, category,
653 filename, lineno, module, registry);
654 if (args == NULL)
655 return -1;
656 res = PyEval_CallObject(func, args);
657 Py_DECREF(args);
658 if (res == NULL)
659 return -1;
660 Py_DECREF(res);
661 return 0;
662 }
663}
664
665
Martin v. Löwiscfeb3b62002-03-03 21:30:27 +0000666/* Set file and line information for the current exception.
667 If the exception is not a SyntaxError, also sets additional attributes
668 to make printing of exceptions believe it is a syntax error. */
Guido van Rossum2fd45652001-02-28 21:46:24 +0000669
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000670void
Martin v. Löwis95292d62002-12-11 14:04:59 +0000671PyErr_SyntaxLocation(const char *filename, int lineno)
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000672{
673 PyObject *exc, *v, *tb, *tmp;
674
675 /* add attributes for the line number and filename for the error */
676 PyErr_Fetch(&exc, &v, &tb);
677 PyErr_NormalizeException(&exc, &v, &tb);
678 /* XXX check that it is, indeed, a syntax error */
679 tmp = PyInt_FromLong(lineno);
680 if (tmp == NULL)
681 PyErr_Clear();
682 else {
683 if (PyObject_SetAttrString(v, "lineno", tmp))
684 PyErr_Clear();
685 Py_DECREF(tmp);
686 }
687 if (filename != NULL) {
688 tmp = PyString_FromString(filename);
689 if (tmp == NULL)
690 PyErr_Clear();
691 else {
692 if (PyObject_SetAttrString(v, "filename", tmp))
693 PyErr_Clear();
694 Py_DECREF(tmp);
695 }
696
697 tmp = PyErr_ProgramText(filename, lineno);
698 if (tmp) {
699 PyObject_SetAttrString(v, "text", tmp);
700 Py_DECREF(tmp);
701 }
702 }
Martin v. Löwiscfeb3b62002-03-03 21:30:27 +0000703 if (PyObject_SetAttrString(v, "offset", Py_None)) {
704 PyErr_Clear();
705 }
706 if (exc != PyExc_SyntaxError) {
707 if (!PyObject_HasAttrString(v, "msg")) {
708 tmp = PyObject_Str(v);
709 if (tmp) {
710 if (PyObject_SetAttrString(v, "msg", tmp))
711 PyErr_Clear();
712 Py_DECREF(tmp);
713 } else {
714 PyErr_Clear();
715 }
716 }
717 if (!PyObject_HasAttrString(v, "print_file_and_line")) {
718 if (PyObject_SetAttrString(v, "print_file_and_line",
719 Py_None))
720 PyErr_Clear();
721 }
722 }
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000723 PyErr_Restore(exc, v, tb);
724}
725
726/* com_fetch_program_text will attempt to load the line of text that
727 the exception refers to. If it fails, it will return NULL but will
728 not set an exception.
729
730 XXX The functionality of this function is quite similar to the
731 functionality in tb_displayline() in traceback.c.
732*/
733
734PyObject *
Martin v. Löwis95292d62002-12-11 14:04:59 +0000735PyErr_ProgramText(const char *filename, int lineno)
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000736{
737 FILE *fp;
738 int i;
739 char linebuf[1000];
740
741 if (filename == NULL || lineno <= 0)
742 return NULL;
Jack Jansen7b8c7542002-04-14 20:12:41 +0000743 fp = fopen(filename, "r" PY_STDIOTEXTMODE);
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000744 if (fp == NULL)
745 return NULL;
746 for (i = 0; i < lineno; i++) {
747 char *pLastChar = &linebuf[sizeof(linebuf) - 2];
748 do {
749 *pLastChar = '\0';
Jack Jansen7b8c7542002-04-14 20:12:41 +0000750 if (Py_UniversalNewlineFgets(linebuf, sizeof linebuf, fp, NULL) == NULL)
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000751 break;
752 /* fgets read *something*; if it didn't get as
753 far as pLastChar, it must have found a newline
754 or hit the end of the file; if pLastChar is \n,
755 it obviously found a newline; else we haven't
756 yet seen a newline, so must continue */
757 } while (*pLastChar != '\0' && *pLastChar != '\n');
758 }
759 fclose(fp);
760 if (i == lineno) {
761 char *p = linebuf;
762 while (*p == ' ' || *p == '\t' || *p == '\014')
763 p++;
764 return PyString_FromString(p);
765 }
766 return NULL;
767}