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));
58 PyErr_Restore(type
, value
, traceback
)
63 PyThreadState
*tstate
= PyThreadState_GET();
64 PyObject
*oldtype
, *oldvalue
, *oldtraceback
;
66 if (traceback
!= NULL
&& !PyTraceBack_Check(traceback
)) {
67 /* XXX Should never happen -- fatal error instead? */
72 /* Save these in locals to safeguard against recursive
73 invocation through Py_XDECREF */
74 oldtype
= tstate
->curexc_type
;
75 oldvalue
= tstate
->curexc_value
;
76 oldtraceback
= tstate
->curexc_traceback
;
78 tstate
->curexc_type
= type
;
79 tstate
->curexc_value
= value
;
80 tstate
->curexc_traceback
= traceback
;
84 Py_XDECREF(oldtraceback
);
88 PyErr_SetObject(exception
, value
)
92 Py_XINCREF(exception
);
94 PyErr_Restore(exception
, value
, (PyObject
*)NULL
);
98 PyErr_SetNone(exception
)
101 PyErr_SetObject(exception
, (PyObject
*)NULL
);
105 PyErr_SetString(exception
, string
)
109 PyObject
*value
= PyString_FromString(string
);
110 PyErr_SetObject(exception
, value
);
118 PyThreadState
*tstate
= PyThreadState_Get();
120 return tstate
->curexc_type
;
125 PyErr_GivenExceptionMatches(err
, exc
)
128 if (PyTuple_Check(exc
)) {
130 n
= PyTuple_Size(exc
);
131 for (i
= 0; i
< n
; i
++) {
132 /* Test recursively */
133 if (PyErr_GivenExceptionMatches(
134 err
, PyTuple_GET_ITEM(exc
, i
)))
141 /* err might be an instance, so check its class. */
142 if (PyInstance_Check(err
))
143 err
= (PyObject
*)((PyInstanceObject
*)err
)->in_class
;
145 if (PyClass_Check(err
) && PyClass_Check(exc
))
146 return PyClass_IsSubclass(err
, exc
);
153 PyErr_ExceptionMatches(exc
)
156 return PyErr_GivenExceptionMatches(PyErr_Occurred(), exc
);
160 /* Used in many places to normalize a raised exception, including in
161 eval_code2(), do_raise(), and PyErr_Print()
164 PyErr_NormalizeException(exc
, val
, tb
)
169 PyObject
*type
= *exc
;
170 PyObject
*value
= *val
;
171 PyObject
*inclass
= NULL
;
173 /* If PyErr_SetNone() was used, the value will have been actually
181 if (PyInstance_Check(value
))
182 inclass
= (PyObject
*)((PyInstanceObject
*)value
)->in_class
;
184 /* Normalize the exception so that if the type is a class, the
185 value will be an instance.
187 if (PyClass_Check(type
)) {
188 /* if the value was not an instance, or is not an instance
189 whose class is (or is derived from) type, then use the
190 value as an argument to instantiation of the type
193 if (!inclass
|| !PyClass_IsSubclass(inclass
, type
)) {
194 PyObject
*args
, *res
;
196 if (value
== Py_None
)
197 args
= Py_BuildValue("()");
198 else if (PyTuple_Check(value
)) {
203 args
= Py_BuildValue("(O)", value
);
207 res
= PyEval_CallObject(type
, args
);
214 /* if the class of the instance doesn't exactly match the
215 class of the type, believe the instance
217 else if (inclass
!= type
) {
230 PyErr_Fetch(exc
, val
, tb
);
231 /* normalize recursively */
232 PyErr_NormalizeException(exc
, val
, tb
);
237 PyErr_Fetch(p_type
, p_value
, p_traceback
)
240 PyObject
**p_traceback
;
242 PyThreadState
*tstate
= PyThreadState_Get();
244 *p_type
= tstate
->curexc_type
;
245 *p_value
= tstate
->curexc_value
;
246 *p_traceback
= tstate
->curexc_traceback
;
248 tstate
->curexc_type
= NULL
;
249 tstate
->curexc_value
= NULL
;
250 tstate
->curexc_traceback
= NULL
;
256 PyErr_Restore(NULL
, NULL
, NULL
);
259 /* Convenience functions to set a type error exception and return 0 */
264 PyErr_SetString(PyExc_TypeError
,
265 "illegal argument type for built-in operation");
272 /* raise the pre-allocated instance if it still exists */
273 if (PyExc_MemoryErrorInst
)
274 PyErr_SetObject(PyExc_MemoryError
, PyExc_MemoryErrorInst
);
276 /* this will probably fail since there's no memory and hee,
277 hee, we have to instantiate this class
279 PyErr_SetNone(PyExc_MemoryError
);
285 PyErr_SetFromErrnoWithFilename(exc
, filename
)
293 if (i
== EINTR
&& PyErr_CheckSignals())
297 s
= "Error"; /* Sometimes errno didn't get set */
303 int len
= FormatMessage(
304 FORMAT_MESSAGE_ALLOCATE_BUFFER
|
305 FORMAT_MESSAGE_FROM_SYSTEM
|
306 FORMAT_MESSAGE_IGNORE_INSERTS
,
307 NULL
, /* no message source */
309 MAKELANGID(LANG_NEUTRAL
,
310 SUBLANG_DEFAULT
), /* Default language */
312 0, /* size not used */
314 /* remove trailing cr/lf and dots */
315 while (len
> 0 && s
[len
-1] <= '.')
319 if (filename
!= NULL
&& Py_UseClassExceptionsFlag
)
320 v
= Py_BuildValue("(iss)", i
, s
, filename
);
322 v
= Py_BuildValue("(is)", i
, s
);
324 PyErr_SetObject(exc
, v
);
335 PyErr_SetFromErrno(exc
)
338 return PyErr_SetFromErrnoWithFilename(exc
, NULL
);
342 PyErr_BadInternalCall()
344 PyErr_SetString(PyExc_SystemError
,
345 "bad argument to internal function");
349 #ifdef HAVE_STDARG_PROTOTYPES
351 PyErr_Format(PyObject
*exception
, const char *format
, ...)
354 PyErr_Format(exception
, format
, va_alist
)
361 char buffer
[500]; /* Caller is responsible for limiting the format */
363 #ifdef HAVE_STDARG_PROTOTYPES
364 va_start(vargs
, format
);
369 vsprintf(buffer
, format
, vargs
);
370 PyErr_SetString(exception
, buffer
);
376 PyErr_NewException(name
, base
, dict
)
377 char *name
; /* modulename.classname */
382 PyObject
*modulename
= NULL
;
383 PyObject
*classname
= NULL
;
384 PyObject
*mydict
= NULL
;
385 PyObject
*bases
= NULL
;
386 PyObject
*result
= NULL
;
387 dot
= strrchr(name
, '.');
389 PyErr_SetString(PyExc_SystemError
,
390 "PyErr_NewException: name must be module.class");
394 base
= PyExc_Exception
;
395 if (!PyClass_Check(base
)) {
396 /* Must be using string-based standard exceptions (-X) */
397 return PyString_FromString(name
);
400 dict
= mydict
= PyDict_New();
404 if (PyDict_GetItemString(dict
, "__module__") == NULL
) {
405 modulename
= PyString_FromStringAndSize(name
, (int)(dot
-name
));
406 if (modulename
== NULL
)
408 if (PyDict_SetItemString(dict
, "__module__", modulename
) != 0)
411 classname
= PyString_FromString(dot
+1);
412 if (classname
== NULL
)
414 bases
= Py_BuildValue("(O)", base
);
417 result
= PyClass_New(bases
, dict
, classname
);
421 Py_XDECREF(classname
);
422 Py_XDECREF(modulename
);