source: sage/ext/python_exc.pxi @ 1765:c10d47e46b20

Revision 1765:c10d47e46b20, 12.8 KB checked in by William Stein <wstein@…>, 7 years ago (diff)

Added and finish transition to new Python/C API pxi files; add Parent and ParentWithGens?.

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