Coverage Report

Created: 2025-07-04 06:49

/src/cpython/Python/clinic/_warnings.c.h
Line
Count
Source (jump to first uncovered line)
1
/*[clinic input]
2
preserve
3
[clinic start generated code]*/
4
5
#if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
6
#  include "pycore_gc.h"          // PyGC_Head
7
#  include "pycore_runtime.h"     // _Py_ID()
8
#endif
9
#include "pycore_abstract.h"      // _PyNumber_Index()
10
#include "pycore_modsupport.h"    // _PyArg_UnpackKeywords()
11
12
PyDoc_STRVAR(warnings_acquire_lock__doc__,
13
"_acquire_lock($module, /)\n"
14
"--\n"
15
"\n");
16
17
#define WARNINGS_ACQUIRE_LOCK_METHODDEF    \
18
    {"_acquire_lock", (PyCFunction)warnings_acquire_lock, METH_NOARGS, warnings_acquire_lock__doc__},
19
20
static PyObject *
21
warnings_acquire_lock_impl(PyObject *module);
22
23
static PyObject *
24
warnings_acquire_lock(PyObject *module, PyObject *Py_UNUSED(ignored))
25
2
{
26
2
    return warnings_acquire_lock_impl(module);
27
2
}
28
29
PyDoc_STRVAR(warnings_release_lock__doc__,
30
"_release_lock($module, /)\n"
31
"--\n"
32
"\n");
33
34
#define WARNINGS_RELEASE_LOCK_METHODDEF    \
35
    {"_release_lock", (PyCFunction)warnings_release_lock, METH_NOARGS, warnings_release_lock__doc__},
36
37
static PyObject *
38
warnings_release_lock_impl(PyObject *module);
39
40
static PyObject *
41
warnings_release_lock(PyObject *module, PyObject *Py_UNUSED(ignored))
42
2
{
43
2
    return warnings_release_lock_impl(module);
44
2
}
45
46
PyDoc_STRVAR(warnings_warn__doc__,
47
"warn($module, /, message, category=None, stacklevel=1, source=None, *,\n"
48
"     skip_file_prefixes=<unrepresentable>)\n"
49
"--\n"
50
"\n"
51
"Issue a warning, or maybe ignore it or raise an exception.\n"
52
"\n"
53
"  message\n"
54
"    Text of the warning message.\n"
55
"  category\n"
56
"    The Warning category subclass. Defaults to UserWarning.\n"
57
"  stacklevel\n"
58
"    How far up the call stack to make this warning appear. A value of 2 for\n"
59
"    example attributes the warning to the caller of the code calling warn().\n"
60
"  source\n"
61
"    If supplied, the destroyed object which emitted a ResourceWarning\n"
62
"  skip_file_prefixes\n"
63
"    An optional tuple of module filename prefixes indicating frames to skip\n"
64
"    during stacklevel computations for stack frame attribution.");
65
66
#define WARNINGS_WARN_METHODDEF    \
67
    {"warn", _PyCFunction_CAST(warnings_warn), METH_FASTCALL|METH_KEYWORDS, warnings_warn__doc__},
68
69
static PyObject *
70
warnings_warn_impl(PyObject *module, PyObject *message, PyObject *category,
71
                   Py_ssize_t stacklevel, PyObject *source,
72
                   PyTupleObject *skip_file_prefixes);
