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

7 8
/* Error objects */

9
#ifndef Py_LIMITED_API
10
/* PyException_HEAD defines the initial segment of every exception class. */
11
#define PyException_HEAD PyObject_HEAD PyObject *dict;\
12
             PyObject *args; PyObject *traceback;\
13
             PyObject *context; PyObject *cause;\
14
             char suppress_context;
15

16
typedef struct {
17
    PyException_HEAD
18 19 20
} PyBaseExceptionObject;

typedef struct {
21
    PyException_HEAD
22 23 24 25 26 27 28 29
    PyObject *msg;
    PyObject *filename;
    PyObject *lineno;
    PyObject *offset;
    PyObject *text;
    PyObject *print_file_and_line;
} PySyntaxErrorObject;

30 31 32 33 34 35 36
typedef struct {
    PyException_HEAD
    PyObject *msg;
    PyObject *name;
    PyObject *path;
} PyImportErrorObject;

37
typedef struct {
38
    PyException_HEAD
39 40
    PyObject *encoding;
    PyObject *object;
41 42
    Py_ssize_t start;
    Py_ssize_t end;
43 44 45 46
    PyObject *reason;
} PyUnicodeErrorObject;

typedef struct {
47
    PyException_HEAD
48 49 50 51
    PyObject *code;
} PySystemExitObject;

typedef struct {
52
    PyException_HEAD
53 54 55
    PyObject *myerrno;
    PyObject *strerror;
    PyObject *filename;
56
    PyObject *filename2;
57 58 59
#ifdef MS_WINDOWS
    PyObject *winerror;
#endif
60 61 62
    Py_ssize_t written;   /* only for BlockingIOError, -1 otherwise */
} PyOSErrorObject;

63 64 65 66 67
typedef struct {
    PyException_HEAD
    PyObject *value;
} PyStopIterationObject;

68 69 70 71
/* Compatibility typedefs */
typedef PyOSErrorObject PyEnvironmentErrorObject;
#ifdef MS_WINDOWS
typedef PyOSErrorObject PyWindowsErrorObject;
72
#endif
73
#endif /* !Py_LIMITED_API */
74

Guido van Rossum's avatar
Guido van Rossum committed
75 76
/* Error handling definitions */

77 78
PyAPI_FUNC(void) PyErr_SetNone(PyObject *);
PyAPI_FUNC(void) PyErr_SetObject(PyObject *, PyObject *);
79
#ifndef Py_LIMITED_API
80
PyAPI_FUNC(void) _PyErr_SetKeyError(PyObject *);
81
#endif
82 83 84 85
PyAPI_FUNC(void) PyErr_SetString(
    PyObject *exception,
    const char *string   /* decoded from utf-8 */
    );
86 87 88 89
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 *);
90 91
PyAPI_FUNC(void) PyErr_GetExcInfo(PyObject **, PyObject **, PyObject **);
PyAPI_FUNC(void) PyErr_SetExcInfo(PyObject *, PyObject *, PyObject *);
92 93

#if defined(__clang__) || \
94
    (defined(__GNUC_MAJOR__) && \
95 96 97 98 99 100 101 102
     ((__GNUC_MAJOR__ >= 3) || \
      (__GNUC_MAJOR__ == 2) && (__GNUC_MINOR__ >= 5)))
#define _Py_NO_RETURN __attribute__((__noreturn__))
#else
#define _Py_NO_RETURN
#endif

PyAPI_FUNC(void) Py_FatalError(const char *message) _Py_NO_RETURN;
Guido van Rossum's avatar
Guido van Rossum committed
103

104
#if defined(Py_DEBUG) || defined(Py_LIMITED_API)
105 106
#define _PyErr_OCCURRED() PyErr_Occurred()
#else
107
#define _PyErr_OCCURRED() (PyThreadState_GET()->curexc_type)
108 109
#endif

110
/* Error testing and normalization */
111 112 113
PyAPI_FUNC(int) PyErr_GivenExceptionMatches(PyObject *, PyObject *);
PyAPI_FUNC(int) PyErr_ExceptionMatches(PyObject *);
PyAPI_FUNC(void) PyErr_NormalizeException(PyObject**, PyObject**, PyObject**);
114

115 116 117 118 119 120 121 122 123 124 125
/* 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 *);
126 127 128
#ifndef Py_LIMITED_API
PyAPI_FUNC(void) _PyErr_ChainExceptions(PyObject *, PyObject *, PyObject *);
#endif
129

130
/* */
131

132 133 134
#define PyExceptionClass_Check(x)                                       \
    (PyType_Check((x)) &&                                               \
     PyType_FastSubclass((PyTypeObject*)(x), Py_TPFLAGS_BASE_EXC_SUBCLASS))
135

136 137
#define PyExceptionInstance_Check(x)                    \
    PyType_FastSubclass((x)->ob_type, Py_TPFLAGS_BASE_EXC_SUBCLASS)
138

139
#define PyExceptionClass_Name(x) \
140
     ((char *)(((PyTypeObject*)(x))->tp_name))
141

142
#define PyExceptionInstance_Class(x) ((PyObject*)((x)->ob_type))
143

144

Guido van Rossum's avatar
Guido van Rossum committed
145
/* Predefined exceptions */
146

147
PyAPI_DATA(PyObject *) PyExc_BaseException;
148 149
PyAPI_DATA(PyObject *) PyExc_Exception;
PyAPI_DATA(PyObject *) PyExc_StopIteration;
150
PyAPI_DATA(PyObject *) PyExc_GeneratorExit;
151 152 153 154 155
PyAPI_DATA(PyObject *) PyExc_ArithmeticError;
PyAPI_DATA(PyObject *) PyExc_LookupError;

PyAPI_DATA(PyObject *) PyExc_AssertionError;
PyAPI_DATA(PyObject *) PyExc_AttributeError;
156
PyAPI_DATA(PyObject *) PyExc_BufferError;
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
PyAPI_DATA(PyObject *) PyExc_EOFError;
PyAPI_DATA(PyObject *) PyExc_FloatingPointError;
PyAPI_DATA(PyObject *) PyExc_OSError;
PyAPI_DATA(PyObject *) PyExc_ImportError;
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;
PyAPI_DATA(PyObject *) PyExc_NotImplementedError;
PyAPI_DATA(PyObject *) PyExc_SyntaxError;
PyAPI_DATA(PyObject *) PyExc_IndentationError;
PyAPI_DATA(PyObject *) PyExc_TabError;
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;
178 179 180
PyAPI_DATA(PyObject *) PyExc_UnicodeEncodeError;
PyAPI_DATA(PyObject *) PyExc_UnicodeDecodeError;
PyAPI_DATA(PyObject *) PyExc_UnicodeTranslateError;
181 182
PyAPI_DATA(PyObject *) PyExc_ValueError;
PyAPI_DATA(PyObject *) PyExc_ZeroDivisionError;
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203

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;


/* Compatibility aliases */
PyAPI_DATA(PyObject *) PyExc_EnvironmentError;
PyAPI_DATA(PyObject *) PyExc_IOError;
204
#ifdef MS_WINDOWS
205
PyAPI_DATA(PyObject *) PyExc_WindowsError;
206
#endif
207

208
PyAPI_DATA(PyObject *) PyExc_RecursionErrorInst;
209

210
/* Predefined warning categories */
211 212 213 214 215 216
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;
217
PyAPI_DATA(PyObject *) PyExc_FutureWarning;
218
PyAPI_DATA(PyObject *) PyExc_ImportWarning;
219
PyAPI_DATA(PyObject *) PyExc_UnicodeWarning;
220
PyAPI_DATA(PyObject *) PyExc_BytesWarning;
221
PyAPI_DATA(PyObject *) PyExc_ResourceWarning;
222

223

224 225
/* Convenience functions */

226 227 228
PyAPI_FUNC(int) PyErr_BadArgument(void);
PyAPI_FUNC(PyObject *) PyErr_NoMemory(void);
PyAPI_FUNC(PyObject *) PyErr_SetFromErrno(PyObject *);
229
PyAPI_FUNC(PyObject *) PyErr_SetFromErrnoWithFilenameObject(
230
    PyObject *, PyObject *);
231 232
PyAPI_FUNC(PyObject *) PyErr_SetFromErrnoWithFilenameObjects(
    PyObject *, PyObject *, PyObject *);
Benjamin Peterson's avatar
Benjamin Peterson committed
233
PyAPI_FUNC(PyObject *) PyErr_SetFromErrnoWithFilename(
234 235 236
    PyObject *exc,
    const char *filename   /* decoded from the filesystem encoding */
    );
237
#if defined(MS_WINDOWS) && !defined(Py_LIMITED_API)
238
PyAPI_FUNC(PyObject *) PyErr_SetFromErrnoWithUnicodeFilename(
239
    PyObject *, const Py_UNICODE *);
240
#endif /* MS_WINDOWS */
241

242 243 244 245 246
PyAPI_FUNC(PyObject *) PyErr_Format(
    PyObject *exception,
    const char *format,   /* ASCII-encoded string  */
    ...
    );
247

248
#ifdef MS_WINDOWS
249
PyAPI_FUNC(PyObject *) PyErr_SetFromWindowsErrWithFilename(
250
    int ierr,
251
    const char *filename        /* decoded from the filesystem encoding */
252
    );
253 254
#ifndef Py_LIMITED_API
/* XXX redeclare to use WSTRING */
255
PyAPI_FUNC(PyObject *) PyErr_SetFromWindowsErrWithUnicodeFilename(
256
    int, const Py_UNICODE *);
257
#endif
258
PyAPI_FUNC(PyObject *) PyErr_SetFromWindowsErr(int);
259
PyAPI_FUNC(PyObject *) PyErr_SetExcFromWindowsErrWithFilenameObject(
260
    PyObject *,int, PyObject *);
261 262
PyAPI_FUNC(PyObject *) PyErr_SetExcFromWindowsErrWithFilenameObjects(
    PyObject *,int, PyObject *, PyObject *);
263
PyAPI_FUNC(PyObject *) PyErr_SetExcFromWindowsErrWithFilename(
264 265
    PyObject *exc,
    int ierr,
266
    const char *filename        /* decoded from the filesystem encoding */
267
    );
268
#ifndef Py_LIMITED_API
269
PyAPI_FUNC(PyObject *) PyErr_SetExcFromWindowsErrWithUnicodeFilename(
270
    PyObject *,int, const Py_UNICODE *);
271
#endif
272
PyAPI_FUNC(PyObject *) PyErr_SetExcFromWindowsErr(PyObject *, int);
273
#endif /* MS_WINDOWS */
274

275 276
PyAPI_FUNC(PyObject *) PyErr_SetExcWithArgsKwargs(PyObject *, PyObject *,
    PyObject *);
277 278
PyAPI_FUNC(PyObject *) PyErr_SetImportError(PyObject *, PyObject *,
    PyObject *);
279

280
/* Export the old function so that the existing API remains available: */
281
PyAPI_FUNC(void) PyErr_BadInternalCall(void);
282
PyAPI_FUNC(void) _PyErr_BadInternalCall(const char *filename, int lineno);
283 284 285
/* 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__)
286

287
/* Function to create a new exception */
288
PyAPI_FUNC(PyObject *) PyErr_NewException(
289
    const char *name, PyObject *base, PyObject *dict);
290
PyAPI_FUNC(PyObject *) PyErr_NewExceptionWithDoc(
291
    const char *name, const char *doc, PyObject *base, PyObject *dict);
292
PyAPI_FUNC(void) PyErr_WriteUnraisable(PyObject *);
293

294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
/* In exceptions.c */
#ifndef Py_LIMITED_API
/* Helper that attempts to replace the current exception with one of the
 * same type but with a prefix added to the exception text. The resulting
 * exception description looks like:
 *
 *     prefix (exc_type: original_exc_str)
 *
 * Only some exceptions can be safely replaced. If the function determines
 * it isn't safe to perform the replacement, it will leave the original
 * unmodified exception in place.
 *
 * Returns a borrowed reference to the new exception (if any), NULL if the
 * existing exception was left in place.
 */
PyAPI_FUNC(PyObject *) _PyErr_TrySetFromCause(
    const char *prefix_format,   /* ASCII-encoded string  */
    ...
    );
#endif


