summaryrefslogtreecommitdiffstats
path: root/Python
diff options
context:
space:
mode:
authorVictor Stinner <victor.stinner@haypocalc.com>2010-08-13 14:03:48 (GMT)
committerVictor Stinner <victor.stinner@haypocalc.com>2010-08-13 14:03:48 (GMT)
commit4a2b7a1b141fcbed6da81d942c9db776874c2fa9 (patch)
tree0288e22145e35c9d5d92c051800bf1c85e5858b8 /Python
parentb4b8eb916372dcb566740455122a28b5ed9631f9 (diff)
downloadcpython-4a2b7a1b141fcbed6da81d942c9db776874c2fa9.zip
cpython-4a2b7a1b141fcbed6da81d942c9db776874c2fa9.tar.gz
cpython-4a2b7a1b141fcbed6da81d942c9db776874c2fa9.tar.bz2
Issue #9425: Create PyErr_WarnFormat() function
Similar to PyErr_WarnEx() but use PyUnicode_FromFormatV() to format the warning message. Strip also some trailing spaces.
Diffstat (limited to 'Python')
-rw-r--r--Python/_warnings.c46
1 files changed, 40 insertions, 6 deletions
diff --git a/Python/_warnings.c b/Python/_warnings.c
index 6067ce3..63bcbff 100644
--- a/Python/_warnings.c
+++ b/Python/_warnings.c
@@ -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. */