pyerrors.h 12.5 KB
Newer Older
1 2 3 4 5 6
#ifndef Py_ERRORS_H
#define Py_ERRORS_H
#ifdef __cplusplus
extern "C" {
#endif

Guido van Rossum's avatar
Guido van Rossum committed
7 8
/* Error handling definitions */

9 10
PyAPI_FUNC(void) PyErr_SetNone(PyObject *);
PyAPI_FUNC(void) PyErr_SetObject(PyObject *, PyObject *);
11 12 13 14
PyAPI_FUNC(void) PyErr_SetString(
    PyObject *exception,
    const char *string   /* decoded from utf-8 */
    );
15 16 17 18
PyAPI_FUNC(PyObject *) PyErr_Occurred(void);
PyAPI_FUNC(void) PyErr_Clear(void);
PyAPI_FUNC(void) PyErr_Fetch(PyObject **, PyObject **, PyObject **);
PyAPI_FUNC(void) PyErr_Restore(PyObject *, PyObject *, PyObject *);
19
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03030000
20 21
PyAPI_FUNC(void) PyErr_GetExcInfo(PyObject **, PyObject **, PyObject **);
PyAPI_FUNC(void) PyErr_SetExcInfo(PyObject *, PyObject *, PyObject *);
22
#endif
23

24
/* Defined in Python/pylifecycle.c */
25
PyAPI_FUNC(void) _Py_NO_RETURN Py_FatalError(const char *message);
Guido van Rossum's avatar
Guido van Rossum committed
26

27
#if defined(Py_DEBUG) || defined(Py_LIMITED_API)
28 29
#define _PyErr_OCCURRED() PyErr_Occurred()
#else
30
#define _PyErr_OCCURRED() (PyThreadState_GET()->curexc_type)
31 32
#endif

33
/* Error testing and normalization */
34 35 36
PyAPI_FUNC(int) PyErr_GivenExceptionMatches(PyObject *, PyObject *);
PyAPI_FUNC(int) PyErr_ExceptionMatches(PyObject *);
PyAPI_FUNC(void) PyErr_NormalizeException(PyObject**, PyObject**, PyObject**);
37

38 39 40 41 42 43 44 45 46 47 48 49
/* Traceback manipulation (PEP 3134) */
PyAPI_FUNC(int) PyException_SetTraceback(PyObject *, PyObject *);
PyAPI_FUNC(PyObject *) PyException_GetTraceback(PyObject *);

/* Cause manipulation (PEP 3134) */
PyAPI_FUNC(PyObject *) PyException_GetCause(PyObject *);
PyAPI_FUNC(void) PyException_SetCause(PyObject *, PyObject *);

/* Context manipulation (PEP 3134) */
PyAPI_FUNC(PyObject *) PyException_GetContext(PyObject *);
PyAPI_FUNC(void) PyException_SetContext(PyObject *, PyObject *);

50
/* */
51

52 53 54
#define PyExceptionClass_Check(x)                                       \
    (PyType_Check((x)) &&                                               \
     PyType_FastSubclass((PyTypeObject*)(x), Py_TPFLAGS_BASE_EXC_SUBCLASS))
55

56 57
#define PyExceptionInstance_Check(x)                    \
    PyType_FastSubclass((x)->ob_type, Py_TPFLAGS_BASE_EXC_SUBCLASS)
58

59
PyAPI_FUNC(const char *) PyExceptionClass_Name(PyObject *);
60

61
#define PyExceptionInstance_Class(x) ((PyObject*)((x)->ob_type))
62

63

Guido van Rossum's avatar
Guido van Rossum committed
64
/* Predefined exceptions */
65

66
PyAPI_DATA(PyObject *) PyExc_BaseException;
67
PyAPI_DATA(PyObject *) PyExc_Exception;
68
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03050000
69
PyAPI_DATA(PyObject *) PyExc_StopAsyncIteration;
70
#endif
71
PyAPI_DATA(PyObject *) PyExc_StopIteration;
72
PyAPI_DATA(PyObject *) PyExc_GeneratorExit;
73 74 75 76 77
PyAPI_DATA(PyObject *) PyExc_ArithmeticError;
PyAPI_DATA(PyObject *) PyExc_LookupError;

PyAPI_DATA(PyObject *) PyExc_AssertionError;
PyAPI_DATA(PyObject *) PyExc_AttributeError;
78
PyAPI_DATA(PyObject *) PyExc_BufferError;
79 80 81 82
PyAPI_DATA(PyObject *) PyExc_EOFError;
PyAPI_DATA(PyObject *) PyExc_FloatingPointError;
PyAPI_DATA(PyObject *) PyExc_OSError;
PyAPI_DATA(PyObject *) PyExc_ImportError;
83
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03060000
84
PyAPI_DATA(PyObject *) PyExc_ModuleNotFoundError;
85
#endif
86 87 88 89 90 91 92
PyAPI_DATA(PyObject *) PyExc_IndexError;
PyAPI_DATA(PyObject *) PyExc_KeyError;
PyAPI_DATA(PyObject *) PyExc_KeyboardInterrupt;
PyAPI_DATA(PyObject *) PyExc_MemoryError;
PyAPI_DATA(PyObject *) PyExc_NameError;
PyAPI_DATA(PyObject *) PyExc_OverflowError;
PyAPI_DATA(PyObject *) PyExc_RuntimeError;
93
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03050000
94
PyAPI_DATA(PyObject *) PyExc_RecursionError;
95
#endif
96 97 98 99
PyAPI_DATA(PyObject *) PyExc_NotImplementedError;
PyAPI_DATA(PyObject *) PyExc_SyntaxError;
PyAPI_DATA(PyObject *) PyExc_IndentationError;
PyAPI_DATA(PyObject *) PyExc_TabError;
100
PyAPI_DATA(PyObject *) PyExc_TargetScopeError;
101 102 103 104 105 106
PyAPI_DATA(PyObject *) PyExc_ReferenceError;
PyAPI_DATA(PyObject *) PyExc_SystemError;
PyAPI_DATA(PyObject *) PyExc_SystemExit;
PyAPI_DATA(PyObject *) PyExc_TypeError;
PyAPI_DATA(PyObject *) PyExc_UnboundLocalError;
PyAPI_DATA(PyObject *) PyExc_UnicodeError;
107 108 109
PyAPI_DATA(PyObject *) PyExc_UnicodeEncodeError;
PyAPI_DATA(PyObject *) PyExc_UnicodeDecodeError;
PyAPI_DATA(PyObject *) PyExc_UnicodeTranslateError;
110 111
PyAPI_DATA(PyObject *) PyExc_ValueError;
PyAPI_DATA(PyObject *) PyExc_ZeroDivisionError;
112

113
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03030000
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
PyAPI_DATA(PyObject *) PyExc_BlockingIOError;
PyAPI_DATA(PyObject *) PyExc_BrokenPipeError;
PyAPI_DATA(PyObject *) PyExc_ChildProcessError;
PyAPI_DATA(PyObject *) PyExc_ConnectionError;
PyAPI_DATA(PyObject *) PyExc_ConnectionAbortedError;
PyAPI_DATA(PyObject *) PyExc_ConnectionRefusedError;
PyAPI_DATA(PyObject *) PyExc_ConnectionResetError;
PyAPI_DATA(PyObject *) PyExc_FileExistsError;
PyAPI_DATA(PyObject *) PyExc_FileNotFoundError;
PyAPI_DATA(PyObject *) PyExc_InterruptedError;
PyAPI_DATA(PyObject *) PyExc_IsADirectoryError;
PyAPI_DATA(PyObject *) PyExc_NotADirectoryError;
PyAPI_DATA(PyObject *) PyExc_PermissionError;
PyAPI_DATA(PyObject *) PyExc_ProcessLookupError;
PyAPI_DATA(PyObject *) PyExc_TimeoutError;
129
#endif
130 131 132 133 134


/* Compatibility aliases */
PyAPI_DATA(PyObject *) PyExc_EnvironmentError;
PyAPI_DATA(PyObject *) PyExc_IOError;
135
#ifdef MS_WINDOWS
136
PyAPI_DATA(PyObject *) PyExc_WindowsError;
137
#endif
138

139
/* Predefined warning categories */
140 141 142 143 144 145
PyAPI_DATA(PyObject *) PyExc_Warning;
PyAPI_DATA(PyObject *) PyExc_UserWarning;
PyAPI_DATA(PyObject *) PyExc_DeprecationWarning;
PyAPI_DATA(PyObject *) PyExc_PendingDeprecationWarning;
PyAPI_DATA(PyObject *) PyExc_SyntaxWarning;
PyAPI_DATA(PyObject *) PyExc_RuntimeWarning;
146
PyAPI_DATA(PyObject *) PyExc_FutureWarning;
147
PyAPI_DATA(PyObject *) PyExc_ImportWarning;
148
PyAPI_DATA(PyObject *) PyExc_UnicodeWarning;
149
PyAPI_DATA(PyObject *) PyExc_BytesWarning;
150
PyAPI_DATA(PyObject *) PyExc_ResourceWarning;
151

152

153 154
/* Convenience functions */

155 156 157
PyAPI_FUNC(int) PyErr_BadArgument(void);
PyAPI_FUNC(PyObject *) PyErr_NoMemory(void);
PyAPI_FUNC(PyObject *) PyErr_SetFromErrno(PyObject *);
158
PyAPI_FUNC(PyObject *) PyErr_SetFromErrnoWithFilenameObject(
159
    PyObject *, PyObject *);
160
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03040000
161 162
PyAPI_FUNC(PyObject *) PyErr_SetFromErrnoWithFilenameObjects(
    PyObject *, PyObject *, PyObject *);
163
#endif
Benjamin Peterson's avatar
Benjamin Peterson committed
164
PyAPI_FUNC(PyObject *) PyErr_SetFromErrnoWithFilename(
165 166 167
    PyObject *exc,
    const char *filename   /* decoded from the filesystem encoding */
    );
168

169 170 171 172 173
PyAPI_FUNC(PyObject *) PyErr_Format(
    PyObject *exception,
    const char *format,   /* ASCII-encoded string  */
    ...
    );
174 175 176 177 178 179
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03050000
PyAPI_FUNC(PyObject *) PyErr_FormatV(
    PyObject *exception,
    const char *format,
    va_list vargs);
#endif
180

181
#ifdef MS_WINDOWS
182
PyAPI_FUNC(PyObject *) PyErr_SetFromWindowsErrWithFilename(
183
    int ierr,
184
    const char *filename        /* decoded from the filesystem encoding */
185
    );
186
PyAPI_FUNC(PyObject *) PyErr_SetFromWindowsErr(int);
187
PyAPI_FUNC(PyObject *) PyErr_SetExcFromWindowsErrWithFilenameObject(
188
    PyObject *,int, PyObject *);
189
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03040000
190 191
PyAPI_FUNC(PyObject *) PyErr_SetExcFromWindowsErrWithFilenameObjects(
    PyObject *,int, PyObject *, PyObject *);
192
#endif
193
PyAPI_FUNC(PyObject *) PyErr_SetExcFromWindowsErrWithFilename(
194 195
    PyObject *exc,
    int ierr,
196
    const char *filename        /* decoded from the filesystem encoding */
197
    );
198
PyAPI_FUNC(PyObject *) PyErr_SetExcFromWindowsErr(PyObject *, int);
199
#endif /* MS_WINDOWS */
200

201
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03060000
202 203
PyAPI_FUNC(PyObject *) PyErr_SetImportErrorSubclass(PyObject *, PyObject *,
    PyObject *, PyObject *);
204 205
#endif
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03030000
206 207
PyAPI_FUNC(PyObject *) PyErr_SetImportError(PyObject *, PyObject *,
    PyObject *);
208
#endif
209

210
/* Export the old function so that the existing API remains available: */
211
PyAPI_FUNC(void) PyErr_BadInternalCall(void);
212
PyAPI_FUNC(void) _PyErr_BadInternalCall(const char *filename, int lineno);
213 214 215
/* Mask the old API with a call to the new API for code compiled under
   Python 2.0: */
#define PyErr_BadInternalCall() _PyErr_BadInternalCall(__FILE__, __LINE__)
216

217
/* Function to create a new exception */
218
PyAPI_FUNC(PyObject *) PyErr_NewException(
219
    const char *name, PyObject *base, PyObject *dict);
220
PyAPI_FUNC(PyObject *) PyErr_NewExceptionWithDoc(
221
    const char *name, const char *doc, PyObject *base, PyObject *dict);
222
PyAPI_FUNC(void) PyErr_WriteUnraisable(PyObject *);
223

224

225
/* In signalmodule.c */
226 227
PyAPI_FUNC(int) PyErr_CheckSignals(void);
PyAPI_FUNC(void) PyErr_SetInterrupt(void);
228 229

/* Support for adding program text to SyntaxErrors */
230 231 232 233 234 235 236 237 238 239
PyAPI_FUNC(void) PyErr_SyntaxLocation(
    const char *filename,       /* decoded from the filesystem encoding */
    int lineno);
PyAPI_FUNC(void) PyErr_SyntaxLocationEx(
    const char *filename,       /* decoded from the filesystem encoding */
    int lineno,
    int col_offset);
PyAPI_FUNC(PyObject *) PyErr_ProgramText(
    const char *filename,       /* decoded from the filesystem encoding */
    int lineno);
240

241 242
/* The following functions are used to create and modify unicode
   exceptions from C */
243

244 245
/* create a UnicodeDecodeError object */
PyAPI_FUNC(PyObject *) PyUnicodeDecodeError_Create(
246 247 248 249 250 251 252
    const char *encoding,       /* UTF-8 encoded string */
    const char *object,
    Py_ssize_t length,
    Py_ssize_t start,
    Py_ssize_t end,
    const char *reason          /* UTF-8 encoded string */
    );
253 254 255 256 257 258 259 260 261 262 263 264

/* get the encoding attribute */
PyAPI_FUNC(PyObject *) PyUnicodeEncodeError_GetEncoding(PyObject *);
PyAPI_FUNC(PyObject *) PyUnicodeDecodeError_GetEncoding(PyObject *);

/* get the object attribute */
PyAPI_FUNC(PyObject *) PyUnicodeEncodeError_GetObject(PyObject *);
PyAPI_FUNC(PyObject *) PyUnicodeDecodeError_GetObject(PyObject *);
PyAPI_FUNC(PyObject *) PyUnicodeTranslateError_GetObject(PyObject *);

/* get the value of the start attribute (the int * may not be NULL)
   return 0 on success, -1 on failure */
Martin v. Löwis's avatar
Martin v. Löwis committed
265 266 267
PyAPI_FUNC(int) PyUnicodeEncodeError_GetStart(PyObject *, Py_ssize_t *);
PyAPI_FUNC(int) PyUnicodeDecodeError_GetStart(PyObject *, Py_ssize_t *);
PyAPI_FUNC(int) PyUnicodeTranslateError_GetStart(PyObject *, Py_ssize_t *);
268 269 270

/* assign a new value to the start attribute
   return 0 on success, -1 on failure */
Martin v. Löwis's avatar
Martin v. Löwis committed
271 272 273
PyAPI_FUNC(int) PyUnicodeEncodeError_SetStart(PyObject *, Py_ssize_t);
PyAPI_FUNC(int) PyUnicodeDecodeError_SetStart(PyObject *, Py_ssize_t);
PyAPI_FUNC(int) PyUnicodeTranslateError_SetStart(PyObject *, Py_ssize_t);
274 275 276

/* get the value of the end attribute (the int *may not be NULL)
 return 0 on success, -1 on failure */
Martin v. Löwis's avatar
Martin v. Löwis committed
277 278 279
PyAPI_FUNC(int) PyUnicodeEncodeError_GetEnd(PyObject *, Py_ssize_t *);
PyAPI_FUNC(int) PyUnicodeDecodeError_GetEnd(PyObject *, Py_ssize_t *);
PyAPI_FUNC(int) PyUnicodeTranslateError_GetEnd(PyObject *, Py_ssize_t *);
280 281 282

/* assign a new value to the end attribute
   return 0 on success, -1 on failure */
Martin v. Löwis's avatar
Martin v. Löwis committed
283 284 285
PyAPI_FUNC(int) PyUnicodeEncodeError_SetEnd(PyObject *, Py_ssize_t);
PyAPI_FUNC(int) PyUnicodeDecodeError_SetEnd(PyObject *, Py_ssize_t);
PyAPI_FUNC(int) PyUnicodeTranslateError_SetEnd(PyObject *, Py_ssize_t);
286 287 288 289 290 291 292 293 294

/* get the value of the reason attribute */
PyAPI_FUNC(PyObject *) PyUnicodeEncodeError_GetReason(PyObject *);
PyAPI_FUNC(PyObject *) PyUnicodeDecodeError_GetReason(PyObject *);
PyAPI_FUNC(PyObject *) PyUnicodeTranslateError_GetReason(PyObject *);

/* assign a new value to the reason attribute
   return 0 on success, -1 on failure */
PyAPI_FUNC(int) PyUnicodeEncodeError_SetReason(
295 296 297
    PyObject *exc,
    const char *reason          /* UTF-8 encoded string */
    );
298
PyAPI_FUNC(int) PyUnicodeDecodeError_SetReason(
299 300 301
    PyObject *exc,
    const char *reason          /* UTF-8 encoded string */
    );
302
PyAPI_FUNC(int) PyUnicodeTranslateError_SetReason(
303 304 305
    PyObject *exc,
    const char *reason          /* UTF-8 encoded string */
    );
306

307 308 309
/* These APIs aren't really part of the error implementation, but
   often needed to format error messages; the native C lib APIs are
   not available on all platforms, which is why we provide emulations
310 311 312 313 314
   for those platforms in Python/mysnprintf.c,
   WARNING:  The return value of snprintf varies across platforms; do
   not rely on any particular behavior; eventually the C99 defn may
   be reliable.
*/
315 316 317 318 319
#if defined(MS_WIN32) && !defined(HAVE_SNPRINTF)
# define HAVE_SNPRINTF
# define snprintf _snprintf
# define vsnprintf _vsnprintf
#endif
320

321
#include <stdarg.h>
322
PyAPI_FUNC(int) PyOS_snprintf(char *str, size_t size, const char  *format, ...)
323
                        Py_GCC_ATTRIBUTE((format(printf, 3, 4)));
324
PyAPI_FUNC(int) PyOS_vsnprintf(char *str, size_t size, const char  *format, va_list va)
325
                        Py_GCC_ATTRIBUTE((format(printf, 3, 0)));
326

327 328 329 330 331 332
#ifndef Py_LIMITED_API
#  define Py_CPYTHON_ERRORS_H
#  include  "cpython/pyerrors.h"
#  undef Py_CPYTHON_ERRORS_H
#endif

333 334 335 336
#ifdef __cplusplus
}
#endif
#endif /* !Py_ERRORS_H */