summaryrefslogtreecommitdiffstats
path: root/Python
diff options
context:
space:
mode:
Diffstat (limited to 'Python')
-rw-r--r--Python/_warnings.c127
-rw-r--r--Python/clinic/_warnings.c.h50
2 files changed, 143 insertions, 34 deletions
diff --git a/Python/_warnings.c b/Python/_warnings.c
index 046c37e..a8c1129 100644
--- a/Python/_warnings.c
+++ b/Python/_warnings.c
@@ -761,56 +761,99 @@ warn_explicit(PyThreadState *tstate, PyObject *category, PyObject *message,
return result; /* Py_None or NULL. */
}
-static int
-is_internal_frame(PyFrameObject *frame)
+static PyObject *
+get_frame_filename(PyFrameObject *frame)
{
- if (frame == NULL) {
- return 0;
- }
-
PyCodeObject *code = PyFrame_GetCode(frame);
PyObject *filename = code->co_filename;
Py_DECREF(code);
+ return filename;
+}
- if (filename == NULL) {
- return 0;
- }
+static bool
+is_internal_filename(PyObject *filename)
+{
if (!PyUnicode_Check(filename)) {
- return 0;
+ return false;
}
int contains = PyUnicode_Contains(filename, &_Py_ID(importlib));
if (contains < 0) {
- return 0;
+ return false;
}
else if (contains > 0) {
contains = PyUnicode_Contains(filename, &_Py_ID(_bootstrap));
if (contains < 0) {
- return 0;
+ return false;
}
else if (contains > 0) {
- return 1;
+ return true;
}
}
- return 0;
+ return false;
+}
+
+static bool
+is_filename_to_skip(PyObject *filename, PyTupleObject *skip_file_prefixes)
+{
+ if (skip_file_prefixes) {
+ if (!PyUnicode_Check(filename)) {
+ return false;
+ }
+
+ Py_ssize_t prefixes = PyTuple_GET_SIZE(skip_file_prefixes);
+ for (Py_ssize_t idx = 0; idx < prefixes; ++idx)
+ {
+ PyObject *prefix = PyTuple_GET_ITEM(skip_file_prefixes, idx);
+ int found = PyUnicode_Tailmatch(filename, prefix, 0, -1, -1);
+ if (found == 1) {
+ return true;
+ }
+ if (found < 0) {
+ return false;
+ }
+ }
+ }
+ return false;
+}
+
+static bool
+is_internal_frame(PyFrameObject *frame)
+{
+ if (frame == NULL) {
+ return false;
+ }
+
+ PyObject *filename = get_frame_filename(frame);
+ if (filename == NULL) {
+ return false;
+ }
+
+ return is_internal_filename(filename);
}
static PyFrameObject *
-next_external_frame(PyFrameObject *frame)
+next_external_frame(PyFrameObject *frame, PyTupleObject *skip_file_prefixes)
{
+ PyObject *frame_filename;
do {
PyFrameObject *back = PyFrame_GetBack(frame);
Py_SETREF(frame, back);
- } while (frame != NULL && is_internal_frame(frame));
+ } while (frame != NULL && (frame_filename = get_frame_filename(frame)) &&
+ (is_internal_filename(frame_filename) ||
+ is_filename_to_skip(frame_filename, skip_file_prefixes)));
return frame;
}
/* filename, module, and registry are new refs, globals is borrowed */
+/* skip_file_prefixes is either NULL or a tuple of strs. */
/* Returns 0 on error (no new refs), 1 on success */
static int
-setup_context(Py_ssize_t stack_level, PyObject **filename, int *lineno,
+setup_context(Py_ssize_t stack_level,
+ PyTupleObject *skip_file_prefixes,
+ PyObject **filename, int *lineno,
PyObject **module, PyObject **registry)
{
PyObject *globals;
@@ -820,6 +863,21 @@ setup_context(Py_ssize_t stack_level, PyObject **filename, int *lineno,
if (tstate == NULL) {
return 0;
}
+ if (skip_file_prefixes) {
+ /* Type check our data structure up front. Later code that uses it
+ * isn't structured to report errors. */
+ Py_ssize_t prefixes = PyTuple_GET_SIZE(skip_file_prefixes);
+ for (Py_ssize_t idx = 0; idx < prefixes; ++idx)
+ {
+ PyObject *prefix = PyTuple_GET_ITEM(skip_file_prefixes, idx);
+ if (!PyUnicode_Check(prefix)) {
+ PyErr_Format(PyExc_TypeError,
+ "Found non-str '%s' in skip_file_prefixes.",
+ Py_TYPE(prefix)->tp_name);
+ return 0;
+ }
+ }
+ }
PyInterpreterState *interp = tstate->interp;
PyFrameObject *f = PyThreadState_GetFrame(tstate);
// Stack level comparisons to Python code is off by one as there is no
@@ -832,7 +890,7 @@ setup_context(Py_ssize_t stack_level, PyObject **filename, int *lineno,
}
else {
while (--stack_level > 0 && f != NULL) {
- f = next_external_frame(f);
+ f = next_external_frame(f, skip_file_prefixes);
}
}
@@ -925,7 +983,7 @@ get_category(PyObject *message, PyObject *category)
static PyObject *
do_warn(PyObject *message, PyObject *category, Py_ssize_t stack_level,
- PyObject *source)
+ PyObject *source, PyTupleObject *skip_file_prefixes)
{
PyObject *filename, *module, *registry, *res;
int lineno;
@@ -935,7 +993,8 @@ do_warn(PyObject *message, PyObject *category, Py_ssize_t stack_level,
return NULL;
}
- if (!setup_context(stack_level, &filename, &lineno, &module, &registry))
+ if (!setup_context(stack_level, skip_file_prefixes,
+ &filename, &lineno, &module, &registry))
return NULL;
res = warn_explicit(tstate, category, message, filename, lineno, module, registry,
@@ -950,22 +1009,42 @@ do_warn(PyObject *message, PyObject *category, Py_ssize_t stack_level,
warn as warnings_warn
message: object
+ Text of the warning message.
category: object = None
+ The Warning category subclass. Defaults to UserWarning.
stacklevel: Py_ssize_t = 1
+ How far up the call stack to make this warning appear. A value of 2 for
+ example attributes the warning to the caller of the code calling warn().
source: object = None
+ If supplied, the destroyed object which emitted a ResourceWarning
+ *
+ skip_file_prefixes: object(type='PyTupleObject *', subclass_of='&PyTuple_Type') = NULL
+ An optional tuple of module filename prefixes indicating frames to skip
+ during stacklevel computations for stack frame attribution.
Issue a warning, or maybe ignore it or raise an exception.
[clinic start generated code]*/
static PyObject *
warnings_warn_impl(PyObject *module, PyObject *message, PyObject *category,
- Py_ssize_t stacklevel, PyObject *source)
-/*[clinic end generated code: output=31ed5ab7d8d760b2 input=bfdf5cf99f6c4edd]*/
+ Py_ssize_t stacklevel, PyObject *source,
+ PyTupleObject *skip_file_prefixes)
+/*[clinic end generated code: output=a68e0f6906c65f80 input=eb37c6a18bec4ea1]*/
{
category = get_category(message, category);
if (category == NULL)
return NULL;
- return do_warn(message, category, stacklevel, source);
+ if (skip_file_prefixes) {
+ if (PyTuple_GET_SIZE(skip_file_prefixes) > 0) {
+ if (stacklevel < 2) {
+ stacklevel = 2;
+ }
+ } else {
+ Py_DECREF((PyObject *)skip_file_prefixes);
+ skip_file_prefixes = NULL;
+ }
+ }
+ return do_warn(message, category, stacklevel, source, skip_file_prefixes);
}
static PyObject *
@@ -1113,7 +1192,7 @@ warn_unicode(PyObject *category, PyObject *message,
if (category == NULL)
category = PyExc_RuntimeWarning;
- res = do_warn(message, category, stack_level, source);
+ res = do_warn(message, category, stack_level, source, NULL);
if (res == NULL)
return -1;
Py_DECREF(res);
diff --git a/Python/clinic/_warnings.c.h b/Python/clinic/_warnings.c.h
index 8838a42..432e554 100644
--- a/Python/clinic/_warnings.c.h
+++ b/Python/clinic/_warnings.c.h
@@ -9,17 +9,32 @@ preserve
PyDoc_STRVAR(warnings_warn__doc__,
-"warn($module, /, message, category=None, stacklevel=1, source=None)\n"
+"warn($module, /, message, category=None, stacklevel=1, source=None, *,\n"
+" skip_file_prefixes=<unrepresentable>)\n"
"--\n"
"\n"
-"Issue a warning, or maybe ignore it or raise an exception.");
+"Issue a warning, or maybe ignore it or raise an exception.\n"
+"\n"
+" message\n"
+" Text of the warning message.\n"
+" category\n"
+" The Warning category subclass. Defaults to UserWarning.\n"
+" stacklevel\n"
+" How far up the call stack to make this warning appear. A value of 2 for\n"
+" example attributes the warning to the caller of the code calling warn().\n"
+" source\n"
+" If supplied, the destroyed object which emitted a ResourceWarning\n"
+" skip_file_prefixes\n"
+" An optional tuple of module filename prefixes indicating frames to skip\n"
+" during stacklevel computations for stack frame attribution.");
#define WARNINGS_WARN_METHODDEF \
{"warn", _PyCFunction_CAST(warnings_warn), METH_FASTCALL|METH_KEYWORDS, warnings_warn__doc__},
static PyObject *
warnings_warn_impl(PyObject *module, PyObject *message, PyObject *category,
- Py_ssize_t stacklevel, PyObject *source);
+ Py_ssize_t stacklevel, PyObject *source,
+ PyTupleObject *skip_file_prefixes);
static PyObject *
warnings_warn(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
@@ -27,14 +42,14 @@ warnings_warn(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObjec
PyObject *return_value = NULL;
#if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
- #define NUM_KEYWORDS 4
+ #define NUM_KEYWORDS 5
static struct {
PyGC_Head _this_is_not_used;
PyObject_VAR_HEAD
PyObject *ob_item[NUM_KEYWORDS];
} _kwtuple = {
.ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
- .ob_item = { &_Py_ID(message), &_Py_ID(category), &_Py_ID(stacklevel), &_Py_ID(source), },
+ .ob_item = { &_Py_ID(message), &_Py_ID(category), &_Py_ID(stacklevel), &_Py_ID(source), &_Py_ID(skip_file_prefixes), },
};
#undef NUM_KEYWORDS
#define KWTUPLE (&_kwtuple.ob_base.ob_base)
@@ -43,19 +58,20 @@ warnings_warn(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObjec
# define KWTUPLE NULL
#endif // !Py_BUILD_CORE
- static const char * const _keywords[] = {"message", "category", "stacklevel", "source", NULL};
+ static const char * const _keywords[] = {"message", "category", "stacklevel", "source", "skip_file_prefixes", NULL};
static _PyArg_Parser _parser = {
.keywords = _keywords,
.fname = "warn",
.kwtuple = KWTUPLE,
};
#undef KWTUPLE
- PyObject *argsbuf[4];
+ PyObject *argsbuf[5];
Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
PyObject *message;
PyObject *category = Py_None;
Py_ssize_t stacklevel = 1;
PyObject *source = Py_None;
+ PyTupleObject *skip_file_prefixes = NULL;
args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 4, 0, argsbuf);
if (!args) {
@@ -88,9 +104,23 @@ warnings_warn(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObjec
goto skip_optional_pos;
}
}
- source = args[3];
+ if (args[3]) {
+ source = args[3];
+ if (!--noptargs) {
+ goto skip_optional_pos;
+ }
+ }
skip_optional_pos:
- return_value = warnings_warn_impl(module, message, category, stacklevel, source);
+ if (!noptargs) {
+ goto skip_optional_kwonly;
+ }
+ if (!PyTuple_Check(args[4])) {
+ _PyArg_BadArgument("warn", "argument 'skip_file_prefixes'", "tuple", args[4]);
+ goto exit;
+ }
+ skip_file_prefixes = (PyTupleObject *)args[4];
+skip_optional_kwonly:
+ return_value = warnings_warn_impl(module, message, category, stacklevel, source, skip_file_prefixes);
exit:
return return_value;
@@ -216,4 +246,4 @@ warnings_filters_mutated(PyObject *module, PyObject *Py_UNUSED(ignored))
{
return warnings_filters_mutated_impl(module);
}
-/*[clinic end generated code: output=0d264d1ddfc37100 input=a9049054013a1b77]*/
+/*[clinic end generated code: output=20429719d7223bdc input=a9049054013a1b77]*/