7 extern char *PyMac_StrError(int);
9 #define strerror PyMac_StrError
10 #endif /* macintosh */
14 extern char *strerror(int);
26 PyErr_Restore(PyObject
*type
, PyObject
*value
, PyObject
*traceback
)
28 PyThreadState
*tstate
= PyThreadState_GET();
29 PyObject
*oldtype
, *oldvalue
, *oldtraceback
;
31 if (traceback
!= NULL
&& !PyTraceBack_Check(traceback
)) {
32 /* XXX Should never happen -- fatal error instead? */
37 /* 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
;
43 tstate
->curexc_type
= type
;
44 tstate
->curexc_value
= value
;
45 tstate
->curexc_traceback
= traceback
;
49 Py_XDECREF(oldtraceback
);
53 PyErr_SetObject(PyObject
*exception
, PyObject
*value
)
55 Py_XINCREF(exception
);
57 PyErr_Restore(exception
, value
, (PyObject
*)NULL
);
61 PyErr_SetNone(PyObject
*exception
)
63 PyErr_SetObject(exception
, (PyObject
*)NULL
);
67 PyErr_SetString(PyObject
*exception
, const char *string
)
69 PyObject
*value
= PyString_FromString(string
);
70 PyErr_SetObject(exception
, value
);
78 PyThreadState
*tstate
= PyThreadState_GET();
80 return tstate
->curexc_type
;
85 PyErr_GivenExceptionMatches(PyObject
*err
, PyObject
*exc
)
87 if (err
== NULL
|| exc
== NULL
) {
88 /* maybe caused by "import exceptions" that failed early on */
91 if (PyTuple_Check(exc
)) {
93 n
= PyTuple_Size(exc
);
94 for (i
= 0; i
< n
; i
++) {
95 /* Test recursively */
96 if (PyErr_GivenExceptionMatches(
97 err
, PyTuple_GET_ITEM(exc
, i
)))
104 /* err might be an instance, so check its class. */
105 if (PyInstance_Check(err
))
106 err
= (PyObject
*)((PyInstanceObject
*)err
)->in_class
;
108 if (PyClass_Check(err
) && PyClass_Check(exc
))
109 return PyClass_IsSubclass(err
, exc
);
116 PyErr_ExceptionMatches(PyObject
*exc
)
118 return PyErr_GivenExceptionMatches(PyErr_Occurred(), exc
);
122 /* Used in many places to normalize a raised exception, including in
123 eval_code2(), do_raise(), and PyErr_Print()
126 PyErr_NormalizeException(PyObject
**exc
, PyObject
**val
, PyObject
**tb
)
128 PyObject
*type
= *exc
;
129 PyObject
*value
= *val
;
130 PyObject
*inclass
= NULL
;
131 PyObject
*initial_tb
= NULL
;
134 /* This is a bug. Should never happen. Don't dump core. */
135 PyErr_SetString(PyExc_SystemError
,
136 "PyErr_NormalizeException() called without exception");
139 /* If PyErr_SetNone() was used, the value will have been actually
147 if (PyInstance_Check(value
))
148 inclass
= (PyObject
*)((PyInstanceObject
*)value
)->in_class
;
150 /* Normalize the exception so that if the type is a class, the
151 value will be an instance.
153 if (PyClass_Check(type
)) {
154 /* if the value was not an instance, or is not an instance
155 whose class is (or is derived from) type, then use the
156 value as an argument to instantiation of the type
159 if (!inclass
|| !PyClass_IsSubclass(inclass
, type
)) {
160 PyObject
*args
, *res
;
162 if (value
== Py_None
)
163 args
= Py_BuildValue("()");
164 else if (PyTuple_Check(value
)) {
169 args
= Py_BuildValue("(O)", value
);
173 res
= PyEval_CallObject(type
, args
);
180 /* if the class of the instance doesn't exactly match the
181 class of the type, believe the instance
183 else if (inclass
!= type
) {
195 /* If the new exception doesn't set a traceback and the old
196 exception had a traceback, use the old traceback for the
197 new exception. It's better than nothing.
200 PyErr_Fetch(exc
, val
, tb
);
201 if (initial_tb
!= NULL
) {
205 Py_DECREF(initial_tb
);
207 /* normalize recursively */
208 PyErr_NormalizeException(exc
, val
, tb
);
213 PyErr_Fetch(PyObject
**p_type
, PyObject
**p_value
, PyObject
**p_traceback
)
215 PyThreadState
*tstate
= PyThreadState_Get();
217 *p_type
= tstate
->curexc_type
;
218 *p_value
= tstate
->curexc_value
;
219 *p_traceback
= tstate
->curexc_traceback
;
221 tstate
->curexc_type
= NULL
;
222 tstate
->curexc_value
= NULL
;
223 tstate
->curexc_traceback
= NULL
;
229 PyErr_Restore(NULL
, NULL
, NULL
);
232 /* Convenience functions to set a type error exception and return 0 */
235 PyErr_BadArgument(void)
237 PyErr_SetString(PyExc_TypeError
,
238 "bad argument type for built-in operation");
245 if (PyErr_ExceptionMatches(PyExc_MemoryError
))
246 /* already current */
249 /* raise the pre-allocated instance if it still exists */
250 if (PyExc_MemoryErrorInst
)
251 PyErr_SetObject(PyExc_MemoryError
, PyExc_MemoryErrorInst
);
253 /* this will probably fail since there's no memory and hee,
254 hee, we have to instantiate this class
256 PyErr_SetNone(PyExc_MemoryError
);
262 PyErr_SetFromErrnoWithFilename(PyObject
*exc
, char *filename
)
271 if (i
== EINTR
&& PyErr_CheckSignals())
275 s
= "Error"; /* Sometimes errno didn't get set */
281 /* Note that the Win32 errors do not lineup with the
282 errno error. So if the error is in the MSVC error
283 table, we use it, otherwise we assume it really _is_
286 if (i
> 0 && i
< _sys_nerr
) {
290 int len
= FormatMessage(
291 FORMAT_MESSAGE_ALLOCATE_BUFFER
|
292 FORMAT_MESSAGE_FROM_SYSTEM
|
293 FORMAT_MESSAGE_IGNORE_INSERTS
,
294 NULL
, /* no message source */
296 MAKELANGID(LANG_NEUTRAL
,
298 /* Default language */
300 0, /* size not used */
303 /* remove trailing cr/lf and dots */
304 while (len
> 0 && (s
[len
-1] <= ' ' || s
[len
-1] == '.'))
309 if (filename
!= NULL
)
310 v
= Py_BuildValue("(iss)", i
, s
, filename
);
312 v
= Py_BuildValue("(is)", i
, s
);
314 PyErr_SetObject(exc
, v
);
325 PyErr_SetFromErrno(PyObject
*exc
)
327 return PyErr_SetFromErrnoWithFilename(exc
, NULL
);
331 /* Windows specific error code handling */
332 PyObject
*PyErr_SetFromWindowsErrWithFilename(
334 const char *filename
)
339 DWORD err
= (DWORD
)ierr
;
340 if (err
==0) err
= GetLastError();
342 /* Error API error */
343 FORMAT_MESSAGE_ALLOCATE_BUFFER
|
344 FORMAT_MESSAGE_FROM_SYSTEM
|
345 FORMAT_MESSAGE_IGNORE_INSERTS
,
346 NULL
, /* no message source */
348 MAKELANGID(LANG_NEUTRAL
,
349 SUBLANG_DEFAULT
), /* Default language */
351 0, /* size not used */
353 /* remove trailing cr/lf and dots */
354 while (len
> 0 && (s
[len
-1] <= ' ' || s
[len
-1] == '.'))
356 if (filename
!= NULL
)
357 v
= Py_BuildValue("(iss)", err
, s
, filename
);
359 v
= Py_BuildValue("(is)", err
, s
);
361 PyErr_SetObject(PyExc_WindowsError
, v
);
368 PyObject
*PyErr_SetFromWindowsErr(int ierr
)
370 return PyErr_SetFromWindowsErrWithFilename(ierr
, NULL
);
372 #endif /* MS_WINDOWS */
375 _PyErr_BadInternalCall(char *filename
, int lineno
)
377 PyErr_Format(PyExc_SystemError
,
378 "%s:%d: bad argument to internal function",
382 /* Remove the preprocessor macro for PyErr_BadInternalCall() so that we can
383 export the entry point for existing object code: */
384 #undef PyErr_BadInternalCall
386 PyErr_BadInternalCall(void)
388 PyErr_Format(PyExc_SystemError
,
389 "bad argument to internal function");
391 #define PyErr_BadInternalCall() _PyErr_BadInternalCall(__FILE__, __LINE__)
396 PyErr_Format(PyObject
*exception
, const char *format
, ...)
401 #ifdef HAVE_STDARG_PROTOTYPES
402 va_start(vargs
, format
);
407 string
= PyString_FromFormatV(format
, vargs
);
408 PyErr_SetObject(exception
, string
);
416 PyErr_NewException(char *name
, PyObject
*base
, PyObject
*dict
)
419 PyObject
*modulename
= NULL
;
420 PyObject
*classname
= NULL
;
421 PyObject
*mydict
= NULL
;
422 PyObject
*bases
= NULL
;
423 PyObject
*result
= NULL
;
424 dot
= strrchr(name
, '.');
426 PyErr_SetString(PyExc_SystemError
,
427 "PyErr_NewException: name must be module.class");
431 base
= PyExc_Exception
;
432 if (!PyClass_Check(base
)) {
433 /* Must be using string-based standard exceptions (-X) */
434 return PyString_FromString(name
);
437 dict
= mydict
= PyDict_New();
441 if (PyDict_GetItemString(dict
, "__module__") == NULL
) {
442 modulename
= PyString_FromStringAndSize(name
, (int)(dot
-name
));
443 if (modulename
== NULL
)
445 if (PyDict_SetItemString(dict
, "__module__", modulename
) != 0)
448 classname
= PyString_FromString(dot
+1);
449 if (classname
== NULL
)
451 bases
= Py_BuildValue("(O)", base
);
454 result
= PyClass_New(bases
, dict
, classname
);
458 Py_XDECREF(classname
);
459 Py_XDECREF(modulename
);
463 /* Call when an exception has occurred but there is no way for Python
464 to handle it. Examples: exception in __del__ or during GC. */
466 PyErr_WriteUnraisable(PyObject
*obj
)
468 PyObject
*f
, *t
, *v
, *tb
;
469 PyErr_Fetch(&t
, &v
, &tb
);
470 f
= PySys_GetObject("stderr");
472 PyFile_WriteString("Exception ", f
);
474 PyFile_WriteObject(t
, f
, Py_PRINT_RAW
);
475 if (v
&& v
!= Py_None
) {
476 PyFile_WriteString(": ", f
);
477 PyFile_WriteObject(v
, f
, 0);
480 PyFile_WriteString(" in ", f
);
481 PyFile_WriteObject(obj
, f
, 0);
482 PyFile_WriteString(" ignored\n", f
);
483 PyErr_Clear(); /* Just in case */
491 /* Function to issue a warning message; may raise an exception. */
493 PyErr_Warn(PyObject
*category
, char *message
)
495 PyObject
*mod
, *dict
, *func
= NULL
;
497 mod
= PyImport_ImportModule("warnings");
499 dict
= PyModule_GetDict(mod
);
500 func
= PyDict_GetItemString(dict
, "warn");
504 PySys_WriteStderr("warning: %s\n", message
);
508 PyObject
*args
, *res
;
510 if (category
== NULL
)
511 category
= PyExc_RuntimeWarning
;
512 args
= Py_BuildValue("(sO)", message
, category
);
515 res
= PyEval_CallObject(func
, args
);
525 /* Warning with explicit origin */
527 PyErr_WarnExplicit(PyObject
*category
, char *message
,
528 char *filename
, int lineno
,
529 char *module
, PyObject
*registry
)
531 PyObject
*mod
, *dict
, *func
= NULL
;
533 mod
= PyImport_ImportModule("warnings");
535 dict
= PyModule_GetDict(mod
);
536 func
= PyDict_GetItemString(dict
, "warn_explicit");
540 PySys_WriteStderr("warning: %s\n", message
);
544 PyObject
*args
, *res
;
546 if (category
== NULL
)
547 category
= PyExc_RuntimeWarning
;
548 if (registry
== NULL
)
550 args
= Py_BuildValue("(sOsizO)", message
, category
,
551 filename
, lineno
, module
, registry
);
554 res
= PyEval_CallObject(func
, args
);
564 /* XXX There's a comment missing here */
567 PyErr_SyntaxLocation(char *filename
, int lineno
)
569 PyObject
*exc
, *v
, *tb
, *tmp
;
571 /* add attributes for the line number and filename for the error */
572 PyErr_Fetch(&exc
, &v
, &tb
);
573 PyErr_NormalizeException(&exc
, &v
, &tb
);
574 /* XXX check that it is, indeed, a syntax error */
575 tmp
= PyInt_FromLong(lineno
);
579 if (PyObject_SetAttrString(v
, "lineno", tmp
))
583 if (filename
!= NULL
) {
584 tmp
= PyString_FromString(filename
);
588 if (PyObject_SetAttrString(v
, "filename", tmp
))
593 tmp
= PyErr_ProgramText(filename
, lineno
);
595 PyObject_SetAttrString(v
, "text", tmp
);
599 PyErr_Restore(exc
, v
, tb
);
602 /* com_fetch_program_text will attempt to load the line of text that
603 the exception refers to. If it fails, it will return NULL but will
604 not set an exception.
606 XXX The functionality of this function is quite similar to the
607 functionality in tb_displayline() in traceback.c.
611 PyErr_ProgramText(char *filename
, int lineno
)
617 if (filename
== NULL
|| lineno
<= 0)
619 fp
= fopen(filename
, "r");
622 for (i
= 0; i
< lineno
; i
++) {
623 char *pLastChar
= &linebuf
[sizeof(linebuf
) - 2];
626 if (fgets(linebuf
, sizeof linebuf
, fp
) == NULL
)
628 /* fgets read *something*; if it didn't get as
629 far as pLastChar, it must have found a newline
630 or hit the end of the file; if pLastChar is \n,
631 it obviously found a newline; else we haven't
632 yet seen a newline, so must continue */
633 } while (*pLastChar
!= '\0' && *pLastChar
!= '\n');
638 while (*p
== ' ' || *p
== '\t' || *p
== '\014')
640 return PyString_FromString(p
);