mirror of
https://github.com/python/cpython.git
synced 2025-08-04 08:59:19 +00:00
Issue #9425: Create PyErr_WarnFormat() function
Similar to PyErr_WarnEx() but use PyUnicode_FromFormatV() to format the warning message. Strip also some trailing spaces.
This commit is contained in:
parent
b4b8eb9163
commit
4a2b7a1b14
7 changed files with 74 additions and 34 deletions
|
@ -710,19 +710,17 @@ warnings_warn_explicit(PyObject *self, PyObject *args, PyObject *kwds)
|
|||
|
||||
|
||||
/* Function to issue a warning message; may raise an exception. */
|
||||
int
|
||||
PyErr_WarnEx(PyObject *category, const char *text, Py_ssize_t stack_level)
|
||||
|
||||
static int
|
||||
warn_unicode(PyObject *category, PyObject *message,
|
||||
Py_ssize_t stack_level)
|
||||
{
|
||||
PyObject *res;
|
||||
PyObject *message = PyUnicode_FromString(text);
|
||||
if (message == NULL)
|
||||
return -1;
|
||||
|
||||
if (category == NULL)
|
||||
category = PyExc_RuntimeWarning;
|
||||
|
||||
res = do_warn(message, category, stack_level);
|
||||
Py_DECREF(message);
|
||||
if (res == NULL)
|
||||
return -1;
|
||||
Py_DECREF(res);
|
||||
|
@ -730,6 +728,42 @@ PyErr_WarnEx(PyObject *category, const char *text, Py_ssize_t stack_level)
|
|||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
PyErr_WarnFormat(PyObject *category, Py_ssize_t stack_level,
|
||||
const char *format, ...)
|
||||
{
|
||||
int ret;
|
||||
PyObject *message;
|
||||
va_list vargs;
|
||||
|
||||
#ifdef HAVE_STDARG_PROTOTYPES
|
||||
va_start(vargs, format);
|
||||
#else
|
||||
va_start(vargs);
|
||||
#endif
|
||||
message = PyUnicode_FromFormatV(format, vargs);
|
||||
if (message != NULL) {
|
||||
ret = warn_unicode(category, message, stack_level);
|
||||
Py_DECREF(message);
|
||||
}
|
||||
else
|
||||
ret = -1;
|
||||
va_end(vargs);
|
||||
return ret;
|
||||
}
|
||||
|
||||
int
|
||||
PyErr_WarnEx(PyObject *category, const char *text, Py_ssize_t stack_level)
|
||||
{
|
||||
int ret;
|
||||
PyObject *message = PyUnicode_FromString(text);
|
||||
if (message == NULL)
|
||||
return -1;
|
||||
ret = warn_unicode(category, message, stack_level);
|
||||
Py_DECREF(message);
|
||||
return ret;
|
||||
}
|
||||
|
||||
/* PyErr_Warn is only for backwards compatability and will be removed.
|
||||
Use PyErr_WarnEx instead. */
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue