blob: ad6b9a8e2428cd9c3794d5e13bb1df08f972492f [file] [log] [blame]
Victor Stinner22f18752016-12-09 18:08:18 +01001/*[clinic input]
2preserve
3[clinic start generated code]*/
4
5PyDoc_STRVAR(warnings_warn__doc__,
6"warn($module, /, message, category=None, stacklevel=1, source=None)\n"
7"--\n"
8"\n"
9"Issue a warning, or maybe ignore it or raise an exception.");
10
11#define WARNINGS_WARN_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +020012 {"warn", (PyCFunction)(void(*)(void))warnings_warn, METH_FASTCALL|METH_KEYWORDS, warnings_warn__doc__},
Victor Stinner22f18752016-12-09 18:08:18 +010013
14static PyObject *
15warnings_warn_impl(PyObject *module, PyObject *message, PyObject *category,
16 Py_ssize_t stacklevel, PyObject *source);
17
18static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +020019warnings_warn(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Victor Stinner22f18752016-12-09 18:08:18 +010020{
21 PyObject *return_value = NULL;
22 static const char * const _keywords[] = {"message", "category", "stacklevel", "source", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +020023 static _PyArg_Parser _parser = {NULL, _keywords, "warn", 0};
24 PyObject *argsbuf[4];
25 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
Victor Stinner22f18752016-12-09 18:08:18 +010026 PyObject *message;
27 PyObject *category = Py_None;
28 Py_ssize_t stacklevel = 1;
29 PyObject *source = Py_None;
30
Serhiy Storchaka31913912019-03-14 10:32:22 +020031 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 4, 0, argsbuf);
32 if (!args) {
Victor Stinner22f18752016-12-09 18:08:18 +010033 goto exit;
34 }
Serhiy Storchaka31913912019-03-14 10:32:22 +020035 message = args[0];
36 if (!noptargs) {
37 goto skip_optional_pos;
38 }
39 if (args[1]) {
40 category = args[1];
41 if (!--noptargs) {
42 goto skip_optional_pos;
43 }
44 }
45 if (args[2]) {
Serhiy Storchaka31913912019-03-14 10:32:22 +020046 {
47 Py_ssize_t ival = -1;
Serhiy Storchaka5f4b229d2020-05-28 10:33:45 +030048 PyObject *iobj = _PyNumber_Index(args[2]);
Serhiy Storchaka31913912019-03-14 10:32:22 +020049 if (iobj != NULL) {
50 ival = PyLong_AsSsize_t(iobj);
51 Py_DECREF(iobj);
52 }
53 if (ival == -1 && PyErr_Occurred()) {
54 goto exit;
55 }
56 stacklevel = ival;
57 }
58 if (!--noptargs) {
59 goto skip_optional_pos;
60 }
61 }
62 source = args[3];
63skip_optional_pos:
Victor Stinner22f18752016-12-09 18:08:18 +010064 return_value = warnings_warn_impl(module, message, category, stacklevel, source);
65
66exit:
67 return return_value;
68}
Serhiy Storchaka5f4b229d2020-05-28 10:33:45 +030069/*[clinic end generated code: output=eb9997fa998fdbad input=a9049054013a1b77]*/