| 1 | \chapter{Exception Handling \label{exceptionHandling}}
|
|---|
| 2 |
|
|---|
| 3 | The functions described in this chapter will let you handle and raise Python
|
|---|
| 4 | exceptions. It is important to understand some of the basics of
|
|---|
| 5 | Python exception handling. It works somewhat like the
|
|---|
| 6 | \UNIX{} \cdata{errno} variable: there is a global indicator (per
|
|---|
| 7 | thread) of the last error that occurred. Most functions don't clear
|
|---|
| 8 | this on success, but will set it to indicate the cause of the error on
|
|---|
| 9 | failure. Most functions also return an error indicator, usually
|
|---|
| 10 | \NULL{} if they are supposed to return a pointer, or \code{-1} if they
|
|---|
| 11 | return an integer (exception: the \cfunction{PyArg_*()} functions
|
|---|
| 12 | return \code{1} for success and \code{0} for failure).
|
|---|
| 13 |
|
|---|
| 14 | When a function must fail because some function it called failed, it
|
|---|
| 15 | generally doesn't set the error indicator; the function it called
|
|---|
| 16 | already set it. It is responsible for either handling the error and
|
|---|
| 17 | clearing the exception or returning after cleaning up any resources it
|
|---|
| 18 | holds (such as object references or memory allocations); it should
|
|---|
| 19 | \emph{not} continue normally if it is not prepared to handle the
|
|---|
| 20 | error. If returning due to an error, it is important to indicate to
|
|---|
| 21 | the caller that an error has been set. If the error is not handled or
|
|---|
| 22 | carefully propagated, additional calls into the Python/C API may not
|
|---|
| 23 | behave as intended and may fail in mysterious ways.
|
|---|
| 24 |
|
|---|
| 25 | The error indicator consists of three Python objects corresponding to
|
|---|
| 26 | \withsubitem{(in module sys)}{
|
|---|
| 27 | \ttindex{exc_type}\ttindex{exc_value}\ttindex{exc_traceback}}
|
|---|
| 28 | the Python variables \code{sys.exc_type}, \code{sys.exc_value} and
|
|---|
| 29 | \code{sys.exc_traceback}. API functions exist to interact with the
|
|---|
| 30 | error indicator in various ways. There is a separate error indicator
|
|---|
| 31 | for each thread.
|
|---|
| 32 |
|
|---|
| 33 | % XXX Order of these should be more thoughtful.
|
|---|
| 34 | % Either alphabetical or some kind of structure.
|
|---|
| 35 |
|
|---|
| 36 | \begin{cfuncdesc}{void}{PyErr_Print}{}
|
|---|
| 37 | Print a standard traceback to \code{sys.stderr} and clear the error
|
|---|
| 38 | indicator. Call this function only when the error indicator is
|
|---|
| 39 | set. (Otherwise it will cause a fatal error!)
|
|---|
| 40 | \end{cfuncdesc}
|
|---|
| 41 |
|
|---|
| 42 | \begin{cfuncdesc}{PyObject*}{PyErr_Occurred}{}
|
|---|
| 43 | Test whether the error indicator is set. If set, return the
|
|---|
| 44 | exception \emph{type} (the first argument to the last call to one of
|
|---|
| 45 | the \cfunction{PyErr_Set*()} functions or to
|
|---|
| 46 | \cfunction{PyErr_Restore()}). If not set, return \NULL. You do
|
|---|
| 47 | not own a reference to the return value, so you do not need to
|
|---|
| 48 | \cfunction{Py_DECREF()} it. \note{Do not compare the return value
|
|---|
| 49 | to a specific exception; use \cfunction{PyErr_ExceptionMatches()}
|
|---|
| 50 | instead, shown below. (The comparison could easily fail since the
|
|---|
| 51 | exception may be an instance instead of a class, in the case of a
|
|---|
| 52 | class exception, or it may the a subclass of the expected
|
|---|
| 53 | exception.)}
|
|---|
| 54 | \end{cfuncdesc}
|
|---|
| 55 |
|
|---|
| 56 | \begin{cfuncdesc}{int}{PyErr_ExceptionMatches}{PyObject *exc}
|
|---|
| 57 | Equivalent to \samp{PyErr_GivenExceptionMatches(PyErr_Occurred(),
|
|---|
| 58 | \var{exc})}. This should only be called when an exception is
|
|---|
| 59 | actually set; a memory access violation will occur if no exception
|
|---|
| 60 | has been raised.
|
|---|
| 61 | \end{cfuncdesc}
|
|---|
| 62 |
|
|---|
| 63 | \begin{cfuncdesc}{int}{PyErr_GivenExceptionMatches}{PyObject *given, PyObject *exc}
|
|---|
| 64 | Return true if the \var{given} exception matches the exception in
|
|---|
| 65 | \var{exc}. If \var{exc} is a class object, this also returns true
|
|---|
| 66 | when \var{given} is an instance of a subclass. If \var{exc} is a
|
|---|
| 67 | tuple, all exceptions in the tuple (and recursively in subtuples)
|
|---|
| 68 | are searched for a match. If \var{given} is \NULL, a memory access
|
|---|
| 69 | violation will occur.
|
|---|
| 70 | \end{cfuncdesc}
|
|---|
| 71 |
|
|---|
| 72 | \begin{cfuncdesc}{void}{PyErr_NormalizeException}{PyObject**exc, PyObject**val, PyObject**tb}
|
|---|
| 73 | Under certain circumstances, the values returned by
|
|---|
| 74 | \cfunction{PyErr_Fetch()} below can be ``unnormalized'', meaning
|
|---|
| 75 | that \code{*\var{exc}} is a class object but \code{*\var{val}} is
|
|---|
| 76 | not an instance of the same class. This function can be used to
|
|---|
| 77 | instantiate the class in that case. If the values are already
|
|---|
| 78 | normalized, nothing happens. The delayed normalization is
|
|---|
| 79 | implemented to improve performance.
|
|---|
| 80 | \end{cfuncdesc}
|
|---|
| 81 |
|
|---|
| 82 | \begin{cfuncdesc}{void}{PyErr_Clear}{}
|
|---|
| 83 | Clear the error indicator. If the error indicator is not set, there
|
|---|
| 84 | is no effect.
|
|---|
| 85 | \end{cfuncdesc}
|
|---|
| 86 |
|
|---|
| 87 | \begin{cfuncdesc}{void}{PyErr_Fetch}{PyObject **ptype, PyObject **pvalue,
|
|---|
| 88 | PyObject **ptraceback}
|
|---|
| 89 | Retrieve the error indicator into three variables whose addresses
|
|---|
| 90 | are passed. If the error indicator is not set, set all three
|
|---|
| 91 | variables to \NULL. If it is set, it will be cleared and you own a
|
|---|
| 92 | reference to each object retrieved. The value and traceback object
|
|---|
| 93 | may be \NULL{} even when the type object is not. \note{This
|
|---|
| 94 | function is normally only used by code that needs to handle
|
|---|
| 95 | exceptions or by code that needs to save and restore the error
|
|---|
| 96 | indicator temporarily.}
|
|---|
| 97 | \end{cfuncdesc}
|
|---|
| 98 |
|
|---|
| 99 | \begin{cfuncdesc}{void}{PyErr_Restore}{PyObject *type, PyObject *value,
|
|---|
| 100 | PyObject *traceback}
|
|---|
| 101 | Set the error indicator from the three objects. If the error
|
|---|
| 102 | indicator is already set, it is cleared first. If the objects are
|
|---|
| 103 | \NULL, the error indicator is cleared. Do not pass a \NULL{} type
|
|---|
| 104 | and non-\NULL{} value or traceback. The exception type should be a
|
|---|
| 105 | class. Do not pass an invalid exception type or value.
|
|---|
| 106 | (Violating these rules will cause subtle problems later.) This call
|
|---|
| 107 | takes away a reference to each object: you must own a reference to
|
|---|
| 108 | each object before the call and after the call you no longer own
|
|---|
| 109 | these references. (If you don't understand this, don't use this
|
|---|
| 110 | function. I warned you.) \note{This function is normally only used
|
|---|
| 111 | by code that needs to save and restore the error indicator
|
|---|
| 112 | temporarily; use \cfunction{PyErr_Fetch()} to save the current
|
|---|
| 113 | exception state.}
|
|---|
| 114 | \end{cfuncdesc}
|
|---|
| 115 |
|
|---|
| 116 | \begin{cfuncdesc}{void}{PyErr_SetString}{PyObject *type, const char *message}
|
|---|
| 117 | This is the most common way to set the error indicator. The first
|
|---|
| 118 | argument specifies the exception type; it is normally one of the
|
|---|
| 119 | standard exceptions, e.g. \cdata{PyExc_RuntimeError}. You need not
|
|---|
| 120 | increment its reference count. The second argument is an error
|
|---|
| 121 | message; it is converted to a string object.
|
|---|
| 122 | \end{cfuncdesc}
|
|---|
| 123 |
|
|---|
| 124 | \begin{cfuncdesc}{void}{PyErr_SetObject}{PyObject *type, PyObject *value}
|
|---|
| 125 | This function is similar to \cfunction{PyErr_SetString()} but lets
|
|---|
| 126 | you specify an arbitrary Python object for the ``value'' of the
|
|---|
| 127 | exception.
|
|---|
| 128 | \end{cfuncdesc}
|
|---|
| 129 |
|
|---|
| 130 | \begin{cfuncdesc}{PyObject*}{PyErr_Format}{PyObject *exception,
|
|---|
| 131 | const char *format, \moreargs}
|
|---|
| 132 | This function sets the error indicator and returns \NULL.
|
|---|
| 133 | \var{exception} should be a Python exception (class, not
|
|---|
| 134 | an instance). \var{format} should be a string, containing format
|
|---|
| 135 | codes, similar to \cfunction{printf()}. The \code{width.precision}
|
|---|
| 136 | before a format code is parsed, but the width part is ignored.
|
|---|
| 137 |
|
|---|
| 138 | % This should be exactly the same as the table in PyString_FromFormat.
|
|---|
| 139 | % One should just refer to the other.
|
|---|
| 140 |
|
|---|
| 141 | % The descriptions for %zd and %zu are wrong, but the truth is complicated
|
|---|
| 142 | % because not all compilers support the %z width modifier -- we fake it
|
|---|
| 143 | % when necessary via interpolating PY_FORMAT_SIZE_T.
|
|---|
| 144 |
|
|---|
| 145 | % %u, %lu, %zu should have "new in Python 2.5" blurbs.
|
|---|
| 146 |
|
|---|
| 147 | \begin{tableiii}{l|l|l}{member}{Format Characters}{Type}{Comment}
|
|---|
| 148 | \lineiii{\%\%}{\emph{n/a}}{The literal \% character.}
|
|---|
| 149 | \lineiii{\%c}{int}{A single character, represented as an C int.}
|
|---|
| 150 | \lineiii{\%d}{int}{Exactly equivalent to \code{printf("\%d")}.}
|
|---|
| 151 | \lineiii{\%u}{unsigned int}{Exactly equivalent to \code{printf("\%u")}.}
|
|---|
| 152 | \lineiii{\%ld}{long}{Exactly equivalent to \code{printf("\%ld")}.}
|
|---|
| 153 | \lineiii{\%lu}{unsigned long}{Exactly equivalent to \code{printf("\%lu")}.}
|
|---|
| 154 | \lineiii{\%zd}{Py_ssize_t}{Exactly equivalent to \code{printf("\%zd")}.}
|
|---|
| 155 | \lineiii{\%zu}{size_t}{Exactly equivalent to \code{printf("\%zu")}.}
|
|---|
| 156 | \lineiii{\%i}{int}{Exactly equivalent to \code{printf("\%i")}.}
|
|---|
| 157 | \lineiii{\%x}{int}{Exactly equivalent to \code{printf("\%x")}.}
|
|---|
| 158 | \lineiii{\%s}{char*}{A null-terminated C character array.}
|
|---|
| 159 | \lineiii{\%p}{void*}{The hex representation of a C pointer.
|
|---|
| 160 | Mostly equivalent to \code{printf("\%p")} except that it is
|
|---|
| 161 | guaranteed to start with the literal \code{0x} regardless of
|
|---|
| 162 | what the platform's \code{printf} yields.}
|
|---|
| 163 | \end{tableiii}
|
|---|
| 164 |
|
|---|
| 165 | An unrecognized format character causes all the rest of the format
|
|---|
| 166 | string to be copied as-is to the result string, and any extra
|
|---|
| 167 | arguments discarded.
|
|---|
| 168 | \end{cfuncdesc}
|
|---|
| 169 |
|
|---|
| 170 | \begin{cfuncdesc}{void}{PyErr_SetNone}{PyObject *type}
|
|---|
| 171 | This is a shorthand for \samp{PyErr_SetObject(\var{type},
|
|---|
| 172 | Py_None)}.
|
|---|
| 173 | \end{cfuncdesc}
|
|---|
| 174 |
|
|---|
| 175 | \begin{cfuncdesc}{int}{PyErr_BadArgument}{}
|
|---|
| 176 | This is a shorthand for \samp{PyErr_SetString(PyExc_TypeError,
|
|---|
| 177 | \var{message})}, where \var{message} indicates that a built-in
|
|---|
| 178 | operation was invoked with an illegal argument. It is mostly for
|
|---|
| 179 | internal use.
|
|---|
| 180 | \end{cfuncdesc}
|
|---|
| 181 |
|
|---|
| 182 | \begin{cfuncdesc}{PyObject*}{PyErr_NoMemory}{}
|
|---|
| 183 | This is a shorthand for \samp{PyErr_SetNone(PyExc_MemoryError)}; it
|
|---|
| 184 | returns \NULL{} so an object allocation function can write
|
|---|
| 185 | \samp{return PyErr_NoMemory();} when it runs out of memory.
|
|---|
| 186 | \end{cfuncdesc}
|
|---|
| 187 |
|
|---|
| 188 | \begin{cfuncdesc}{PyObject*}{PyErr_SetFromErrno}{PyObject *type}
|
|---|
| 189 | This is a convenience function to raise an exception when a C
|
|---|
| 190 | library function has returned an error and set the C variable
|
|---|
| 191 | \cdata{errno}. It constructs a tuple object whose first item is the
|
|---|
| 192 | integer \cdata{errno} value and whose second item is the
|
|---|
| 193 | corresponding error message (gotten from
|
|---|
| 194 | \cfunction{strerror()}\ttindex{strerror()}), and then calls
|
|---|
| 195 | \samp{PyErr_SetObject(\var{type}, \var{object})}. On \UNIX, when
|
|---|
| 196 | the \cdata{errno} value is \constant{EINTR}, indicating an
|
|---|
| 197 | interrupted system call, this calls
|
|---|
| 198 | \cfunction{PyErr_CheckSignals()}, and if that set the error
|
|---|
| 199 | indicator, leaves it set to that. The function always returns
|
|---|
| 200 | \NULL, so a wrapper function around a system call can write
|
|---|
| 201 | \samp{return PyErr_SetFromErrno(\var{type});} when the system call
|
|---|
| 202 | returns an error.
|
|---|
| 203 | \end{cfuncdesc}
|
|---|
| 204 |
|
|---|
| 205 | \begin{cfuncdesc}{PyObject*}{PyErr_SetFromErrnoWithFilename}{PyObject *type,
|
|---|
| 206 | const char *filename}
|
|---|
| 207 | Similar to \cfunction{PyErr_SetFromErrno()}, with the additional
|
|---|
| 208 | behavior that if \var{filename} is not \NULL, it is passed to the
|
|---|
| 209 | constructor of \var{type} as a third parameter. In the case of
|
|---|
| 210 | exceptions such as \exception{IOError} and \exception{OSError}, this
|
|---|
| 211 | is used to define the \member{filename} attribute of the exception
|
|---|
| 212 | instance.
|
|---|
| 213 | \end{cfuncdesc}
|
|---|
| 214 |
|
|---|
| 215 | \begin{cfuncdesc}{PyObject*}{PyErr_SetFromWindowsErr}{int ierr}
|
|---|
| 216 | This is a convenience function to raise \exception{WindowsError}.
|
|---|
| 217 | If called with \var{ierr} of \cdata{0}, the error code returned by a
|
|---|
| 218 | call to \cfunction{GetLastError()} is used instead. It calls the
|
|---|
| 219 | Win32 function \cfunction{FormatMessage()} to retrieve the Windows
|
|---|
| 220 | description of error code given by \var{ierr} or
|
|---|
| 221 | \cfunction{GetLastError()}, then it constructs a tuple object whose
|
|---|
| 222 | first item is the \var{ierr} value and whose second item is the
|
|---|
| 223 | corresponding error message (gotten from
|
|---|
| 224 | \cfunction{FormatMessage()}), and then calls
|
|---|
| 225 | \samp{PyErr_SetObject(\var{PyExc_WindowsError}, \var{object})}.
|
|---|
| 226 | This function always returns \NULL.
|
|---|
| 227 | Availability: Windows.
|
|---|
| 228 | \end{cfuncdesc}
|
|---|
| 229 |
|
|---|
| 230 | \begin{cfuncdesc}{PyObject*}{PyErr_SetExcFromWindowsErr}{PyObject *type,
|
|---|
| 231 | int ierr}
|
|---|
| 232 | Similar to \cfunction{PyErr_SetFromWindowsErr()}, with an additional
|
|---|
| 233 | parameter specifying the exception type to be raised.
|
|---|
| 234 | Availability: Windows.
|
|---|
| 235 | \versionadded{2.3}
|
|---|
| 236 | \end{cfuncdesc}
|
|---|
| 237 |
|
|---|
| 238 | \begin{cfuncdesc}{PyObject*}{PyErr_SetFromWindowsErrWithFilename}{int ierr,
|
|---|
| 239 | const char *filename}
|
|---|
| 240 | Similar to \cfunction{PyErr_SetFromWindowsErr()}, with the
|
|---|
| 241 | additional behavior that if \var{filename} is not \NULL, it is
|
|---|
| 242 | passed to the constructor of \exception{WindowsError} as a third
|
|---|
| 243 | parameter.
|
|---|
| 244 | Availability: Windows.
|
|---|
| 245 | \end{cfuncdesc}
|
|---|
| 246 |
|
|---|
| 247 | \begin{cfuncdesc}{PyObject*}{PyErr_SetExcFromWindowsErrWithFilename}
|
|---|
| 248 | {PyObject *type, int ierr, char *filename}
|
|---|
| 249 | Similar to \cfunction{PyErr_SetFromWindowsErrWithFilename()}, with
|
|---|
| 250 | an additional parameter specifying the exception type to be raised.
|
|---|
| 251 | Availability: Windows.
|
|---|
| 252 | \versionadded{2.3}
|
|---|
| 253 | \end{cfuncdesc}
|
|---|
| 254 |
|
|---|
| 255 | \begin{cfuncdesc}{void}{PyErr_BadInternalCall}{}
|
|---|
| 256 | This is a shorthand for \samp{PyErr_SetString(PyExc_TypeError,
|
|---|
| 257 | \var{message})}, where \var{message} indicates that an internal
|
|---|
| 258 | operation (e.g. a Python/C API function) was invoked with an illegal
|
|---|
| 259 | argument. It is mostly for internal use.
|
|---|
| 260 | \end{cfuncdesc}
|
|---|
| 261 |
|
|---|
| 262 | \begin{cfuncdesc}{int}{PyErr_WarnEx}{PyObject *category, char *message, int stacklevel}
|
|---|
| 263 | Issue a warning message. The \var{category} argument is a warning
|
|---|
| 264 | category (see below) or \NULL; the \var{message} argument is a
|
|---|
| 265 | message string. \var{stacklevel} is a positive number giving a
|
|---|
| 266 | number of stack frames; the warning will be issued from the
|
|---|
| 267 | currently executing line of code in that stack frame. A \var{stacklevel}
|
|---|
| 268 | of 1 is the function calling \cfunction{PyErr_WarnEx()}, 2 is
|
|---|
| 269 | the function above that, and so forth.
|
|---|
| 270 |
|
|---|
| 271 | This function normally prints a warning message to \var{sys.stderr};
|
|---|
| 272 | however, it is also possible that the user has specified that
|
|---|
| 273 | warnings are to be turned into errors, and in that case this will
|
|---|
| 274 | raise an exception. It is also possible that the function raises an
|
|---|
| 275 | exception because of a problem with the warning machinery (the
|
|---|
| 276 | implementation imports the \module{warnings} module to do the heavy
|
|---|
| 277 | lifting). The return value is \code{0} if no exception is raised,
|
|---|
| 278 | or \code{-1} if an exception is raised. (It is not possible to
|
|---|
| 279 | determine whether a warning message is actually printed, nor what
|
|---|
| 280 | the reason is for the exception; this is intentional.) If an
|
|---|
| 281 | exception is raised, the caller should do its normal exception
|
|---|
| 282 | handling (for example, \cfunction{Py_DECREF()} owned references and
|
|---|
| 283 | return an error value).
|
|---|
| 284 |
|
|---|
| 285 | Warning categories must be subclasses of \cdata{Warning}; the
|
|---|
| 286 | default warning category is \cdata{RuntimeWarning}. The standard
|
|---|
| 287 | Python warning categories are available as global variables whose
|
|---|
| 288 | names are \samp{PyExc_} followed by the Python exception name.
|
|---|
| 289 | These have the type \ctype{PyObject*}; they are all class objects.
|
|---|
| 290 | Their names are \cdata{PyExc_Warning}, \cdata{PyExc_UserWarning},
|
|---|
| 291 | \cdata{PyExc_UnicodeWarning}, \cdata{PyExc_DeprecationWarning},
|
|---|
| 292 | \cdata{PyExc_SyntaxWarning}, \cdata{PyExc_RuntimeWarning}, and
|
|---|
| 293 | \cdata{PyExc_FutureWarning}. \cdata{PyExc_Warning} is a subclass of
|
|---|
| 294 | \cdata{PyExc_Exception}; the other warning categories are subclasses
|
|---|
| 295 | of \cdata{PyExc_Warning}.
|
|---|
| 296 |
|
|---|
| 297 | For information about warning control, see the documentation for the
|
|---|
| 298 | \module{warnings} module and the \programopt{-W} option in the
|
|---|
| 299 | command line documentation. There is no C API for warning control.
|
|---|
| 300 | \end{cfuncdesc}
|
|---|
| 301 |
|
|---|
| 302 | \begin{cfuncdesc}{int}{PyErr_Warn}{PyObject *category, char *message}
|
|---|
| 303 | Issue a warning message. The \var{category} argument is a warning
|
|---|
| 304 | category (see below) or \NULL; the \var{message} argument is a
|
|---|
| 305 | message string. The warning will appear to be issued from the function
|
|---|
| 306 | calling \cfunction{PyErr_Warn()}, equivalent to calling
|
|---|
| 307 | \cfunction{PyErr_WarnEx()} with a \var{stacklevel} of 1.
|
|---|
| 308 |
|
|---|
| 309 | Deprecated; use \cfunction{PyErr_WarnEx()} instead.
|
|---|
| 310 | \end{cfuncdesc}
|
|---|
| 311 |
|
|---|
| 312 | \begin{cfuncdesc}{int}{PyErr_WarnExplicit}{PyObject *category,
|
|---|
| 313 | const char *message, const char *filename, int lineno,
|
|---|
| 314 | const char *module, PyObject *registry}
|
|---|
| 315 | Issue a warning message with explicit control over all warning
|
|---|
| 316 | attributes. This is a straightforward wrapper around the Python
|
|---|
| 317 | function \function{warnings.warn_explicit()}, see there for more
|
|---|
| 318 | information. The \var{module} and \var{registry} arguments may be
|
|---|
| 319 | set to \NULL{} to get the default effect described there.
|
|---|
| 320 | \end{cfuncdesc}
|
|---|
| 321 |
|
|---|
| 322 | \begin{cfuncdesc}{int}{PyErr_CheckSignals}{}
|
|---|
| 323 | This function interacts with Python's signal handling. It checks
|
|---|
| 324 | whether a signal has been sent to the processes and if so, invokes
|
|---|
| 325 | the corresponding signal handler. If the
|
|---|
| 326 | \module{signal}\refbimodindex{signal} module is supported, this can
|
|---|
| 327 | invoke a signal handler written in Python. In all cases, the
|
|---|
| 328 | default effect for \constant{SIGINT}\ttindex{SIGINT} is to raise the
|
|---|
| 329 | \withsubitem{(built-in exception)}{\ttindex{KeyboardInterrupt}}
|
|---|
| 330 | \exception{KeyboardInterrupt} exception. If an exception is raised
|
|---|
| 331 | the error indicator is set and the function returns \code{1};
|
|---|
| 332 | otherwise the function returns \code{0}. The error indicator may or
|
|---|
| 333 | may not be cleared if it was previously set.
|
|---|
| 334 | \end{cfuncdesc}
|
|---|
| 335 |
|
|---|
| 336 | \begin{cfuncdesc}{void}{PyErr_SetInterrupt}{}
|
|---|
| 337 | This function simulates the effect of a
|
|---|
| 338 | \constant{SIGINT}\ttindex{SIGINT} signal arriving --- the next time
|
|---|
| 339 | \cfunction{PyErr_CheckSignals()} is called,
|
|---|
| 340 | \withsubitem{(built-in exception)}{\ttindex{KeyboardInterrupt}}
|
|---|
| 341 | \exception{KeyboardInterrupt} will be raised. It may be called
|
|---|
| 342 | without holding the interpreter lock.
|
|---|
| 343 | % XXX This was described as obsolete, but is used in
|
|---|
| 344 | % thread.interrupt_main() (used from IDLE), so it's still needed.
|
|---|
| 345 | \end{cfuncdesc}
|
|---|
| 346 |
|
|---|
| 347 | \begin{cfuncdesc}{PyObject*}{PyErr_NewException}{char *name,
|
|---|
| 348 | PyObject *base,
|
|---|
| 349 | PyObject *dict}
|
|---|
| 350 | This utility function creates and returns a new exception object.
|
|---|
| 351 | The \var{name} argument must be the name of the new exception, a C
|
|---|
| 352 | string of the form \code{module.class}. The \var{base} and
|
|---|
| 353 | \var{dict} arguments are normally \NULL. This creates a class
|
|---|
| 354 | object derived from \exception{Exception} (accessible in C as
|
|---|
| 355 | \cdata{PyExc_Exception}).
|
|---|
| 356 |
|
|---|
| 357 | The \member{__module__} attribute of the new class is set to the
|
|---|
| 358 | first part (up to the last dot) of the \var{name} argument, and the
|
|---|
| 359 | class name is set to the last part (after the last dot). The
|
|---|
| 360 | \var{base} argument can be used to specify alternate base classes;
|
|---|
| 361 | it can either be only one class or a tuple of classes.
|
|---|
| 362 | The \var{dict} argument can be used to specify a dictionary of class
|
|---|
| 363 | variables and methods.
|
|---|
| 364 | \end{cfuncdesc}
|
|---|
| 365 |
|
|---|
| 366 | \begin{cfuncdesc}{void}{PyErr_WriteUnraisable}{PyObject *obj}
|
|---|
| 367 | This utility function prints a warning message to \code{sys.stderr}
|
|---|
| 368 | when an exception has been set but it is impossible for the
|
|---|
| 369 | interpreter to actually raise the exception. It is used, for
|
|---|
| 370 | example, when an exception occurs in an \method{__del__()} method.
|
|---|
| 371 |
|
|---|
| 372 | The function is called with a single argument \var{obj} that
|
|---|
| 373 | identifies the context in which the unraisable exception occurred.
|
|---|
| 374 | The repr of \var{obj} will be printed in the warning message.
|
|---|
| 375 | \end{cfuncdesc}
|
|---|
| 376 |
|
|---|
| 377 | \section{Standard Exceptions \label{standardExceptions}}
|
|---|
| 378 |
|
|---|
| 379 | All standard Python exceptions are available as global variables whose
|
|---|
| 380 | names are \samp{PyExc_} followed by the Python exception name. These
|
|---|
| 381 | have the type \ctype{PyObject*}; they are all class objects. For
|
|---|
| 382 | completeness, here are all the variables:
|
|---|
| 383 |
|
|---|
| 384 | \begin{tableiii}{l|l|c}{cdata}{C Name}{Python Name}{Notes}
|
|---|
| 385 | \lineiii{PyExc_BaseException\ttindex{PyExc_BaseException}}{\exception{BaseException}}{(1), (4)}
|
|---|
| 386 | \lineiii{PyExc_Exception\ttindex{PyExc_Exception}}{\exception{Exception}}{(1)}
|
|---|
| 387 | \lineiii{PyExc_StandardError\ttindex{PyExc_StandardError}}{\exception{StandardError}}{(1)}
|
|---|
| 388 | \lineiii{PyExc_ArithmeticError\ttindex{PyExc_ArithmeticError}}{\exception{ArithmeticError}}{(1)}
|
|---|
| 389 | \lineiii{PyExc_LookupError\ttindex{PyExc_LookupError}}{\exception{LookupError}}{(1)}
|
|---|
| 390 | \lineiii{PyExc_AssertionError\ttindex{PyExc_AssertionError}}{\exception{AssertionError}}{}
|
|---|
| 391 | \lineiii{PyExc_AttributeError\ttindex{PyExc_AttributeError}}{\exception{AttributeError}}{}
|
|---|
| 392 | \lineiii{PyExc_EOFError\ttindex{PyExc_EOFError}}{\exception{EOFError}}{}
|
|---|
| 393 | \lineiii{PyExc_EnvironmentError\ttindex{PyExc_EnvironmentError}}{\exception{EnvironmentError}}{(1)}
|
|---|
| 394 | \lineiii{PyExc_FloatingPointError\ttindex{PyExc_FloatingPointError}}{\exception{FloatingPointError}}{}
|
|---|
| 395 | \lineiii{PyExc_IOError\ttindex{PyExc_IOError}}{\exception{IOError}}{}
|
|---|
| 396 | \lineiii{PyExc_ImportError\ttindex{PyExc_ImportError}}{\exception{ImportError}}{}
|
|---|
| 397 | \lineiii{PyExc_IndexError\ttindex{PyExc_IndexError}}{\exception{IndexError}}{}
|
|---|
| 398 | \lineiii{PyExc_KeyError\ttindex{PyExc_KeyError}}{\exception{KeyError}}{}
|
|---|
| 399 | \lineiii{PyExc_KeyboardInterrupt\ttindex{PyExc_KeyboardInterrupt}}{\exception{KeyboardInterrupt}}{}
|
|---|
| 400 | \lineiii{PyExc_MemoryError\ttindex{PyExc_MemoryError}}{\exception{MemoryError}}{}
|
|---|
| 401 | \lineiii{PyExc_NameError\ttindex{PyExc_NameError}}{\exception{NameError}}{}
|
|---|
| 402 | \lineiii{PyExc_NotImplementedError\ttindex{PyExc_NotImplementedError}}{\exception{NotImplementedError}}{}
|
|---|
| 403 | \lineiii{PyExc_OSError\ttindex{PyExc_OSError}}{\exception{OSError}}{}
|
|---|
| 404 | \lineiii{PyExc_OverflowError\ttindex{PyExc_OverflowError}}{\exception{OverflowError}}{}
|
|---|
| 405 | \lineiii{PyExc_ReferenceError\ttindex{PyExc_ReferenceError}}{\exception{ReferenceError}}{(2)}
|
|---|
| 406 | \lineiii{PyExc_RuntimeError\ttindex{PyExc_RuntimeError}}{\exception{RuntimeError}}{}
|
|---|
| 407 | \lineiii{PyExc_SyntaxError\ttindex{PyExc_SyntaxError}}{\exception{SyntaxError}}{}
|
|---|
| 408 | \lineiii{PyExc_SystemError\ttindex{PyExc_SystemError}}{\exception{SystemError}}{}
|
|---|
| 409 | \lineiii{PyExc_SystemExit\ttindex{PyExc_SystemExit}}{\exception{SystemExit}}{}
|
|---|
| 410 | \lineiii{PyExc_TypeError\ttindex{PyExc_TypeError}}{\exception{TypeError}}{}
|
|---|
| 411 | \lineiii{PyExc_ValueError\ttindex{PyExc_ValueError}}{\exception{ValueError}}{}
|
|---|
| 412 | \lineiii{PyExc_WindowsError\ttindex{PyExc_WindowsError}}{\exception{WindowsError}}{(3)}
|
|---|
| 413 | \lineiii{PyExc_ZeroDivisionError\ttindex{PyExc_ZeroDivisionError}}{\exception{ZeroDivisionError}}{}
|
|---|
| 414 | \end{tableiii}
|
|---|
| 415 |
|
|---|
| 416 | \noindent
|
|---|
| 417 | Notes:
|
|---|
| 418 | \begin{description}
|
|---|
| 419 | \item[(1)]
|
|---|
| 420 | This is a base class for other standard exceptions.
|
|---|
| 421 |
|
|---|
| 422 | \item[(2)]
|
|---|
| 423 | This is the same as \exception{weakref.ReferenceError}.
|
|---|
| 424 |
|
|---|
| 425 | \item[(3)]
|
|---|
| 426 | Only defined on Windows; protect code that uses this by testing that
|
|---|
| 427 | the preprocessor macro \code{MS_WINDOWS} is defined.
|
|---|
| 428 |
|
|---|
| 429 | \item[(4)]
|
|---|
| 430 | \versionadded{2.5}
|
|---|
| 431 | \end{description}
|
|---|
| 432 |
|
|---|
| 433 |
|
|---|
| 434 | \section{Deprecation of String Exceptions}
|
|---|
| 435 |
|
|---|
| 436 | All exceptions built into Python or provided in the standard library
|
|---|
| 437 | are derived from \exception{BaseException}.
|
|---|
| 438 | \withsubitem{(built-in exception)}{\ttindex{BaseException}}
|
|---|
| 439 |
|
|---|
| 440 | String exceptions are still supported in the interpreter to allow
|
|---|
| 441 | existing code to run unmodified, but this will also change in a future
|
|---|
| 442 | release.
|
|---|