316
/* In sigcheck.c or signalmodule.c */
317 318
PyAPI_FUNC(int) PyErr_CheckSignals(void);
PyAPI_FUNC(void) PyErr_SetInterrupt(void);
319

320
/* In signalmodule.c */
321
#ifndef Py_LIMITED_API
322
int PySignal_SetWakeupFd(int fd);
323
#endif
324

325
/* Support for adding program text to SyntaxErrors */
326 327 328 329 330 331 332
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);
333
#ifndef Py_LIMITED_API
334 335 336 337
PyAPI_FUNC(void) PyErr_SyntaxLocationObject(
    PyObject *filename,
    int lineno,
    int col_offset);
338
#endif
339 340 341
PyAPI_FUNC(PyObject *) PyErr_ProgramText(
    const char *filename,       /* decoded from the filesystem encoding */
    int lineno);
342
#ifndef Py_LIMITED_API
343 344 345
PyAPI_FUNC(PyObject *) PyErr_ProgramTextObject(
    PyObject *filename,
    int lineno);
346
#endif
347

348 349
/* The following functions are used to create and modify unicode
   exceptions from C */
350

351 352
/* create a UnicodeDecodeError object */
PyAPI_FUNC(PyObject *) PyUnicodeDecodeError_Create(
353 354 355 356 357 358 359
    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 */
    );
360 361

/* create a UnicodeEncodeError object */
362
#ifndef Py_LIMITED_API
363
PyAPI_FUNC(PyObject *) PyUnicodeEncodeError_Create(
364 365 366 367 368 369 370
    const char *encoding,       /* UTF-8 encoded string */
    const Py_UNICODE *object,
    Py_ssize_t length,
    Py_ssize_t start,
    Py_ssize_t end,
    const char *reason          /* UTF-8 encoded string */
    );
371
#endif
372 373

/* create a UnicodeTranslateError object */
374
#ifndef Py_LIMITED_API
375
PyAPI_FUNC(PyObject *) PyUnicodeTranslateError_Create(
376 377 378 379 380 381
    const Py_UNICODE *object,
    Py_ssize_t length,
    Py_ssize_t start,
    Py_ssize_t end,
    const char *reason          /* UTF-8 encoded string */
    );
Martin v. Löwis's avatar
Martin v. Löwis committed
382 383 384 385 386 387
PyAPI_FUNC(PyObject *) _PyUnicodeTranslateError_Create(
    PyObject *object,
    Py_ssize_t start,
    Py_ssize_t end,
    const char *reason          /* UTF-8 encoded string */
    );
388
#endif
389 390 391 392 393 394 395 396 397 398 399 400

/* 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
401 402 403
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 *);
404 405 406

/* 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
407 408 409
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);
410 411 412

/* 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
413 414 415
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 *);
416 417 418

/* 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
419 420 421
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);
422 423 424 425 426 427 428 429 430

/* 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(
431 432 433
    PyObject *exc,
    const char *reason          /* UTF-8 encoded string */
    );
434
PyAPI_FUNC(int) PyUnicodeDecodeError_SetReason(
435 436 437
    PyObject *exc,
    const char *reason          /* UTF-8 encoded string */
    );
438
PyAPI_FUNC(int) PyUnicodeTranslateError_SetReason(
439 440 441
    PyObject *exc,
    const char *reason          /* UTF-8 encoded string */
    );
442

443 444 445
/* 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
446 447 448 449 450
   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.
*/
451 452 453 454 455
#if defined(MS_WIN32) && !defined(HAVE_SNPRINTF)
# define HAVE_SNPRINTF
# define snprintf _snprintf
# define vsnprintf _vsnprintf
#endif
456

457
#include <stdarg.h>
458
PyAPI_FUNC(int) PyOS_snprintf(char *str, size_t size, const char  *format, ...)
459
                        Py_GCC_ATTRIBUTE((format(printf, 3, 4)));
460
PyAPI_FUNC(int) PyOS_vsnprintf(char *str, size_t size, const char  *format, va_list va)
461
                        Py_GCC_ATTRIBUTE((format(printf, 3, 0)));
462

463 464 465 466
#ifdef __cplusplus
}
#endif
#endif /* !Py_ERRORS_H */