73
74
static PyObject *
75
warnings_warn(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
76
0
{
77
0
    PyObject *return_value = NULL;
78
0
    #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
79
80
0
    #define NUM_KEYWORDS 5
81
0
    static struct {
82
0
        PyGC_Head _this_is_not_used;
83
0
        PyObject_VAR_HEAD
84
0
        Py_hash_t ob_hash;
85
0
        PyObject *ob_item[NUM_KEYWORDS];
86
0
    } _kwtuple = {
87
0
        .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
88
0
        .ob_hash = -1,
89
0
        .ob_item = { &_Py_ID(message), &_Py_ID(category), &_Py_ID(stacklevel), &_Py_ID(source), &_Py_ID(skip_file_prefixes), },
90
0
    };
91
0
    #undef NUM_KEYWORDS
92
0
    #define KWTUPLE (&_kwtuple.ob_base.ob_base)
93
94
    #else  // !Py_BUILD_CORE
95
    #  define KWTUPLE NULL
96
    #endif  // !Py_BUILD_CORE
97
98
0
    static const char * const _keywords[] = {"message", "category", "stacklevel", "source", "skip_file_prefixes", NULL};
99
0
    static _PyArg_Parser _parser = {
100
0
        .keywords = _keywords,
101
0
        .fname = "warn",
102
0
        .kwtuple = KWTUPLE,
103
0
    };
104
0
    #undef KWTUPLE
105
0
    PyObject *argsbuf[5];
106
0
    Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
107
0
    PyObject *message;
108
0
    PyObject *category = Py_None;
109
0
    Py_ssize_t stacklevel = 1;
110
0
    PyObject *source = Py_None;
111
0
    PyTupleObject *skip_file_prefixes = NULL;
112
113
0
    args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser,
114
0
            /*minpos*/ 1, /*maxpos*/ 4, /*minkw*/ 0, /*varpos*/ 0, argsbuf);
115
0
    if (!args) {
116
0
        goto exit;
117
0
    }
118
0
    message = args[0];
119
0
    if (!noptargs) {
120
0
        goto skip_optional_pos;
121
0
    }
122
0
    if (args[1]) {
123
0
        category = args[1];
124
0
        if (!--noptargs) {
125
0
            goto skip_optional_pos;
126
0
        }
127
0
    }
128
0
    if (args[2]) {
129
0
        {
130
0
            Py_ssize_t ival = -1;
131
0
            PyObject *iobj = _PyNumber_Index(args[2]);
132
0
            if (iobj != NULL) {
133
0
                ival = PyLong_AsSsize_t(iobj);
134
0
                Py_DECREF(iobj);
135
0
            }
136
0
            if (ival == -1 && PyErr_Occurred()) {
137
0
                goto exit;
138
0
            }
139
0
            stacklevel = ival;
140
0
        }
141
0
        if (!--noptargs) {
142
0
            goto skip_optional_pos;
143
0
        }
144
0
    }
145
0
    if (args[3]) {
146
0
        source = args[3];
147
0
        if (!--noptargs) {
148
0
            goto skip_optional_pos;
149
0
        }
150
0
    }
151
0
skip_optional_pos:
152
0
    if (!noptargs) {
153
0
        goto skip_optional_kwonly;
154
0
    }
155
0
    if (!PyTuple_Check(args[4])) {
156
0
        _PyArg_BadArgument("warn", "argument 'skip_file_prefixes'", "tuple", args[4]);
157
0
        goto exit;
158
0
    }
159
0
    skip_file_prefixes = (PyTupleObject *)args[4];
160
0
skip_optional_kwonly:
161
0
    return_value = warnings_warn_impl(module, message, category, stacklevel, source, skip_file_prefixes);
162
163
0
exit:
164
0
    return return_value;
165
0
}
166
167
PyDoc_STRVAR(warnings_warn_explicit__doc__,
168
"warn_explicit($module, /, message, category, filename, lineno,\n"
169
"              module=<unrepresentable>, registry=None,\n"
170
"              module_globals=None, source=None)\n"
171
"--\n"
172
"\n"
173
"Issue a warning, or maybe ignore it or raise an exception.");
174
175
#define WARNINGS_WARN_EXPLICIT_METHODDEF    \
176
    {"warn_explicit", _PyCFunction_CAST(warnings_warn_explicit), METH_FASTCALL|METH_KEYWORDS, warnings_warn_explicit__doc__},
177
178
static PyObject *
179
warnings_warn_explicit_impl(PyObject *module, PyObject *message,
180
                            PyObject *category, PyObject *filename,
181
                            int lineno, PyObject *mod, PyObject *registry,
182
                            PyObject *module_globals, PyObject *sourceobj);
