pyerrors.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. #ifndef Py_ERRORS_H
  2. #define Py_ERRORS_H
  3. #ifdef __cplusplus
  4. extern "C" {
  5. #endif
  6. /* Error handling definitions */
  7. PyAPI_FUNC(void) PyErr_SetNone(PyObject *);
  8. PyAPI_FUNC(void) PyErr_SetObject(PyObject *, PyObject *);
  9. PyAPI_FUNC(void) PyErr_SetString(
  10. PyObject *exception,
  11. const char *string /* decoded from utf-8 */
  12. );
  13. PyAPI_FUNC(PyObject *) PyErr_Occurred(void);
  14. PyAPI_FUNC(void) PyErr_Clear(void);
  15. PyAPI_FUNC(void) PyErr_Fetch(PyObject **, PyObject **, PyObject **);
  16. PyAPI_FUNC(void) PyErr_Restore(PyObject *, PyObject *, PyObject *);
  17. #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03030000
  18. PyAPI_FUNC(void) PyErr_GetExcInfo(PyObject **, PyObject **, PyObject **);
  19. PyAPI_FUNC(void) PyErr_SetExcInfo(PyObject *, PyObject *, PyObject *);
  20. #endif
  21. /* Defined in Python/pylifecycle.c */
  22. PyAPI_FUNC(void) _Py_NO_RETURN Py_FatalError(const char *message);
  23. #if defined(Py_DEBUG) || defined(Py_LIMITED_API)
  24. #define _PyErr_OCCURRED() PyErr_Occurred()
  25. #else
  26. #define _PyErr_OCCURRED() (PyThreadState_GET()->curexc_type)
  27. #endif
  28. /* Error testing and normalization */
  29. PyAPI_FUNC(int) PyErr_GivenExceptionMatches(PyObject *, PyObject *);
  30. PyAPI_FUNC(int) PyErr_ExceptionMatches(PyObject *);
  31. PyAPI_FUNC(void) PyErr_NormalizeException(PyObject**, PyObject**, PyObject**);
  32. /* Traceback manipulation (PEP 3134) */
  33. PyAPI_FUNC(int) PyException_SetTraceback(PyObject *, PyObject *);
  34. PyAPI_FUNC(PyObject *) PyException_GetTraceback(PyObject *);
  35. /* Cause manipulation (PEP 3134) */
  36. PyAPI_FUNC(PyObject *) PyException_GetCause(PyObject *);
  37. PyAPI_FUNC(void) PyException_SetCause(PyObject *, PyObject *);
  38. /* Context manipulation (PEP 3134) */
  39. PyAPI_FUNC(PyObject *) PyException_GetContext(PyObject *);
  40. PyAPI_FUNC(void) PyException_SetContext(PyObject *, PyObject *);
  41. /* */
  42. #define PyExceptionClass_Check(x) \
  43. (PyType_Check((x)) && \
  44. PyType_FastSubclass((PyTypeObject*)(x), Py_TPFLAGS_BASE_EXC_SUBCLASS))
  45. #define PyExceptionInstance_Check(x) \
  46. PyType_FastSubclass((x)->ob_type, Py_TPFLAGS_BASE_EXC_SUBCLASS)
  47. PyAPI_FUNC(const char *) PyExceptionClass_Name(PyObject *);
  48. #define PyExceptionInstance_Class(x) ((PyObject*)((x)->ob_type))
  49. /* Predefined exceptions */
  50. PyAPI_DATA(PyObject *) PyExc_BaseException;
  51. PyAPI_DATA(PyObject *) PyExc_Exception;
  52. #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03050000
  53. PyAPI_DATA(PyObject *) PyExc_StopAsyncIteration;
  54. #endif
  55. PyAPI_DATA(PyObject *) PyExc_StopIteration;
  56. PyAPI_DATA(PyObject *) PyExc_GeneratorExit;
  57. PyAPI_DATA(PyObject *) PyExc_ArithmeticError;
  58. PyAPI_DATA(PyObject *) PyExc_LookupError;
  59. PyAPI_DATA(PyObject *) PyExc_AssertionError;
  60. PyAPI_DATA(PyObject *) PyExc_AttributeError;
  61. PyAPI_DATA(PyObject *) PyExc_BufferError;
  62. PyAPI_DATA(PyObject *) PyExc_EOFError;
  63. PyAPI_DATA(PyObject *) PyExc_FloatingPointError;
  64. PyAPI_DATA(PyObject *) PyExc_OSError;
  65. PyAPI_DATA(PyObject *) PyExc_ImportError;
  66. #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03060000
  67. PyAPI_DATA(PyObject *) PyExc_ModuleNotFoundError;
  68. #endif
  69. PyAPI_DATA(PyObject *) PyExc_IndexError;
  70. PyAPI_DATA(PyObject *) PyExc_KeyError;
  71. PyAPI_DATA(PyObject *) PyExc_KeyboardInterrupt;
  72. PyAPI_DATA(PyObject *) PyExc_MemoryError;
  73. PyAPI_DATA(PyObject *) PyExc_NameError;
  74. PyAPI_DATA(PyObject *) PyExc_OverflowError;
  75. PyAPI_DATA(PyObject *) PyExc_RuntimeError;
  76. #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03050000
  77. PyAPI_DATA(PyObject *) PyExc_RecursionError;
  78. #endif
  79. PyAPI_DATA(PyObject *) PyExc_NotImplementedError;
  80. PyAPI_DATA(PyObject *) PyExc_SyntaxError;
  81. PyAPI_DATA(PyObject *) PyExc_IndentationError;
  82. PyAPI_DATA(PyObject *) PyExc_TabError;
  83. PyAPI_DATA(PyObject *) PyExc_ReferenceError;
  84. PyAPI_DATA(PyObject *) PyExc_SystemError;
  85. PyAPI_DATA(PyObject *) PyExc_SystemExit;
  86. PyAPI_DATA(PyObject *) PyExc_TypeError;
  87. PyAPI_DATA(PyObject *) PyExc_UnboundLocalError;
  88. PyAPI_DATA(PyObject *) PyExc_UnicodeError;
  89. PyAPI_DATA(PyObject *) PyExc_UnicodeEncodeError;
  90. PyAPI_DATA(PyObject *) PyExc_UnicodeDecodeError;
  91. PyAPI_DATA(PyObject *) PyExc_UnicodeTranslateError;
  92. PyAPI_DATA(PyObject *) PyExc_ValueError;
  93. PyAPI_DATA(PyObject *) PyExc_ZeroDivisionError;
  94. #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03030000
  95. PyAPI_DATA(PyObject *) PyExc_BlockingIOError;
  96. PyAPI_DATA(PyObject *) PyExc_BrokenPipeError;
  97. PyAPI_DATA(PyObject *) PyExc_ChildProcessError;
  98. PyAPI_DATA(PyObject *) PyExc_ConnectionError;
  99. PyAPI_DATA(PyObject *) PyExc_ConnectionAbortedError;
  100. PyAPI_DATA(PyObject *) PyExc_ConnectionRefusedError;
  101. PyAPI_DATA(PyObject *) PyExc_ConnectionResetError;
  102. PyAPI_DATA(PyObject *) PyExc_FileExistsError;
  103. PyAPI_DATA(PyObject *) PyExc_FileNotFoundError;
  104. PyAPI_DATA(PyObject *) PyExc_InterruptedError;
  105. PyAPI_DATA(PyObject *) PyExc_IsADirectoryError;
  106. PyAPI_DATA(PyObject *) PyExc_NotADirectoryError;
  107. PyAPI_DATA(PyObject *) PyExc_PermissionError;
  108. PyAPI_DATA(PyObject *) PyExc_ProcessLookupError;
  109. PyAPI_DATA(PyObject *) PyExc_TimeoutError;
  110. #endif
  111. /* Compatibility aliases */
  112. PyAPI_DATA(PyObject *) PyExc_EnvironmentError;
  113. PyAPI_DATA(PyObject *) PyExc_IOError;
  114. #ifdef MS_WINDOWS
  115. PyAPI_DATA(PyObject *) PyExc_WindowsError;
  116. #endif
  117. /* Predefined warning categories */
  118. PyAPI_DATA(PyObject *) PyExc_Warning;
  119. PyAPI_DATA(PyObject *) PyExc_UserWarning;
  120. PyAPI_DATA(PyObject *) PyExc_DeprecationWarning;
  121. PyAPI_DATA(PyObject *) PyExc_PendingDeprecationWarning;
  122. PyAPI_DATA(PyObject *) PyExc_SyntaxWarning;
  123. PyAPI_DATA(PyObject *) PyExc_RuntimeWarning;
  124. PyAPI_DATA(PyObject *) PyExc_FutureWarning;
  125. PyAPI_DATA(PyObject *) PyExc_ImportWarning;
  126. PyAPI_DATA(PyObject *) PyExc_UnicodeWarning;
  127. PyAPI_DATA(PyObject *) PyExc_BytesWarning;
  128. PyAPI_DATA(PyObject *) PyExc_ResourceWarning;
  129. /* Convenience functions */
  130. PyAPI_FUNC(int) PyErr_BadArgument(void);
  131. PyAPI_FUNC(PyObject *) PyErr_NoMemory(void);
  132. PyAPI_FUNC(PyObject *) PyErr_SetFromErrno(PyObject *);
  133. PyAPI_FUNC(PyObject *) PyErr_SetFromErrnoWithFilenameObject(
  134. PyObject *, PyObject *);
  135. #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03040000
  136. PyAPI_FUNC(PyObject *) PyErr_SetFromErrnoWithFilenameObjects(
  137. PyObject *, PyObject *, PyObject *);
  138. #endif
  139. PyAPI_FUNC(PyObject *) PyErr_SetFromErrnoWithFilename(
  140. PyObject *exc,
  141. const char *filename /* decoded from the filesystem encoding */
  142. );
  143. PyAPI_FUNC(PyObject *) PyErr_Format(
  144. PyObject *exception,
  145. const char *format, /* ASCII-encoded string */
  146. ...
  147. );
  148. #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03050000
  149. PyAPI_FUNC(PyObject *) PyErr_FormatV(
  150. PyObject *exception,
  151. const char *format,
  152. va_list vargs);
  153. #endif
  154. #ifdef MS_WINDOWS
  155. PyAPI_FUNC(PyObject *) PyErr_SetFromWindowsErrWithFilename(
  156. int ierr,
  157. const char *filename /* decoded from the filesystem encoding */
  158. );
  159. PyAPI_FUNC(PyObject *) PyErr_SetFromWindowsErr(int);
  160. PyAPI_FUNC(PyObject *) PyErr_SetExcFromWindowsErrWithFilenameObject(
  161. PyObject *,int, PyObject *);
  162. #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03040000
  163. PyAPI_FUNC(PyObject *) PyErr_SetExcFromWindowsErrWithFilenameObjects(
  164. PyObject *,int, PyObject *, PyObject *);
  165. #endif
  166. PyAPI_FUNC(PyObject *) PyErr_SetExcFromWindowsErrWithFilename(
  167. PyObject *exc,
  168. int ierr,
  169. const char *filename /* decoded from the filesystem encoding */
  170. );
  171. PyAPI_FUNC(PyObject *) PyErr_SetExcFromWindowsErr(PyObject *, int);
  172. #endif /* MS_WINDOWS */
  173. #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03060000
  174. PyAPI_FUNC(PyObject *) PyErr_SetImportErrorSubclass(PyObject *, PyObject *,
  175. PyObject *, PyObject *);
  176. #endif
  177. #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03030000
  178. PyAPI_FUNC(PyObject *) PyErr_SetImportError(PyObject *, PyObject *,
  179. PyObject *);
  180. #endif
  181. /* Export the old function so that the existing API remains available: */
  182. PyAPI_FUNC(void) PyErr_BadInternalCall(void);
  183. PyAPI_FUNC(void) _PyErr_BadInternalCall(const char *filename, int lineno);
  184. /* Mask the old API with a call to the new API for code compiled under
  185. Python 2.0: */
  186. #define PyErr_BadInternalCall() _PyErr_BadInternalCall(__FILE__, __LINE__)
  187. /* Function to create a new exception */
  188. PyAPI_FUNC(PyObject *) PyErr_NewException(
  189. const char *name, PyObject *base, PyObject *dict);
  190. PyAPI_FUNC(PyObject *) PyErr_NewExceptionWithDoc(
  191. const char *name, const char *doc, PyObject *base, PyObject *dict);
  192. PyAPI_FUNC(void) PyErr_WriteUnraisable(PyObject *);
  193. /* In signalmodule.c */
  194. PyAPI_FUNC(int) PyErr_CheckSignals(void);
  195. PyAPI_FUNC(void) PyErr_SetInterrupt(void);
  196. /* Support for adding program text to SyntaxErrors */
  197. PyAPI_FUNC(void) PyErr_SyntaxLocation(
  198. const char *filename, /* decoded from the filesystem encoding */
  199. int lineno);
  200. PyAPI_FUNC(void) PyErr_SyntaxLocationEx(
  201. const char *filename, /* decoded from the filesystem encoding */
  202. int lineno,
  203. int col_offset);
  204. PyAPI_FUNC(PyObject *) PyErr_ProgramText(
  205. const char *filename, /* decoded from the filesystem encoding */
  206. int lineno);
  207. /* The following functions are used to create and modify unicode
  208. exceptions from C */
  209. /* create a UnicodeDecodeError object */
  210. PyAPI_FUNC(PyObject *) PyUnicodeDecodeError_Create(
  211. const char *encoding, /* UTF-8 encoded string */
  212. const char *object,
  213. Py_ssize_t length,
  214. Py_ssize_t start,
  215. Py_ssize_t end,
  216. const char *reason /* UTF-8 encoded string */
  217. );
  218. /* get the encoding attribute */
  219. PyAPI_FUNC(PyObject *) PyUnicodeEncodeError_GetEncoding(PyObject *);
  220. PyAPI_FUNC(PyObject *) PyUnicodeDecodeError_GetEncoding(PyObject *);
  221. /* get the object attribute */
  222. PyAPI_FUNC(PyObject *) PyUnicodeEncodeError_GetObject(PyObject *);
  223. PyAPI_FUNC(PyObject *) PyUnicodeDecodeError_GetObject(PyObject *);
  224. PyAPI_FUNC(PyObject *) PyUnicodeTranslateError_GetObject(PyObject *);
  225. /* get the value of the start attribute (the int * may not be NULL)
  226. return 0 on success, -1 on failure */
  227. PyAPI_FUNC(int) PyUnicodeEncodeError_GetStart(PyObject *, Py_ssize_t *);
  228. PyAPI_FUNC(int) PyUnicodeDecodeError_GetStart(PyObject *, Py_ssize_t *);
  229. PyAPI_FUNC(int) PyUnicodeTranslateError_GetStart(PyObject *, Py_ssize_t *);
  230. /* assign a new value to the start attribute
  231. return 0 on success, -1 on failure */
  232. PyAPI_FUNC(int) PyUnicodeEncodeError_SetStart(PyObject *, Py_ssize_t);
  233. PyAPI_FUNC(int) PyUnicodeDecodeError_SetStart(PyObject *, Py_ssize_t);
  234. PyAPI_FUNC(int) PyUnicodeTranslateError_SetStart(PyObject *, Py_ssize_t);
  235. /* get the value of the end attribute (the int *may not be NULL)
  236. return 0 on success, -1 on failure */
  237. PyAPI_FUNC(int) PyUnicodeEncodeError_GetEnd(PyObject *, Py_ssize_t *);
  238. PyAPI_FUNC(int) PyUnicodeDecodeError_GetEnd(PyObject *, Py_ssize_t *);
  239. PyAPI_FUNC(int) PyUnicodeTranslateError_GetEnd(PyObject *, Py_ssize_t *);
  240. /* assign a new value to the end attribute
  241. return 0 on success, -1 on failure */
  242. PyAPI_FUNC(int) PyUnicodeEncodeError_SetEnd(PyObject *, Py_ssize_t);
  243. PyAPI_FUNC(int) PyUnicodeDecodeError_SetEnd(PyObject *, Py_ssize_t);
  244. PyAPI_FUNC(int) PyUnicodeTranslateError_SetEnd(PyObject *, Py_ssize_t);
  245. /* get the value of the reason attribute */
  246. PyAPI_FUNC(PyObject *) PyUnicodeEncodeError_GetReason(PyObject *);
  247. PyAPI_FUNC(PyObject *) PyUnicodeDecodeError_GetReason(PyObject *);
  248. PyAPI_FUNC(PyObject *) PyUnicodeTranslateError_GetReason(PyObject *);
  249. /* assign a new value to the reason attribute
  250. return 0 on success, -1 on failure */
  251. PyAPI_FUNC(int) PyUnicodeEncodeError_SetReason(
  252. PyObject *exc,
  253. const char *reason /* UTF-8 encoded string */
  254. );
  255. PyAPI_FUNC(int) PyUnicodeDecodeError_SetReason(
  256. PyObject *exc,
  257. const char *reason /* UTF-8 encoded string */
  258. );
  259. PyAPI_FUNC(int) PyUnicodeTranslateError_SetReason(
  260. PyObject *exc,
  261. const char *reason /* UTF-8 encoded string */
  262. );
  263. /* These APIs aren't really part of the error implementation, but
  264. often needed to format error messages; the native C lib APIs are
  265. not available on all platforms, which is why we provide emulations
  266. for those platforms in Python/mysnprintf.c,
  267. WARNING: The return value of snprintf varies across platforms; do
  268. not rely on any particular behavior; eventually the C99 defn may
  269. be reliable.
  270. */
  271. #if defined(MS_WIN32) && !defined(HAVE_SNPRINTF)
  272. # define HAVE_SNPRINTF
  273. # define snprintf _snprintf
  274. # define vsnprintf _vsnprintf
  275. #endif
  276. #include <stdarg.h>
  277. PyAPI_FUNC(int) PyOS_snprintf(char *str, size_t size, const char *format, ...)
  278. Py_GCC_ATTRIBUTE((format(printf, 3, 4)));
  279. PyAPI_FUNC(int) PyOS_vsnprintf(char *str, size_t size, const char *format, va_list va)
  280. Py_GCC_ATTRIBUTE((format(printf, 3, 0)));
  281. #ifndef Py_LIMITED_API
  282. # define Py_CPYTHON_ERRORS_H
  283. # include "cpython/pyerrors.h"
  284. # undef Py_CPYTHON_ERRORS_H
  285. #endif
  286. #ifdef __cplusplus
  287. }
  288. #endif
  289. #endif /* !Py_ERRORS_H */