blob: 53dd9a9650d0c15b5378a5a959fa8e1664672e98 [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;
74 if (value == NULL || value == Py_None)
75 args = PyTuple_New(0);
76 else if (PyTuple_Check(value)) {
77 Py_INCREF(value);
78 args = value;
79 }
80 else
81 args = PyTuple_Pack(1, value);
82 fixed_value = args ?
83 PyEval_CallObject(exception, args) : NULL;
84 Py_XDECREF(args);
85 Py_XDECREF(value);
86 if (fixed_value == NULL)
87 return;
88 value = fixed_value;
89 }
90 /* Avoid reference cycles through the context chain.
91 This is O(chain length) but context chains are
92 usually very short. Sensitive readers may try
93 to inline the call to PyException_GetContext. */
94 if (exc_value != value) {
95 PyObject *o = exc_value, *context;
96 while ((context = PyException_GetContext(o))) {
97 Py_DECREF(context);
98 if (context == value) {
99 PyException_SetContext(o, NULL);
100 break;
101 }
102 o = context;
103 }
104 PyException_SetContext(value, exc_value);
105 } else {
106 Py_DECREF(exc_value);
107 }
108 }
109 if (value != NULL && PyExceptionInstance_Check(value))
110 tb = PyException_GetTraceback(value);
111 Py_XINCREF(exception);
112 PyErr_Restore(exception, value, tb);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000113}
114
115void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000116PyErr_SetNone(PyObject *exception)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000117{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000118 PyErr_SetObject(exception, (PyObject *)NULL);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000119}
120
121void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000122PyErr_SetString(PyObject *exception, const char *string)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000123{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000124 PyObject *value = PyUnicode_FromString(string);
125 PyErr_SetObject(exception, value);
126 Py_XDECREF(value);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000127}
128
Guido van Rossum3a241811994-08-29 12:14:12 +0000129
Guido van Rossum373c8691997-04-29 18:22:47 +0000130PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000131PyErr_Occurred(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000132{
Jeffrey Yasskin8e0bdfd2010-05-13 18:31:05 +0000133 /* If there is no thread state, PyThreadState_GET calls
134 Py_FatalError, which calls PyErr_Occurred. To avoid the
135 resulting infinite loop, we inline PyThreadState_GET here and
136 treat no thread as no error. */
137 PyThreadState *tstate =
138 ((PyThreadState*)_Py_atomic_load_relaxed(&_PyThreadState_Current));
Guido van Rossuma027efa1997-05-05 20:56:21 +0000139
Jeffrey Yasskin8e0bdfd2010-05-13 18:31:05 +0000140 return tstate == NULL ? NULL : tstate->curexc_type;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000141}
142
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000143
144int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000145PyErr_GivenExceptionMatches(PyObject *err, PyObject *exc)
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000146{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000147 if (err == NULL || exc == NULL) {
148 /* maybe caused by "import exceptions" that failed early on */
149 return 0;
150 }
151 if (PyTuple_Check(exc)) {
152 Py_ssize_t i, n;
153 n = PyTuple_Size(exc);
154 for (i = 0; i < n; i++) {
155 /* Test recursively */
156 if (PyErr_GivenExceptionMatches(
157 err, PyTuple_GET_ITEM(exc, i)))
158 {
159 return 1;
160 }
161 }
162 return 0;
163 }
164 /* err might be an instance, so check its class. */
165 if (PyExceptionInstance_Check(err))
166 err = PyExceptionInstance_Class(err);
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000167
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000168 if (PyExceptionClass_Check(err) && PyExceptionClass_Check(exc)) {
169 int res = 0;
170 PyObject *exception, *value, *tb;
171 PyErr_Fetch(&exception, &value, &tb);
172 /* PyObject_IsSubclass() can recurse and therefore is
173 not safe (see test_bad_getattr in test.pickletester). */
174 res = PyType_IsSubtype((PyTypeObject *)err, (PyTypeObject *)exc);
175 /* This function must not fail, so print the error here */
176 if (res == -1) {
177 PyErr_WriteUnraisable(err);
178 res = 0;
179 }
180 PyErr_Restore(exception, value, tb);
181 return res;
182 }
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000183
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000184 return err == exc;
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000185}
Guido van Rossum743007d1999-04-21 15:27:31 +0000186
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000187
188int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000189PyErr_ExceptionMatches(PyObject *exc)
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000190{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000191 return PyErr_GivenExceptionMatches(PyErr_Occurred(), exc);
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000192}
193
194
195/* Used in many places to normalize a raised exception, including in
196 eval_code2(), do_raise(), and PyErr_Print()
Benjamin Petersone6528212008-07-15 15:32:09 +0000197
198 XXX: should PyErr_NormalizeException() also call
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000199 PyException_SetTraceback() with the resulting value and tb?
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000200*/
201void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000202PyErr_NormalizeException(PyObject **exc, PyObject **val, PyObject **tb)
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000203{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000204 PyObject *type = *exc;
205 PyObject *value = *val;
206 PyObject *inclass = NULL;
207 PyObject *initial_tb = NULL;
208 PyThreadState *tstate = NULL;
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000209
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000210 if (type == NULL) {
211 /* There was no exception, so nothing to do. */
212 return;
213 }
Guido van Rossumed473a42000-08-07 19:18:27 +0000214
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000215 /* If PyErr_SetNone() was used, the value will have been actually
216 set to NULL.
217 */
218 if (!value) {
219 value = Py_None;
220 Py_INCREF(value);
221 }
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000222
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000223 if (PyExceptionInstance_Check(value))
224 inclass = PyExceptionInstance_Class(value);
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000225
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000226 /* Normalize the exception so that if the type is a class, the
227 value will be an instance.
228 */
229 if (PyExceptionClass_Check(type)) {
Victor Stinner74a7fa62013-07-17 00:44:53 +0200230 int is_subclass;
231 if (inclass) {
232 is_subclass = PyObject_IsSubclass(inclass, type);
233 if (is_subclass < 0)
234 goto finally;
235 }
236 else
237 is_subclass = 0;
238
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000239 /* if the value was not an instance, or is not an instance
240 whose class is (or is derived from) type, then use the
241 value as an argument to instantiation of the type
242 class.
243 */
Victor Stinner74a7fa62013-07-17 00:44:53 +0200244 if (!inclass || !is_subclass) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000245 PyObject *args, *res;
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000246
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000247 if (value == Py_None)
248 args = PyTuple_New(0);
249 else if (PyTuple_Check(value)) {
250 Py_INCREF(value);
251 args = value;
252 }
253 else
254 args = PyTuple_Pack(1, value);
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000255
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000256 if (args == NULL)
257 goto finally;
258 res = PyEval_CallObject(type, args);
259 Py_DECREF(args);
260 if (res == NULL)
261 goto finally;
262 Py_DECREF(value);
263 value = res;
264 }
265 /* if the class of the instance doesn't exactly match the
266 class of the type, believe the instance
267 */
268 else if (inclass != type) {
269 Py_DECREF(type);
270 type = inclass;
271 Py_INCREF(type);
272 }
273 }
274 *exc = type;
275 *val = value;
276 return;
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000277finally:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000278 Py_DECREF(type);
279 Py_DECREF(value);
280 /* If the new exception doesn't set a traceback and the old
281 exception had a traceback, use the old traceback for the
282 new exception. It's better than nothing.
283 */
284 initial_tb = *tb;
285 PyErr_Fetch(exc, val, tb);
286 if (initial_tb != NULL) {
287 if (*tb == NULL)
288 *tb = initial_tb;
289 else
290 Py_DECREF(initial_tb);
291 }
292 /* normalize recursively */
293 tstate = PyThreadState_GET();
294 if (++tstate->recursion_depth > Py_GetRecursionLimit()) {
295 --tstate->recursion_depth;
296 /* throw away the old exception... */
297 Py_DECREF(*exc);
298 Py_DECREF(*val);
299 /* ... and use the recursion error instead */
300 *exc = PyExc_RuntimeError;
301 *val = PyExc_RecursionErrorInst;
302 Py_INCREF(*exc);
303 Py_INCREF(*val);
304 /* just keeping the old traceback */
305 return;
306 }
307 PyErr_NormalizeException(exc, val, tb);
308 --tstate->recursion_depth;
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000309}
310
311
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000312void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000313PyErr_Fetch(PyObject **p_type, PyObject **p_value, PyObject **p_traceback)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000314{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000315 PyThreadState *tstate = PyThreadState_GET();
Guido van Rossuma027efa1997-05-05 20:56:21 +0000316
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000317 *p_type = tstate->curexc_type;
318 *p_value = tstate->curexc_value;
319 *p_traceback = tstate->curexc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000320
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000321 tstate->curexc_type = NULL;
322 tstate->curexc_value = NULL;
323 tstate->curexc_traceback = NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000324}
325
326void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000327PyErr_Clear(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000328{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000329 PyErr_Restore(NULL, NULL, NULL);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000330}
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000331
Martin v. Löwisaa2efcb2012-04-19 14:33:43 +0200332void
333PyErr_GetExcInfo(PyObject **p_type, PyObject **p_value, PyObject **p_traceback)
334{
335 PyThreadState *tstate = PyThreadState_GET();
336
337 *p_type = tstate->exc_type;
338 *p_value = tstate->exc_value;
339 *p_traceback = tstate->exc_traceback;
340
341 Py_XINCREF(*p_type);
342 Py_XINCREF(*p_value);
343 Py_XINCREF(*p_traceback);
344}
345
346void
347PyErr_SetExcInfo(PyObject *p_type, PyObject *p_value, PyObject *p_traceback)
348{
349 PyObject *oldtype, *oldvalue, *oldtraceback;
350 PyThreadState *tstate = PyThreadState_GET();
351
352 oldtype = tstate->exc_type;
353 oldvalue = tstate->exc_value;
354 oldtraceback = tstate->exc_traceback;
355
356 tstate->exc_type = p_type;
357 tstate->exc_value = p_value;
358 tstate->exc_traceback = p_traceback;
359
360 Py_XDECREF(oldtype);
361 Py_XDECREF(oldvalue);
362 Py_XDECREF(oldtraceback);
363}
364
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000365/* Convenience functions to set a type error exception and return 0 */
366
367int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000368PyErr_BadArgument(void)
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000369{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000370 PyErr_SetString(PyExc_TypeError,
371 "bad argument type for built-in operation");
372 return 0;
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000373}
374
Guido van Rossum373c8691997-04-29 18:22:47 +0000375PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000376PyErr_NoMemory(void)
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000377{
Antoine Pitrou07e20ef2010-10-28 22:56:58 +0000378 PyErr_SetNone(PyExc_MemoryError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000379 return NULL;
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000380}
381
Guido van Rossum373c8691997-04-29 18:22:47 +0000382PyObject *
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000383PyErr_SetFromErrnoWithFilenameObject(PyObject *exc, PyObject *filenameObject)
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000384{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000385 PyObject *message;
Antoine Pitrou5d6fbe82011-10-12 19:39:57 +0200386 PyObject *v, *args;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000387 int i = errno;
Victor Stinner1f33f2b2011-12-17 04:45:09 +0100388#ifdef MS_WINDOWS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000389 WCHAR *s_buf = NULL;
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000390#endif /* Unix/Windows */
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000391
Guido van Rossume9fbc091995-02-18 14:52:19 +0000392#ifdef EINTR
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000393 if (i == EINTR && PyErr_CheckSignals())
394 return NULL;
Guido van Rossume9fbc091995-02-18 14:52:19 +0000395#endif
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000396
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000397#ifndef MS_WINDOWS
Victor Stinner1f33f2b2011-12-17 04:45:09 +0100398 if (i != 0) {
399 char *s = strerror(i);
Victor Stinner1b579672011-12-17 05:47:23 +0100400 message = PyUnicode_DecodeLocale(s, "surrogateescape");
Victor Stinner1f33f2b2011-12-17 04:45:09 +0100401 }
402 else {
403 /* Sometimes errno didn't get set */
404 message = PyUnicode_FromString("Error");
405 }
Guido van Rossum743007d1999-04-21 15:27:31 +0000406#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000407 if (i == 0)
408 message = PyUnicode_FromString("Error"); /* Sometimes errno didn't get set */
409 else
410 {
411 /* Note that the Win32 errors do not lineup with the
412 errno error. So if the error is in the MSVC error
413 table, we use it, otherwise we assume it really _is_
414 a Win32 error code
415 */
416 if (i > 0 && i < _sys_nerr) {
417 message = PyUnicode_FromString(_sys_errlist[i]);
418 }
419 else {
420 int len = FormatMessageW(
421 FORMAT_MESSAGE_ALLOCATE_BUFFER |
422 FORMAT_MESSAGE_FROM_SYSTEM |
423 FORMAT_MESSAGE_IGNORE_INSERTS,
424 NULL, /* no message source */
425 i,
426 MAKELANGID(LANG_NEUTRAL,
427 SUBLANG_DEFAULT),
428 /* Default language */
429 (LPWSTR) &s_buf,
430 0, /* size not used */
431 NULL); /* no args */
432 if (len==0) {
433 /* Only ever seen this in out-of-mem
434 situations */
435 s_buf = NULL;
436 message = PyUnicode_FromFormat("Windows Error 0x%X", i);
437 } else {
438 /* remove trailing cr/lf and dots */
439 while (len > 0 && (s_buf[len-1] <= L' ' || s_buf[len-1] == L'.'))
440 s_buf[--len] = L'\0';
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200441 message = PyUnicode_FromWideChar(s_buf, len);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000442 }
443 }
444 }
Martin v. Löwis3484a182002-03-09 12:07:51 +0000445#endif /* Unix/Windows */
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000446
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000447 if (message == NULL)
448 {
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000449#ifdef MS_WINDOWS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000450 LocalFree(s_buf);
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000451#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000452 return NULL;
453 }
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000454
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000455 if (filenameObject != NULL)
Antoine Pitrou5d6fbe82011-10-12 19:39:57 +0200456 args = Py_BuildValue("(iOO)", i, message, filenameObject);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000457 else
Antoine Pitrou5d6fbe82011-10-12 19:39:57 +0200458 args = Py_BuildValue("(iO)", i, message);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000459 Py_DECREF(message);
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000460
Antoine Pitrou5d6fbe82011-10-12 19:39:57 +0200461 if (args != NULL) {
462 v = PyObject_Call(exc, args, NULL);
463 Py_DECREF(args);
464 if (v != NULL) {
465 PyErr_SetObject((PyObject *) Py_TYPE(v), v);
466 Py_DECREF(v);
467 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000468 }
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000469#ifdef MS_WINDOWS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000470 LocalFree(s_buf);
Guido van Rossum743007d1999-04-21 15:27:31 +0000471#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000472 return NULL;
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000473}
Guido van Rossum743007d1999-04-21 15:27:31 +0000474
Barry Warsaw97d95151998-07-23 16:05:56 +0000475
476PyObject *
Neal Norwitzb382b842007-08-24 20:00:37 +0000477PyErr_SetFromErrnoWithFilename(PyObject *exc, const char *filename)
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000478{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000479 PyObject *name = filename ? PyUnicode_DecodeFSDefault(filename) : NULL;
480 PyObject *result = PyErr_SetFromErrnoWithFilenameObject(exc, name);
481 Py_XDECREF(name);
482 return result;
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000483}
484
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +0000485#ifdef MS_WINDOWS
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000486PyObject *
Neal Norwitzb382b842007-08-24 20:00:37 +0000487PyErr_SetFromErrnoWithUnicodeFilename(PyObject *exc, const Py_UNICODE *filename)
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000488{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000489 PyObject *name = filename ?
490 PyUnicode_FromUnicode(filename, wcslen(filename)) :
491 NULL;
492 PyObject *result = PyErr_SetFromErrnoWithFilenameObject(exc, name);
493 Py_XDECREF(name);
494 return result;
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000495}
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +0000496#endif /* MS_WINDOWS */
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000497
498PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000499PyErr_SetFromErrno(PyObject *exc)
Barry Warsaw97d95151998-07-23 16:05:56 +0000500{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000501 return PyErr_SetFromErrnoWithFilenameObject(exc, NULL);
Barry Warsaw97d95151998-07-23 16:05:56 +0000502}
Guido van Rossum683a0721990-10-21 22:09:12 +0000503
Brett Cannonbf364092006-03-01 04:25:17 +0000504#ifdef MS_WINDOWS
Guido van Rossum795e1892000-02-17 15:19:15 +0000505/* Windows specific error code handling */
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000506PyObject *PyErr_SetExcFromWindowsErrWithFilenameObject(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000507 PyObject *exc,
508 int ierr,
509 PyObject *filenameObject)
Guido van Rossum795e1892000-02-17 15:19:15 +0000510{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000511 int len;
512 WCHAR *s_buf = NULL; /* Free via LocalFree */
513 PyObject *message;
Victor Stinner9ea8e4c2011-10-17 20:18:58 +0200514 PyObject *args, *v;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000515 DWORD err = (DWORD)ierr;
516 if (err==0) err = GetLastError();
517 len = FormatMessageW(
518 /* Error API error */
519 FORMAT_MESSAGE_ALLOCATE_BUFFER |
520 FORMAT_MESSAGE_FROM_SYSTEM |
521 FORMAT_MESSAGE_IGNORE_INSERTS,
522 NULL, /* no message source */
523 err,
524 MAKELANGID(LANG_NEUTRAL,
525 SUBLANG_DEFAULT), /* Default language */
526 (LPWSTR) &s_buf,
527 0, /* size not used */
528 NULL); /* no args */
529 if (len==0) {
530 /* Only seen this in out of mem situations */
531 message = PyUnicode_FromFormat("Windows Error 0x%X", err);
532 s_buf = NULL;
533 } else {
534 /* remove trailing cr/lf and dots */
535 while (len > 0 && (s_buf[len-1] <= L' ' || s_buf[len-1] == L'.'))
536 s_buf[--len] = L'\0';
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200537 message = PyUnicode_FromWideChar(s_buf, len);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000538 }
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000539
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000540 if (message == NULL)
541 {
542 LocalFree(s_buf);
543 return NULL;
544 }
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000545
Antoine Pitrou6b4883d2011-10-12 02:54:14 +0200546 if (filenameObject == NULL)
547 filenameObject = Py_None;
548 /* This is the constructor signature for passing a Windows error code.
549 The POSIX translation will be figured out by the constructor. */
Victor Stinner9ea8e4c2011-10-17 20:18:58 +0200550 args = Py_BuildValue("(iOOi)", 0, message, filenameObject, err);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000551 Py_DECREF(message);
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000552
Victor Stinner9ea8e4c2011-10-17 20:18:58 +0200553 if (args != NULL) {
554 v = PyObject_Call(exc, args, NULL);
555 Py_DECREF(args);
556 if (v != NULL) {
557 PyErr_SetObject((PyObject *) Py_TYPE(v), v);
558 Py_DECREF(v);
559 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000560 }
561 LocalFree(s_buf);
562 return NULL;
Guido van Rossum795e1892000-02-17 15:19:15 +0000563}
564
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000565PyObject *PyErr_SetExcFromWindowsErrWithFilename(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000566 PyObject *exc,
567 int ierr,
568 const char *filename)
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000569{
Victor Stinner92be9392010-12-28 00:28:21 +0000570 PyObject *name = filename ? PyUnicode_DecodeFSDefault(filename) : NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000571 PyObject *ret = PyErr_SetExcFromWindowsErrWithFilenameObject(exc,
572 ierr,
573 name);
574 Py_XDECREF(name);
575 return ret;
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000576}
577
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000578PyObject *PyErr_SetExcFromWindowsErrWithUnicodeFilename(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000579 PyObject *exc,
580 int ierr,
581 const Py_UNICODE *filename)
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000582{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000583 PyObject *name = filename ?
584 PyUnicode_FromUnicode(filename, wcslen(filename)) :
585 NULL;
586 PyObject *ret = PyErr_SetExcFromWindowsErrWithFilenameObject(exc,
587 ierr,
588 name);
589 Py_XDECREF(name);
590 return ret;
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000591}
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000592
Thomas Heller085358a2002-07-29 14:27:41 +0000593PyObject *PyErr_SetExcFromWindowsErr(PyObject *exc, int ierr)
594{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000595 return PyErr_SetExcFromWindowsErrWithFilename(exc, ierr, NULL);
Thomas Heller085358a2002-07-29 14:27:41 +0000596}
597
Guido van Rossum795e1892000-02-17 15:19:15 +0000598PyObject *PyErr_SetFromWindowsErr(int ierr)
599{
Andrew Svetlov2606a6f2012-12-19 14:33:35 +0200600 return PyErr_SetExcFromWindowsErrWithFilename(PyExc_OSError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000601 ierr, NULL);
Thomas Heller085358a2002-07-29 14:27:41 +0000602}
603PyObject *PyErr_SetFromWindowsErrWithFilename(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000604 int ierr,
605 const char *filename)
Thomas Heller085358a2002-07-29 14:27:41 +0000606{
Victor Stinner92be9392010-12-28 00:28:21 +0000607 PyObject *name = filename ? PyUnicode_DecodeFSDefault(filename) : NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000608 PyObject *result = PyErr_SetExcFromWindowsErrWithFilenameObject(
Andrew Svetlov2606a6f2012-12-19 14:33:35 +0200609 PyExc_OSError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000610 ierr, name);
611 Py_XDECREF(name);
612 return result;
Guido van Rossum795e1892000-02-17 15:19:15 +0000613}
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000614
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000615PyObject *PyErr_SetFromWindowsErrWithUnicodeFilename(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000616 int ierr,
617 const Py_UNICODE *filename)
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000618{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000619 PyObject *name = filename ?
620 PyUnicode_FromUnicode(filename, wcslen(filename)) :
621 NULL;
622 PyObject *result = PyErr_SetExcFromWindowsErrWithFilenameObject(
Andrew Svetlov2606a6f2012-12-19 14:33:35 +0200623 PyExc_OSError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000624 ierr, name);
625 Py_XDECREF(name);
626 return result;
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000627}
Guido van Rossum795e1892000-02-17 15:19:15 +0000628#endif /* MS_WINDOWS */
629
Brett Cannon79ec55e2012-04-12 20:24:54 -0400630PyObject *
Brett Cannon82da8882013-07-04 17:48:16 -0400631PyErr_SetImportError(PyObject *msg, PyObject *name, PyObject *path)
Brett Cannon79ec55e2012-04-12 20:24:54 -0400632{
Brian Curtin09b86d12012-04-17 16:57:09 -0500633 PyObject *args, *kwargs, *error;
634
Brett Cannon82da8882013-07-04 17:48:16 -0400635 if (msg == NULL)
Brian Curtin94c001b2012-04-18 08:30:51 -0500636 return NULL;
637
Antoine Pitrouec9bac42012-04-18 16:57:54 +0200638 args = PyTuple_New(1);
Brian Curtin09b86d12012-04-17 16:57:09 -0500639 if (args == NULL)
640 return NULL;
641
642 kwargs = PyDict_New();
Benjamin Petersonda20cd22012-04-18 10:48:00 -0400643 if (kwargs == NULL) {
644 Py_DECREF(args);
Brian Curtin09b86d12012-04-17 16:57:09 -0500645 return NULL;
Benjamin Petersonda20cd22012-04-18 10:48:00 -0400646 }
Brian Curtin09b86d12012-04-17 16:57:09 -0500647
Brian Curtin94c001b2012-04-18 08:30:51 -0500648 if (name == NULL) {
Brian Curtin09b86d12012-04-17 16:57:09 -0500649 name = Py_None;
Brian Curtin94c001b2012-04-18 08:30:51 -0500650 }
Brian Curtin09b86d12012-04-17 16:57:09 -0500651
Brian Curtin94c001b2012-04-18 08:30:51 -0500652 if (path == NULL) {
Brian Curtin09b86d12012-04-17 16:57:09 -0500653 path = Py_None;
Brian Curtin94c001b2012-04-18 08:30:51 -0500654 }
Brian Curtin09b86d12012-04-17 16:57:09 -0500655
656 Py_INCREF(msg);
Benjamin Petersonda20cd22012-04-18 10:48:00 -0400657 PyTuple_SET_ITEM(args, 0, msg);
Brian Curtin09b86d12012-04-17 16:57:09 -0500658 PyDict_SetItemString(kwargs, "name", name);
659 PyDict_SetItemString(kwargs, "path", path);
Brett Cannon79ec55e2012-04-12 20:24:54 -0400660
Brett Cannon82da8882013-07-04 17:48:16 -0400661 error = PyObject_Call(PyExc_ImportError, args, kwargs);
Benjamin Petersonda20cd22012-04-18 10:48:00 -0400662 if (error != NULL) {
663 PyErr_SetObject((PyObject *)Py_TYPE(error), error);
Brian Curtin09b86d12012-04-17 16:57:09 -0500664 Py_DECREF(error);
Brett Cannon79ec55e2012-04-12 20:24:54 -0400665 }
666
Brett Cannon79ec55e2012-04-12 20:24:54 -0400667 Py_DECREF(args);
668 Py_DECREF(kwargs);
669
Brian Curtin09b86d12012-04-17 16:57:09 -0500670 return NULL;
Brett Cannon79ec55e2012-04-12 20:24:54 -0400671}
672
Guido van Rossum683a0721990-10-21 22:09:12 +0000673void
Neal Norwitzb382b842007-08-24 20:00:37 +0000674_PyErr_BadInternalCall(const char *filename, int lineno)
Fred Drake6d63adf2000-08-24 22:38:39 +0000675{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000676 PyErr_Format(PyExc_SystemError,
677 "%s:%d: bad argument to internal function",
678 filename, lineno);
Fred Drake6d63adf2000-08-24 22:38:39 +0000679}
680
681/* Remove the preprocessor macro for PyErr_BadInternalCall() so that we can
682 export the entry point for existing object code: */
683#undef PyErr_BadInternalCall
684void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000685PyErr_BadInternalCall(void)
Guido van Rossum683a0721990-10-21 22:09:12 +0000686{
Victor Stinnerfb3a6302013-07-12 00:37:30 +0200687 assert(0 && "bad argument to internal function");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000688 PyErr_Format(PyExc_SystemError,
689 "bad argument to internal function");
Guido van Rossum683a0721990-10-21 22:09:12 +0000690}
Fred Drake6d63adf2000-08-24 22:38:39 +0000691#define PyErr_BadInternalCall() _PyErr_BadInternalCall(__FILE__, __LINE__)
692
Guido van Rossum1548bac1997-02-14 17:09:47 +0000693
694
Guido van Rossum1548bac1997-02-14 17:09:47 +0000695PyObject *
696PyErr_Format(PyObject *exception, const char *format, ...)
Guido van Rossum1548bac1997-02-14 17:09:47 +0000697{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000698 va_list vargs;
699 PyObject* string;
Guido van Rossum1548bac1997-02-14 17:09:47 +0000700
Jeremy Hyltonb69a27e2000-09-01 03:49:47 +0000701#ifdef HAVE_STDARG_PROTOTYPES
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000702 va_start(vargs, format);
Jeremy Hyltonb69a27e2000-09-01 03:49:47 +0000703#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000704 va_start(vargs);
Jeremy Hyltonb69a27e2000-09-01 03:49:47 +0000705#endif
Guido van Rossum1548bac1997-02-14 17:09:47 +0000706
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000707 string = PyUnicode_FromFormatV(format, vargs);
708 PyErr_SetObject(exception, string);
709 Py_XDECREF(string);
710 va_end(vargs);
711 return NULL;
Guido van Rossum1548bac1997-02-14 17:09:47 +0000712}
Guido van Rossum7617e051997-09-16 18:43:50 +0000713
714
Thomas Wouters477c8d52006-05-27 19:21:47 +0000715
Guido van Rossum7617e051997-09-16 18:43:50 +0000716PyObject *
Neal Norwitzb382b842007-08-24 20:00:37 +0000717PyErr_NewException(const char *name, PyObject *base, PyObject *dict)
Guido van Rossum7617e051997-09-16 18:43:50 +0000718{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000719 const char *dot;
720 PyObject *modulename = NULL;
721 PyObject *classname = NULL;
722 PyObject *mydict = NULL;
723 PyObject *bases = NULL;
724 PyObject *result = NULL;
725 dot = strrchr(name, '.');
726 if (dot == NULL) {
727 PyErr_SetString(PyExc_SystemError,
728 "PyErr_NewException: name must be module.class");
729 return NULL;
730 }
731 if (base == NULL)
732 base = PyExc_Exception;
733 if (dict == NULL) {
734 dict = mydict = PyDict_New();
735 if (dict == NULL)
736 goto failure;
737 }
738 if (PyDict_GetItemString(dict, "__module__") == NULL) {
739 modulename = PyUnicode_FromStringAndSize(name,
740 (Py_ssize_t)(dot-name));
741 if (modulename == NULL)
742 goto failure;
743 if (PyDict_SetItemString(dict, "__module__", modulename) != 0)
744 goto failure;
745 }
746 if (PyTuple_Check(base)) {
747 bases = base;
748 /* INCREF as we create a new ref in the else branch */
749 Py_INCREF(bases);
750 } else {
751 bases = PyTuple_Pack(1, base);
752 if (bases == NULL)
753 goto failure;
754 }
Florent Xiclunaaa6c1d22011-12-12 18:54:29 +0100755 /* Create a real class. */
Victor Stinner7eeb5b52010-06-07 19:57:46 +0000756 result = PyObject_CallFunction((PyObject *)&PyType_Type, "sOO",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000757 dot+1, bases, dict);
Guido van Rossum7617e051997-09-16 18:43:50 +0000758 failure:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000759 Py_XDECREF(bases);
760 Py_XDECREF(mydict);
761 Py_XDECREF(classname);
762 Py_XDECREF(modulename);
763 return result;
Guido van Rossum7617e051997-09-16 18:43:50 +0000764}
Jeremy Hyltonb709df32000-09-01 02:47:25 +0000765
Georg Brandl1e28a272009-12-28 08:41:01 +0000766
767/* Create an exception with docstring */
768PyObject *
769PyErr_NewExceptionWithDoc(const char *name, const char *doc,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000770 PyObject *base, PyObject *dict)
Georg Brandl1e28a272009-12-28 08:41:01 +0000771{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000772 int result;
773 PyObject *ret = NULL;
774 PyObject *mydict = NULL; /* points to the dict only if we create it */
775 PyObject *docobj;
Georg Brandl1e28a272009-12-28 08:41:01 +0000776
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000777 if (dict == NULL) {
778 dict = mydict = PyDict_New();
779 if (dict == NULL) {
780 return NULL;
781 }
782 }
Georg Brandl1e28a272009-12-28 08:41:01 +0000783
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000784 if (doc != NULL) {
785 docobj = PyUnicode_FromString(doc);
786 if (docobj == NULL)
787 goto failure;
788 result = PyDict_SetItemString(dict, "__doc__", docobj);
789 Py_DECREF(docobj);
790 if (result < 0)
791 goto failure;
792 }
Georg Brandl1e28a272009-12-28 08:41:01 +0000793
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000794 ret = PyErr_NewException(name, base, dict);
Georg Brandl1e28a272009-12-28 08:41:01 +0000795 failure:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000796 Py_XDECREF(mydict);
797 return ret;
Georg Brandl1e28a272009-12-28 08:41:01 +0000798}
799
800
Jeremy Hyltonb709df32000-09-01 02:47:25 +0000801/* Call when an exception has occurred but there is no way for Python
802 to handle it. Examples: exception in __del__ or during GC. */
803void
804PyErr_WriteUnraisable(PyObject *obj)
805{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200806 _Py_IDENTIFIER(__module__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000807 PyObject *f, *t, *v, *tb;
808 PyErr_Fetch(&t, &v, &tb);
809 f = PySys_GetObject("stderr");
810 if (f != NULL && f != Py_None) {
Andrew Svetlov76bcff22012-11-03 15:56:05 +0200811 if (obj) {
812 PyFile_WriteString("Exception ignored in: ", f);
813 PyFile_WriteObject(obj, f, 0);
814 PyFile_WriteString("\n", f);
815 }
816 PyTraceBack_Print(tb, f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000817 if (t) {
818 PyObject* moduleName;
819 char* className;
820 assert(PyExceptionClass_Check(t));
821 className = PyExceptionClass_Name(t);
822 if (className != NULL) {
823 char *dot = strrchr(className, '.');
824 if (dot != NULL)
825 className = dot+1;
826 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000827
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +0200828 moduleName = _PyObject_GetAttrId(t, &PyId___module__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000829 if (moduleName == NULL)
830 PyFile_WriteString("<unknown>", f);
831 else {
832 char* modstr = _PyUnicode_AsString(moduleName);
833 if (modstr &&
834 strcmp(modstr, "builtins") != 0)
835 {
836 PyFile_WriteString(modstr, f);
837 PyFile_WriteString(".", f);
838 }
839 }
840 if (className == NULL)
841 PyFile_WriteString("<unknown>", f);
842 else
843 PyFile_WriteString(className, f);
844 if (v && v != Py_None) {
845 PyFile_WriteString(": ", f);
Andrew Svetlov76bcff22012-11-03 15:56:05 +0200846 PyFile_WriteObject(v, f, Py_PRINT_RAW);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000847 }
Andrew Svetlov76bcff22012-11-03 15:56:05 +0200848 PyFile_WriteString("\n", f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000849 Py_XDECREF(moduleName);
850 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000851 PyErr_Clear(); /* Just in case */
852 }
853 Py_XDECREF(t);
854 Py_XDECREF(v);
855 Py_XDECREF(tb);
Jeremy Hyltonb709df32000-09-01 02:47:25 +0000856}
Guido van Rossumcfd42b52000-12-15 21:58:52 +0000857
Armin Rigo092381a2003-10-25 14:29:27 +0000858extern PyObject *PyModule_GetWarningsModule(void);
Guido van Rossumcfd42b52000-12-15 21:58:52 +0000859
Guido van Rossum2fd45652001-02-28 21:46:24 +0000860
Benjamin Peterson2c539712010-09-20 22:42:10 +0000861void
862PyErr_SyntaxLocation(const char *filename, int lineno) {
863 PyErr_SyntaxLocationEx(filename, lineno, -1);
864}
865
866
Martin v. Löwiscfeb3b62002-03-03 21:30:27 +0000867/* Set file and line information for the current exception.
868 If the exception is not a SyntaxError, also sets additional attributes
869 to make printing of exceptions believe it is a syntax error. */
Guido van Rossum2fd45652001-02-28 21:46:24 +0000870
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000871void
Benjamin Peterson2c539712010-09-20 22:42:10 +0000872PyErr_SyntaxLocationEx(const char *filename, int lineno, int col_offset)
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000873{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000874 PyObject *exc, *v, *tb, *tmp;
Martin v. Löwis1c67dd92011-10-14 15:16:45 +0200875 _Py_IDENTIFIER(filename);
876 _Py_IDENTIFIER(lineno);
877 _Py_IDENTIFIER(msg);
878 _Py_IDENTIFIER(offset);
879 _Py_IDENTIFIER(print_file_and_line);
880 _Py_IDENTIFIER(text);
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000881
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000882 /* add attributes for the line number and filename for the error */
883 PyErr_Fetch(&exc, &v, &tb);
884 PyErr_NormalizeException(&exc, &v, &tb);
885 /* XXX check that it is, indeed, a syntax error. It might not
886 * be, though. */
887 tmp = PyLong_FromLong(lineno);
888 if (tmp == NULL)
889 PyErr_Clear();
890 else {
Martin v. Löwis1c67dd92011-10-14 15:16:45 +0200891 if (_PyObject_SetAttrId(v, &PyId_lineno, tmp))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000892 PyErr_Clear();
893 Py_DECREF(tmp);
894 }
Benjamin Peterson2c539712010-09-20 22:42:10 +0000895 if (col_offset >= 0) {
896 tmp = PyLong_FromLong(col_offset);
897 if (tmp == NULL)
898 PyErr_Clear();
899 else {
Martin v. Löwis1c67dd92011-10-14 15:16:45 +0200900 if (_PyObject_SetAttrId(v, &PyId_offset, tmp))
Benjamin Peterson2c539712010-09-20 22:42:10 +0000901 PyErr_Clear();
902 Py_DECREF(tmp);
903 }
904 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000905 if (filename != NULL) {
Victor Stinner15a71cd2010-10-17 19:03:16 +0000906 tmp = PyUnicode_DecodeFSDefault(filename);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000907 if (tmp == NULL)
908 PyErr_Clear();
909 else {
Martin v. Löwis1c67dd92011-10-14 15:16:45 +0200910 if (_PyObject_SetAttrId(v, &PyId_filename, tmp))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000911 PyErr_Clear();
912 Py_DECREF(tmp);
913 }
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000914
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000915 tmp = PyErr_ProgramText(filename, lineno);
916 if (tmp) {
Martin v. Löwis1c67dd92011-10-14 15:16:45 +0200917 if (_PyObject_SetAttrId(v, &PyId_text, tmp))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000918 PyErr_Clear();
919 Py_DECREF(tmp);
920 }
921 }
Martin v. Löwis1c67dd92011-10-14 15:16:45 +0200922 if (_PyObject_SetAttrId(v, &PyId_offset, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000923 PyErr_Clear();
924 }
925 if (exc != PyExc_SyntaxError) {
Martin v. Löwis1c67dd92011-10-14 15:16:45 +0200926 if (!_PyObject_HasAttrId(v, &PyId_msg)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000927 tmp = PyObject_Str(v);
928 if (tmp) {
Martin v. Löwis1c67dd92011-10-14 15:16:45 +0200929 if (_PyObject_SetAttrId(v, &PyId_msg, tmp))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000930 PyErr_Clear();
931 Py_DECREF(tmp);
932 } else {
933 PyErr_Clear();
934 }
935 }
Martin v. Löwis1c67dd92011-10-14 15:16:45 +0200936 if (!_PyObject_HasAttrId(v, &PyId_print_file_and_line)) {
937 if (_PyObject_SetAttrId(v, &PyId_print_file_and_line,
938 Py_None))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000939 PyErr_Clear();
940 }
941 }
942 PyErr_Restore(exc, v, tb);
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000943}
944
Guido van Rossumebe8f8a2007-10-10 18:53:36 +0000945/* Attempt to load the line of text that the exception refers to. If it
946 fails, it will return NULL but will not set an exception.
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000947
948 XXX The functionality of this function is quite similar to the
Guido van Rossumebe8f8a2007-10-10 18:53:36 +0000949 functionality in tb_displayline() in traceback.c. */
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000950
951PyObject *
Martin v. Löwis95292d62002-12-11 14:04:59 +0000952PyErr_ProgramText(const char *filename, int lineno)
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000953{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000954 FILE *fp;
955 int i;
956 char linebuf[1000];
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000957
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000958 if (filename == NULL || *filename == '\0' || lineno <= 0)
959 return NULL;
960 fp = fopen(filename, "r" PY_STDIOTEXTMODE);
961 if (fp == NULL)
962 return NULL;
963 for (i = 0; i < lineno; i++) {
964 char *pLastChar = &linebuf[sizeof(linebuf) - 2];
965 do {
966 *pLastChar = '\0';
967 if (Py_UniversalNewlineFgets(linebuf, sizeof linebuf,
968 fp, NULL) == NULL)
969 break;
970 /* fgets read *something*; if it didn't get as
971 far as pLastChar, it must have found a newline
972 or hit the end of the file; if pLastChar is \n,
973 it obviously found a newline; else we haven't
974 yet seen a newline, so must continue */
975 } while (*pLastChar != '\0' && *pLastChar != '\n');
976 }
977 fclose(fp);
978 if (i == lineno) {
979 char *p = linebuf;
980 PyObject *res;
981 while (*p == ' ' || *p == '\t' || *p == '\014')
982 p++;
983 res = PyUnicode_FromString(p);
984 if (res == NULL)
985 PyErr_Clear();
986 return res;
987 }
988 return NULL;
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000989}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000990
991#ifdef __cplusplus
992}
993#endif