blob: 63eebe2e779cba8128c3077f2dc40f346069c1a2 [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)) {
230 /* if the value was not an instance, or is not an instance
231 whose class is (or is derived from) type, then use the
232 value as an argument to instantiation of the type
233 class.
234 */
235 if (!inclass || !PyObject_IsSubclass(inclass, type)) {
236 PyObject *args, *res;
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000237
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000238 if (value == Py_None)
239 args = PyTuple_New(0);
240 else if (PyTuple_Check(value)) {
241 Py_INCREF(value);
242 args = value;
243 }
244 else
245 args = PyTuple_Pack(1, value);
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000246
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000247 if (args == NULL)
248 goto finally;
249 res = PyEval_CallObject(type, args);
250 Py_DECREF(args);
251 if (res == NULL)
252 goto finally;
253 Py_DECREF(value);
254 value = res;
255 }
256 /* if the class of the instance doesn't exactly match the
257 class of the type, believe the instance
258 */
259 else if (inclass != type) {
260 Py_DECREF(type);
261 type = inclass;
262 Py_INCREF(type);
263 }
264 }
265 *exc = type;
266 *val = value;
267 return;
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000268finally:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000269 Py_DECREF(type);
270 Py_DECREF(value);
271 /* If the new exception doesn't set a traceback and the old
272 exception had a traceback, use the old traceback for the
273 new exception. It's better than nothing.
274 */
275 initial_tb = *tb;
276 PyErr_Fetch(exc, val, tb);
277 if (initial_tb != NULL) {
278 if (*tb == NULL)
279 *tb = initial_tb;
280 else
281 Py_DECREF(initial_tb);
282 }
283 /* normalize recursively */
284 tstate = PyThreadState_GET();
285 if (++tstate->recursion_depth > Py_GetRecursionLimit()) {
286 --tstate->recursion_depth;
287 /* throw away the old exception... */
288 Py_DECREF(*exc);
289 Py_DECREF(*val);
290 /* ... and use the recursion error instead */
291 *exc = PyExc_RuntimeError;
292 *val = PyExc_RecursionErrorInst;
293 Py_INCREF(*exc);
294 Py_INCREF(*val);
295 /* just keeping the old traceback */
296 return;
297 }
298 PyErr_NormalizeException(exc, val, tb);
299 --tstate->recursion_depth;
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000300}
301
302
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000303void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000304PyErr_Fetch(PyObject **p_type, PyObject **p_value, PyObject **p_traceback)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000305{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000306 PyThreadState *tstate = PyThreadState_GET();
Guido van Rossuma027efa1997-05-05 20:56:21 +0000307
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000308 *p_type = tstate->curexc_type;
309 *p_value = tstate->curexc_value;
310 *p_traceback = tstate->curexc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000311
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000312 tstate->curexc_type = NULL;
313 tstate->curexc_value = NULL;
314 tstate->curexc_traceback = NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000315}
316
317void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000318PyErr_Clear(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000319{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000320 PyErr_Restore(NULL, NULL, NULL);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000321}
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000322
323/* Convenience functions to set a type error exception and return 0 */
324
325int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000326PyErr_BadArgument(void)
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000327{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000328 PyErr_SetString(PyExc_TypeError,
329 "bad argument type for built-in operation");
330 return 0;
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000331}
332
Guido van Rossum373c8691997-04-29 18:22:47 +0000333PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000334PyErr_NoMemory(void)
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000335{
Antoine Pitrou07e20ef2010-10-28 22:56:58 +0000336 PyErr_SetNone(PyExc_MemoryError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000337 return NULL;
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000338}
339
Guido van Rossum373c8691997-04-29 18:22:47 +0000340PyObject *
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000341PyErr_SetFromErrnoWithFilenameObject(PyObject *exc, PyObject *filenameObject)
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000342{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000343 PyObject *message;
Antoine Pitrou5d6fbe82011-10-12 19:39:57 +0200344 PyObject *v, *args;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000345 int i = errno;
Victor Stinner1f33f2b2011-12-17 04:45:09 +0100346#ifdef MS_WINDOWS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000347 WCHAR *s_buf = NULL;
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000348#endif /* Unix/Windows */
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000349
Guido van Rossume9fbc091995-02-18 14:52:19 +0000350#ifdef EINTR
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000351 if (i == EINTR && PyErr_CheckSignals())
352 return NULL;
Guido van Rossume9fbc091995-02-18 14:52:19 +0000353#endif
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000354
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000355#ifndef MS_WINDOWS
Victor Stinner1f33f2b2011-12-17 04:45:09 +0100356 if (i != 0) {
357 char *s = strerror(i);
Victor Stinner1b579672011-12-17 05:47:23 +0100358 message = PyUnicode_DecodeLocale(s, "surrogateescape");
Victor Stinner1f33f2b2011-12-17 04:45:09 +0100359 }
360 else {
361 /* Sometimes errno didn't get set */
362 message = PyUnicode_FromString("Error");
363 }
Guido van Rossum743007d1999-04-21 15:27:31 +0000364#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000365 if (i == 0)
366 message = PyUnicode_FromString("Error"); /* Sometimes errno didn't get set */
367 else
368 {
369 /* Note that the Win32 errors do not lineup with the
370 errno error. So if the error is in the MSVC error
371 table, we use it, otherwise we assume it really _is_
372 a Win32 error code
373 */
374 if (i > 0 && i < _sys_nerr) {
375 message = PyUnicode_FromString(_sys_errlist[i]);
376 }
377 else {
378 int len = FormatMessageW(
379 FORMAT_MESSAGE_ALLOCATE_BUFFER |
380 FORMAT_MESSAGE_FROM_SYSTEM |
381 FORMAT_MESSAGE_IGNORE_INSERTS,
382 NULL, /* no message source */
383 i,
384 MAKELANGID(LANG_NEUTRAL,
385 SUBLANG_DEFAULT),
386 /* Default language */
387 (LPWSTR) &s_buf,
388 0, /* size not used */
389 NULL); /* no args */
390 if (len==0) {
391 /* Only ever seen this in out-of-mem
392 situations */
393 s_buf = NULL;
394 message = PyUnicode_FromFormat("Windows Error 0x%X", i);
395 } else {
396 /* remove trailing cr/lf and dots */
397 while (len > 0 && (s_buf[len-1] <= L' ' || s_buf[len-1] == L'.'))
398 s_buf[--len] = L'\0';
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200399 message = PyUnicode_FromWideChar(s_buf, len);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000400 }
401 }
402 }
Martin v. Löwis3484a182002-03-09 12:07:51 +0000403#endif /* Unix/Windows */
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000404
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000405 if (message == NULL)
406 {
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000407#ifdef MS_WINDOWS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000408 LocalFree(s_buf);
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000409#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000410 return NULL;
411 }
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000412
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000413 if (filenameObject != NULL)
Antoine Pitrou5d6fbe82011-10-12 19:39:57 +0200414 args = Py_BuildValue("(iOO)", i, message, filenameObject);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000415 else
Antoine Pitrou5d6fbe82011-10-12 19:39:57 +0200416 args = Py_BuildValue("(iO)", i, message);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000417 Py_DECREF(message);
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000418
Antoine Pitrou5d6fbe82011-10-12 19:39:57 +0200419 if (args != NULL) {
420 v = PyObject_Call(exc, args, NULL);
421 Py_DECREF(args);
422 if (v != NULL) {
423 PyErr_SetObject((PyObject *) Py_TYPE(v), v);
424 Py_DECREF(v);
425 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000426 }
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000427#ifdef MS_WINDOWS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000428 LocalFree(s_buf);
Guido van Rossum743007d1999-04-21 15:27:31 +0000429#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000430 return NULL;
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000431}
Guido van Rossum743007d1999-04-21 15:27:31 +0000432
Barry Warsaw97d95151998-07-23 16:05:56 +0000433
434PyObject *
Neal Norwitzb382b842007-08-24 20:00:37 +0000435PyErr_SetFromErrnoWithFilename(PyObject *exc, const char *filename)
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000436{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000437 PyObject *name = filename ? PyUnicode_DecodeFSDefault(filename) : NULL;
438 PyObject *result = PyErr_SetFromErrnoWithFilenameObject(exc, name);
439 Py_XDECREF(name);
440 return result;
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000441}
442
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +0000443#ifdef MS_WINDOWS
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000444PyObject *
Neal Norwitzb382b842007-08-24 20:00:37 +0000445PyErr_SetFromErrnoWithUnicodeFilename(PyObject *exc, const Py_UNICODE *filename)
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000446{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000447 PyObject *name = filename ?
448 PyUnicode_FromUnicode(filename, wcslen(filename)) :
449 NULL;
450 PyObject *result = PyErr_SetFromErrnoWithFilenameObject(exc, name);
451 Py_XDECREF(name);
452 return result;
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000453}
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +0000454#endif /* MS_WINDOWS */
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000455
456PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000457PyErr_SetFromErrno(PyObject *exc)
Barry Warsaw97d95151998-07-23 16:05:56 +0000458{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000459 return PyErr_SetFromErrnoWithFilenameObject(exc, NULL);
Barry Warsaw97d95151998-07-23 16:05:56 +0000460}
Guido van Rossum683a0721990-10-21 22:09:12 +0000461
Brett Cannonbf364092006-03-01 04:25:17 +0000462#ifdef MS_WINDOWS
Guido van Rossum795e1892000-02-17 15:19:15 +0000463/* Windows specific error code handling */
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000464PyObject *PyErr_SetExcFromWindowsErrWithFilenameObject(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000465 PyObject *exc,
466 int ierr,
467 PyObject *filenameObject)
Guido van Rossum795e1892000-02-17 15:19:15 +0000468{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000469 int len;
470 WCHAR *s_buf = NULL; /* Free via LocalFree */
471 PyObject *message;
Victor Stinner9ea8e4c2011-10-17 20:18:58 +0200472 PyObject *args, *v;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000473 DWORD err = (DWORD)ierr;
474 if (err==0) err = GetLastError();
475 len = FormatMessageW(
476 /* Error API error */
477 FORMAT_MESSAGE_ALLOCATE_BUFFER |
478 FORMAT_MESSAGE_FROM_SYSTEM |
479 FORMAT_MESSAGE_IGNORE_INSERTS,
480 NULL, /* no message source */
481 err,
482 MAKELANGID(LANG_NEUTRAL,
483 SUBLANG_DEFAULT), /* Default language */
484 (LPWSTR) &s_buf,
485 0, /* size not used */
486 NULL); /* no args */
487 if (len==0) {
488 /* Only seen this in out of mem situations */
489 message = PyUnicode_FromFormat("Windows Error 0x%X", err);
490 s_buf = NULL;
491 } else {
492 /* remove trailing cr/lf and dots */
493 while (len > 0 && (s_buf[len-1] <= L' ' || s_buf[len-1] == L'.'))
494 s_buf[--len] = L'\0';
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200495 message = PyUnicode_FromWideChar(s_buf, len);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000496 }
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000497
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000498 if (message == NULL)
499 {
500 LocalFree(s_buf);
501 return NULL;
502 }
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000503
Antoine Pitrou6b4883d2011-10-12 02:54:14 +0200504 if (filenameObject == NULL)
505 filenameObject = Py_None;
506 /* This is the constructor signature for passing a Windows error code.
507 The POSIX translation will be figured out by the constructor. */
Victor Stinner9ea8e4c2011-10-17 20:18:58 +0200508 args = Py_BuildValue("(iOOi)", 0, message, filenameObject, err);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000509 Py_DECREF(message);
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000510
Victor Stinner9ea8e4c2011-10-17 20:18:58 +0200511 if (args != NULL) {
512 v = PyObject_Call(exc, args, NULL);
513 Py_DECREF(args);
514 if (v != NULL) {
515 PyErr_SetObject((PyObject *) Py_TYPE(v), v);
516 Py_DECREF(v);
517 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000518 }
519 LocalFree(s_buf);
520 return NULL;
Guido van Rossum795e1892000-02-17 15:19:15 +0000521}
522
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000523PyObject *PyErr_SetExcFromWindowsErrWithFilename(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000524 PyObject *exc,
525 int ierr,
526 const char *filename)
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000527{
Victor Stinner92be9392010-12-28 00:28:21 +0000528 PyObject *name = filename ? PyUnicode_DecodeFSDefault(filename) : NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000529 PyObject *ret = PyErr_SetExcFromWindowsErrWithFilenameObject(exc,
530 ierr,
531 name);
532 Py_XDECREF(name);
533 return ret;
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000534}
535
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000536PyObject *PyErr_SetExcFromWindowsErrWithUnicodeFilename(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000537 PyObject *exc,
538 int ierr,
539 const Py_UNICODE *filename)
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000540{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000541 PyObject *name = filename ?
542 PyUnicode_FromUnicode(filename, wcslen(filename)) :
543 NULL;
544 PyObject *ret = PyErr_SetExcFromWindowsErrWithFilenameObject(exc,
545 ierr,
546 name);
547 Py_XDECREF(name);
548 return ret;
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000549}
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000550
Thomas Heller085358a2002-07-29 14:27:41 +0000551PyObject *PyErr_SetExcFromWindowsErr(PyObject *exc, int ierr)
552{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000553 return PyErr_SetExcFromWindowsErrWithFilename(exc, ierr, NULL);
Thomas Heller085358a2002-07-29 14:27:41 +0000554}
555
Guido van Rossum795e1892000-02-17 15:19:15 +0000556PyObject *PyErr_SetFromWindowsErr(int ierr)
557{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000558 return PyErr_SetExcFromWindowsErrWithFilename(PyExc_WindowsError,
559 ierr, NULL);
Thomas Heller085358a2002-07-29 14:27:41 +0000560}
561PyObject *PyErr_SetFromWindowsErrWithFilename(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000562 int ierr,
563 const char *filename)
Thomas Heller085358a2002-07-29 14:27:41 +0000564{
Victor Stinner92be9392010-12-28 00:28:21 +0000565 PyObject *name = filename ? PyUnicode_DecodeFSDefault(filename) : NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000566 PyObject *result = PyErr_SetExcFromWindowsErrWithFilenameObject(
567 PyExc_WindowsError,
568 ierr, name);
569 Py_XDECREF(name);
570 return result;
Guido van Rossum795e1892000-02-17 15:19:15 +0000571}
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000572
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000573PyObject *PyErr_SetFromWindowsErrWithUnicodeFilename(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000574 int ierr,
575 const Py_UNICODE *filename)
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000576{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000577 PyObject *name = filename ?
578 PyUnicode_FromUnicode(filename, wcslen(filename)) :
579 NULL;
580 PyObject *result = PyErr_SetExcFromWindowsErrWithFilenameObject(
581 PyExc_WindowsError,
582 ierr, name);
583 Py_XDECREF(name);
584 return result;
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000585}
Guido van Rossum795e1892000-02-17 15:19:15 +0000586#endif /* MS_WINDOWS */
587
Brett Cannon79ec55e2012-04-12 20:24:54 -0400588PyObject *
Brian Curtin09b86d12012-04-17 16:57:09 -0500589PyErr_SetImportError(PyObject *msg, PyObject *name, PyObject *path)
Brett Cannon79ec55e2012-04-12 20:24:54 -0400590{
Brian Curtin09b86d12012-04-17 16:57:09 -0500591 PyObject *args, *kwargs, *error;
592
Brian Curtin94c001b2012-04-18 08:30:51 -0500593 if (msg == NULL)
594 return NULL;
595
Brian Curtin09b86d12012-04-17 16:57:09 -0500596 args = PyTuple_New(1);
597 if (args == NULL)
598 return NULL;
599
600 kwargs = PyDict_New();
Brian Curtin94c001b2012-04-18 08:30:51 -0500601 if (kwargs == NULL)
Brian Curtin09b86d12012-04-17 16:57:09 -0500602 return NULL;
603
Brian Curtin94c001b2012-04-18 08:30:51 -0500604 if (name == NULL) {
605 Py_INCREF(Py_None);
Brian Curtin09b86d12012-04-17 16:57:09 -0500606 name = Py_None;
Brian Curtin94c001b2012-04-18 08:30:51 -0500607 }
Brian Curtin09b86d12012-04-17 16:57:09 -0500608
Brian Curtin94c001b2012-04-18 08:30:51 -0500609 if (path == NULL) {
610 Py_INCREF(Py_None);
Brian Curtin09b86d12012-04-17 16:57:09 -0500611 path = Py_None;
Brian Curtin94c001b2012-04-18 08:30:51 -0500612 }
Brian Curtin09b86d12012-04-17 16:57:09 -0500613
614 Py_INCREF(msg);
Brian Curtin94c001b2012-04-18 08:30:51 -0500615 PyTuple_SetItem(args, 0, NULL);//msg);
Brian Curtin09b86d12012-04-17 16:57:09 -0500616 PyDict_SetItemString(kwargs, "name", name);
617 PyDict_SetItemString(kwargs, "path", path);
Brett Cannon79ec55e2012-04-12 20:24:54 -0400618
Brian Curtin09b86d12012-04-17 16:57:09 -0500619 error = PyObject_Call(PyExc_ImportError, args, kwargs);
620 if (error!= NULL) {
621 PyErr_SetObject((PyObject *) Py_TYPE(error), error);
622 Py_DECREF(error);
Brett Cannon79ec55e2012-04-12 20:24:54 -0400623 }
624
Brett Cannon79ec55e2012-04-12 20:24:54 -0400625 Py_DECREF(args);
626 Py_DECREF(kwargs);
627
Brian Curtin09b86d12012-04-17 16:57:09 -0500628 return NULL;
Brett Cannon79ec55e2012-04-12 20:24:54 -0400629}
630
Guido van Rossum683a0721990-10-21 22:09:12 +0000631void
Neal Norwitzb382b842007-08-24 20:00:37 +0000632_PyErr_BadInternalCall(const char *filename, int lineno)
Fred Drake6d63adf2000-08-24 22:38:39 +0000633{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000634 PyErr_Format(PyExc_SystemError,
635 "%s:%d: bad argument to internal function",
636 filename, lineno);
Fred Drake6d63adf2000-08-24 22:38:39 +0000637}
638
639/* Remove the preprocessor macro for PyErr_BadInternalCall() so that we can
640 export the entry point for existing object code: */
641#undef PyErr_BadInternalCall
642void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000643PyErr_BadInternalCall(void)
Guido van Rossum683a0721990-10-21 22:09:12 +0000644{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000645 PyErr_Format(PyExc_SystemError,
646 "bad argument to internal function");
Guido van Rossum683a0721990-10-21 22:09:12 +0000647}
Fred Drake6d63adf2000-08-24 22:38:39 +0000648#define PyErr_BadInternalCall() _PyErr_BadInternalCall(__FILE__, __LINE__)
649
Guido van Rossum1548bac1997-02-14 17:09:47 +0000650
651
Guido van Rossum1548bac1997-02-14 17:09:47 +0000652PyObject *
653PyErr_Format(PyObject *exception, const char *format, ...)
Guido van Rossum1548bac1997-02-14 17:09:47 +0000654{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000655 va_list vargs;
656 PyObject* string;
Guido van Rossum1548bac1997-02-14 17:09:47 +0000657
Jeremy Hyltonb69a27e2000-09-01 03:49:47 +0000658#ifdef HAVE_STDARG_PROTOTYPES
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000659 va_start(vargs, format);
Jeremy Hyltonb69a27e2000-09-01 03:49:47 +0000660#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000661 va_start(vargs);
Jeremy Hyltonb69a27e2000-09-01 03:49:47 +0000662#endif
Guido van Rossum1548bac1997-02-14 17:09:47 +0000663
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000664 string = PyUnicode_FromFormatV(format, vargs);
665 PyErr_SetObject(exception, string);
666 Py_XDECREF(string);
667 va_end(vargs);
668 return NULL;
Guido van Rossum1548bac1997-02-14 17:09:47 +0000669}
Guido van Rossum7617e051997-09-16 18:43:50 +0000670
671
Thomas Wouters477c8d52006-05-27 19:21:47 +0000672
Guido van Rossum7617e051997-09-16 18:43:50 +0000673PyObject *
Neal Norwitzb382b842007-08-24 20:00:37 +0000674PyErr_NewException(const char *name, PyObject *base, PyObject *dict)
Guido van Rossum7617e051997-09-16 18:43:50 +0000675{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000676 const char *dot;
677 PyObject *modulename = NULL;
678 PyObject *classname = NULL;
679 PyObject *mydict = NULL;
680 PyObject *bases = NULL;
681 PyObject *result = NULL;
682 dot = strrchr(name, '.');
683 if (dot == NULL) {
684 PyErr_SetString(PyExc_SystemError,
685 "PyErr_NewException: name must be module.class");
686 return NULL;
687 }
688 if (base == NULL)
689 base = PyExc_Exception;
690 if (dict == NULL) {
691 dict = mydict = PyDict_New();
692 if (dict == NULL)
693 goto failure;
694 }
695 if (PyDict_GetItemString(dict, "__module__") == NULL) {
696 modulename = PyUnicode_FromStringAndSize(name,
697 (Py_ssize_t)(dot-name));
698 if (modulename == NULL)
699 goto failure;
700 if (PyDict_SetItemString(dict, "__module__", modulename) != 0)
701 goto failure;
702 }
703 if (PyTuple_Check(base)) {
704 bases = base;
705 /* INCREF as we create a new ref in the else branch */
706 Py_INCREF(bases);
707 } else {
708 bases = PyTuple_Pack(1, base);
709 if (bases == NULL)
710 goto failure;
711 }
Florent Xiclunaaa6c1d22011-12-12 18:54:29 +0100712 /* Create a real class. */
Victor Stinner7eeb5b52010-06-07 19:57:46 +0000713 result = PyObject_CallFunction((PyObject *)&PyType_Type, "sOO",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000714 dot+1, bases, dict);
Guido van Rossum7617e051997-09-16 18:43:50 +0000715 failure:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000716 Py_XDECREF(bases);
717 Py_XDECREF(mydict);
718 Py_XDECREF(classname);
719 Py_XDECREF(modulename);
720 return result;
Guido van Rossum7617e051997-09-16 18:43:50 +0000721}
Jeremy Hyltonb709df32000-09-01 02:47:25 +0000722
Georg Brandl1e28a272009-12-28 08:41:01 +0000723
724/* Create an exception with docstring */
725PyObject *
726PyErr_NewExceptionWithDoc(const char *name, const char *doc,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000727 PyObject *base, PyObject *dict)
Georg Brandl1e28a272009-12-28 08:41:01 +0000728{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000729 int result;
730 PyObject *ret = NULL;
731 PyObject *mydict = NULL; /* points to the dict only if we create it */
732 PyObject *docobj;
Georg Brandl1e28a272009-12-28 08:41:01 +0000733
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000734 if (dict == NULL) {
735 dict = mydict = PyDict_New();
736 if (dict == NULL) {
737 return NULL;
738 }
739 }
Georg Brandl1e28a272009-12-28 08:41:01 +0000740
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000741 if (doc != NULL) {
742 docobj = PyUnicode_FromString(doc);
743 if (docobj == NULL)
744 goto failure;
745 result = PyDict_SetItemString(dict, "__doc__", docobj);
746 Py_DECREF(docobj);
747 if (result < 0)
748 goto failure;
749 }
Georg Brandl1e28a272009-12-28 08:41:01 +0000750
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000751 ret = PyErr_NewException(name, base, dict);
Georg Brandl1e28a272009-12-28 08:41:01 +0000752 failure:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000753 Py_XDECREF(mydict);
754 return ret;
Georg Brandl1e28a272009-12-28 08:41:01 +0000755}
756
757
Jeremy Hyltonb709df32000-09-01 02:47:25 +0000758/* Call when an exception has occurred but there is no way for Python
759 to handle it. Examples: exception in __del__ or during GC. */
760void
761PyErr_WriteUnraisable(PyObject *obj)
762{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200763 _Py_IDENTIFIER(__module__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000764 PyObject *f, *t, *v, *tb;
765 PyErr_Fetch(&t, &v, &tb);
766 f = PySys_GetObject("stderr");
767 if (f != NULL && f != Py_None) {
768 PyFile_WriteString("Exception ", f);
769 if (t) {
770 PyObject* moduleName;
771 char* className;
772 assert(PyExceptionClass_Check(t));
773 className = PyExceptionClass_Name(t);
774 if (className != NULL) {
775 char *dot = strrchr(className, '.');
776 if (dot != NULL)
777 className = dot+1;
778 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000779
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +0200780 moduleName = _PyObject_GetAttrId(t, &PyId___module__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000781 if (moduleName == NULL)
782 PyFile_WriteString("<unknown>", f);
783 else {
784 char* modstr = _PyUnicode_AsString(moduleName);
785 if (modstr &&
786 strcmp(modstr, "builtins") != 0)
787 {
788 PyFile_WriteString(modstr, f);
789 PyFile_WriteString(".", f);
790 }
791 }
792 if (className == NULL)
793 PyFile_WriteString("<unknown>", f);
794 else
795 PyFile_WriteString(className, f);
796 if (v && v != Py_None) {
797 PyFile_WriteString(": ", f);
798 PyFile_WriteObject(v, f, 0);
799 }
800 Py_XDECREF(moduleName);
801 }
Georg Brandl08be72d2010-10-24 15:11:22 +0000802 if (obj) {
803 PyFile_WriteString(" in ", f);
804 PyFile_WriteObject(obj, f, 0);
805 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000806 PyFile_WriteString(" ignored\n", f);
807 PyErr_Clear(); /* Just in case */
808 }
809 Py_XDECREF(t);
810 Py_XDECREF(v);
811 Py_XDECREF(tb);
Jeremy Hyltonb709df32000-09-01 02:47:25 +0000812}
Guido van Rossumcfd42b52000-12-15 21:58:52 +0000813
Armin Rigo092381a2003-10-25 14:29:27 +0000814extern PyObject *PyModule_GetWarningsModule(void);
Guido van Rossumcfd42b52000-12-15 21:58:52 +0000815
Guido van Rossum2fd45652001-02-28 21:46:24 +0000816
Benjamin Peterson2c539712010-09-20 22:42:10 +0000817void
818PyErr_SyntaxLocation(const char *filename, int lineno) {
819 PyErr_SyntaxLocationEx(filename, lineno, -1);
820}
821
822
Martin v. Löwiscfeb3b62002-03-03 21:30:27 +0000823/* Set file and line information for the current exception.
824 If the exception is not a SyntaxError, also sets additional attributes
825 to make printing of exceptions believe it is a syntax error. */
Guido van Rossum2fd45652001-02-28 21:46:24 +0000826
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000827void
Benjamin Peterson2c539712010-09-20 22:42:10 +0000828PyErr_SyntaxLocationEx(const char *filename, int lineno, int col_offset)
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000829{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000830 PyObject *exc, *v, *tb, *tmp;
Martin v. Löwis1c67dd92011-10-14 15:16:45 +0200831 _Py_IDENTIFIER(filename);
832 _Py_IDENTIFIER(lineno);
833 _Py_IDENTIFIER(msg);
834 _Py_IDENTIFIER(offset);
835 _Py_IDENTIFIER(print_file_and_line);
836 _Py_IDENTIFIER(text);
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000837
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000838 /* add attributes for the line number and filename for the error */
839 PyErr_Fetch(&exc, &v, &tb);
840 PyErr_NormalizeException(&exc, &v, &tb);
841 /* XXX check that it is, indeed, a syntax error. It might not
842 * be, though. */
843 tmp = PyLong_FromLong(lineno);
844 if (tmp == NULL)
845 PyErr_Clear();
846 else {
Martin v. Löwis1c67dd92011-10-14 15:16:45 +0200847 if (_PyObject_SetAttrId(v, &PyId_lineno, tmp))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000848 PyErr_Clear();
849 Py_DECREF(tmp);
850 }
Benjamin Peterson2c539712010-09-20 22:42:10 +0000851 if (col_offset >= 0) {
852 tmp = PyLong_FromLong(col_offset);
853 if (tmp == NULL)
854 PyErr_Clear();
855 else {
Martin v. Löwis1c67dd92011-10-14 15:16:45 +0200856 if (_PyObject_SetAttrId(v, &PyId_offset, tmp))
Benjamin Peterson2c539712010-09-20 22:42:10 +0000857 PyErr_Clear();
858 Py_DECREF(tmp);
859 }
860 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000861 if (filename != NULL) {
Victor Stinner15a71cd2010-10-17 19:03:16 +0000862 tmp = PyUnicode_DecodeFSDefault(filename);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000863 if (tmp == NULL)
864 PyErr_Clear();
865 else {
Martin v. Löwis1c67dd92011-10-14 15:16:45 +0200866 if (_PyObject_SetAttrId(v, &PyId_filename, tmp))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000867 PyErr_Clear();
868 Py_DECREF(tmp);
869 }
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000870
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000871 tmp = PyErr_ProgramText(filename, lineno);
872 if (tmp) {
Martin v. Löwis1c67dd92011-10-14 15:16:45 +0200873 if (_PyObject_SetAttrId(v, &PyId_text, tmp))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000874 PyErr_Clear();
875 Py_DECREF(tmp);
876 }
877 }
Martin v. Löwis1c67dd92011-10-14 15:16:45 +0200878 if (_PyObject_SetAttrId(v, &PyId_offset, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000879 PyErr_Clear();
880 }
881 if (exc != PyExc_SyntaxError) {
Martin v. Löwis1c67dd92011-10-14 15:16:45 +0200882 if (!_PyObject_HasAttrId(v, &PyId_msg)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000883 tmp = PyObject_Str(v);
884 if (tmp) {
Martin v. Löwis1c67dd92011-10-14 15:16:45 +0200885 if (_PyObject_SetAttrId(v, &PyId_msg, tmp))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000886 PyErr_Clear();
887 Py_DECREF(tmp);
888 } else {
889 PyErr_Clear();
890 }
891 }
Martin v. Löwis1c67dd92011-10-14 15:16:45 +0200892 if (!_PyObject_HasAttrId(v, &PyId_print_file_and_line)) {
893 if (_PyObject_SetAttrId(v, &PyId_print_file_and_line,
894 Py_None))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000895 PyErr_Clear();
896 }
897 }
898 PyErr_Restore(exc, v, tb);
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000899}
900
Guido van Rossumebe8f8a2007-10-10 18:53:36 +0000901/* Attempt to load the line of text that the exception refers to. If it
902 fails, it will return NULL but will not set an exception.
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000903
904 XXX The functionality of this function is quite similar to the
Guido van Rossumebe8f8a2007-10-10 18:53:36 +0000905 functionality in tb_displayline() in traceback.c. */
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000906
907PyObject *
Martin v. Löwis95292d62002-12-11 14:04:59 +0000908PyErr_ProgramText(const char *filename, int lineno)
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000909{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000910 FILE *fp;
911 int i;
912 char linebuf[1000];
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000913
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000914 if (filename == NULL || *filename == '\0' || lineno <= 0)
915 return NULL;
916 fp = fopen(filename, "r" PY_STDIOTEXTMODE);
917 if (fp == NULL)
918 return NULL;
919 for (i = 0; i < lineno; i++) {
920 char *pLastChar = &linebuf[sizeof(linebuf) - 2];
921 do {
922 *pLastChar = '\0';
923 if (Py_UniversalNewlineFgets(linebuf, sizeof linebuf,
924 fp, NULL) == NULL)
925 break;
926 /* fgets read *something*; if it didn't get as
927 far as pLastChar, it must have found a newline
928 or hit the end of the file; if pLastChar is \n,
929 it obviously found a newline; else we haven't
930 yet seen a newline, so must continue */
931 } while (*pLastChar != '\0' && *pLastChar != '\n');
932 }
933 fclose(fp);
934 if (i == lineno) {
935 char *p = linebuf;
936 PyObject *res;
937 while (*p == ' ' || *p == '\t' || *p == '\014')
938 p++;
939 res = PyUnicode_FromString(p);
940 if (res == NULL)
941 PyErr_Clear();
942 return res;
943 }
944 return NULL;
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000945}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000946
947#ifdef __cplusplus
948}
949#endif