Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-91102: Port 8-argument _warnings.warn_explicit to Argument Clinic #92891

Merged
merged 9 commits into from
Jul 20, 2022
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
8-argument :meth:`_warnings.warn_explicit` is ported to Argument Clinic.
Benefits:

1. :c:data:`METH_VARARGS` calling convention is replaced with
a little faster :c:data:`METH_FASTCALL` (no extra tuple creation)
2. :c:func:`PyArg_ParseTupleAndKeywords` call is replaced with
:c:func:`_PyArg_UnpackKeywords` and a tailored parser generated specially
for the ported function.
45 changes: 22 additions & 23 deletions Python/_warnings.c
Original file line number Diff line number Diff line change
Expand Up @@ -1031,28 +1031,31 @@ get_source_line(PyInterpreterState *interp, PyObject *module_globals, int lineno
return source_line;
}

/*[clinic input]
warn_explicit as warnings_warn_explicit

message: object
category: object
filename: unicode
lineno: int
mod: object = NULL
erlend-aasland marked this conversation as resolved.
Show resolved Hide resolved
erlend-aasland marked this conversation as resolved.
Show resolved Hide resolved
registry: object = None
module_globals: object = None
source as sourceobj: object = None

Issue a warning, or maybe ignore it or raise an exception.
[clinic start generated code]*/

static PyObject *
warnings_warn_explicit(PyObject *self, PyObject *args, PyObject *kwds)
warnings_warn_explicit_impl(PyObject *module, PyObject *message,
PyObject *category, PyObject *filename,
int lineno, PyObject *mod, PyObject *registry,
PyObject *module_globals, PyObject *sourceobj)
/*[clinic end generated code: output=c49c62b15a49a186 input=81e348c13d2f0cda]*/
{
static char *kwd_list[] = {"message", "category", "filename", "lineno",
"module", "registry", "module_globals",
"source", 0};
PyObject *message;
PyObject *category;
PyObject *filename;
int lineno;
PyObject *module = NULL;
PyObject *registry = NULL;
PyObject *module_globals = NULL;
PyObject *sourceobj = NULL;
PyObject *source_line = NULL;
PyObject *returned;

if (!PyArg_ParseTupleAndKeywords(args, kwds, "OOUi|OOOO:warn_explicit",
kwd_list, &message, &category, &filename, &lineno, &module,
&registry, &module_globals, &sourceobj))
return NULL;

PyThreadState *tstate = get_current_tstate();
if (tstate == NULL) {
return NULL;
Expand All @@ -1071,7 +1074,7 @@ warnings_warn_explicit(PyObject *self, PyObject *args, PyObject *kwds)
return NULL;
}
}
returned = warn_explicit(tstate, category, message, filename, lineno, module,
returned = warn_explicit(tstate, category, message, filename, lineno, mod,
registry, source_line, sourceobj);
Py_XDECREF(source_line);
return returned;
Expand Down Expand Up @@ -1348,13 +1351,9 @@ _PyErr_WarnUnawaitedCoroutine(PyObject *coro)
}
}

PyDoc_STRVAR(warn_explicit_doc,
"Low-level interface to warnings functionality.");

static PyMethodDef warnings_functions[] = {
WARNINGS_WARN_METHODDEF
{"warn_explicit", _PyCFunction_CAST(warnings_warn_explicit),
METH_VARARGS | METH_KEYWORDS, warn_explicit_doc},
WARNINGS_WARN_EXPLICIT_METHODDEF
{"_filters_mutated", _PyCFunction_CAST(warnings_filters_mutated), METH_NOARGS,
NULL},
/* XXX(brett.cannon): add showwarning? */
Expand Down
83 changes: 82 additions & 1 deletion Python/clinic/_warnings.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.