Issue #9425: Create PyErr_WarnFormat() function

Similar to PyErr_WarnEx() but use PyUnicode_FromFormatV() to format the warning
message.

Strip also some trailing spaces.
diff --git a/Objects/moduleobject.c b/Objects/moduleobject.c
index 6c6eac1..7b8e1b6 100644
--- a/Objects/moduleobject.c
+++ b/Objects/moduleobject.c
@@ -56,10 +56,6 @@
     return NULL;
 }
 
-static char api_version_warning[] =
-"Python C API version mismatch for module %.100s:\
- This Python has API version %d, module %.100s has version %d.";
-
 PyObject *
 PyModule_Create2(struct PyModuleDef* module, int module_api_version)
 {
@@ -79,12 +75,13 @@
     }
     name = module->m_name;
     if (module_api_version != PYTHON_API_VERSION) {
-        char message[512];
-        PyOS_snprintf(message, sizeof(message),
-                      api_version_warning, name,
-                      PYTHON_API_VERSION, name,
-                      module_api_version);
-        if (PyErr_WarnEx(PyExc_RuntimeWarning, message, 1))
+        int err;
+        err = PyErr_WarnFormat(PyExc_RuntimeWarning, 1,
+            "Python C API version mismatch for module %.100s: "
+            "This Python has API version %d, module %.100s has version %d.",
+             name,
+             PYTHON_API_VERSION, name, module_api_version);
+        if (err)
             return NULL;
     }
     /* Make sure name is fully qualified.
diff --git a/Objects/typeobject.c b/Objects/typeobject.c
index 268a924..7bb6864 100644
--- a/Objects/typeobject.c
+++ b/Objects/typeobject.c
@@ -3892,13 +3892,10 @@
        tp_reserved) but not tp_richcompare. */
     if (type->tp_reserved && !type->tp_richcompare) {
         int error;
-        char msg[240];
-        PyOS_snprintf(msg, sizeof(msg),
-                      "Type %.100s defines tp_reserved (formerly "
-                      "tp_compare) but not tp_richcompare. "
-                      "Comparisons may not behave as intended.",
-                      type->tp_name);
-        error = PyErr_WarnEx(PyExc_DeprecationWarning, msg, 1);
+        error = PyErr_WarnFormat(PyExc_DeprecationWarning, 1,
+            "Type %.100s defines tp_reserved (formerly tp_compare) "
+            "but not tp_richcompare. Comparisons may not behave as intended.",
+            type->tp_name);
         if (error == -1)
             goto error;
     }
diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c
index 849f33e..7c9b882 100644
--- a/Objects/unicodeobject.c
+++ b/Objects/unicodeobject.c
@@ -755,7 +755,7 @@
     char fmt[61]; /* should be enough for %0width.precisionlld */
     const char *copy;
 
-    Py_VA_COPY(count, vargs); 
+    Py_VA_COPY(count, vargs);
     /* step 1: count the number of %S/%R/%A/%s format specifications
      * (we call PyObject_Str()/PyObject_Repr()/PyObject_ASCII()/
      * PyUnicode_DecodeUTF8() for these objects once during step 3 and put the
@@ -1548,12 +1548,13 @@
 
     /* If the codec returns a buffer, raise a warning and convert to bytes */
     if (PyByteArray_Check(v)) {
-        char msg[100];
+        int error;
         PyObject *b;
-        PyOS_snprintf(msg, sizeof(msg),
-                      "encoder %s returned buffer instead of bytes",
-                      encoding);
-        if (PyErr_WarnEx(PyExc_RuntimeWarning, msg, 1) < 0) {
+
+        error = PyErr_WarnFormat(PyExc_RuntimeWarning, 1,
+            "encoder %s returned bytearray instead of bytes",
+            encoding);
+        if (error) {
             Py_DECREF(v);
             return NULL;
         }
@@ -2279,7 +2280,7 @@
        illegal prefix.  See RFC 3629 for details */
     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 00-0F */
     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
-    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
+    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,