exc.pxd 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. from .object cimport PyObject
  2. cdef extern from "Python.h":
  3. #####################################################################
  4. # 3. Exception Handling
  5. #####################################################################
  6. # The functions described in this chapter will let you handle and
  7. # raise Python exceptions. It is important to understand some of
  8. # the basics of Python exception handling. It works somewhat like
  9. # the Unix errno variable: there is a global indicator (per
  10. # thread) of the last error that occurred. Most functions don't
  11. # clear this on success, but will set it to indicate the cause of
  12. # the error on failure. Most functions also return an error
  13. # indicator, usually NULL if they are supposed to return a
  14. # pointer, or -1 if they return an integer (exception: the
  15. # PyArg_*() functions return 1 for success and 0 for failure).
  16. # When a function must fail because some function it called
  17. # failed, it generally doesn't set the error indicator; the
  18. # function it called already set it. It is responsible for either
  19. # handling the error and clearing the exception or returning after
  20. # cleaning up any resources it holds (such as object references or
  21. # memory allocations); it should not continue normally if it is
  22. # not prepared to handle the error. If returning due to an error,
  23. # it is important to indicate to the caller that an error has been
  24. # set. If the error is not handled or carefully propagated,
  25. # additional calls into the Python/C API may not behave as
  26. # intended and may fail in mysterious ways.
  27. # The error indicator consists of three Python objects
  28. # corresponding to the Python variables sys.exc_type,
  29. # sys.exc_value and sys.exc_traceback. API functions exist to
  30. # interact with the error indicator in various ways. There is a
  31. # separate error indicator for each thread.
  32. void PyErr_Print()
  33. # Print a standard traceback to sys.stderr and clear the error
  34. # indicator. Call this function only when the error indicator is
  35. # set. (Otherwise it will cause a fatal error!)
  36. PyObject* PyErr_Occurred()
  37. # Return value: Borrowed reference.
  38. # Test whether the error indicator is set. If set, return the
  39. # exception type (the first argument to the last call to one of
  40. # the PyErr_Set*() functions or to PyErr_Restore()). If not set,
  41. # return NULL. You do not own a reference to the return value, so
  42. # you do not need to Py_DECREF() it. Note: Do not compare the
  43. # return value to a specific exception; use
  44. # PyErr_ExceptionMatches() instead, shown below. (The comparison
  45. # could easily fail since the exception may be an instance instead
  46. # of a class, in the case of a class exception, or it may be a
  47. # subclass of the expected exception.)
  48. bint PyErr_ExceptionMatches(object exc)
  49. # Equivalent to "PyErr_GivenExceptionMatches(PyErr_Occurred(),
  50. # exc)". This should only be called when an exception is actually
  51. # set; a memory access violation will occur if no exception has
  52. # been raised.
  53. bint PyErr_GivenExceptionMatches(object given, object exc)
  54. # Return true if the given exception matches the exception in
  55. # exc. If exc is a class object, this also returns true when given
  56. # is an instance of a subclass. If exc is a tuple, all exceptions
  57. # in the tuple (and recursively in subtuples) are searched for a
  58. # match. If given is NULL, a memory access violation will occur.
  59. void PyErr_NormalizeException(PyObject** exc, PyObject** val, PyObject** tb)
  60. # Under certain circumstances, the values returned by
  61. # PyErr_Fetch() below can be ``unnormalized'', meaning that *exc
  62. # is a class object but *val is not an instance of the same
  63. # class. This function can be used to instantiate the class in
  64. # that case. If the values are already normalized, nothing
  65. # happens. The delayed normalization is implemented to improve
  66. # performance.
  67. void PyErr_Clear()
  68. # Clear the error indicator. If the error indicator is not set, there is no effect.
  69. void PyErr_Fetch(PyObject** ptype, PyObject** pvalue, PyObject** ptraceback)
  70. # Retrieve the error indicator into three variables whose
  71. # addresses are passed. If the error indicator is not set, set all
  72. # three variables to NULL. If it is set, it will be cleared and
  73. # you own a reference to each object retrieved. The value and
  74. # traceback object may be NULL even when the type object is
  75. # not. Note: This function is normally only used by code that
  76. # needs to handle exceptions or by code that needs to save and
  77. # restore the error indicator temporarily.
  78. void PyErr_Restore(PyObject* type, PyObject* value, PyObject* traceback)
  79. # Set the error indicator from the three objects. If the error
  80. # indicator is already set, it is cleared first. If the objects
  81. # are NULL, the error indicator is cleared. Do not pass a NULL
  82. # type and non-NULL value or traceback. The exception type should
  83. # be a class. Do not pass an invalid exception type or
  84. # value. (Violating these rules will cause subtle problems later.)
  85. # This call takes away a reference to each object: you must own a
  86. # reference to each object before the call and after the call you
  87. # no longer own these references. (If you don't understand this,
  88. # don't use this function. I warned you.) Note: This function is
  89. # normally only used by code that needs to save and restore the
  90. # error indicator temporarily; use PyErr_Fetch() to save the
  91. # current exception state.
  92. void PyErr_SetString(object type, char *message)
  93. # This is the most common way to set the error indicator. The
  94. # first argument specifies the exception type; it is normally one
  95. # of the standard exceptions, e.g. PyExc_RuntimeError. You need
  96. # not increment its reference count. The second argument is an
  97. # error message; it is converted to a string object.
  98. void PyErr_SetObject(object type, object value)
  99. # This function is similar to PyErr_SetString() but lets you
  100. # specify an arbitrary Python object for the ``value'' of the
  101. # exception.
  102. PyObject* PyErr_Format(object exception, char *format, ...) except NULL
  103. # Return value: Always NULL.
  104. # This function sets the error indicator and returns
  105. # NULL. exception should be a Python exception (class, not an
  106. # instance). format should be a string, containing format codes,
  107. # similar to printf(). The width.precision before a format code is
  108. # parsed, but the width part is ignored.
  109. void PyErr_SetNone(object type)
  110. # This is a shorthand for "PyErr_SetObject(type, Py_None)".
  111. int PyErr_BadArgument() except 0
  112. # This is a shorthand for "PyErr_SetString(PyExc_TypeError,
  113. # message)", where message indicates that a built-in operation was
  114. # invoked with an illegal argument. It is mostly for internal use.
  115. PyObject* PyErr_NoMemory() except NULL
  116. # Return value: Always NULL.
  117. # This is a shorthand for "PyErr_SetNone(PyExc_MemoryError)"; it
  118. # returns NULL so an object allocation function can write "return
  119. # PyErr_NoMemory();" when it runs out of memory.
  120. PyObject* PyErr_SetFromErrno(object type) except NULL
  121. # Return value: Always NULL.
  122. # This is a convenience function to raise an exception when a C
  123. # library function has returned an error and set the C variable
  124. # errno. It constructs a tuple object whose first item is the
  125. # integer errno value and whose second item is the corresponding
  126. # error message (gotten from strerror()), and then calls
  127. # "PyErr_SetObject(type, object)". On Unix, when the errno value
  128. # is EINTR, indicating an interrupted system call, this calls
  129. # PyErr_CheckSignals(), and if that set the error indicator,
  130. # leaves it set to that. The function always returns NULL, so a
  131. # wrapper function around a system call can write "return
  132. # PyErr_SetFromErrno(type);" when the system call returns an
  133. # error.
  134. PyObject* PyErr_SetFromErrnoWithFilenameObject(object type, object filenameObject) except NULL
  135. # Similar to PyErr_SetFromErrno(), with the additional behavior
  136. # that if filenameObject is not NULL, it is passed to the
  137. # constructor of type as a third parameter.
  138. # In the case of OSError exception, this is used to define
  139. # the filename attribute of the exception instance.
  140. PyObject* PyErr_SetFromErrnoWithFilename(object type, char *filename) except NULL
  141. # Return value: Always NULL. Similar to PyErr_SetFromErrno(),
  142. # with the additional behavior that if filename is not NULL, it is
  143. # passed to the constructor of type as a third parameter. In the
  144. # case of exceptions such as IOError and OSError, this is used to
  145. # define the filename attribute of the exception instance.
  146. PyObject* PyErr_SetFromWindowsErr(int ierr) except NULL
  147. # Return value: Always NULL. This is a convenience function to
  148. # raise WindowsError. If called with ierr of 0, the error code
  149. # returned by a call to GetLastError() is used instead. It calls
  150. # the Win32 function FormatMessage() to retrieve the Windows
  151. # description of error code given by ierr or GetLastError(), then
  152. # it constructs a tuple object whose first item is the ierr value
  153. # and whose second item is the corresponding error message (gotten
  154. # from FormatMessage()), and then calls
  155. # "PyErr_SetObject(PyExc_WindowsError, object)". This function
  156. # always returns NULL. Availability: Windows.
  157. PyObject* PyErr_SetExcFromWindowsErr(object type, int ierr) except NULL
  158. # Return value: Always NULL. Similar to
  159. # PyErr_SetFromWindowsErr(), with an additional parameter
  160. # specifying the exception type to be raised. Availability:
  161. # Windows. New in version 2.3.
  162. PyObject* PyErr_SetFromWindowsErrWithFilename(int ierr, char *filename) except NULL
  163. # Return value: Always NULL. Similar to
  164. # PyErr_SetFromWindowsErr(), with the additional behavior that if
  165. # filename is not NULL, it is passed to the constructor of
  166. # WindowsError as a third parameter. Availability: Windows.
  167. PyObject* PyErr_SetExcFromWindowsErrWithFilename(object type, int ierr, char *filename) except NULL
  168. # Return value: Always NULL.
  169. # Similar to PyErr_SetFromWindowsErrWithFilename(), with an
  170. # additional parameter specifying the exception type to be
  171. # raised. Availability: Windows.
  172. void PyErr_BadInternalCall()
  173. # This is a shorthand for "PyErr_SetString(PyExc_TypeError,
  174. # message)", where message indicates that an internal operation
  175. # (e.g. a Python/C API function) was invoked with an illegal
  176. # argument. It is mostly for internal use.
  177. int PyErr_WarnEx(object category, char *message, int stacklevel) except -1
  178. # Issue a warning message. The category argument is a warning
  179. # category (see below) or NULL; the message argument is a message
  180. # string. stacklevel is a positive number giving a number of stack
  181. # frames; the warning will be issued from the currently executing
  182. # line of code in that stack frame. A stacklevel of 1 is the
  183. # function calling PyErr_WarnEx(), 2 is the function above that,
  184. # and so forth.
  185. int PyErr_WarnExplicit(object category, char *message, char *filename, int lineno, char *module, object registry) except -1
  186. # Issue a warning message with explicit control over all warning
  187. # attributes. This is a straightforward wrapper around the Python
  188. # function warnings.warn_explicit(), see there for more
  189. # information. The module and registry arguments may be set to
  190. # NULL to get the default effect described there.
  191. int PyErr_CheckSignals() except -1
  192. # This function interacts with Python's signal handling. It checks
  193. # whether a signal has been sent to the processes and if so,
  194. # invokes the corresponding signal handler. If the signal module
  195. # is supported, this can invoke a signal handler written in
  196. # Python. In all cases, the default effect for SIGINT is to raise
  197. # the KeyboardInterrupt exception. If an exception is raised the
  198. # error indicator is set and the function returns 1; otherwise the
  199. # function returns 0. The error indicator may or may not be
  200. # cleared if it was previously set.
  201. void PyErr_SetInterrupt() nogil
  202. # This function simulates the effect of a SIGINT signal arriving
  203. # -- the next time PyErr_CheckSignals() is called,
  204. # KeyboardInterrupt will be raised. It may be called without
  205. # holding the interpreter lock.
  206. object PyErr_NewException(char *name, object base, object dict)
  207. # Return value: New reference.
  208. # This utility function creates and returns a new exception
  209. # object. The name argument must be the name of the new exception,
  210. # a C string of the form module.class. The base and dict arguments
  211. # are normally NULL. This creates a class object derived from
  212. # Exception (accessible in C as PyExc_Exception).
  213. void PyErr_WriteUnraisable(object obj)
  214. # This utility function prints a warning message to sys.stderr
  215. # when an exception has been set but it is impossible for the
  216. # interpreter to actually raise the exception. It is used, for
  217. # example, when an exception occurs in an __del__() method.
  218. #
  219. # The function is called with a single argument obj that
  220. # identifies the context in which the unraisable exception
  221. # occurred. The repr of obj will be printed in the warning
  222. # message.