_warnings.c.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /*[clinic input]
  2. preserve
  3. [clinic start generated code]*/
  4. #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
  5. # include "pycore_gc.h" // PyGC_Head
  6. # include "pycore_runtime.h" // _Py_ID()
  7. #endif
  8. PyDoc_STRVAR(warnings_warn__doc__,
  9. "warn($module, /, message, category=None, stacklevel=1, source=None, *,\n"
  10. " skip_file_prefixes=<unrepresentable>)\n"
  11. "--\n"
  12. "\n"
  13. "Issue a warning, or maybe ignore it or raise an exception.\n"
  14. "\n"
  15. " message\n"
  16. " Text of the warning message.\n"
  17. " category\n"
  18. " The Warning category subclass. Defaults to UserWarning.\n"
  19. " stacklevel\n"
  20. " How far up the call stack to make this warning appear. A value of 2 for\n"
  21. " example attributes the warning to the caller of the code calling warn().\n"
  22. " source\n"
  23. " If supplied, the destroyed object which emitted a ResourceWarning\n"
  24. " skip_file_prefixes\n"
  25. " An optional tuple of module filename prefixes indicating frames to skip\n"
  26. " during stacklevel computations for stack frame attribution.");
  27. #define WARNINGS_WARN_METHODDEF \
  28. {"warn", _PyCFunction_CAST(warnings_warn), METH_FASTCALL|METH_KEYWORDS, warnings_warn__doc__},
  29. static PyObject *
  30. warnings_warn_impl(PyObject *module, PyObject *message, PyObject *category,
  31. Py_ssize_t stacklevel, PyObject *source,
  32. PyTupleObject *skip_file_prefixes);
  33. static PyObject *
  34. warnings_warn(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
  35. {
  36. PyObject *return_value = NULL;
  37. #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
  38. #define NUM_KEYWORDS 5
  39. static struct {
  40. PyGC_Head _this_is_not_used;
  41. PyObject_VAR_HEAD
  42. PyObject *ob_item[NUM_KEYWORDS];
  43. } _kwtuple = {
  44. .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
  45. .ob_item = { &_Py_ID(message), &_Py_ID(category), &_Py_ID(stacklevel), &_Py_ID(source), &_Py_ID(skip_file_prefixes), },
  46. };
  47. #undef NUM_KEYWORDS
  48. #define KWTUPLE (&_kwtuple.ob_base.ob_base)
  49. #else // !Py_BUILD_CORE
  50. # define KWTUPLE NULL
  51. #endif // !Py_BUILD_CORE
  52. static const char * const _keywords[] = {"message", "category", "stacklevel", "source", "skip_file_prefixes", NULL};
  53. static _PyArg_Parser _parser = {
  54. .keywords = _keywords,
  55. .fname = "warn",
  56. .kwtuple = KWTUPLE,
  57. };
  58. #undef KWTUPLE
  59. PyObject *argsbuf[5];
  60. Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
  61. PyObject *message;
  62. PyObject *category = Py_None;
  63. Py_ssize_t stacklevel = 1;
  64. PyObject *source = Py_None;
  65. PyTupleObject *skip_file_prefixes = NULL;
  66. args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 4, 0, argsbuf);
  67. if (!args) {
  68. goto exit;
  69. }
  70. message = args[0];
  71. if (!noptargs) {
  72. goto skip_optional_pos;
  73. }
  74. if (args[1]) {
  75. category = args[1];
  76. if (!--noptargs) {
  77. goto skip_optional_pos;
  78. }
  79. }
  80. if (args[2]) {
  81. {
  82. Py_ssize_t ival = -1;
  83. PyObject *iobj = _PyNumber_Index(args[2]);
  84. if (iobj != NULL) {
  85. ival = PyLong_AsSsize_t(iobj);
  86. Py_DECREF(iobj);
  87. }
  88. if (ival == -1 && PyErr_Occurred()) {
  89. goto exit;
  90. }
  91. stacklevel = ival;
  92. }
  93. if (!--noptargs) {
  94. goto skip_optional_pos;
  95. }
  96. }
  97. if (args[3]) {
  98. source = args[3];
  99. if (!--noptargs) {
  100. goto skip_optional_pos;
  101. }
  102. }
  103. skip_optional_pos:
  104. if (!noptargs) {
  105. goto skip_optional_kwonly;
  106. }
  107. if (!PyTuple_Check(args[4])) {
  108. _PyArg_BadArgument("warn", "argument 'skip_file_prefixes'", "tuple", args[4]);
  109. goto exit;
  110. }
  111. skip_file_prefixes = (PyTupleObject *)args[4];
  112. skip_optional_kwonly:
  113. return_value = warnings_warn_impl(module, message, category, stacklevel, source, skip_file_prefixes);
  114. exit:
  115. return return_value;
  116. }
  117. PyDoc_STRVAR(warnings_warn_explicit__doc__,
  118. "warn_explicit($module, /, message, category, filename, lineno,\n"
  119. " module=<unrepresentable>, registry=None,\n"
  120. " module_globals=None, source=None)\n"
  121. "--\n"
  122. "\n"
  123. "Issue a warning, or maybe ignore it or raise an exception.");
  124. #define WARNINGS_WARN_EXPLICIT_METHODDEF \
  125. {"warn_explicit", _PyCFunction_CAST(warnings_warn_explicit), METH_FASTCALL|METH_KEYWORDS, warnings_warn_explicit__doc__},
  126. static PyObject *
  127. warnings_warn_explicit_impl(PyObject *module, PyObject *message,
  128. PyObject *category, PyObject *filename,
  129. int lineno, PyObject *mod, PyObject *registry,
  130. PyObject *module_globals, PyObject *sourceobj);
  131. static PyObject *
  132. warnings_warn_explicit(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
  133. {
  134. PyObject *return_value = NULL;
  135. #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
  136. #define NUM_KEYWORDS 8
  137. static struct {
  138. PyGC_Head _this_is_not_used;
  139. PyObject_VAR_HEAD
  140. PyObject *ob_item[NUM_KEYWORDS];
  141. } _kwtuple = {
  142. .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
  143. .ob_item = { &_Py_ID(message), &_Py_ID(category), &_Py_ID(filename), &_Py_ID(lineno), &_Py_ID(module), &_Py_ID(registry), &_Py_ID(module_globals), &_Py_ID(source), },
  144. };
  145. #undef NUM_KEYWORDS
  146. #define KWTUPLE (&_kwtuple.ob_base.ob_base)
  147. #else // !Py_BUILD_CORE
  148. # define KWTUPLE NULL
  149. #endif // !Py_BUILD_CORE
  150. static const char * const _keywords[] = {"message", "category", "filename", "lineno", "module", "registry", "module_globals", "source", NULL};
  151. static _PyArg_Parser _parser = {
  152. .keywords = _keywords,
  153. .fname = "warn_explicit",
  154. .kwtuple = KWTUPLE,
  155. };
  156. #undef KWTUPLE
  157. PyObject *argsbuf[8];
  158. Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 4;
  159. PyObject *message;
  160. PyObject *category;
  161. PyObject *filename;
  162. int lineno;
  163. PyObject *mod = NULL;
  164. PyObject *registry = Py_None;
  165. PyObject *module_globals = Py_None;
  166. PyObject *sourceobj = Py_None;
  167. args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 4, 8, 0, argsbuf);
  168. if (!args) {
  169. goto exit;
  170. }
  171. message = args[0];
  172. category = args[1];
  173. if (!PyUnicode_Check(args[2])) {
  174. _PyArg_BadArgument("warn_explicit", "argument 'filename'", "str", args[2]);
  175. goto exit;
  176. }
  177. if (PyUnicode_READY(args[2]) == -1) {
  178. goto exit;
  179. }
  180. filename = args[2];
  181. lineno = _PyLong_AsInt(args[3]);
  182. if (lineno == -1 && PyErr_Occurred()) {
  183. goto exit;
  184. }
  185. if (!noptargs) {
  186. goto skip_optional_pos;
  187. }
  188. if (args[4]) {
  189. mod = args[4];
  190. if (!--noptargs) {
  191. goto skip_optional_pos;
  192. }
  193. }
  194. if (args[5]) {
  195. registry = args[5];
  196. if (!--noptargs) {
  197. goto skip_optional_pos;
  198. }
  199. }
  200. if (args[6]) {
  201. module_globals = args[6];
  202. if (!--noptargs) {
  203. goto skip_optional_pos;
  204. }
  205. }
  206. sourceobj = args[7];
  207. skip_optional_pos:
  208. return_value = warnings_warn_explicit_impl(module, message, category, filename, lineno, mod, registry, module_globals, sourceobj);
  209. exit:
  210. return return_value;
  211. }
  212. PyDoc_STRVAR(warnings_filters_mutated__doc__,
  213. "_filters_mutated($module, /)\n"
  214. "--\n"
  215. "\n");
  216. #define WARNINGS_FILTERS_MUTATED_METHODDEF \
  217. {"_filters_mutated", (PyCFunction)warnings_filters_mutated, METH_NOARGS, warnings_filters_mutated__doc__},
  218. static PyObject *
  219. warnings_filters_mutated_impl(PyObject *module);
  220. static PyObject *
  221. warnings_filters_mutated(PyObject *module, PyObject *Py_UNUSED(ignored))
  222. {
  223. return warnings_filters_mutated_impl(module);
  224. }
  225. /*[clinic end generated code: output=20429719d7223bdc input=a9049054013a1b77]*/