blob: 2d5eb6c21ec91e206aa7c0401651cda7ad277762 [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
Martin v. Löwis5d12abe2007-09-03 07:40:24 +000013#include <windows.h>
14#include <winbase.h>
Guido van Rossum743007d1999-04-21 15:27:31 +000015#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{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000027 PyThreadState *tstate = PyThreadState_GET();
28 PyObject *oldtype, *oldvalue, *oldtraceback;
Guido van Rossum1ae940a1995-01-02 19:04:15 +000029
Antoine Pitrouf95a1b32010-05-09 15:52:27 +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 Pitrouf95a1b32010-05-09 15:52:27 +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 Pitrouf95a1b32010-05-09 15:52:27 +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 Pitrouf95a1b32010-05-09 15:52:27 +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 Pitrouf95a1b32010-05-09 15:52:27 +000055 PyThreadState *tstate = PyThreadState_GET();
56 PyObject *exc_value;
57 PyObject *tb = NULL;
Guido van Rossumb4fb6e42008-06-14 20:20:24 +000058
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000059 if (exception != NULL &&
60 !PyExceptionClass_Check(exception)) {
61 PyErr_Format(PyExc_SystemError,
62 "exception %R not a BaseException subclass",
63 exception);
64 return;
65 }
66 Py_XINCREF(value);
67 exc_value = tstate->exc_value;
68 if (exc_value != NULL && exc_value != Py_None) {
69 /* Implicit exception chaining */
70 Py_INCREF(exc_value);
71 if (value == NULL || !PyExceptionInstance_Check(value)) {
72 /* We must normalize the value right now */
73 PyObject *args, *fixed_value;
Victor Stinnerace47d72013-07-18 01:41:08 +020074#ifdef Py_DEBUG
75 /* in debug mode, PyEval_EvalFrameEx() fails with an assertion
76 error if an exception is set when it is called */
77 PyErr_Clear();
78#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000079 if (value == NULL || value == Py_None)
80 args = PyTuple_New(0);
81 else if (PyTuple_Check(value)) {
82 Py_INCREF(value);
83 args = value;
84 }
85 else
86 args = PyTuple_Pack(1, value);
87 fixed_value = args ?
88 PyEval_CallObject(exception, args) : NULL;
89 Py_XDECREF(args);
90 Py_XDECREF(value);
91 if (fixed_value == NULL)
92 return;
93 value = fixed_value;
94 }
95 /* Avoid reference cycles through the context chain.
96 This is O(chain length) but context chains are
97 usually very short. Sensitive readers may try
98 to inline the call to PyException_GetContext. */
99 if (exc_value != value) {
100 PyObject *o = exc_value, *context;
101 while ((context = PyException_GetContext(o))) {
102 Py_DECREF(context);
103 if (context == value) {
104 PyException_SetContext(o, NULL);
105 break;
106 }
107 o = context;
108 }
109 PyException_SetContext(value, exc_value);
110 } else {
111 Py_DECREF(exc_value);
112 }
113 }
114 if (value != NULL && PyExceptionInstance_Check(value))
115 tb = PyException_GetTraceback(value);
116 Py_XINCREF(exception);
117 PyErr_Restore(exception, value, tb);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000118}
119
120void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000121PyErr_SetNone(PyObject *exception)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000122{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000123 PyErr_SetObject(exception, (PyObject *)NULL);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000124}
125
126void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000127PyErr_SetString(PyObject *exception, const char *string)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000128{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000129 PyObject *value = PyUnicode_FromString(string);
130 PyErr_SetObject(exception, value);
131 Py_XDECREF(value);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000132}
133
Guido van Rossum3a241811994-08-29 12:14:12 +0000134
Guido van Rossum373c8691997-04-29 18:22:47 +0000135PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000136PyErr_Occurred(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000137{
Jeffrey Yasskin8e0bdfd2010-05-13 18:31:05 +0000138 /* If there is no thread state, PyThreadState_GET calls
139 Py_FatalError, which calls PyErr_Occurred. To avoid the
140 resulting infinite loop, we inline PyThreadState_GET here and
141 treat no thread as no error. */
142 PyThreadState *tstate =
143 ((PyThreadState*)_Py_atomic_load_relaxed(&_PyThreadState_Current));
Guido van Rossuma027efa1997-05-05 20:56:21 +0000144
Jeffrey Yasskin8e0bdfd2010-05-13 18:31:05 +0000145 return tstate == NULL ? NULL : tstate->curexc_type;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000146}
147
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000148
149int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000150PyErr_GivenExceptionMatches(PyObject *err, PyObject *exc)
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000151{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000152 if (err == NULL || exc == NULL) {
153 /* maybe caused by "import exceptions" that failed early on */
154 return 0;
155 }
156 if (PyTuple_Check(exc)) {
157 Py_ssize_t i, n;
158 n = PyTuple_Size(exc);
159 for (i = 0; i < n; i++) {
160 /* Test recursively */
161 if (PyErr_GivenExceptionMatches(
162 err, PyTuple_GET_ITEM(exc, i)))
163 {
164 return 1;
165 }
166 }
167 return 0;
168 }
169 /* err might be an instance, so check its class. */
170 if (PyExceptionInstance_Check(err))
171 err = PyExceptionInstance_Class(err);
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000172
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000173 if (PyExceptionClass_Check(err) && PyExceptionClass_Check(exc)) {
174 int res = 0;
175 PyObject *exception, *value, *tb;
176 PyErr_Fetch(&exception, &value, &tb);
177 /* PyObject_IsSubclass() can recurse and therefore is
178 not safe (see test_bad_getattr in test.pickletester). */
179 res = PyType_IsSubtype((PyTypeObject *)err, (PyTypeObject *)exc);
180 /* This function must not fail, so print the error here */
181 if (res == -1) {
182 PyErr_WriteUnraisable(err);
183 res = 0;
184 }
185 PyErr_Restore(exception, value, tb);
186 return res;
187 }
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000188
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000189 return err == exc;
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000190}
Guido van Rossum743007d1999-04-21 15:27:31 +0000191
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000192
193int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000194PyErr_ExceptionMatches(PyObject *exc)
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000195{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000196 return PyErr_GivenExceptionMatches(PyErr_Occurred(), exc);
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000197}
198
199
200/* Used in many places to normalize a raised exception, including in
201 eval_code2(), do_raise(), and PyErr_Print()
Benjamin Petersone6528212008-07-15 15:32:09 +0000202
203 XXX: should PyErr_NormalizeException() also call
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000204 PyException_SetTraceback() with the resulting value and tb?
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000205*/
206void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000207PyErr_NormalizeException(PyObject **exc, PyObject **val, PyObject **tb)
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000208{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000209 PyObject *type = *exc;
210 PyObject *value = *val;
211 PyObject *inclass = NULL;
212 PyObject *initial_tb = NULL;
213 PyThreadState *tstate = NULL;
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000214
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000215 if (type == NULL) {
216 /* There was no exception, so nothing to do. */
217 return;
218 }
Guido van Rossumed473a42000-08-07 19:18:27 +0000219
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000220 /* If PyErr_SetNone() was used, the value will have been actually
221 set to NULL.
222 */
223 if (!value) {
224 value = Py_None;
225 Py_INCREF(value);
226 }
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000227
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000228 if (PyExceptionInstance_Check(value))
229 inclass = PyExceptionInstance_Class(value);
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000230
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000231 /* Normalize the exception so that if the type is a class, the
232 value will be an instance.
233 */
234 if (PyExceptionClass_Check(type)) {
Victor Stinner74a7fa62013-07-17 00:44:53 +0200235 int is_subclass;
236 if (inclass) {
237 is_subclass = PyObject_IsSubclass(inclass, type);
238 if (is_subclass < 0)
239 goto finally;
240 }
241 else
242 is_subclass = 0;
243
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000244 /* if the value was not an instance, or is not an instance
245 whose class is (or is derived from) type, then use the
246 value as an argument to instantiation of the type
247 class.
248 */
Victor Stinner74a7fa62013-07-17 00:44:53 +0200249 if (!inclass || !is_subclass) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000250 PyObject *args, *res;
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000251
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000252 if (value == Py_None)
253 args = PyTuple_New(0);
254 else if (PyTuple_Check(value)) {
255 Py_INCREF(value);
256 args = value;
257 }
258 else
259 args = PyTuple_Pack(1, value);
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000260
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000261 if (args == NULL)
262 goto finally;
263 res = PyEval_CallObject(type, args);
264 Py_DECREF(args);
265 if (res == NULL)
266 goto finally;
267 Py_DECREF(value);
268 value = res;
269 }
270 /* if the class of the instance doesn't exactly match the
271 class of the type, believe the instance
272 */
273 else if (inclass != type) {
274 Py_DECREF(type);
275 type = inclass;
276 Py_INCREF(type);
277 }
278 }
279 *exc = type;
280 *val = value;
281 return;
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000282finally:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000283 Py_DECREF(type);
284 Py_DECREF(value);
285 /* If the new exception doesn't set a traceback and the old
286 exception had a traceback, use the old traceback for the
287 new exception. It's better than nothing.
288 */
289 initial_tb = *tb;
290 PyErr_Fetch(exc, val, tb);
291 if (initial_tb != NULL) {
292 if (*tb == NULL)
293 *tb = initial_tb;
294 else
295 Py_DECREF(initial_tb);
296 }
297 /* normalize recursively */
298 tstate = PyThreadState_GET();
299 if (++tstate->recursion_depth > Py_GetRecursionLimit()) {
300 --tstate->recursion_depth;
301 /* throw away the old exception... */
302 Py_DECREF(*exc);
303 Py_DECREF(*val);
304 /* ... and use the recursion error instead */
305 *exc = PyExc_RuntimeError;
306 *val = PyExc_RecursionErrorInst;
307 Py_INCREF(*exc);
308 Py_INCREF(*val);
309 /* just keeping the old traceback */
310 return;
311 }
312 PyErr_NormalizeException(exc, val, tb);
313 --tstate->recursion_depth;
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000314}
315
316
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000317void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000318PyErr_Fetch(PyObject **p_type, PyObject **p_value, PyObject **p_traceback)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000319{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000320 PyThreadState *tstate = PyThreadState_GET();
Guido van Rossuma027efa1997-05-05 20:56:21 +0000321
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000322 *p_type = tstate->curexc_type;
323 *p_value = tstate->curexc_value;
324 *p_traceback = tstate->curexc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000325
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000326 tstate->curexc_type = NULL;
327 tstate->curexc_value = NULL;
328 tstate->curexc_traceback = NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000329}
330
331void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000332PyErr_Clear(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000333{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000334 PyErr_Restore(NULL, NULL, NULL);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000335}
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000336
Martin v. Löwisaa2efcb2012-04-19 14:33:43 +0200337void
338PyErr_GetExcInfo(PyObject **p_type, PyObject **p_value, PyObject **p_traceback)
339{
340 PyThreadState *tstate = PyThreadState_GET();
341
342 *p_type = tstate->exc_type;
343 *p_value = tstate->exc_value;
344 *p_traceback = tstate->exc_traceback;
345
346 Py_XINCREF(*p_type);
347 Py_XINCREF(*p_value);
348 Py_XINCREF(*p_traceback);
349}
350
351void
352PyErr_SetExcInfo(PyObject *p_type, PyObject *p_value, PyObject *p_traceback)
353{
354 PyObject *oldtype, *oldvalue, *oldtraceback;
355 PyThreadState *tstate = PyThreadState_GET();
356
357 oldtype = tstate->exc_type;
358 oldvalue = tstate->exc_value;
359 oldtraceback = tstate->exc_traceback;
360
361 tstate->exc_type = p_type;
362 tstate->exc_value = p_value;
363 tstate->exc_traceback = p_traceback;
364
365 Py_XDECREF(oldtype);
366 Py_XDECREF(oldvalue);
367 Py_XDECREF(oldtraceback);
368}
369
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000370/* Convenience functions to set a type error exception and return 0 */
371
372int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000373PyErr_BadArgument(void)
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000374{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000375 PyErr_SetString(PyExc_TypeError,
376 "bad argument type for built-in operation");
377 return 0;
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000378}
379
Guido van Rossum373c8691997-04-29 18:22:47 +0000380PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000381PyErr_NoMemory(void)
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000382{
Victor Stinnerf54a5742013-07-22 22:28:37 +0200383 if (Py_TYPE(PyExc_MemoryError) == NULL) {
384 /* PyErr_NoMemory() has been called before PyExc_MemoryError has been
385 initialized by _PyExc_Init() */
386 Py_FatalError("Out of memory and PyExc_MemoryError is not "
387 "initialized yet");
388 }
Antoine Pitrou07e20ef2010-10-28 22:56:58 +0000389 PyErr_SetNone(PyExc_MemoryError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000390 return NULL;
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000391}
392
Guido van Rossum373c8691997-04-29 18:22:47 +0000393PyObject *
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000394PyErr_SetFromErrnoWithFilenameObject(PyObject *exc, PyObject *filenameObject)
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000395{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000396 PyObject *message;
Antoine Pitrou5d6fbe82011-10-12 19:39:57 +0200397 PyObject *v, *args;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000398 int i = errno;
Victor Stinner1f33f2b2011-12-17 04:45:09 +0100399#ifdef MS_WINDOWS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000400 WCHAR *s_buf = NULL;
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000401#endif /* Unix/Windows */
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000402
Guido van Rossume9fbc091995-02-18 14:52:19 +0000403#ifdef EINTR
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000404 if (i == EINTR && PyErr_CheckSignals())
405 return NULL;
Guido van Rossume9fbc091995-02-18 14:52:19 +0000406#endif
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000407
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000408#ifndef MS_WINDOWS
Victor Stinner1f33f2b2011-12-17 04:45:09 +0100409 if (i != 0) {
410 char *s = strerror(i);
Victor Stinner1b579672011-12-17 05:47:23 +0100411 message = PyUnicode_DecodeLocale(s, "surrogateescape");
Victor Stinner1f33f2b2011-12-17 04:45:09 +0100412 }
413 else {
414 /* Sometimes errno didn't get set */
415 message = PyUnicode_FromString("Error");
416 }
Guido van Rossum743007d1999-04-21 15:27:31 +0000417#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000418 if (i == 0)
419 message = PyUnicode_FromString("Error"); /* Sometimes errno didn't get set */
420 else
421 {
422 /* Note that the Win32 errors do not lineup with the
423 errno error. So if the error is in the MSVC error
424 table, we use it, otherwise we assume it really _is_
425 a Win32 error code
426 */
427 if (i > 0 && i < _sys_nerr) {
428 message = PyUnicode_FromString(_sys_errlist[i]);
429 }
430 else {
431 int len = FormatMessageW(
432 FORMAT_MESSAGE_ALLOCATE_BUFFER |
433 FORMAT_MESSAGE_FROM_SYSTEM |
434 FORMAT_MESSAGE_IGNORE_INSERTS,
435 NULL, /* no message source */
436 i,
437 MAKELANGID(LANG_NEUTRAL,
438 SUBLANG_DEFAULT),
439 /* Default language */
440 (LPWSTR) &s_buf,
441 0, /* size not used */
442 NULL); /* no args */
443 if (len==0) {
444 /* Only ever seen this in out-of-mem
445 situations */
446 s_buf = NULL;
447 message = PyUnicode_FromFormat("Windows Error 0x%X", i);
448 } else {
449 /* remove trailing cr/lf and dots */
450 while (len > 0 && (s_buf[len-1] <= L' ' || s_buf[len-1] == L'.'))
451 s_buf[--len] = L'\0';
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200452 message = PyUnicode_FromWideChar(s_buf, len);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000453 }
454 }
455 }
Martin v. Löwis3484a182002-03-09 12:07:51 +0000456#endif /* Unix/Windows */
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000457
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000458 if (message == NULL)
459 {
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000460#ifdef MS_WINDOWS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000461 LocalFree(s_buf);
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000462#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000463 return NULL;
464 }
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000465
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000466 if (filenameObject != NULL)
Antoine Pitrou5d6fbe82011-10-12 19:39:57 +0200467 args = Py_BuildValue("(iOO)", i, message, filenameObject);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000468 else
Antoine Pitrou5d6fbe82011-10-12 19:39:57 +0200469 args = Py_BuildValue("(iO)", i, message);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000470 Py_DECREF(message);
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000471
Antoine Pitrou5d6fbe82011-10-12 19:39:57 +0200472 if (args != NULL) {
473 v = PyObject_Call(exc, args, NULL);
474 Py_DECREF(args);
475 if (v != NULL) {
476 PyErr_SetObject((PyObject *) Py_TYPE(v), v);
477 Py_DECREF(v);
478 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000479 }
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000480#ifdef MS_WINDOWS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000481 LocalFree(s_buf);
Guido van Rossum743007d1999-04-21 15:27:31 +0000482#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000483 return NULL;
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000484}
Guido van Rossum743007d1999-04-21 15:27:31 +0000485
Barry Warsaw97d95151998-07-23 16:05:56 +0000486
487PyObject *
Neal Norwitzb382b842007-08-24 20:00:37 +0000488PyErr_SetFromErrnoWithFilename(PyObject *exc, const char *filename)
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000489{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000490 PyObject *name = filename ? PyUnicode_DecodeFSDefault(filename) : NULL;
491 PyObject *result = PyErr_SetFromErrnoWithFilenameObject(exc, name);
492 Py_XDECREF(name);
493 return result;
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000494}
495
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +0000496#ifdef MS_WINDOWS
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000497PyObject *
Neal Norwitzb382b842007-08-24 20:00:37 +0000498PyErr_SetFromErrnoWithUnicodeFilename(PyObject *exc, const Py_UNICODE *filename)
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000499{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000500 PyObject *name = filename ?
501 PyUnicode_FromUnicode(filename, wcslen(filename)) :
502 NULL;
503 PyObject *result = PyErr_SetFromErrnoWithFilenameObject(exc, name);
504 Py_XDECREF(name);
505 return result;
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000506}
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +0000507#endif /* MS_WINDOWS */
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000508
509PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000510PyErr_SetFromErrno(PyObject *exc)
Barry Warsaw97d95151998-07-23 16:05:56 +0000511{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000512 return PyErr_SetFromErrnoWithFilenameObject(exc, NULL);
Barry Warsaw97d95151998-07-23 16:05:56 +0000513}
Guido van Rossum683a0721990-10-21 22:09:12 +0000514
Brett Cannonbf364092006-03-01 04:25:17 +0000515#ifdef MS_WINDOWS
Guido van Rossum795e1892000-02-17 15:19:15 +0000516/* Windows specific error code handling */
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000517PyObject *PyErr_SetExcFromWindowsErrWithFilenameObject(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000518 PyObject *exc,
519 int ierr,
520 PyObject *filenameObject)
Guido van Rossum795e1892000-02-17 15:19:15 +0000521{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000522 int len;
523 WCHAR *s_buf = NULL; /* Free via LocalFree */
524 PyObject *message;
Victor Stinner9ea8e4c2011-10-17 20:18:58 +0200525 PyObject *args, *v;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000526 DWORD err = (DWORD)ierr;
527 if (err==0) err = GetLastError();
528 len = FormatMessageW(
529 /* Error API error */
530 FORMAT_MESSAGE_ALLOCATE_BUFFER |
531 FORMAT_MESSAGE_FROM_SYSTEM |
532 FORMAT_MESSAGE_IGNORE_INSERTS,
533 NULL, /* no message source */
534 err,
535 MAKELANGID(LANG_NEUTRAL,
536 SUBLANG_DEFAULT), /* Default language */
537 (LPWSTR) &s_buf,
538 0, /* size not used */
539 NULL); /* no args */
540 if (len==0) {
541 /* Only seen this in out of mem situations */
542 message = PyUnicode_FromFormat("Windows Error 0x%X", err);
543 s_buf = NULL;
544 } else {
545 /* remove trailing cr/lf and dots */
546 while (len > 0 && (s_buf[len-1] <= L' ' || s_buf[len-1] == L'.'))
547 s_buf[--len] = L'\0';
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200548 message = PyUnicode_FromWideChar(s_buf, len);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000549 }
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000550
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000551 if (message == NULL)
552 {
553 LocalFree(s_buf);
554 return NULL;
555 }
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000556
Antoine Pitrou6b4883d2011-10-12 02:54:14 +0200557 if (filenameObject == NULL)
558 filenameObject = Py_None;
559 /* This is the constructor signature for passing a Windows error code.
560 The POSIX translation will be figured out by the constructor. */
Victor Stinner9ea8e4c2011-10-17 20:18:58 +0200561 args = Py_BuildValue("(iOOi)", 0, message, filenameObject, err);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000562 Py_DECREF(message);
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000563
Victor Stinner9ea8e4c2011-10-17 20:18:58 +0200564 if (args != NULL) {
565 v = PyObject_Call(exc, args, NULL);
566 Py_DECREF(args);
567 if (v != NULL) {
568 PyErr_SetObject((PyObject *) Py_TYPE(v), v);
569 Py_DECREF(v);
570 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000571 }
572 LocalFree(s_buf);
573 return NULL;
Guido van Rossum795e1892000-02-17 15:19:15 +0000574}
575
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000576PyObject *PyErr_SetExcFromWindowsErrWithFilename(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000577 PyObject *exc,
578 int ierr,
579 const char *filename)
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000580{
Victor Stinner92be9392010-12-28 00:28:21 +0000581 PyObject *name = filename ? PyUnicode_DecodeFSDefault(filename) : NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000582 PyObject *ret = PyErr_SetExcFromWindowsErrWithFilenameObject(exc,
583 ierr,
584 name);
585 Py_XDECREF(name);
586 return ret;
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000587}
588
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000589PyObject *PyErr_SetExcFromWindowsErrWithUnicodeFilename(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000590 PyObject *exc,
591 int ierr,
592 const Py_UNICODE *filename)
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000593{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000594 PyObject *name = filename ?
595 PyUnicode_FromUnicode(filename, wcslen(filename)) :
596 NULL;
597 PyObject *ret = PyErr_SetExcFromWindowsErrWithFilenameObject(exc,
598 ierr,
599 name);
600 Py_XDECREF(name);
601 return ret;
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000602}
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000603
Thomas Heller085358a2002-07-29 14:27:41 +0000604PyObject *PyErr_SetExcFromWindowsErr(PyObject *exc, int ierr)
605{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000606 return PyErr_SetExcFromWindowsErrWithFilename(exc, ierr, NULL);
Thomas Heller085358a2002-07-29 14:27:41 +0000607}
608
Guido van Rossum795e1892000-02-17 15:19:15 +0000609PyObject *PyErr_SetFromWindowsErr(int ierr)
610{
Andrew Svetlov2606a6f2012-12-19 14:33:35 +0200611 return PyErr_SetExcFromWindowsErrWithFilename(PyExc_OSError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000612 ierr, NULL);
Thomas Heller085358a2002-07-29 14:27:41 +0000613}
614PyObject *PyErr_SetFromWindowsErrWithFilename(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000615 int ierr,
616 const char *filename)
Thomas Heller085358a2002-07-29 14:27:41 +0000617{
Victor Stinner92be9392010-12-28 00:28:21 +0000618 PyObject *name = filename ? PyUnicode_DecodeFSDefault(filename) : NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000619 PyObject *result = PyErr_SetExcFromWindowsErrWithFilenameObject(
Andrew Svetlov2606a6f2012-12-19 14:33:35 +0200620 PyExc_OSError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000621 ierr, name);
622 Py_XDECREF(name);
623 return result;
Guido van Rossum795e1892000-02-17 15:19:15 +0000624}
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000625
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000626PyObject *PyErr_SetFromWindowsErrWithUnicodeFilename(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000627 int ierr,
628 const Py_UNICODE *filename)
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000629{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000630 PyObject *name = filename ?
631 PyUnicode_FromUnicode(filename, wcslen(filename)) :
632 NULL;
633 PyObject *result = PyErr_SetExcFromWindowsErrWithFilenameObject(
Andrew Svetlov2606a6f2012-12-19 14:33:35 +0200634 PyExc_OSError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000635 ierr, name);
636 Py_XDECREF(name);
637 return result;
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000638}
Guido van Rossum795e1892000-02-17 15:19:15 +0000639#endif /* MS_WINDOWS */
640
Brett Cannon79ec55e2012-04-12 20:24:54 -0400641PyObject *
Brett Cannon82da8882013-07-04 17:48:16 -0400642PyErr_SetImportError(PyObject *msg, PyObject *name, PyObject *path)
Brett Cannon79ec55e2012-04-12 20:24:54 -0400643{
Brian Curtin09b86d12012-04-17 16:57:09 -0500644 PyObject *args, *kwargs, *error;
645
Brett Cannon82da8882013-07-04 17:48:16 -0400646 if (msg == NULL)
Brian Curtin94c001b2012-04-18 08:30:51 -0500647 return NULL;
648
Antoine Pitrouec9bac42012-04-18 16:57:54 +0200649 args = PyTuple_New(1);
Brian Curtin09b86d12012-04-17 16:57:09 -0500650 if (args == NULL)
651 return NULL;
652
653 kwargs = PyDict_New();
Benjamin Petersonda20cd22012-04-18 10:48:00 -0400654 if (kwargs == NULL) {
655 Py_DECREF(args);
Brian Curtin09b86d12012-04-17 16:57:09 -0500656 return NULL;
Benjamin Petersonda20cd22012-04-18 10:48:00 -0400657 }
Brian Curtin09b86d12012-04-17 16:57:09 -0500658
Brian Curtin94c001b2012-04-18 08:30:51 -0500659 if (name == NULL) {
Brian Curtin09b86d12012-04-17 16:57:09 -0500660 name = Py_None;
Brian Curtin94c001b2012-04-18 08:30:51 -0500661 }
Brian Curtin09b86d12012-04-17 16:57:09 -0500662
Brian Curtin94c001b2012-04-18 08:30:51 -0500663 if (path == NULL) {
Brian Curtin09b86d12012-04-17 16:57:09 -0500664 path = Py_None;
Brian Curtin94c001b2012-04-18 08:30:51 -0500665 }
Brian Curtin09b86d12012-04-17 16:57:09 -0500666
667 Py_INCREF(msg);
Benjamin Petersonda20cd22012-04-18 10:48:00 -0400668 PyTuple_SET_ITEM(args, 0, msg);
Victor Stinner479054b2013-07-17 21:54:25 +0200669
670 if (PyDict_SetItemString(kwargs, "name", name) < 0)
671 return NULL;
672 if (PyDict_SetItemString(kwargs, "path", path) < 0)
673 return NULL;
Brett Cannon79ec55e2012-04-12 20:24:54 -0400674
Brett Cannon82da8882013-07-04 17:48:16 -0400675 error = PyObject_Call(PyExc_ImportError, args, kwargs);
Benjamin Petersonda20cd22012-04-18 10:48:00 -0400676 if (error != NULL) {
677 PyErr_SetObject((PyObject *)Py_TYPE(error), error);
Brian Curtin09b86d12012-04-17 16:57:09 -0500678 Py_DECREF(error);
Brett Cannon79ec55e2012-04-12 20:24:54 -0400679 }
680
Brett Cannon79ec55e2012-04-12 20:24:54 -0400681 Py_DECREF(args);
682 Py_DECREF(kwargs);
683
Brian Curtin09b86d12012-04-17 16:57:09 -0500684 return NULL;
Brett Cannon79ec55e2012-04-12 20:24:54 -0400685}
686
Guido van Rossum683a0721990-10-21 22:09:12 +0000687void
Neal Norwitzb382b842007-08-24 20:00:37 +0000688_PyErr_BadInternalCall(const char *filename, int lineno)
Fred Drake6d63adf2000-08-24 22:38:39 +0000689{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000690 PyErr_Format(PyExc_SystemError,
691 "%s:%d: bad argument to internal function",
692 filename, lineno);
Fred Drake6d63adf2000-08-24 22:38:39 +0000693}
694
695/* Remove the preprocessor macro for PyErr_BadInternalCall() so that we can
696 export the entry point for existing object code: */
697#undef PyErr_BadInternalCall
698void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000699PyErr_BadInternalCall(void)
Guido van Rossum683a0721990-10-21 22:09:12 +0000700{
Victor Stinnerfb3a6302013-07-12 00:37:30 +0200701 assert(0 && "bad argument to internal function");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000702 PyErr_Format(PyExc_SystemError,
703 "bad argument to internal function");
Guido van Rossum683a0721990-10-21 22:09:12 +0000704}
Fred Drake6d63adf2000-08-24 22:38:39 +0000705#define PyErr_BadInternalCall() _PyErr_BadInternalCall(__FILE__, __LINE__)
706
Guido van Rossum1548bac1997-02-14 17:09:47 +0000707
708
Guido van Rossum1548bac1997-02-14 17:09:47 +0000709PyObject *
710PyErr_Format(PyObject *exception, const char *format, ...)
Guido van Rossum1548bac1997-02-14 17:09:47 +0000711{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000712 va_list vargs;
713 PyObject* string;
Guido van Rossum1548bac1997-02-14 17:09:47 +0000714
Jeremy Hyltonb69a27e2000-09-01 03:49:47 +0000715#ifdef HAVE_STDARG_PROTOTYPES
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000716 va_start(vargs, format);
Jeremy Hyltonb69a27e2000-09-01 03:49:47 +0000717#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000718 va_start(vargs);
Jeremy Hyltonb69a27e2000-09-01 03:49:47 +0000719#endif
Guido van Rossum1548bac1997-02-14 17:09:47 +0000720
Victor Stinnerace47d72013-07-18 01:41:08 +0200721#ifdef Py_DEBUG
722 /* in debug mode, PyEval_EvalFrameEx() fails with an assertion error
723 if an exception is set when it is called */
724 PyErr_Clear();
725#endif
726
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000727 string = PyUnicode_FromFormatV(format, vargs);
728 PyErr_SetObject(exception, string);
729 Py_XDECREF(string);
730 va_end(vargs);
731 return NULL;
Guido van Rossum1548bac1997-02-14 17:09:47 +0000732}
Guido van Rossum7617e051997-09-16 18:43:50 +0000733
734
Thomas Wouters477c8d52006-05-27 19:21:47 +0000735
Guido van Rossum7617e051997-09-16 18:43:50 +0000736PyObject *
Neal Norwitzb382b842007-08-24 20:00:37 +0000737PyErr_NewException(const char *name, PyObject *base, PyObject *dict)
Guido van Rossum7617e051997-09-16 18:43:50 +0000738{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000739 const char *dot;
740 PyObject *modulename = NULL;
741 PyObject *classname = NULL;
742 PyObject *mydict = NULL;
743 PyObject *bases = NULL;
744 PyObject *result = NULL;
745 dot = strrchr(name, '.');
746 if (dot == NULL) {
747 PyErr_SetString(PyExc_SystemError,
748 "PyErr_NewException: name must be module.class");
749 return NULL;
750 }
751 if (base == NULL)
752 base = PyExc_Exception;
753 if (dict == NULL) {
754 dict = mydict = PyDict_New();
755 if (dict == NULL)
756 goto failure;
757 }
758 if (PyDict_GetItemString(dict, "__module__") == NULL) {
759 modulename = PyUnicode_FromStringAndSize(name,
760 (Py_ssize_t)(dot-name));
761 if (modulename == NULL)
762 goto failure;
763 if (PyDict_SetItemString(dict, "__module__", modulename) != 0)
764 goto failure;
765 }
766 if (PyTuple_Check(base)) {
767 bases = base;
768 /* INCREF as we create a new ref in the else branch */
769 Py_INCREF(bases);
770 } else {
771 bases = PyTuple_Pack(1, base);
772 if (bases == NULL)
773 goto failure;
774 }
Florent Xiclunaaa6c1d22011-12-12 18:54:29 +0100775 /* Create a real class. */
Victor Stinner7eeb5b52010-06-07 19:57:46 +0000776 result = PyObject_CallFunction((PyObject *)&PyType_Type, "sOO",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000777 dot+1, bases, dict);
Guido van Rossum7617e051997-09-16 18:43:50 +0000778 failure:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000779 Py_XDECREF(bases);
780 Py_XDECREF(mydict);
781 Py_XDECREF(classname);
782 Py_XDECREF(modulename);
783 return result;
Guido van Rossum7617e051997-09-16 18:43:50 +0000784}
Jeremy Hyltonb709df32000-09-01 02:47:25 +0000785
Georg Brandl1e28a272009-12-28 08:41:01 +0000786
787/* Create an exception with docstring */
788PyObject *
789PyErr_NewExceptionWithDoc(const char *name, const char *doc,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000790 PyObject *base, PyObject *dict)
Georg Brandl1e28a272009-12-28 08:41:01 +0000791{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000792 int result;
793 PyObject *ret = NULL;
794 PyObject *mydict = NULL; /* points to the dict only if we create it */
795 PyObject *docobj;
Georg Brandl1e28a272009-12-28 08:41:01 +0000796
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000797 if (dict == NULL) {
798 dict = mydict = PyDict_New();
799 if (dict == NULL) {
800 return NULL;
801 }
802 }
Georg Brandl1e28a272009-12-28 08:41:01 +0000803
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000804 if (doc != NULL) {
805 docobj = PyUnicode_FromString(doc);
806 if (docobj == NULL)
807 goto failure;
808 result = PyDict_SetItemString(dict, "__doc__", docobj);
809 Py_DECREF(docobj);
810 if (result < 0)
811 goto failure;
812 }
Georg Brandl1e28a272009-12-28 08:41:01 +0000813
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000814 ret = PyErr_NewException(name, base, dict);
Georg Brandl1e28a272009-12-28 08:41:01 +0000815 failure:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000816 Py_XDECREF(mydict);
817 return ret;
Georg Brandl1e28a272009-12-28 08:41:01 +0000818}
819
820
Jeremy Hyltonb709df32000-09-01 02:47:25 +0000821/* Call when an exception has occurred but there is no way for Python
822 to handle it. Examples: exception in __del__ or during GC. */
823void
824PyErr_WriteUnraisable(PyObject *obj)
825{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200826 _Py_IDENTIFIER(__module__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000827 PyObject *f, *t, *v, *tb;
Victor Stinnerc82bfd82013-08-26 14:04:10 +0200828 PyObject *moduleName = NULL;
829 char* className;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000830
Victor Stinnerc82bfd82013-08-26 14:04:10 +0200831 PyErr_Fetch(&t, &v, &tb);
832
833 f = PySys_GetObject("stderr");
834 if (f == NULL || f == Py_None)
835 goto done;
836
837 if (obj) {
838 if (PyFile_WriteString("Exception ignored in: ", f) < 0)
839 goto done;
840 if (PyFile_WriteObject(obj, f, 0) < 0)
841 goto done;
842 if (PyFile_WriteString("\n", f) < 0)
843 goto done;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000844 }
Victor Stinnerc82bfd82013-08-26 14:04:10 +0200845
846 if (PyTraceBack_Print(tb, f) < 0)
847 goto done;
848
849 if (!t)
850 goto done;
851
852 assert(PyExceptionClass_Check(t));
853 className = PyExceptionClass_Name(t);
854 if (className != NULL) {
855 char *dot = strrchr(className, '.');
856 if (dot != NULL)
857 className = dot+1;
858 }
859
860 moduleName = _PyObject_GetAttrId(t, &PyId___module__);
861 if (moduleName == NULL) {
862 PyErr_Clear();
863 if (PyFile_WriteString("<unknown>", f) < 0)
864 goto done;
865 }
866 else {
867 if (PyUnicode_CompareWithASCIIString(moduleName, "builtins") != 0) {
868 if (PyFile_WriteObject(moduleName, f, Py_PRINT_RAW) < 0)
869 goto done;
870 if (PyFile_WriteString(".", f) < 0)
871 goto done;
872 }
873 }
874 if (className == NULL) {
875 if (PyFile_WriteString("<unknown>", f) < 0)
876 goto done;
877 }
878 else {
879 if (PyFile_WriteString(className, f) < 0)
880 goto done;
881 }
882
883 if (v && v != Py_None) {
884 if (PyFile_WriteString(": ", f) < 0)
885 goto done;
886 if (PyFile_WriteObject(v, f, Py_PRINT_RAW) < 0)
887 goto done;
888 }
889 if (PyFile_WriteString("\n", f) < 0)
890 goto done;
891
892done:
893 Py_XDECREF(moduleName);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000894 Py_XDECREF(t);
895 Py_XDECREF(v);
896 Py_XDECREF(tb);
Victor Stinnerc82bfd82013-08-26 14:04:10 +0200897 PyErr_Clear(); /* Just in case */
Jeremy Hyltonb709df32000-09-01 02:47:25 +0000898}
Guido van Rossumcfd42b52000-12-15 21:58:52 +0000899
Armin Rigo092381a2003-10-25 14:29:27 +0000900extern PyObject *PyModule_GetWarningsModule(void);
Guido van Rossumcfd42b52000-12-15 21:58:52 +0000901
Guido van Rossum2fd45652001-02-28 21:46:24 +0000902
Benjamin Peterson2c539712010-09-20 22:42:10 +0000903void
904PyErr_SyntaxLocation(const char *filename, int lineno) {
905 PyErr_SyntaxLocationEx(filename, lineno, -1);
906}
907
908
Martin v. Löwiscfeb3b62002-03-03 21:30:27 +0000909/* Set file and line information for the current exception.
910 If the exception is not a SyntaxError, also sets additional attributes
911 to make printing of exceptions believe it is a syntax error. */
Guido van Rossum2fd45652001-02-28 21:46:24 +0000912
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000913void
Benjamin Peterson2c539712010-09-20 22:42:10 +0000914PyErr_SyntaxLocationEx(const char *filename, int lineno, int col_offset)
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000915{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000916 PyObject *exc, *v, *tb, *tmp;
Martin v. Löwis1c67dd92011-10-14 15:16:45 +0200917 _Py_IDENTIFIER(filename);
918 _Py_IDENTIFIER(lineno);
919 _Py_IDENTIFIER(msg);
920 _Py_IDENTIFIER(offset);
921 _Py_IDENTIFIER(print_file_and_line);
922 _Py_IDENTIFIER(text);
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000923
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000924 /* add attributes for the line number and filename for the error */
925 PyErr_Fetch(&exc, &v, &tb);
926 PyErr_NormalizeException(&exc, &v, &tb);
927 /* XXX check that it is, indeed, a syntax error. It might not
928 * be, though. */
929 tmp = PyLong_FromLong(lineno);
930 if (tmp == NULL)
931 PyErr_Clear();
932 else {
Martin v. Löwis1c67dd92011-10-14 15:16:45 +0200933 if (_PyObject_SetAttrId(v, &PyId_lineno, tmp))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000934 PyErr_Clear();
935 Py_DECREF(tmp);
936 }
Benjamin Peterson2c539712010-09-20 22:42:10 +0000937 if (col_offset >= 0) {
938 tmp = PyLong_FromLong(col_offset);
939 if (tmp == NULL)
940 PyErr_Clear();
941 else {
Martin v. Löwis1c67dd92011-10-14 15:16:45 +0200942 if (_PyObject_SetAttrId(v, &PyId_offset, tmp))
Benjamin Peterson2c539712010-09-20 22:42:10 +0000943 PyErr_Clear();
944 Py_DECREF(tmp);
945 }
946 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000947 if (filename != NULL) {
Victor Stinner15a71cd2010-10-17 19:03:16 +0000948 tmp = PyUnicode_DecodeFSDefault(filename);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000949 if (tmp == NULL)
950 PyErr_Clear();
951 else {
Martin v. Löwis1c67dd92011-10-14 15:16:45 +0200952 if (_PyObject_SetAttrId(v, &PyId_filename, tmp))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000953 PyErr_Clear();
954 Py_DECREF(tmp);
955 }
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000956
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000957 tmp = PyErr_ProgramText(filename, lineno);
958 if (tmp) {
Martin v. Löwis1c67dd92011-10-14 15:16:45 +0200959 if (_PyObject_SetAttrId(v, &PyId_text, tmp))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000960 PyErr_Clear();
961 Py_DECREF(tmp);
962 }
963 }
Martin v. Löwis1c67dd92011-10-14 15:16:45 +0200964 if (_PyObject_SetAttrId(v, &PyId_offset, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000965 PyErr_Clear();
966 }
967 if (exc != PyExc_SyntaxError) {
Martin v. Löwis1c67dd92011-10-14 15:16:45 +0200968 if (!_PyObject_HasAttrId(v, &PyId_msg)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000969 tmp = PyObject_Str(v);
970 if (tmp) {
Martin v. Löwis1c67dd92011-10-14 15:16:45 +0200971 if (_PyObject_SetAttrId(v, &PyId_msg, tmp))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000972 PyErr_Clear();
973 Py_DECREF(tmp);
974 } else {
975 PyErr_Clear();
976 }
977 }
Martin v. Löwis1c67dd92011-10-14 15:16:45 +0200978 if (!_PyObject_HasAttrId(v, &PyId_print_file_and_line)) {
979 if (_PyObject_SetAttrId(v, &PyId_print_file_and_line,
980 Py_None))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000981 PyErr_Clear();
982 }
983 }
984 PyErr_Restore(exc, v, tb);
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000985}
986
Guido van Rossumebe8f8a2007-10-10 18:53:36 +0000987/* Attempt to load the line of text that the exception refers to. If it
988 fails, it will return NULL but will not set an exception.
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000989
990 XXX The functionality of this function is quite similar to the
Guido van Rossumebe8f8a2007-10-10 18:53:36 +0000991 functionality in tb_displayline() in traceback.c. */
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000992
993PyObject *
Martin v. Löwis95292d62002-12-11 14:04:59 +0000994PyErr_ProgramText(const char *filename, int lineno)
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000995{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000996 FILE *fp;
997 int i;
998 char linebuf[1000];
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000999
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001000 if (filename == NULL || *filename == '\0' || lineno <= 0)
1001 return NULL;
1002 fp = fopen(filename, "r" PY_STDIOTEXTMODE);
1003 if (fp == NULL)
1004 return NULL;
1005 for (i = 0; i < lineno; i++) {
1006 char *pLastChar = &linebuf[sizeof(linebuf) - 2];
1007 do {
1008 *pLastChar = '\0';
1009 if (Py_UniversalNewlineFgets(linebuf, sizeof linebuf,
1010 fp, NULL) == NULL)
1011 break;
1012 /* fgets read *something*; if it didn't get as
1013 far as pLastChar, it must have found a newline
1014 or hit the end of the file; if pLastChar is \n,
1015 it obviously found a newline; else we haven't
1016 yet seen a newline, so must continue */
1017 } while (*pLastChar != '\0' && *pLastChar != '\n');
1018 }
1019 fclose(fp);
1020 if (i == lineno) {
1021 char *p = linebuf;
1022 PyObject *res;
1023 while (*p == ' ' || *p == '\t' || *p == '\014')
1024 p++;
1025 res = PyUnicode_FromString(p);
1026 if (res == NULL)
1027 PyErr_Clear();
1028 return res;
1029 }
1030 return NULL;
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +00001031}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001032
1033#ifdef __cplusplus
1034}
1035#endif