blob: ae1d9905eb641e97bbbef51520f166ffef2f76f5 [file] [log] [blame]
Guido van Rossuma3309961993-07-28 09:05:47 +00001#ifndef Py_ERRORS_H
2#define Py_ERRORS_H
3#ifdef __cplusplus
4extern "C" {
5#endif
6
Richard Jones7b9558d2006-05-27 12:29:24 +00007/* Error objects */
8
9typedef struct {
10 PyObject_HEAD
11 PyObject *dict;
12 PyObject *args;
13 PyObject *message;
14} PyBaseExceptionObject;
15
16typedef struct {
17 PyObject_HEAD
18 PyObject *dict;
19 PyObject *args;
20 PyObject *message;
21 PyObject *msg;
22 PyObject *filename;
23 PyObject *lineno;
24 PyObject *offset;
25 PyObject *text;
26 PyObject *print_file_and_line;
27} PySyntaxErrorObject;
28
29#ifdef Py_USING_UNICODE
30typedef struct {
31 PyObject_HEAD
32 PyObject *dict;
33 PyObject *args;
34 PyObject *message;
35 PyObject *encoding;
36 PyObject *object;
37 PyObject *start;
38 PyObject *end;
39 PyObject *reason;
40} PyUnicodeErrorObject;
41#endif
42
43typedef struct {
44 PyObject_HEAD
45 PyObject *dict;
46 PyObject *args;
47 PyObject *message;
48 PyObject *code;
49} PySystemExitObject;
50
51typedef struct {
52 PyObject_HEAD
53 PyObject *dict;
54 PyObject *args;
55 PyObject *message;
56 PyObject *myerrno;
57 PyObject *strerror;
58 PyObject *filename;
59} PyEnvironmentErrorObject;
60
61#ifdef MS_WINDOWS
62typedef struct {
63 PyObject_HEAD
64 PyObject *dict;
65 PyObject *args;
66 PyObject *message;
67 PyObject *myerrno;
68 PyObject *strerror;
69 PyObject *filename;
70 PyObject *winerror;
71} PyWindowsErrorObject;
72#endif
Guido van Rossumf70e43a1991-02-19 12:39:46 +000073
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000074/* Error handling definitions */
75
Mark Hammond91a681d2002-08-12 07:21:58 +000076PyAPI_FUNC(void) PyErr_SetNone(PyObject *);
77PyAPI_FUNC(void) PyErr_SetObject(PyObject *, PyObject *);
78PyAPI_FUNC(void) PyErr_SetString(PyObject *, const char *);
79PyAPI_FUNC(PyObject *) PyErr_Occurred(void);
80PyAPI_FUNC(void) PyErr_Clear(void);
81PyAPI_FUNC(void) PyErr_Fetch(PyObject **, PyObject **, PyObject **);
82PyAPI_FUNC(void) PyErr_Restore(PyObject *, PyObject *, PyObject *);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000083
Raymond Hettinger5ba0cbe2005-08-06 18:31:24 +000084#ifdef Py_DEBUG
85#define _PyErr_OCCURRED() PyErr_Occurred()
86#else
87#define _PyErr_OCCURRED() (_PyThreadState_Current->curexc_type)
88#endif
89
Barry Warsawc0dc92a1997-08-22 21:22:58 +000090/* Error testing and normalization */
Mark Hammond91a681d2002-08-12 07:21:58 +000091PyAPI_FUNC(int) PyErr_GivenExceptionMatches(PyObject *, PyObject *);
92PyAPI_FUNC(int) PyErr_ExceptionMatches(PyObject *);
93PyAPI_FUNC(void) PyErr_NormalizeException(PyObject**, PyObject**, PyObject**);
Barry Warsawc0dc92a1997-08-22 21:22:58 +000094
Brett Cannonbf364092006-03-01 04:25:17 +000095/* */
Barry Warsawc0dc92a1997-08-22 21:22:58 +000096
Brett Cannonbf364092006-03-01 04:25:17 +000097#define PyExceptionClass_Check(x) \
98 (PyClass_Check((x)) \
99 || (PyType_Check((x)) && PyType_IsSubtype( \
100 (PyTypeObject*)(x), (PyTypeObject*)PyExc_BaseException)))
101
102
103#define PyExceptionInstance_Check(x) \
104 (PyInstance_Check((x)) || \
105 (PyType_IsSubtype((x)->ob_type, (PyTypeObject*)PyExc_BaseException)))
106
107#define PyExceptionClass_Name(x) \
108 (PyClass_Check((x)) \
109 ? PyString_AS_STRING(((PyClassObject*)(x))->cl_name) \
110 : (char *)(((PyTypeObject*)(x))->tp_name))
111
112#define PyExceptionInstance_Class(x) \
113 ((PyInstance_Check((x)) \
114 ? (PyObject*)((PyInstanceObject*)(x))->in_class \
115 : (PyObject*)((x)->ob_type)))
116
117
Guido van Rossum3f5da241990-12-20 15:06:42 +0000118/* Predefined exceptions */
Guido van Rossum5c600e11990-10-14 20:00:25 +0000119
Brett Cannonbf364092006-03-01 04:25:17 +0000120PyAPI_DATA(PyObject *) PyExc_BaseException;
Mark Hammond91a681d2002-08-12 07:21:58 +0000121PyAPI_DATA(PyObject *) PyExc_Exception;
122PyAPI_DATA(PyObject *) PyExc_StopIteration;
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000123PyAPI_DATA(PyObject *) PyExc_GeneratorExit;
Mark Hammond91a681d2002-08-12 07:21:58 +0000124PyAPI_DATA(PyObject *) PyExc_StandardError;
125PyAPI_DATA(PyObject *) PyExc_ArithmeticError;
126PyAPI_DATA(PyObject *) PyExc_LookupError;
Barry Warsaw392d8271997-08-29 21:56:07 +0000127
Mark Hammond91a681d2002-08-12 07:21:58 +0000128PyAPI_DATA(PyObject *) PyExc_AssertionError;
129PyAPI_DATA(PyObject *) PyExc_AttributeError;
130PyAPI_DATA(PyObject *) PyExc_EOFError;
131PyAPI_DATA(PyObject *) PyExc_FloatingPointError;
132PyAPI_DATA(PyObject *) PyExc_EnvironmentError;
133PyAPI_DATA(PyObject *) PyExc_IOError;
134PyAPI_DATA(PyObject *) PyExc_OSError;
135PyAPI_DATA(PyObject *) PyExc_ImportError;
136PyAPI_DATA(PyObject *) PyExc_IndexError;
137PyAPI_DATA(PyObject *) PyExc_KeyError;
138PyAPI_DATA(PyObject *) PyExc_KeyboardInterrupt;
139PyAPI_DATA(PyObject *) PyExc_MemoryError;
140PyAPI_DATA(PyObject *) PyExc_NameError;
141PyAPI_DATA(PyObject *) PyExc_OverflowError;
142PyAPI_DATA(PyObject *) PyExc_RuntimeError;
143PyAPI_DATA(PyObject *) PyExc_NotImplementedError;
144PyAPI_DATA(PyObject *) PyExc_SyntaxError;
145PyAPI_DATA(PyObject *) PyExc_IndentationError;
146PyAPI_DATA(PyObject *) PyExc_TabError;
147PyAPI_DATA(PyObject *) PyExc_ReferenceError;
148PyAPI_DATA(PyObject *) PyExc_SystemError;
149PyAPI_DATA(PyObject *) PyExc_SystemExit;
150PyAPI_DATA(PyObject *) PyExc_TypeError;
151PyAPI_DATA(PyObject *) PyExc_UnboundLocalError;
152PyAPI_DATA(PyObject *) PyExc_UnicodeError;
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000153PyAPI_DATA(PyObject *) PyExc_UnicodeEncodeError;
154PyAPI_DATA(PyObject *) PyExc_UnicodeDecodeError;
155PyAPI_DATA(PyObject *) PyExc_UnicodeTranslateError;
Mark Hammond91a681d2002-08-12 07:21:58 +0000156PyAPI_DATA(PyObject *) PyExc_ValueError;
157PyAPI_DATA(PyObject *) PyExc_ZeroDivisionError;
Guido van Rossumdb0c9f72000-02-17 15:17:18 +0000158#ifdef MS_WINDOWS
Mark Hammond91a681d2002-08-12 07:21:58 +0000159PyAPI_DATA(PyObject *) PyExc_WindowsError;
Guido van Rossumdb0c9f72000-02-17 15:17:18 +0000160#endif
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000161#ifdef __VMS
Neal Norwitz21d896c2003-07-01 20:15:21 +0000162PyAPI_DATA(PyObject *) PyExc_VMSError;
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000163#endif
Guido van Rossum3e55cb61990-10-21 22:09:30 +0000164
Mark Hammond91a681d2002-08-12 07:21:58 +0000165PyAPI_DATA(PyObject *) PyExc_MemoryErrorInst;
Barry Warsaw392d8271997-08-29 21:56:07 +0000166
Guido van Rossum3fc30372000-12-15 21:57:34 +0000167/* Predefined warning categories */
Mark Hammond91a681d2002-08-12 07:21:58 +0000168PyAPI_DATA(PyObject *) PyExc_Warning;
169PyAPI_DATA(PyObject *) PyExc_UserWarning;
170PyAPI_DATA(PyObject *) PyExc_DeprecationWarning;
171PyAPI_DATA(PyObject *) PyExc_PendingDeprecationWarning;
172PyAPI_DATA(PyObject *) PyExc_SyntaxWarning;
Mark Hammond91a681d2002-08-12 07:21:58 +0000173PyAPI_DATA(PyObject *) PyExc_RuntimeWarning;
Barry Warsaw9f007392002-08-14 15:51:29 +0000174PyAPI_DATA(PyObject *) PyExc_FutureWarning;
Thomas Wouters9df4e6f2006-04-27 23:13:20 +0000175PyAPI_DATA(PyObject *) PyExc_ImportWarning;
Guido van Rossum3fc30372000-12-15 21:57:34 +0000176
Barry Warsaw392d8271997-08-29 21:56:07 +0000177
Guido van Rossum5c600e11990-10-14 20:00:25 +0000178/* Convenience functions */
179
Mark Hammond91a681d2002-08-12 07:21:58 +0000180PyAPI_FUNC(int) PyErr_BadArgument(void);
181PyAPI_FUNC(PyObject *) PyErr_NoMemory(void);
182PyAPI_FUNC(PyObject *) PyErr_SetFromErrno(PyObject *);
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000183PyAPI_FUNC(PyObject *) PyErr_SetFromErrnoWithFilenameObject(
184 PyObject *, PyObject *);
Mark Hammond91a681d2002-08-12 07:21:58 +0000185PyAPI_FUNC(PyObject *) PyErr_SetFromErrnoWithFilename(PyObject *, char *);
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000186#ifdef Py_WIN_WIDE_FILENAMES
187PyAPI_FUNC(PyObject *) PyErr_SetFromErrnoWithUnicodeFilename(
188 PyObject *, Py_UNICODE *);
189#endif /* Py_WIN_WIDE_FILENAMES */
190
Mark Hammond91a681d2002-08-12 07:21:58 +0000191PyAPI_FUNC(PyObject *) PyErr_Format(PyObject *, const char *, ...)
Neil Schemenauer96aa0ac2002-09-15 14:09:54 +0000192 Py_GCC_ATTRIBUTE((format(printf, 2, 3)));
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000193
Guido van Rossumdb0c9f72000-02-17 15:17:18 +0000194#ifdef MS_WINDOWS
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000195PyAPI_FUNC(PyObject *) PyErr_SetFromWindowsErrWithFilenameObject(
196 int, const char *);
197PyAPI_FUNC(PyObject *) PyErr_SetFromWindowsErrWithFilename(
198 int, const char *);
199#ifdef Py_WIN_WIDE_FILENAMES
200PyAPI_FUNC(PyObject *) PyErr_SetFromWindowsErrWithUnicodeFilename(
201 int, const Py_UNICODE *);
202#endif /* Py_WIN_WIDE_FILENAMES */
Mark Hammond91a681d2002-08-12 07:21:58 +0000203PyAPI_FUNC(PyObject *) PyErr_SetFromWindowsErr(int);
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000204PyAPI_FUNC(PyObject *) PyErr_SetExcFromWindowsErrWithFilenameObject(
205 PyObject *,int, PyObject *);
Mark Hammond91a681d2002-08-12 07:21:58 +0000206PyAPI_FUNC(PyObject *) PyErr_SetExcFromWindowsErrWithFilename(
Thomas Heller085358a2002-07-29 14:27:41 +0000207 PyObject *,int, const char *);
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000208#ifdef Py_WIN_WIDE_FILENAMES
209PyAPI_FUNC(PyObject *) PyErr_SetExcFromWindowsErrWithUnicodeFilename(
210 PyObject *,int, const Py_UNICODE *);
211#endif /* Py_WIN_WIDE_FILENAMES */
Mark Hammond91a681d2002-08-12 07:21:58 +0000212PyAPI_FUNC(PyObject *) PyErr_SetExcFromWindowsErr(PyObject *, int);
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000213#endif /* MS_WINDOWS */
Guido van Rossum3e55cb61990-10-21 22:09:30 +0000214
Fred Drake6d63adf2000-08-24 22:38:39 +0000215/* Export the old function so that the existing API remains available: */
Mark Hammond91a681d2002-08-12 07:21:58 +0000216PyAPI_FUNC(void) PyErr_BadInternalCall(void);
217PyAPI_FUNC(void) _PyErr_BadInternalCall(char *filename, int lineno);
Fred Drake6d63adf2000-08-24 22:38:39 +0000218/* Mask the old API with a call to the new API for code compiled under
219 Python 2.0: */
220#define PyErr_BadInternalCall() _PyErr_BadInternalCall(__FILE__, __LINE__)
Guido van Rossum25831651993-05-19 14:50:45 +0000221
Guido van Rossum2d2c3441997-09-16 21:50:37 +0000222/* Function to create a new exception */
Mark Hammond91a681d2002-08-12 07:21:58 +0000223PyAPI_FUNC(PyObject *) PyErr_NewException(char *name, PyObject *base,
Fred Draked5c84ed2000-07-08 17:25:55 +0000224 PyObject *dict);
Mark Hammond91a681d2002-08-12 07:21:58 +0000225PyAPI_FUNC(void) PyErr_WriteUnraisable(PyObject *);
Guido van Rossum2d2c3441997-09-16 21:50:37 +0000226
Guido van Rossum3fc30372000-12-15 21:57:34 +0000227/* Issue a warning or exception */
Neal Norwitz07aadb12006-07-30 06:55:48 +0000228PyAPI_FUNC(int) PyErr_WarnEx(PyObject *category, const char *msg,
229 Py_ssize_t stack_level);
Martin v. Löwis95292d62002-12-11 14:04:59 +0000230PyAPI_FUNC(int) PyErr_WarnExplicit(PyObject *, const char *,
231 const char *, int,
232 const char *, PyObject *);
Neal Norwitz07aadb12006-07-30 06:55:48 +0000233/* PyErr_Warn is only for backwards compatability and will be removed.
234 Use PyErr_WarnEx instead. */
235#define PyErr_Warn(category, msg) PyErr_WarnEx(category, msg, 1)
Guido van Rossum3fc30372000-12-15 21:57:34 +0000236
Barry Warsawf3f41a91997-01-03 00:15:03 +0000237/* In sigcheck.c or signalmodule.c */
Mark Hammond91a681d2002-08-12 07:21:58 +0000238PyAPI_FUNC(int) PyErr_CheckSignals(void);
239PyAPI_FUNC(void) PyErr_SetInterrupt(void);
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000240
241/* Support for adding program text to SyntaxErrors */
Martin v. Löwis95292d62002-12-11 14:04:59 +0000242PyAPI_FUNC(void) PyErr_SyntaxLocation(const char *, int);
243PyAPI_FUNC(PyObject *) PyErr_ProgramText(const char *, int);
Tim Petersa437d452001-11-28 16:51:49 +0000244
Walter Dörwaldbf73db82002-11-21 20:08:33 +0000245#ifdef Py_USING_UNICODE
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000246/* The following functions are used to create and modify unicode
247 exceptions from C */
Walter Dörwaldbf73db82002-11-21 20:08:33 +0000248
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000249/* create a UnicodeDecodeError object */
250PyAPI_FUNC(PyObject *) PyUnicodeDecodeError_Create(
Martin v. Löwis18e16552006-02-15 17:27:45 +0000251 const char *, const char *, Py_ssize_t, Py_ssize_t, Py_ssize_t, const char *);
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000252
253/* create a UnicodeEncodeError object */
254PyAPI_FUNC(PyObject *) PyUnicodeEncodeError_Create(
Martin v. Löwis18e16552006-02-15 17:27:45 +0000255 const char *, const Py_UNICODE *, Py_ssize_t, Py_ssize_t, Py_ssize_t, const char *);
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000256
257/* create a UnicodeTranslateError object */
258PyAPI_FUNC(PyObject *) PyUnicodeTranslateError_Create(
Martin v. Löwis18e16552006-02-15 17:27:45 +0000259 const Py_UNICODE *, Py_ssize_t, Py_ssize_t, Py_ssize_t, const char *);
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000260
261/* get the encoding attribute */
262PyAPI_FUNC(PyObject *) PyUnicodeEncodeError_GetEncoding(PyObject *);
263PyAPI_FUNC(PyObject *) PyUnicodeDecodeError_GetEncoding(PyObject *);
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000264
265/* get the object attribute */
266PyAPI_FUNC(PyObject *) PyUnicodeEncodeError_GetObject(PyObject *);
267PyAPI_FUNC(PyObject *) PyUnicodeDecodeError_GetObject(PyObject *);
268PyAPI_FUNC(PyObject *) PyUnicodeTranslateError_GetObject(PyObject *);
269
270/* get the value of the start attribute (the int * may not be NULL)
271 return 0 on success, -1 on failure */
Martin v. Löwis18e16552006-02-15 17:27:45 +0000272PyAPI_FUNC(int) PyUnicodeEncodeError_GetStart(PyObject *, Py_ssize_t *);
273PyAPI_FUNC(int) PyUnicodeDecodeError_GetStart(PyObject *, Py_ssize_t *);
274PyAPI_FUNC(int) PyUnicodeTranslateError_GetStart(PyObject *, Py_ssize_t *);
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000275
276/* assign a new value to the start attribute
277 return 0 on success, -1 on failure */
Martin v. Löwis18e16552006-02-15 17:27:45 +0000278PyAPI_FUNC(int) PyUnicodeEncodeError_SetStart(PyObject *, Py_ssize_t);
279PyAPI_FUNC(int) PyUnicodeDecodeError_SetStart(PyObject *, Py_ssize_t);
280PyAPI_FUNC(int) PyUnicodeTranslateError_SetStart(PyObject *, Py_ssize_t);
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000281
282/* get the value of the end attribute (the int *may not be NULL)
283 return 0 on success, -1 on failure */
Martin v. Löwis18e16552006-02-15 17:27:45 +0000284PyAPI_FUNC(int) PyUnicodeEncodeError_GetEnd(PyObject *, Py_ssize_t *);
285PyAPI_FUNC(int) PyUnicodeDecodeError_GetEnd(PyObject *, Py_ssize_t *);
286PyAPI_FUNC(int) PyUnicodeTranslateError_GetEnd(PyObject *, Py_ssize_t *);
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000287
288/* assign a new value to the end attribute
289 return 0 on success, -1 on failure */
Martin v. Löwis18e16552006-02-15 17:27:45 +0000290PyAPI_FUNC(int) PyUnicodeEncodeError_SetEnd(PyObject *, Py_ssize_t);
291PyAPI_FUNC(int) PyUnicodeDecodeError_SetEnd(PyObject *, Py_ssize_t);
292PyAPI_FUNC(int) PyUnicodeTranslateError_SetEnd(PyObject *, Py_ssize_t);
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000293
294/* get the value of the reason attribute */
295PyAPI_FUNC(PyObject *) PyUnicodeEncodeError_GetReason(PyObject *);
296PyAPI_FUNC(PyObject *) PyUnicodeDecodeError_GetReason(PyObject *);
297PyAPI_FUNC(PyObject *) PyUnicodeTranslateError_GetReason(PyObject *);
298
299/* assign a new value to the reason attribute
300 return 0 on success, -1 on failure */
301PyAPI_FUNC(int) PyUnicodeEncodeError_SetReason(
302 PyObject *, const char *);
303PyAPI_FUNC(int) PyUnicodeDecodeError_SetReason(
304 PyObject *, const char *);
305PyAPI_FUNC(int) PyUnicodeTranslateError_SetReason(
306 PyObject *, const char *);
Walter Dörwaldbf73db82002-11-21 20:08:33 +0000307#endif
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000308
309
Marc-André Lemburge5006eb2001-07-31 13:24:44 +0000310/* These APIs aren't really part of the error implementation, but
311 often needed to format error messages; the native C lib APIs are
312 not available on all platforms, which is why we provide emulations
Tim Petersa437d452001-11-28 16:51:49 +0000313 for those platforms in Python/mysnprintf.c,
314 WARNING: The return value of snprintf varies across platforms; do
315 not rely on any particular behavior; eventually the C99 defn may
316 be reliable.
317*/
Marc-André Lemburge5006eb2001-07-31 13:24:44 +0000318#if defined(MS_WIN32) && !defined(HAVE_SNPRINTF)
319# define HAVE_SNPRINTF
320# define snprintf _snprintf
321# define vsnprintf _vsnprintf
322#endif
Marc-André Lemburg03d1b182001-07-31 18:05:33 +0000323
Marc-André Lemburgcecd9e02001-07-31 14:23:52 +0000324#include <stdarg.h>
Mark Hammond91a681d2002-08-12 07:21:58 +0000325PyAPI_FUNC(int) PyOS_snprintf(char *str, size_t size, const char *format, ...)
Neil Schemenauer96aa0ac2002-09-15 14:09:54 +0000326 Py_GCC_ATTRIBUTE((format(printf, 3, 4)));
Mark Hammond91a681d2002-08-12 07:21:58 +0000327PyAPI_FUNC(int) PyOS_vsnprintf(char *str, size_t size, const char *format, va_list va)
Neil Schemenauer96aa0ac2002-09-15 14:09:54 +0000328 Py_GCC_ATTRIBUTE((format(printf, 3, 0)));
Guido van Rossumb6775db1994-08-01 11:34:53 +0000329
Guido van Rossuma3309961993-07-28 09:05:47 +0000330#ifdef __cplusplus
331}
332#endif
333#endif /* !Py_ERRORS_H */