blob: e6285e8b3b9cd3ade00c3886863c068da3776c8b [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
Victor Stinnerbd303c12013-11-07 23:07:29 +010023_Py_IDENTIFIER(builtins);
24_Py_IDENTIFIER(stderr);
25
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000026
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000027void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000028PyErr_Restore(PyObject *type, PyObject *value, PyObject *traceback)
Guido van Rossum1ae940a1995-01-02 19:04:15 +000029{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000030 PyThreadState *tstate = PyThreadState_GET();
31 PyObject *oldtype, *oldvalue, *oldtraceback;
Guido van Rossum1ae940a1995-01-02 19:04:15 +000032
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000033 if (traceback != NULL && !PyTraceBack_Check(traceback)) {
34 /* XXX Should never happen -- fatal error instead? */
35 /* Well, it could be None. */
36 Py_DECREF(traceback);
37 traceback = NULL;
38 }
Guido van Rossuma027efa1997-05-05 20:56:21 +000039
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000040 /* Save these in locals to safeguard against recursive
41 invocation through Py_XDECREF */
42 oldtype = tstate->curexc_type;
43 oldvalue = tstate->curexc_value;
44 oldtraceback = tstate->curexc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +000045
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000046 tstate->curexc_type = type;
47 tstate->curexc_value = value;
48 tstate->curexc_traceback = traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +000049
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000050 Py_XDECREF(oldtype);
51 Py_XDECREF(oldvalue);
52 Py_XDECREF(oldtraceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +000053}
54
Victor Stinner3a840972016-08-22 23:59:08 +020055static PyObject*
56_PyErr_CreateException(PyObject *exception, PyObject *value)
57{
58 if (value == NULL || value == Py_None) {
59 return _PyObject_CallNoArg(exception);
60 }
61 else if (PyTuple_Check(value)) {
62 return PyObject_Call(exception, value, NULL);
63 }
64 else {
65 return _PyObject_CallArg1(exception, value);
66 }
67}
68
Guido van Rossum1ae940a1995-01-02 19:04:15 +000069void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000070PyErr_SetObject(PyObject *exception, PyObject *value)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000071{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000072 PyThreadState *tstate = PyThreadState_GET();
73 PyObject *exc_value;
74 PyObject *tb = NULL;
Guido van Rossumb4fb6e42008-06-14 20:20:24 +000075
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000076 if (exception != NULL &&
77 !PyExceptionClass_Check(exception)) {
78 PyErr_Format(PyExc_SystemError,
79 "exception %R not a BaseException subclass",
80 exception);
81 return;
82 }
Victor Stinner3a840972016-08-22 23:59:08 +020083
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000084 Py_XINCREF(value);
85 exc_value = tstate->exc_value;
86 if (exc_value != NULL && exc_value != Py_None) {
87 /* Implicit exception chaining */
88 Py_INCREF(exc_value);
89 if (value == NULL || !PyExceptionInstance_Check(value)) {
90 /* We must normalize the value right now */
Victor Stinner3a840972016-08-22 23:59:08 +020091 PyObject *fixed_value;
Victor Stinnerde821be2015-03-24 12:41:23 +010092
Victor Stinner3a840972016-08-22 23:59:08 +020093 /* Issue #23571: functions must not be called with an
Victor Stinnerde821be2015-03-24 12:41:23 +010094 exception set */
Victor Stinnerace47d72013-07-18 01:41:08 +020095 PyErr_Clear();
Victor Stinnerde821be2015-03-24 12:41:23 +010096
Victor Stinner3a840972016-08-22 23:59:08 +020097 fixed_value = _PyErr_CreateException(exception, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000098 Py_XDECREF(value);
Victor Stinner3a840972016-08-22 23:59:08 +020099 if (fixed_value == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000100 return;
Victor Stinner3a840972016-08-22 23:59:08 +0200101 }
102
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000103 value = fixed_value;
104 }
Victor Stinner3a840972016-08-22 23:59:08 +0200105
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000106 /* Avoid reference cycles through the context chain.
107 This is O(chain length) but context chains are
108 usually very short. Sensitive readers may try
109 to inline the call to PyException_GetContext. */
110 if (exc_value != value) {
111 PyObject *o = exc_value, *context;
112 while ((context = PyException_GetContext(o))) {
113 Py_DECREF(context);
114 if (context == value) {
115 PyException_SetContext(o, NULL);
116 break;
117 }
118 o = context;
119 }
120 PyException_SetContext(value, exc_value);
Victor Stinner3a840972016-08-22 23:59:08 +0200121 }
122 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000123 Py_DECREF(exc_value);
124 }
125 }
126 if (value != NULL && PyExceptionInstance_Check(value))
127 tb = PyException_GetTraceback(value);
128 Py_XINCREF(exception);
129 PyErr_Restore(exception, value, tb);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000130}
131
Raymond Hettinger69492da2013-09-02 15:59:26 -0700132/* Set a key error with the specified argument, wrapping it in a
133 * tuple automatically so that tuple keys are not unpacked as the
134 * exception arguments. */
135void
136_PyErr_SetKeyError(PyObject *arg)
137{
138 PyObject *tup;
139 tup = PyTuple_Pack(1, arg);
140 if (!tup)
141 return; /* caller will expect error to be set anyway */
142 PyErr_SetObject(PyExc_KeyError, tup);
143 Py_DECREF(tup);
144}
145
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000146void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000147PyErr_SetNone(PyObject *exception)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000148{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000149 PyErr_SetObject(exception, (PyObject *)NULL);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000150}
151
152void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000153PyErr_SetString(PyObject *exception, const char *string)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000154{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000155 PyObject *value = PyUnicode_FromString(string);
156 PyErr_SetObject(exception, value);
157 Py_XDECREF(value);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000158}
159
Guido van Rossum3a241811994-08-29 12:14:12 +0000160
Guido van Rossum373c8691997-04-29 18:22:47 +0000161PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000162PyErr_Occurred(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000163{
Victor Stinnerbfd316e2016-01-20 11:12:38 +0100164 PyThreadState *tstate = _PyThreadState_UncheckedGet();
Jeffrey Yasskin8e0bdfd2010-05-13 18:31:05 +0000165 return tstate == NULL ? NULL : tstate->curexc_type;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000166}
167
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000168
169int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000170PyErr_GivenExceptionMatches(PyObject *err, PyObject *exc)
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000171{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000172 if (err == NULL || exc == NULL) {
173 /* maybe caused by "import exceptions" that failed early on */
174 return 0;
175 }
176 if (PyTuple_Check(exc)) {
177 Py_ssize_t i, n;
178 n = PyTuple_Size(exc);
179 for (i = 0; i < n; i++) {
180 /* Test recursively */
181 if (PyErr_GivenExceptionMatches(
182 err, PyTuple_GET_ITEM(exc, i)))
183 {
184 return 1;
185 }
186 }
187 return 0;
188 }
189 /* err might be an instance, so check its class. */
190 if (PyExceptionInstance_Check(err))
191 err = PyExceptionInstance_Class(err);
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000192
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000193 if (PyExceptionClass_Check(err) && PyExceptionClass_Check(exc)) {
194 int res = 0;
195 PyObject *exception, *value, *tb;
196 PyErr_Fetch(&exception, &value, &tb);
197 /* PyObject_IsSubclass() can recurse and therefore is
198 not safe (see test_bad_getattr in test.pickletester). */
199 res = PyType_IsSubtype((PyTypeObject *)err, (PyTypeObject *)exc);
200 /* This function must not fail, so print the error here */
201 if (res == -1) {
202 PyErr_WriteUnraisable(err);
203 res = 0;
204 }
205 PyErr_Restore(exception, value, tb);
206 return res;
207 }
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000208
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000209 return err == exc;
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000210}
Guido van Rossum743007d1999-04-21 15:27:31 +0000211
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000212
213int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000214PyErr_ExceptionMatches(PyObject *exc)
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000215{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000216 return PyErr_GivenExceptionMatches(PyErr_Occurred(), exc);
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000217}
218
219
220/* Used in many places to normalize a raised exception, including in
221 eval_code2(), do_raise(), and PyErr_Print()
Benjamin Petersone6528212008-07-15 15:32:09 +0000222
223 XXX: should PyErr_NormalizeException() also call
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000224 PyException_SetTraceback() with the resulting value and tb?
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000225*/
226void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000227PyErr_NormalizeException(PyObject **exc, PyObject **val, PyObject **tb)
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000228{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000229 PyObject *type = *exc;
230 PyObject *value = *val;
231 PyObject *inclass = NULL;
232 PyObject *initial_tb = NULL;
233 PyThreadState *tstate = NULL;
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000234
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000235 if (type == NULL) {
236 /* There was no exception, so nothing to do. */
237 return;
238 }
Guido van Rossumed473a42000-08-07 19:18:27 +0000239
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000240 /* If PyErr_SetNone() was used, the value will have been actually
241 set to NULL.
242 */
243 if (!value) {
244 value = Py_None;
245 Py_INCREF(value);
246 }
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000247
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000248 if (PyExceptionInstance_Check(value))
249 inclass = PyExceptionInstance_Class(value);
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000250
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000251 /* Normalize the exception so that if the type is a class, the
252 value will be an instance.
253 */
254 if (PyExceptionClass_Check(type)) {
Victor Stinner74a7fa62013-07-17 00:44:53 +0200255 int is_subclass;
256 if (inclass) {
257 is_subclass = PyObject_IsSubclass(inclass, type);
258 if (is_subclass < 0)
259 goto finally;
260 }
261 else
262 is_subclass = 0;
263
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000264 /* if the value was not an instance, or is not an instance
265 whose class is (or is derived from) type, then use the
266 value as an argument to instantiation of the type
267 class.
268 */
Victor Stinner74a7fa62013-07-17 00:44:53 +0200269 if (!inclass || !is_subclass) {
Victor Stinner3a840972016-08-22 23:59:08 +0200270 PyObject *fixed_value;
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000271
Victor Stinner3a840972016-08-22 23:59:08 +0200272 fixed_value = _PyErr_CreateException(type, value);
273 if (fixed_value == NULL) {
274 goto finally;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000275 }
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000276
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000277 Py_DECREF(value);
Victor Stinner3a840972016-08-22 23:59:08 +0200278 value = fixed_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000279 }
280 /* if the class of the instance doesn't exactly match the
281 class of the type, believe the instance
282 */
283 else if (inclass != type) {
284 Py_DECREF(type);
285 type = inclass;
286 Py_INCREF(type);
287 }
288 }
289 *exc = type;
290 *val = value;
291 return;
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000292finally:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000293 Py_DECREF(type);
294 Py_DECREF(value);
295 /* If the new exception doesn't set a traceback and the old
296 exception had a traceback, use the old traceback for the
297 new exception. It's better than nothing.
298 */
299 initial_tb = *tb;
300 PyErr_Fetch(exc, val, tb);
301 if (initial_tb != NULL) {
302 if (*tb == NULL)
303 *tb = initial_tb;
304 else
305 Py_DECREF(initial_tb);
306 }
307 /* normalize recursively */
308 tstate = PyThreadState_GET();
309 if (++tstate->recursion_depth > Py_GetRecursionLimit()) {
310 --tstate->recursion_depth;
Serhiy Storchaka191321d2015-12-27 15:41:34 +0200311 /* throw away the old exception and use the recursion error instead */
312 Py_INCREF(PyExc_RecursionError);
Serhiy Storchaka57a01d32016-04-10 18:05:40 +0300313 Py_SETREF(*exc, PyExc_RecursionError);
Serhiy Storchaka191321d2015-12-27 15:41:34 +0200314 Py_INCREF(PyExc_RecursionErrorInst);
Serhiy Storchaka57a01d32016-04-10 18:05:40 +0300315 Py_SETREF(*val, PyExc_RecursionErrorInst);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000316 /* just keeping the old traceback */
317 return;
318 }
319 PyErr_NormalizeException(exc, val, tb);
320 --tstate->recursion_depth;
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000321}
322
323
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000324void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000325PyErr_Fetch(PyObject **p_type, PyObject **p_value, PyObject **p_traceback)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000326{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000327 PyThreadState *tstate = PyThreadState_GET();
Guido van Rossuma027efa1997-05-05 20:56:21 +0000328
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000329 *p_type = tstate->curexc_type;
330 *p_value = tstate->curexc_value;
331 *p_traceback = tstate->curexc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000332
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000333 tstate->curexc_type = NULL;
334 tstate->curexc_value = NULL;
335 tstate->curexc_traceback = NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000336}
337
338void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000339PyErr_Clear(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000340{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000341 PyErr_Restore(NULL, NULL, NULL);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000342}
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000343
Martin v. Löwisaa2efcb2012-04-19 14:33:43 +0200344void
345PyErr_GetExcInfo(PyObject **p_type, PyObject **p_value, PyObject **p_traceback)
346{
347 PyThreadState *tstate = PyThreadState_GET();
348
349 *p_type = tstate->exc_type;
350 *p_value = tstate->exc_value;
351 *p_traceback = tstate->exc_traceback;
352
353 Py_XINCREF(*p_type);
354 Py_XINCREF(*p_value);
355 Py_XINCREF(*p_traceback);
356}
357
358void
359PyErr_SetExcInfo(PyObject *p_type, PyObject *p_value, PyObject *p_traceback)
360{
361 PyObject *oldtype, *oldvalue, *oldtraceback;
362 PyThreadState *tstate = PyThreadState_GET();
363
364 oldtype = tstate->exc_type;
365 oldvalue = tstate->exc_value;
366 oldtraceback = tstate->exc_traceback;
367
368 tstate->exc_type = p_type;
369 tstate->exc_value = p_value;
370 tstate->exc_traceback = p_traceback;
371
372 Py_XDECREF(oldtype);
373 Py_XDECREF(oldvalue);
374 Py_XDECREF(oldtraceback);
375}
376
Serhiy Storchakae2bd2a72014-10-08 22:31:52 +0300377/* Like PyErr_Restore(), but if an exception is already set,
378 set the context associated with it.
379 */
380void
381_PyErr_ChainExceptions(PyObject *exc, PyObject *val, PyObject *tb)
382{
383 if (exc == NULL)
384 return;
385
386 if (PyErr_Occurred()) {
387 PyObject *exc2, *val2, *tb2;
388 PyErr_Fetch(&exc2, &val2, &tb2);
389 PyErr_NormalizeException(&exc, &val, &tb);
390 Py_DECREF(exc);
391 Py_XDECREF(tb);
392 PyErr_NormalizeException(&exc2, &val2, &tb2);
393 PyException_SetContext(val2, val);
394 PyErr_Restore(exc2, val2, tb2);
395 }
396 else {
397 PyErr_Restore(exc, val, tb);
398 }
399}
400
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000401/* Convenience functions to set a type error exception and return 0 */
402
403int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000404PyErr_BadArgument(void)
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000405{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000406 PyErr_SetString(PyExc_TypeError,
407 "bad argument type for built-in operation");
408 return 0;
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000409}
410
Guido van Rossum373c8691997-04-29 18:22:47 +0000411PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000412PyErr_NoMemory(void)
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000413{
Victor Stinnerf54a5742013-07-22 22:28:37 +0200414 if (Py_TYPE(PyExc_MemoryError) == NULL) {
415 /* PyErr_NoMemory() has been called before PyExc_MemoryError has been
416 initialized by _PyExc_Init() */
417 Py_FatalError("Out of memory and PyExc_MemoryError is not "
418 "initialized yet");
419 }
Antoine Pitrou07e20ef2010-10-28 22:56:58 +0000420 PyErr_SetNone(PyExc_MemoryError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000421 return NULL;
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000422}
423
Guido van Rossum373c8691997-04-29 18:22:47 +0000424PyObject *
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000425PyErr_SetFromErrnoWithFilenameObject(PyObject *exc, PyObject *filenameObject)
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000426{
Larry Hastingsb0827312014-02-09 22:05:19 -0800427 return PyErr_SetFromErrnoWithFilenameObjects(exc, filenameObject, NULL);
428}
429
430PyObject *
431PyErr_SetFromErrnoWithFilenameObjects(PyObject *exc, PyObject *filenameObject, PyObject *filenameObject2)
432{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000433 PyObject *message;
Antoine Pitrou5d6fbe82011-10-12 19:39:57 +0200434 PyObject *v, *args;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000435 int i = errno;
Victor Stinner1f33f2b2011-12-17 04:45:09 +0100436#ifdef MS_WINDOWS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000437 WCHAR *s_buf = NULL;
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000438#endif /* Unix/Windows */
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000439
Guido van Rossume9fbc091995-02-18 14:52:19 +0000440#ifdef EINTR
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000441 if (i == EINTR && PyErr_CheckSignals())
442 return NULL;
Guido van Rossume9fbc091995-02-18 14:52:19 +0000443#endif
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000444
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000445#ifndef MS_WINDOWS
Victor Stinner1f33f2b2011-12-17 04:45:09 +0100446 if (i != 0) {
447 char *s = strerror(i);
Victor Stinner1b579672011-12-17 05:47:23 +0100448 message = PyUnicode_DecodeLocale(s, "surrogateescape");
Victor Stinner1f33f2b2011-12-17 04:45:09 +0100449 }
450 else {
451 /* Sometimes errno didn't get set */
452 message = PyUnicode_FromString("Error");
453 }
Guido van Rossum743007d1999-04-21 15:27:31 +0000454#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000455 if (i == 0)
456 message = PyUnicode_FromString("Error"); /* Sometimes errno didn't get set */
457 else
458 {
459 /* Note that the Win32 errors do not lineup with the
460 errno error. So if the error is in the MSVC error
461 table, we use it, otherwise we assume it really _is_
462 a Win32 error code
463 */
464 if (i > 0 && i < _sys_nerr) {
465 message = PyUnicode_FromString(_sys_errlist[i]);
466 }
467 else {
468 int len = FormatMessageW(
469 FORMAT_MESSAGE_ALLOCATE_BUFFER |
470 FORMAT_MESSAGE_FROM_SYSTEM |
471 FORMAT_MESSAGE_IGNORE_INSERTS,
472 NULL, /* no message source */
473 i,
474 MAKELANGID(LANG_NEUTRAL,
475 SUBLANG_DEFAULT),
476 /* Default language */
477 (LPWSTR) &s_buf,
478 0, /* size not used */
479 NULL); /* no args */
480 if (len==0) {
481 /* Only ever seen this in out-of-mem
482 situations */
483 s_buf = NULL;
Serhiy Storchakaf41f8f92015-04-02 09:47:27 +0300484 message = PyUnicode_FromFormat("Windows Error 0x%x", i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000485 } else {
486 /* remove trailing cr/lf and dots */
487 while (len > 0 && (s_buf[len-1] <= L' ' || s_buf[len-1] == L'.'))
488 s_buf[--len] = L'\0';
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200489 message = PyUnicode_FromWideChar(s_buf, len);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000490 }
491 }
492 }
Martin v. Löwis3484a182002-03-09 12:07:51 +0000493#endif /* Unix/Windows */
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000494
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000495 if (message == NULL)
496 {
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000497#ifdef MS_WINDOWS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000498 LocalFree(s_buf);
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000499#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000500 return NULL;
501 }
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000502
Larry Hastingsb0827312014-02-09 22:05:19 -0800503 if (filenameObject != NULL) {
504 if (filenameObject2 != NULL)
505 args = Py_BuildValue("(iOOiO)", i, message, filenameObject, 0, filenameObject2);
506 else
507 args = Py_BuildValue("(iOO)", i, message, filenameObject);
508 } else {
509 assert(filenameObject2 == NULL);
Antoine Pitrou5d6fbe82011-10-12 19:39:57 +0200510 args = Py_BuildValue("(iO)", i, message);
Larry Hastingsb0827312014-02-09 22:05:19 -0800511 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000512 Py_DECREF(message);
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000513
Antoine Pitrou5d6fbe82011-10-12 19:39:57 +0200514 if (args != NULL) {
515 v = PyObject_Call(exc, args, NULL);
516 Py_DECREF(args);
517 if (v != NULL) {
518 PyErr_SetObject((PyObject *) Py_TYPE(v), v);
519 Py_DECREF(v);
520 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000521 }
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000522#ifdef MS_WINDOWS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000523 LocalFree(s_buf);
Guido van Rossum743007d1999-04-21 15:27:31 +0000524#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000525 return NULL;
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000526}
Guido van Rossum743007d1999-04-21 15:27:31 +0000527
Barry Warsaw97d95151998-07-23 16:05:56 +0000528PyObject *
Neal Norwitzb382b842007-08-24 20:00:37 +0000529PyErr_SetFromErrnoWithFilename(PyObject *exc, const char *filename)
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000530{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000531 PyObject *name = filename ? PyUnicode_DecodeFSDefault(filename) : NULL;
Larry Hastingsb0827312014-02-09 22:05:19 -0800532 PyObject *result = PyErr_SetFromErrnoWithFilenameObjects(exc, name, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000533 Py_XDECREF(name);
534 return result;
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000535}
536
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +0000537#ifdef MS_WINDOWS
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000538PyObject *
Neal Norwitzb382b842007-08-24 20:00:37 +0000539PyErr_SetFromErrnoWithUnicodeFilename(PyObject *exc, 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;
Larry Hastingsb0827312014-02-09 22:05:19 -0800544 PyObject *result = PyErr_SetFromErrnoWithFilenameObjects(exc, name, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000545 Py_XDECREF(name);
546 return result;
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000547}
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +0000548#endif /* MS_WINDOWS */
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000549
550PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000551PyErr_SetFromErrno(PyObject *exc)
Barry Warsaw97d95151998-07-23 16:05:56 +0000552{
Larry Hastingsb0827312014-02-09 22:05:19 -0800553 return PyErr_SetFromErrnoWithFilenameObjects(exc, NULL, NULL);
Barry Warsaw97d95151998-07-23 16:05:56 +0000554}
Guido van Rossum683a0721990-10-21 22:09:12 +0000555
Brett Cannonbf364092006-03-01 04:25:17 +0000556#ifdef MS_WINDOWS
Guido van Rossum795e1892000-02-17 15:19:15 +0000557/* Windows specific error code handling */
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000558PyObject *PyErr_SetExcFromWindowsErrWithFilenameObject(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000559 PyObject *exc,
560 int ierr,
561 PyObject *filenameObject)
Guido van Rossum795e1892000-02-17 15:19:15 +0000562{
Larry Hastingsb0827312014-02-09 22:05:19 -0800563 return PyErr_SetExcFromWindowsErrWithFilenameObjects(exc, ierr,
564 filenameObject, NULL);
565}
566
567PyObject *PyErr_SetExcFromWindowsErrWithFilenameObjects(
568 PyObject *exc,
569 int ierr,
570 PyObject *filenameObject,
571 PyObject *filenameObject2)
572{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000573 int len;
574 WCHAR *s_buf = NULL; /* Free via LocalFree */
575 PyObject *message;
Victor Stinner9ea8e4c2011-10-17 20:18:58 +0200576 PyObject *args, *v;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000577 DWORD err = (DWORD)ierr;
578 if (err==0) err = GetLastError();
579 len = FormatMessageW(
580 /* Error API error */
581 FORMAT_MESSAGE_ALLOCATE_BUFFER |
582 FORMAT_MESSAGE_FROM_SYSTEM |
583 FORMAT_MESSAGE_IGNORE_INSERTS,
584 NULL, /* no message source */
585 err,
586 MAKELANGID(LANG_NEUTRAL,
587 SUBLANG_DEFAULT), /* Default language */
588 (LPWSTR) &s_buf,
589 0, /* size not used */
590 NULL); /* no args */
591 if (len==0) {
592 /* Only seen this in out of mem situations */
Serhiy Storchakaf41f8f92015-04-02 09:47:27 +0300593 message = PyUnicode_FromFormat("Windows Error 0x%x", err);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000594 s_buf = NULL;
595 } else {
596 /* remove trailing cr/lf and dots */
597 while (len > 0 && (s_buf[len-1] <= L' ' || s_buf[len-1] == L'.'))
598 s_buf[--len] = L'\0';
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200599 message = PyUnicode_FromWideChar(s_buf, len);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000600 }
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000601
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000602 if (message == NULL)
603 {
604 LocalFree(s_buf);
605 return NULL;
606 }
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000607
Larry Hastingsb0827312014-02-09 22:05:19 -0800608 if (filenameObject == NULL) {
609 assert(filenameObject2 == NULL);
610 filenameObject = filenameObject2 = Py_None;
611 }
612 else if (filenameObject2 == NULL)
613 filenameObject2 = Py_None;
614 /* This is the constructor signature for OSError.
Antoine Pitrou6b4883d2011-10-12 02:54:14 +0200615 The POSIX translation will be figured out by the constructor. */
Larry Hastingsb0827312014-02-09 22:05:19 -0800616 args = Py_BuildValue("(iOOiO)", 0, message, filenameObject, err, filenameObject2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000617 Py_DECREF(message);
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000618
Victor Stinner9ea8e4c2011-10-17 20:18:58 +0200619 if (args != NULL) {
620 v = PyObject_Call(exc, args, NULL);
621 Py_DECREF(args);
622 if (v != NULL) {
623 PyErr_SetObject((PyObject *) Py_TYPE(v), v);
624 Py_DECREF(v);
625 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000626 }
627 LocalFree(s_buf);
628 return NULL;
Guido van Rossum795e1892000-02-17 15:19:15 +0000629}
630
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000631PyObject *PyErr_SetExcFromWindowsErrWithFilename(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000632 PyObject *exc,
633 int ierr,
634 const char *filename)
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000635{
Victor Stinner92be9392010-12-28 00:28:21 +0000636 PyObject *name = filename ? PyUnicode_DecodeFSDefault(filename) : NULL;
Larry Hastingsb0827312014-02-09 22:05:19 -0800637 PyObject *ret = PyErr_SetExcFromWindowsErrWithFilenameObjects(exc,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000638 ierr,
Larry Hastingsb0827312014-02-09 22:05:19 -0800639 name,
640 NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000641 Py_XDECREF(name);
642 return ret;
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000643}
644
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000645PyObject *PyErr_SetExcFromWindowsErrWithUnicodeFilename(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000646 PyObject *exc,
647 int ierr,
648 const Py_UNICODE *filename)
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000649{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000650 PyObject *name = filename ?
651 PyUnicode_FromUnicode(filename, wcslen(filename)) :
652 NULL;
Larry Hastingsb0827312014-02-09 22:05:19 -0800653 PyObject *ret = PyErr_SetExcFromWindowsErrWithFilenameObjects(exc,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000654 ierr,
Larry Hastingsb0827312014-02-09 22:05:19 -0800655 name,
656 NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000657 Py_XDECREF(name);
658 return ret;
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000659}
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000660
Thomas Heller085358a2002-07-29 14:27:41 +0000661PyObject *PyErr_SetExcFromWindowsErr(PyObject *exc, int ierr)
662{
Larry Hastings8f9f0f12014-02-10 03:43:57 -0800663 return PyErr_SetExcFromWindowsErrWithFilename(exc, ierr, NULL);
Thomas Heller085358a2002-07-29 14:27:41 +0000664}
665
Guido van Rossum795e1892000-02-17 15:19:15 +0000666PyObject *PyErr_SetFromWindowsErr(int ierr)
667{
Larry Hastings8f9f0f12014-02-10 03:43:57 -0800668 return PyErr_SetExcFromWindowsErrWithFilename(PyExc_OSError,
669 ierr, NULL);
Larry Hastingsb0827312014-02-09 22:05:19 -0800670}
671
Thomas Heller085358a2002-07-29 14:27:41 +0000672PyObject *PyErr_SetFromWindowsErrWithFilename(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000673 int ierr,
674 const char *filename)
Thomas Heller085358a2002-07-29 14:27:41 +0000675{
Victor Stinner92be9392010-12-28 00:28:21 +0000676 PyObject *name = filename ? PyUnicode_DecodeFSDefault(filename) : NULL;
Larry Hastingsb0827312014-02-09 22:05:19 -0800677 PyObject *result = PyErr_SetExcFromWindowsErrWithFilenameObjects(
Andrew Svetlov2606a6f2012-12-19 14:33:35 +0200678 PyExc_OSError,
Larry Hastingsb0827312014-02-09 22:05:19 -0800679 ierr, name, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000680 Py_XDECREF(name);
681 return result;
Guido van Rossum795e1892000-02-17 15:19:15 +0000682}
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000683
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000684PyObject *PyErr_SetFromWindowsErrWithUnicodeFilename(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000685 int ierr,
686 const Py_UNICODE *filename)
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000687{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000688 PyObject *name = filename ?
689 PyUnicode_FromUnicode(filename, wcslen(filename)) :
690 NULL;
Larry Hastingsb0827312014-02-09 22:05:19 -0800691 PyObject *result = PyErr_SetExcFromWindowsErrWithFilenameObjects(
Andrew Svetlov2606a6f2012-12-19 14:33:35 +0200692 PyExc_OSError,
Larry Hastingsb0827312014-02-09 22:05:19 -0800693 ierr, name, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000694 Py_XDECREF(name);
695 return result;
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000696}
Guido van Rossum795e1892000-02-17 15:19:15 +0000697#endif /* MS_WINDOWS */
698
Brett Cannon79ec55e2012-04-12 20:24:54 -0400699PyObject *
Brett Cannon82da8882013-07-04 17:48:16 -0400700PyErr_SetImportError(PyObject *msg, PyObject *name, PyObject *path)
Brett Cannon79ec55e2012-04-12 20:24:54 -0400701{
Victor Stinnerf45a5612016-08-23 00:04:41 +0200702 PyObject *kwargs, *error;
Brian Curtin09b86d12012-04-17 16:57:09 -0500703
Victor Stinnerf45a5612016-08-23 00:04:41 +0200704 if (msg == NULL) {
Brian Curtin94c001b2012-04-18 08:30:51 -0500705 return NULL;
Victor Stinnerf45a5612016-08-23 00:04:41 +0200706 }
Brian Curtin09b86d12012-04-17 16:57:09 -0500707
708 kwargs = PyDict_New();
Benjamin Petersonda20cd22012-04-18 10:48:00 -0400709 if (kwargs == NULL) {
Brian Curtin09b86d12012-04-17 16:57:09 -0500710 return NULL;
Benjamin Petersonda20cd22012-04-18 10:48:00 -0400711 }
Brian Curtin09b86d12012-04-17 16:57:09 -0500712
Brian Curtin94c001b2012-04-18 08:30:51 -0500713 if (name == NULL) {
Brian Curtin09b86d12012-04-17 16:57:09 -0500714 name = Py_None;
Brian Curtin94c001b2012-04-18 08:30:51 -0500715 }
Brian Curtin09b86d12012-04-17 16:57:09 -0500716
Brian Curtin94c001b2012-04-18 08:30:51 -0500717 if (path == NULL) {
Brian Curtin09b86d12012-04-17 16:57:09 -0500718 path = Py_None;
Brian Curtin94c001b2012-04-18 08:30:51 -0500719 }
Brian Curtin09b86d12012-04-17 16:57:09 -0500720
Victor Stinnerf45a5612016-08-23 00:04:41 +0200721 if (PyDict_SetItemString(kwargs, "name", name) < 0) {
Berker Peksagec766d32016-05-01 09:06:36 +0300722 goto done;
Victor Stinnerf45a5612016-08-23 00:04:41 +0200723 }
724 if (PyDict_SetItemString(kwargs, "path", path) < 0) {
Berker Peksagec766d32016-05-01 09:06:36 +0300725 goto done;
Victor Stinnerf45a5612016-08-23 00:04:41 +0200726 }
Brett Cannon79ec55e2012-04-12 20:24:54 -0400727
Victor Stinnerf45a5612016-08-23 00:04:41 +0200728 error = _PyObject_FastCallDict(PyExc_ImportError, &msg, 1, kwargs);
Benjamin Petersonda20cd22012-04-18 10:48:00 -0400729 if (error != NULL) {
730 PyErr_SetObject((PyObject *)Py_TYPE(error), error);
Brian Curtin09b86d12012-04-17 16:57:09 -0500731 Py_DECREF(error);
Brett Cannon79ec55e2012-04-12 20:24:54 -0400732 }
733
Berker Peksagec766d32016-05-01 09:06:36 +0300734done:
Brett Cannon79ec55e2012-04-12 20:24:54 -0400735 Py_DECREF(kwargs);
Brian Curtin09b86d12012-04-17 16:57:09 -0500736 return NULL;
Brett Cannon79ec55e2012-04-12 20:24:54 -0400737}
738
Guido van Rossum683a0721990-10-21 22:09:12 +0000739void
Neal Norwitzb382b842007-08-24 20:00:37 +0000740_PyErr_BadInternalCall(const char *filename, int lineno)
Fred Drake6d63adf2000-08-24 22:38:39 +0000741{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000742 PyErr_Format(PyExc_SystemError,
743 "%s:%d: bad argument to internal function",
744 filename, lineno);
Fred Drake6d63adf2000-08-24 22:38:39 +0000745}
746
747/* Remove the preprocessor macro for PyErr_BadInternalCall() so that we can
748 export the entry point for existing object code: */
749#undef PyErr_BadInternalCall
750void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000751PyErr_BadInternalCall(void)
Guido van Rossum683a0721990-10-21 22:09:12 +0000752{
Victor Stinnerfb3a6302013-07-12 00:37:30 +0200753 assert(0 && "bad argument to internal function");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000754 PyErr_Format(PyExc_SystemError,
755 "bad argument to internal function");
Guido van Rossum683a0721990-10-21 22:09:12 +0000756}
Fred Drake6d63adf2000-08-24 22:38:39 +0000757#define PyErr_BadInternalCall() _PyErr_BadInternalCall(__FILE__, __LINE__)
758
Guido van Rossum1548bac1997-02-14 17:09:47 +0000759
Guido van Rossum1548bac1997-02-14 17:09:47 +0000760PyObject *
Antoine Pitrou0676a402014-09-30 21:16:27 +0200761PyErr_FormatV(PyObject *exception, const char *format, va_list vargs)
Guido van Rossum1548bac1997-02-14 17:09:47 +0000762{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000763 PyObject* string;
Guido van Rossum1548bac1997-02-14 17:09:47 +0000764
Victor Stinnerde821be2015-03-24 12:41:23 +0100765 /* Issue #23571: PyUnicode_FromFormatV() must not be called with an
766 exception set, it calls arbitrary Python code like PyObject_Repr() */
Victor Stinnerace47d72013-07-18 01:41:08 +0200767 PyErr_Clear();
Victor Stinnerace47d72013-07-18 01:41:08 +0200768
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000769 string = PyUnicode_FromFormatV(format, vargs);
Victor Stinnerde821be2015-03-24 12:41:23 +0100770
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000771 PyErr_SetObject(exception, string);
772 Py_XDECREF(string);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000773 return NULL;
Guido van Rossum1548bac1997-02-14 17:09:47 +0000774}
Guido van Rossum7617e051997-09-16 18:43:50 +0000775
776
Antoine Pitrou0676a402014-09-30 21:16:27 +0200777PyObject *
778PyErr_Format(PyObject *exception, const char *format, ...)
779{
780 va_list vargs;
781#ifdef HAVE_STDARG_PROTOTYPES
782 va_start(vargs, format);
783#else
784 va_start(vargs);
785#endif
786 PyErr_FormatV(exception, format, vargs);
787 va_end(vargs);
788 return NULL;
789}
790
Thomas Wouters477c8d52006-05-27 19:21:47 +0000791
Guido van Rossum7617e051997-09-16 18:43:50 +0000792PyObject *
Neal Norwitzb382b842007-08-24 20:00:37 +0000793PyErr_NewException(const char *name, PyObject *base, PyObject *dict)
Guido van Rossum7617e051997-09-16 18:43:50 +0000794{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000795 const char *dot;
796 PyObject *modulename = NULL;
797 PyObject *classname = NULL;
798 PyObject *mydict = NULL;
799 PyObject *bases = NULL;
800 PyObject *result = NULL;
801 dot = strrchr(name, '.');
802 if (dot == NULL) {
803 PyErr_SetString(PyExc_SystemError,
804 "PyErr_NewException: name must be module.class");
805 return NULL;
806 }
807 if (base == NULL)
808 base = PyExc_Exception;
809 if (dict == NULL) {
810 dict = mydict = PyDict_New();
811 if (dict == NULL)
812 goto failure;
813 }
814 if (PyDict_GetItemString(dict, "__module__") == NULL) {
815 modulename = PyUnicode_FromStringAndSize(name,
816 (Py_ssize_t)(dot-name));
817 if (modulename == NULL)
818 goto failure;
819 if (PyDict_SetItemString(dict, "__module__", modulename) != 0)
820 goto failure;
821 }
822 if (PyTuple_Check(base)) {
823 bases = base;
824 /* INCREF as we create a new ref in the else branch */
825 Py_INCREF(bases);
826 } else {
827 bases = PyTuple_Pack(1, base);
828 if (bases == NULL)
829 goto failure;
830 }
Florent Xiclunaaa6c1d22011-12-12 18:54:29 +0100831 /* Create a real class. */
Victor Stinner7eeb5b52010-06-07 19:57:46 +0000832 result = PyObject_CallFunction((PyObject *)&PyType_Type, "sOO",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000833 dot+1, bases, dict);
Guido van Rossum7617e051997-09-16 18:43:50 +0000834 failure:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000835 Py_XDECREF(bases);
836 Py_XDECREF(mydict);
837 Py_XDECREF(classname);
838 Py_XDECREF(modulename);
839 return result;
Guido van Rossum7617e051997-09-16 18:43:50 +0000840}
Jeremy Hyltonb709df32000-09-01 02:47:25 +0000841
Georg Brandl1e28a272009-12-28 08:41:01 +0000842
843/* Create an exception with docstring */
844PyObject *
845PyErr_NewExceptionWithDoc(const char *name, const char *doc,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000846 PyObject *base, PyObject *dict)
Georg Brandl1e28a272009-12-28 08:41:01 +0000847{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000848 int result;
849 PyObject *ret = NULL;
850 PyObject *mydict = NULL; /* points to the dict only if we create it */
851 PyObject *docobj;
Georg Brandl1e28a272009-12-28 08:41:01 +0000852
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000853 if (dict == NULL) {
854 dict = mydict = PyDict_New();
855 if (dict == NULL) {
856 return NULL;
857 }
858 }
Georg Brandl1e28a272009-12-28 08:41:01 +0000859
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000860 if (doc != NULL) {
861 docobj = PyUnicode_FromString(doc);
862 if (docobj == NULL)
863 goto failure;
864 result = PyDict_SetItemString(dict, "__doc__", docobj);
865 Py_DECREF(docobj);
866 if (result < 0)
867 goto failure;
868 }
Georg Brandl1e28a272009-12-28 08:41:01 +0000869
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000870 ret = PyErr_NewException(name, base, dict);
Georg Brandl1e28a272009-12-28 08:41:01 +0000871 failure:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000872 Py_XDECREF(mydict);
873 return ret;
Georg Brandl1e28a272009-12-28 08:41:01 +0000874}
875
876
Jeremy Hyltonb709df32000-09-01 02:47:25 +0000877/* Call when an exception has occurred but there is no way for Python
878 to handle it. Examples: exception in __del__ or during GC. */
879void
880PyErr_WriteUnraisable(PyObject *obj)
881{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200882 _Py_IDENTIFIER(__module__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000883 PyObject *f, *t, *v, *tb;
Victor Stinnerc82bfd82013-08-26 14:04:10 +0200884 PyObject *moduleName = NULL;
885 char* className;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000886
Victor Stinnerc82bfd82013-08-26 14:04:10 +0200887 PyErr_Fetch(&t, &v, &tb);
888
Victor Stinnerbd303c12013-11-07 23:07:29 +0100889 f = _PySys_GetObjectId(&PyId_stderr);
Victor Stinnerc82bfd82013-08-26 14:04:10 +0200890 if (f == NULL || f == Py_None)
891 goto done;
892
893 if (obj) {
894 if (PyFile_WriteString("Exception ignored in: ", f) < 0)
895 goto done;
Martin Panter3263f682016-02-28 03:16:11 +0000896 if (PyFile_WriteObject(obj, f, 0) < 0) {
897 PyErr_Clear();
898 if (PyFile_WriteString("<object repr() failed>", f) < 0) {
899 goto done;
900 }
901 }
Victor Stinnerc82bfd82013-08-26 14:04:10 +0200902 if (PyFile_WriteString("\n", f) < 0)
903 goto done;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000904 }
Victor Stinnerc82bfd82013-08-26 14:04:10 +0200905
906 if (PyTraceBack_Print(tb, f) < 0)
907 goto done;
908
909 if (!t)
910 goto done;
911
912 assert(PyExceptionClass_Check(t));
913 className = PyExceptionClass_Name(t);
914 if (className != NULL) {
915 char *dot = strrchr(className, '.');
916 if (dot != NULL)
917 className = dot+1;
918 }
919
920 moduleName = _PyObject_GetAttrId(t, &PyId___module__);
921 if (moduleName == NULL) {
922 PyErr_Clear();
923 if (PyFile_WriteString("<unknown>", f) < 0)
924 goto done;
925 }
926 else {
Victor Stinnerbd303c12013-11-07 23:07:29 +0100927 if (_PyUnicode_CompareWithId(moduleName, &PyId_builtins) != 0) {
Victor Stinnerc82bfd82013-08-26 14:04:10 +0200928 if (PyFile_WriteObject(moduleName, f, Py_PRINT_RAW) < 0)
929 goto done;
930 if (PyFile_WriteString(".", f) < 0)
931 goto done;
932 }
933 }
934 if (className == NULL) {
935 if (PyFile_WriteString("<unknown>", f) < 0)
936 goto done;
937 }
938 else {
939 if (PyFile_WriteString(className, f) < 0)
940 goto done;
941 }
942
943 if (v && v != Py_None) {
944 if (PyFile_WriteString(": ", f) < 0)
945 goto done;
Martin Panter3263f682016-02-28 03:16:11 +0000946 if (PyFile_WriteObject(v, f, Py_PRINT_RAW) < 0) {
947 PyErr_Clear();
948 if (PyFile_WriteString("<exception str() failed>", f) < 0) {
949 goto done;
950 }
951 }
Victor Stinnerc82bfd82013-08-26 14:04:10 +0200952 }
953 if (PyFile_WriteString("\n", f) < 0)
954 goto done;
955
956done:
957 Py_XDECREF(moduleName);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000958 Py_XDECREF(t);
959 Py_XDECREF(v);
960 Py_XDECREF(tb);
Victor Stinnerc82bfd82013-08-26 14:04:10 +0200961 PyErr_Clear(); /* Just in case */
Jeremy Hyltonb709df32000-09-01 02:47:25 +0000962}
Guido van Rossumcfd42b52000-12-15 21:58:52 +0000963
Armin Rigo092381a2003-10-25 14:29:27 +0000964extern PyObject *PyModule_GetWarningsModule(void);
Guido van Rossumcfd42b52000-12-15 21:58:52 +0000965
Guido van Rossum2fd45652001-02-28 21:46:24 +0000966
Benjamin Peterson2c539712010-09-20 22:42:10 +0000967void
Victor Stinner14e461d2013-08-26 22:28:21 +0200968PyErr_SyntaxLocation(const char *filename, int lineno)
969{
Benjamin Peterson2c539712010-09-20 22:42:10 +0000970 PyErr_SyntaxLocationEx(filename, lineno, -1);
971}
972
973
Martin v. Löwiscfeb3b62002-03-03 21:30:27 +0000974/* Set file and line information for the current exception.
975 If the exception is not a SyntaxError, also sets additional attributes
976 to make printing of exceptions believe it is a syntax error. */
Guido van Rossum2fd45652001-02-28 21:46:24 +0000977
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000978void
Victor Stinner14e461d2013-08-26 22:28:21 +0200979PyErr_SyntaxLocationObject(PyObject *filename, int lineno, int col_offset)
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000980{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000981 PyObject *exc, *v, *tb, *tmp;
Martin v. Löwis1c67dd92011-10-14 15:16:45 +0200982 _Py_IDENTIFIER(filename);
983 _Py_IDENTIFIER(lineno);
984 _Py_IDENTIFIER(msg);
985 _Py_IDENTIFIER(offset);
986 _Py_IDENTIFIER(print_file_and_line);
987 _Py_IDENTIFIER(text);
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000988
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000989 /* add attributes for the line number and filename for the error */
990 PyErr_Fetch(&exc, &v, &tb);
991 PyErr_NormalizeException(&exc, &v, &tb);
992 /* XXX check that it is, indeed, a syntax error. It might not
993 * be, though. */
994 tmp = PyLong_FromLong(lineno);
995 if (tmp == NULL)
996 PyErr_Clear();
997 else {
Martin v. Löwis1c67dd92011-10-14 15:16:45 +0200998 if (_PyObject_SetAttrId(v, &PyId_lineno, tmp))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000999 PyErr_Clear();
1000 Py_DECREF(tmp);
1001 }
Benjamin Peterson2c539712010-09-20 22:42:10 +00001002 if (col_offset >= 0) {
1003 tmp = PyLong_FromLong(col_offset);
1004 if (tmp == NULL)
1005 PyErr_Clear();
1006 else {
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02001007 if (_PyObject_SetAttrId(v, &PyId_offset, tmp))
Benjamin Peterson2c539712010-09-20 22:42:10 +00001008 PyErr_Clear();
1009 Py_DECREF(tmp);
1010 }
1011 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001012 if (filename != NULL) {
Victor Stinner14e461d2013-08-26 22:28:21 +02001013 if (_PyObject_SetAttrId(v, &PyId_filename, filename))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001014 PyErr_Clear();
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +00001015
Victor Stinner14e461d2013-08-26 22:28:21 +02001016 tmp = PyErr_ProgramTextObject(filename, lineno);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001017 if (tmp) {
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02001018 if (_PyObject_SetAttrId(v, &PyId_text, tmp))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001019 PyErr_Clear();
1020 Py_DECREF(tmp);
1021 }
1022 }
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02001023 if (_PyObject_SetAttrId(v, &PyId_offset, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001024 PyErr_Clear();
1025 }
1026 if (exc != PyExc_SyntaxError) {
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02001027 if (!_PyObject_HasAttrId(v, &PyId_msg)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001028 tmp = PyObject_Str(v);
1029 if (tmp) {
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02001030 if (_PyObject_SetAttrId(v, &PyId_msg, tmp))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001031 PyErr_Clear();
1032 Py_DECREF(tmp);
1033 } else {
1034 PyErr_Clear();
1035 }
1036 }
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02001037 if (!_PyObject_HasAttrId(v, &PyId_print_file_and_line)) {
1038 if (_PyObject_SetAttrId(v, &PyId_print_file_and_line,
1039 Py_None))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001040 PyErr_Clear();
1041 }
1042 }
1043 PyErr_Restore(exc, v, tb);
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +00001044}
1045
Victor Stinner14e461d2013-08-26 22:28:21 +02001046void
1047PyErr_SyntaxLocationEx(const char *filename, int lineno, int col_offset)
1048{
1049 PyObject *fileobj;
1050 if (filename != NULL) {
1051 fileobj = PyUnicode_DecodeFSDefault(filename);
1052 if (fileobj == NULL)
1053 PyErr_Clear();
1054 }
1055 else
1056 fileobj = NULL;
1057 PyErr_SyntaxLocationObject(fileobj, lineno, col_offset);
1058 Py_XDECREF(fileobj);
1059}
1060
Guido van Rossumebe8f8a2007-10-10 18:53:36 +00001061/* Attempt to load the line of text that the exception refers to. If it
1062 fails, it will return NULL but will not set an exception.
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +00001063
1064 XXX The functionality of this function is quite similar to the
Guido van Rossumebe8f8a2007-10-10 18:53:36 +00001065 functionality in tb_displayline() in traceback.c. */
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +00001066
Antoine Pitrou409b5382013-10-12 22:41:17 +02001067static PyObject *
Victor Stinner14e461d2013-08-26 22:28:21 +02001068err_programtext(FILE *fp, int lineno)
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +00001069{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001070 int i;
1071 char linebuf[1000];
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +00001072
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001073 if (fp == NULL)
1074 return NULL;
1075 for (i = 0; i < lineno; i++) {
1076 char *pLastChar = &linebuf[sizeof(linebuf) - 2];
1077 do {
1078 *pLastChar = '\0';
1079 if (Py_UniversalNewlineFgets(linebuf, sizeof linebuf,
1080 fp, NULL) == NULL)
1081 break;
1082 /* fgets read *something*; if it didn't get as
1083 far as pLastChar, it must have found a newline
1084 or hit the end of the file; if pLastChar is \n,
1085 it obviously found a newline; else we haven't
1086 yet seen a newline, so must continue */
1087 } while (*pLastChar != '\0' && *pLastChar != '\n');
1088 }
1089 fclose(fp);
1090 if (i == lineno) {
1091 char *p = linebuf;
1092 PyObject *res;
1093 while (*p == ' ' || *p == '\t' || *p == '\014')
1094 p++;
1095 res = PyUnicode_FromString(p);
1096 if (res == NULL)
1097 PyErr_Clear();
1098 return res;
1099 }
1100 return NULL;
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +00001101}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001102
Victor Stinner14e461d2013-08-26 22:28:21 +02001103PyObject *
1104PyErr_ProgramText(const char *filename, int lineno)
1105{
1106 FILE *fp;
1107 if (filename == NULL || *filename == '\0' || lineno <= 0)
1108 return NULL;
Victor Stinnerdaf45552013-08-28 00:53:59 +02001109 fp = _Py_fopen(filename, "r" PY_STDIOTEXTMODE);
Victor Stinner14e461d2013-08-26 22:28:21 +02001110 return err_programtext(fp, lineno);
1111}
1112
1113PyObject *
1114PyErr_ProgramTextObject(PyObject *filename, int lineno)
1115{
1116 FILE *fp;
1117 if (filename == NULL || lineno <= 0)
1118 return NULL;
Victor Stinnerdaf45552013-08-28 00:53:59 +02001119 fp = _Py_fopen_obj(filename, "r" PY_STDIOTEXTMODE);
Victor Stinnere42ccd22015-03-18 01:39:23 +01001120 if (fp == NULL) {
1121 PyErr_Clear();
1122 return NULL;
1123 }
Victor Stinner14e461d2013-08-26 22:28:21 +02001124 return err_programtext(fp, lineno);
1125}
1126
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001127#ifdef __cplusplus
1128}
1129#endif