抛出异常
These functions help you set the current thread’s error indicator. For convenience, some of these functions will always return a NULL
pointer for use in a return
statement.
void PyErr_SetString
(PyObject type*, const char message*)
This is the most common way to set the error indicator. The first argument specifies the exception type; it is normally one of the standard exceptions, e.g. PyExc_RuntimeError
. You need not increment its reference count. The second argument is an error message; it is decoded from 'utf-8
‘.
void PyErr_SetObject
(PyObject type*, PyObject value*)
This function is similar to PyErr_SetString()
but lets you specify an arbitrary Python object for the “value” of the exception.
PyObject* PyErr_Format
(PyObject exception*, const char format*, …)
Return value: Always NULL.
This function sets the error indicator and returns NULL
. exception should be a Python exception class. The format and subsequent parameters help format the error message; they have the same meaning and values as in PyUnicode_FromFormat()
. format is an ASCII-encoded string.
PyObject* PyErr_FormatV
(PyObject exception*, const char format, va_list vargs*)
Return value: Always NULL.
Same as PyErr_Format()
, but taking a va_list
argument rather than a variable number of arguments.
3.5 新版功能.
void PyErr_SetNone
(PyObject *type)
This is a shorthand for PyErr_SetObject(type, Py_None)
.
int PyErr_BadArgument
()
This is a shorthand for PyErr_SetString(PyExc_TypeError, message)
, where message indicates that a built-in operation was invoked with an illegal argument. It is mostly for internal use.
PyObject* PyErr_NoMemory
()
Return value: Always NULL.
This is a shorthand for PyErr_SetNone(PyExc_MemoryError)
; it returns NULL
so an object allocation function can write return PyErr_NoMemory();
when it runs out of memory.
PyObject* PyErr_SetFromErrno
(PyObject *type)
Return value: Always NULL.
This is a convenience function to raise an exception when a C library function has returned an error and set the C variable errno
. It constructs a tuple object whose first item is the integer errno
value and whose second item is the corresponding error message (gotten from strerror()
), and then calls PyErr_SetObject(type, object)
. On Unix, when the errno
value is EINTR
, indicating an interrupted system call, this calls PyErr_CheckSignals()
, and if that set the error indicator, leaves it set to that. The function always returns NULL
, so a wrapper function around a system call can write return PyErr_SetFromErrno(type);
when the system call returns an error.
PyObject* PyErr_SetFromErrnoWithFilenameObject
(PyObject type*, PyObject filenameObject*)
Return value: Always NULL.
Similar to PyErr_SetFromErrno()
, with the additional behavior that if filenameObject is not NULL
, it is passed to the constructor of type as a third parameter. In the case of OSError
exception, this is used to define the filename
attribute of the exception instance.
PyObject* PyErr_SetFromErrnoWithFilenameObjects
(PyObject type*, PyObject filenameObject, PyObject **filenameObject2)
Return value: Always NULL.
Similar to PyErr_SetFromErrnoWithFilenameObject()
, but takes a second filename object, for raising errors when a function that takes two filenames fails.
3.4 新版功能.
PyObject* PyErr_SetFromErrnoWithFilename
(PyObject type*, const char filename*)
Return value: Always NULL.
Similar to PyErr_SetFromErrnoWithFilenameObject()
, but the filename is given as a C string. filename is decoded from the filesystem encoding (os.fsdecode()
).
PyObject* PyErr_SetFromWindowsErr
(int ierr)
Return value: Always NULL.
This is a convenience function to raise WindowsError
. If called with ierr of 0
, the error code returned by a call to GetLastError()
is used instead. It calls the Win32 function FormatMessage()
to retrieve the Windows description of error code given by ierr or GetLastError()
, then it constructs a tuple object whose first item is the ierr value and whose second item is the corresponding error message (gotten from FormatMessage()
), and then calls PyErr_SetObject(PyExc_WindowsError, object)
. This function always returns NULL
.
可用性: Windows。
PyObject* PyErr_SetExcFromWindowsErr
(PyObject *type, int ierr)
Return value: Always NULL.
Similar to PyErr_SetFromWindowsErr()
, with an additional parameter specifying the exception type to be raised.
可用性: Windows。
PyObject* PyErr_SetFromWindowsErrWithFilename
(int ierr, const char *filename)
Return value: Always NULL.
Similar to PyErr_SetFromWindowsErrWithFilenameObject()
, but the filename is given as a C string. filename is decoded from the filesystem encoding (os.fsdecode()
).
可用性: Windows。
PyObject* PyErr_SetExcFromWindowsErrWithFilenameObject
(PyObject type, int ierr*, PyObject filename*)
Return value: Always NULL.
Similar to PyErr_SetFromWindowsErrWithFilenameObject()
, with an additional parameter specifying the exception type to be raised.
可用性: Windows。
PyObject* PyErr_SetExcFromWindowsErrWithFilenameObjects
(PyObject type, int ierr*, PyObject filename, PyObject **filename2)
Return value: Always NULL.
Similar to PyErr_SetExcFromWindowsErrWithFilenameObject()
, but accepts a second filename object.
可用性: Windows。
3.4 新版功能.
PyObject* PyErr_SetExcFromWindowsErrWithFilename
(PyObject type, int ierr*, const char filename*)
Return value: Always NULL.
Similar to PyErr_SetFromWindowsErrWithFilename()
, with an additional parameter specifying the exception type to be raised.
可用性: Windows。
PyObject* PyErr_SetImportError
(PyObject msg*, PyObject name, PyObject **path)
Return value: Always NULL.
This is a convenience function to raise ImportError
. msg will be set as the exception’s message string. name and path, both of which can be NULL
, will be set as the ImportError
‘s respective name
and path
attributes.
3.3 新版功能.
void PyErr_SyntaxLocationObject
(PyObject *filename, int lineno, int col_offset)
Set file, line, and offset information for the current exception. If the current exception is not a SyntaxError
, then it sets additional attributes, which make the exception printing subsystem think the exception is a SyntaxError
.
3.4 新版功能.
void PyErr_SyntaxLocationEx
(const char *filename, int lineno, int col_offset)
Like PyErr_SyntaxLocationObject()
, but filename is a byte string decoded from the filesystem encoding (os.fsdecode()
).
3.2 新版功能.
void PyErr_SyntaxLocation
(const char *filename, int lineno)
Like PyErr_SyntaxLocationEx()
, but the col_offset parameter is omitted.
void PyErr_BadInternalCall
()
This is a shorthand for PyErr_SetString(PyExc_SystemError, message)
, where message indicates that an internal operation (e.g. a Python/C API function) was invoked with an illegal argument. It is mostly for internal use.