pyerrors.h 12 KB

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