183
184
static PyObject *
185
warnings_warn_explicit(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
186
0
{
187
0
    PyObject *return_value = NULL;
188
0
    #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
189
190
0
    #define NUM_KEYWORDS 8
191
0
    static struct {
192
0
        PyGC_Head _this_is_not_used;
193
0
        PyObject_VAR_HEAD
194
0
        Py_hash_t ob_hash;
195
0
        PyObject *ob_item[NUM_KEYWORDS];
196
0
    } _kwtuple = {
197
0
        .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
198
0
        .ob_hash = -1,
199
0
        .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), },
200
0
    };
201
0
    #undef NUM_KEYWORDS
202
0
    #define KWTUPLE (&_kwtuple.ob_base.ob_base)
203
204
    #else  // !Py_BUILD_CORE
205
    #  define KWTUPLE NULL
206
    #endif  // !Py_BUILD_CORE
207
208
0
    static const char * const _keywords[] = {"message", "category", "filename", "lineno", "module", "registry", "module_globals", "source", NULL};
209
0
    static _PyArg_Parser _parser = {
210
0
        .keywords = _keywords,
211
0
        .fname = "warn_explicit",
212
0
        .kwtuple = KWTUPLE,
213
0
    };
214
0
    #undef KWTUPLE
215
0
    PyObject *argsbuf[8];
216
0
    Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 4;
217
0
    PyObject *message;
218
0
    PyObject *category;
219
0
    PyObject *filename;
220
0
    int lineno;
221
0
    PyObject *mod = NULL;
222
0
    PyObject *registry = Py_None;
223
0
    PyObject *module_globals = Py_None;
224
0
    PyObject *sourceobj = Py_None;
225
226
0
    args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser,
227
0
            /*minpos*/ 4, /*maxpos*/ 8, /*minkw*/ 0, /*varpos*/ 0, argsbuf);
228
0
    if (!args) {
229
0
        goto exit;
230
0
    }
231
0
    message = args[0];
232
0
    category = args[1];
233
0
    if (!PyUnicode_Check(args[2])) {
234
0
        _PyArg_BadArgument("warn_explicit", "argument 'filename'", "str", args[2]);
235
0
        goto exit;
236
0
    }
237
0
    filename = args[2];
238
0
    lineno = PyLong_AsInt(args[3]);
239
0
    if (lineno == -1 && PyErr_Occurred()) {
240
0
        goto exit;
241
0
    }
242
0
    if (!noptargs) {
243
0
        goto skip_optional_pos;
244
0
    }
245
0
    if (args[4]) {
246
0
        mod = args[4];
247
0
        if (!--noptargs) {
248
0
            goto skip_optional_pos;
249
0
        }
250
0
    }
251
0
    if (args[5]) {
252
0
        registry = args[5];
253
0
        if (!--noptargs) {
254
0
            goto skip_optional_pos;
255
0
        }
256
0
    }
257
0
    if (args[6]) {
258
0
        module_globals = args[6];
259
0
        if (!--noptargs) {
260
0
            goto skip_optional_pos;
261
0
        }
262
0
    }
263
0
    sourceobj = args[7];
264
0
skip_optional_pos:
265
0
    return_value = warnings_warn_explicit_impl(module, message, category, filename, lineno, mod, registry, module_globals, sourceobj);
266
267
0
exit:
268
0
    return return_value;
269
0
}
270
271
PyDoc_STRVAR(warnings_filters_mutated_lock_held__doc__,
272
"_filters_mutated_lock_held($module, /)\n"
273
"--\n"
274
"\n");
275
276
#define WARNINGS_FILTERS_MUTATED_LOCK_HELD_METHODDEF    \
277
    {"_filters_mutated_lock_held", (PyCFunction)warnings_filters_mutated_lock_held, METH_NOARGS, warnings_filters_mutated_lock_held__doc__},
278
279
static PyObject *
280
warnings_filters_mutated_lock_held_impl(PyObject *module);
281
282
static PyObject *
283
warnings_filters_mutated_lock_held(PyObject *module, PyObject *Py_UNUSED(ignored))
284
2
{
285
2
    return warnings_filters_mutated_lock_held_impl(module);
286
2
}
287
/*[clinic end generated code: output=610ed5764bf40bb5 input=a9049054013a1b77]*/