1 /***********************************************************
2 Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
7 Permission to use, copy, modify, and distribute this software and its
8 documentation for any purpose and without fee is hereby granted,
9 provided that the above copyright notice appear in all copies and that
10 both that copyright notice and this permission notice appear in
11 supporting documentation, and that the names of Stichting Mathematisch
12 Centrum or CWI or Corporation for National Research Initiatives or
13 CNRI not be used in advertising or publicity pertaining to
14 distribution of the software without specific, written prior
17 While CWI is the initial source for this software, a modified version
18 is made available by the Corporation for National Research Initiatives
19 (CNRI) at the Internet address ftp://ftp.python.org.
21 STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
22 REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
23 MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
24 CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
25 DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
26 PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
27 TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
28 PERFORMANCE OF THIS SOFTWARE.
30 ******************************************************************/
36 #ifdef SYMANTEC__CFM68K__
41 extern char *PyMac_StrError
Py_PROTO((int));
43 #define strerror PyMac_StrError
44 #endif /* macintosh */
48 extern char *strerror
Py_PROTO((int));
53 PyErr_Restore(type
, value
, traceback
)
58 PyThreadState
*tstate
= PyThreadState_Get();
59 PyObject
*oldtype
, *oldvalue
, *oldtraceback
;
61 if (traceback
!= NULL
&& !PyTraceBack_Check(traceback
)) {
62 /* XXX Should never happen -- fatal error instead? */
67 /* Save these in locals to safeguard against recursive
68 invocation through Py_XDECREF */
69 oldtype
= tstate
->curexc_type
;
70 oldvalue
= tstate
->curexc_value
;
71 oldtraceback
= tstate
->curexc_traceback
;
73 tstate
->curexc_type
= type
;
74 tstate
->curexc_value
= value
;
75 tstate
->curexc_traceback
= traceback
;
79 Py_XDECREF(oldtraceback
);
83 PyErr_SetObject(exception
, value
)
87 Py_XINCREF(exception
);
89 PyErr_Restore(exception
, value
, (PyObject
*)NULL
);
93 PyErr_SetNone(exception
)
96 PyErr_SetObject(exception
, (PyObject
*)NULL
);
100 PyErr_SetString(exception
, string
)
104 PyObject
*value
= PyString_FromString(string
);
105 PyErr_SetObject(exception
, value
);
113 PyThreadState
*tstate
= PyThreadState_Get();
115 return tstate
->curexc_type
;
120 PyErr_GivenExceptionMatches(err
, exc
)
123 if (PyTuple_Check(exc
)) {
125 n
= PyTuple_Size(exc
);
126 for (i
= 0; i
< n
; i
++) {
127 /* Test recursively */
128 if (PyErr_GivenExceptionMatches(
129 err
, PyTuple_GET_ITEM(exc
, i
)))
136 /* err might be an instance, so check its class. */
137 if (PyInstance_Check(err
))
138 err
= (PyObject
*)((PyInstanceObject
*)err
)->in_class
;
140 if (PyClass_Check(err
) && PyClass_Check(exc
))
141 return PyClass_IsSubclass(err
, exc
);
148 PyErr_ExceptionMatches(exc
)
151 return PyErr_GivenExceptionMatches(PyErr_Occurred(), exc
);
155 /* Used in many places to normalize a raised exception, including in
156 eval_code2(), do_raise(), and PyErr_Print()
159 PyErr_NormalizeException(exc
, val
, tb
)
164 PyObject
*type
= *exc
;
165 PyObject
*value
= *val
;
166 PyObject
*inclass
= NULL
;
168 /* If PyErr_SetNone() was used, the value will have been actually
176 if (PyInstance_Check(value
))
177 inclass
= (PyObject
*)((PyInstanceObject
*)value
)->in_class
;
179 /* Normalize the exception so that if the type is a class, the
180 value will be an instance.
182 if (PyClass_Check(type
)) {
183 /* if the value was not an instance, or is not an instance
184 whose class is (or is derived from) type, then use the
185 value as an argument to instantiation of the type
188 if (!inclass
|| !PyClass_IsSubclass(inclass
, type
)) {
189 PyObject
*args
, *res
;
191 if (value
== Py_None
)
192 args
= Py_BuildValue("()");
193 else if (PyTuple_Check(value
)) {
198 args
= Py_BuildValue("(O)", value
);
202 res
= PyEval_CallObject(type
, args
);
209 /* if the class of the instance doesn't exactly match the
210 class of the type, believe the instance
212 else if (inclass
!= type
) {
225 PyErr_Fetch(exc
, val
, tb
);
226 /* normalize recursively */
227 PyErr_NormalizeException(exc
, val
, tb
);
232 PyErr_Fetch(p_type
, p_value
, p_traceback
)
235 PyObject
**p_traceback
;
237 PyThreadState
*tstate
= PyThreadState_Get();
239 *p_type
= tstate
->curexc_type
;
240 *p_value
= tstate
->curexc_value
;
241 *p_traceback
= tstate
->curexc_traceback
;
243 tstate
->curexc_type
= NULL
;
244 tstate
->curexc_value
= NULL
;
245 tstate
->curexc_traceback
= NULL
;
251 PyErr_Restore(NULL
, NULL
, NULL
);
254 /* Convenience functions to set a type error exception and return 0 */
259 PyErr_SetString(PyExc_TypeError
,
260 "illegal argument type for built-in operation");
267 /* raise the pre-allocated instance if it still exists */
268 if (PyExc_MemoryErrorInst
)
269 PyErr_SetObject(PyExc_MemoryError
, PyExc_MemoryErrorInst
);
271 /* this will probably fail since there's no memory and hee,
272 hee, we have to instantiate this class
274 PyErr_SetNone(PyExc_MemoryError
);
280 PyErr_SetFromErrnoWithFilename(exc
, filename
)
288 if (i
== EINTR
&& PyErr_CheckSignals())
292 s
= "Error"; /* Sometimes errno didn't get set */
295 if (filename
!= NULL
&& Py_UseClassExceptionsFlag
)
296 v
= Py_BuildValue("(iss)", i
, s
, filename
);
298 v
= Py_BuildValue("(is)", i
, s
);
300 PyErr_SetObject(exc
, v
);
308 PyErr_SetFromErrno(exc
)
311 return PyErr_SetFromErrnoWithFilename(exc
, NULL
);
315 PyErr_BadInternalCall()
317 PyErr_SetString(PyExc_SystemError
,
318 "bad argument to internal function");
322 #ifdef HAVE_STDARG_PROTOTYPES
324 PyErr_Format(PyObject
*exception
, const char *format
, ...)
327 PyErr_Format(exception
, format
, va_alist
)
334 char buffer
[500]; /* Caller is responsible for limiting the format */
336 #ifdef HAVE_STDARG_PROTOTYPES
337 va_start(vargs
, format
);
342 vsprintf(buffer
, format
, vargs
);
343 PyErr_SetString(exception
, buffer
);
349 PyErr_NewException(name
, base
, dict
)
350 char *name
; /* modulename.classname */
355 PyObject
*modulename
= NULL
;
356 PyObject
*classname
= NULL
;
357 PyObject
*mydict
= NULL
;
358 PyObject
*bases
= NULL
;
359 PyObject
*result
= NULL
;
360 dot
= strrchr(name
, '.');
362 PyErr_SetString(PyExc_SystemError
,
363 "PyErr_NewException: name must be module.class");
367 base
= PyExc_Exception
;
368 if (!PyClass_Check(base
)) {
369 /* Must be using string-based standard exceptions (-X) */
370 return PyString_FromString(name
);
373 dict
= mydict
= PyDict_New();
377 if (PyDict_GetItemString(dict
, "__module__") == NULL
) {
378 modulename
= PyString_FromStringAndSize(name
, (int)(dot
-name
));
379 if (modulename
== NULL
)
381 if (PyDict_SetItemString(dict
, "__module__", modulename
) != 0)
384 classname
= PyString_FromString(dot
+1);
385 if (classname
== NULL
)
387 bases
= Py_BuildValue("(O)", base
);
390 result
= PyClass_New(bases
, dict
, classname
);
394 Py_XDECREF(classname
);
395 Py_XDECREF(modulename
);