[Python-checkins] r82693 - in python/branches/import_unicode: Include/warnings.h Objects/moduleobject.c Objects/typeobject.c Objects/unicodeobject.c Python/_warnings.c Python/import.c

victor.stinner python-checkins at python.org
Fri Jul 9 01:32:54 CEST 2010


Author: victor.stinner
Date: Fri Jul  9 01:32:54 2010
New Revision: 82693

Log:
Create PyErr_WarnUnicode() and PyErr_WarnFormat()

Modified:
   python/branches/import_unicode/Include/warnings.h
   python/branches/import_unicode/Objects/moduleobject.c
   python/branches/import_unicode/Objects/typeobject.c
   python/branches/import_unicode/Objects/unicodeobject.c
   python/branches/import_unicode/Python/_warnings.c
   python/branches/import_unicode/Python/import.c

Modified: python/branches/import_unicode/Include/warnings.h
==============================================================================
--- python/branches/import_unicode/Include/warnings.h	(original)
+++ python/branches/import_unicode/Include/warnings.h	Fri Jul  9 01:32:54 2010
@@ -7,6 +7,8 @@
 PyAPI_FUNC(PyObject*) _PyWarnings_Init(void);
 
 PyAPI_FUNC(int) PyErr_WarnEx(PyObject *, const char *, Py_ssize_t);
+PyAPI_FUNC(int) PyErr_WarnUnicode(PyObject *, PyObject *, Py_ssize_t);
+PyAPI_FUNC(int) PyErr_WarnFormat(PyObject *, Py_ssize_t, const char *, ...);
 PyAPI_FUNC(int) PyErr_WarnExplicit(PyObject *, const char *, const char *, int,
                                     const char *, PyObject *);
 

Modified: python/branches/import_unicode/Objects/moduleobject.c
==============================================================================
--- python/branches/import_unicode/Objects/moduleobject.c	(original)
+++ python/branches/import_unicode/Objects/moduleobject.c	Fri Jul  9 01:32:54 2010
@@ -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.

Modified: python/branches/import_unicode/Objects/typeobject.c
==============================================================================
--- python/branches/import_unicode/Objects/typeobject.c	(original)
+++ python/branches/import_unicode/Objects/typeobject.c	Fri Jul  9 01:32:54 2010
@@ -3892,13 +3892,11 @@
        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;
     }

Modified: python/branches/import_unicode/Objects/unicodeobject.c
==============================================================================
--- python/branches/import_unicode/Objects/unicodeobject.c	(original)
+++ python/branches/import_unicode/Objects/unicodeobject.c	Fri Jul  9 01:32:54 2010
@@ -1558,12 +1558,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 buffer instead of bytes",
+            encoding);
+        if (error) {
             Py_DECREF(v);
             return NULL;
         }

Modified: python/branches/import_unicode/Python/_warnings.c
==============================================================================
--- python/branches/import_unicode/Python/_warnings.c	(original)
+++ python/branches/import_unicode/Python/_warnings.c	Fri Jul  9 01:32:54 2010
@@ -710,21 +710,18 @@
                                 registry, NULL);
 }
 
-
 /* Function to issue a warning message; may raise an exception. */
+
 int
-PyErr_WarnEx(PyObject *category, const char *text, Py_ssize_t stack_level)
+PyErr_WarnUnicode(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);
@@ -732,6 +729,42 @@
     return 0;
 }
 
+int
+PyErr_WarnFormat(PyObject *category, Py_ssize_t stack_level,
+                 const char *format, ...)
+{
+    int ret;
+    PyObject *unicode;
+    va_list vargs;
+
+#ifdef HAVE_STDARG_PROTOTYPES
+    va_start(vargs, format);
+#else
+    va_start(vargs);
+#endif
+    unicode = PyUnicode_FromFormatV(format, vargs);
+    if (unicode != NULL) {
+        ret = PyErr_WarnUnicode(category, unicode, stack_level);
+        Py_DECREF(unicode);
+    }
+    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 = PyErr_WarnUnicode(category, message, stack_level);
+    Py_DECREF(message);
+    return ret;
+}
+
 /* PyErr_Warn is only for backwards compatability and will be removed.
    Use PyErr_WarnEx instead. */
 

Modified: python/branches/import_unicode/Python/import.c
==============================================================================
--- python/branches/import_unicode/Python/import.c	(original)
+++ python/branches/import_unicode/Python/import.c	Fri Jul  9 01:32:54 2010
@@ -2756,19 +2756,16 @@
     parent = PyDict_GetItemString(modules, buf);
     if (parent == NULL) {
         if (orig_level < 1) {
-            PyObject *err_msg = PyBytes_FromFormat(
+            int err;
+            err = PyErr_WarnFormat(PyExc_RuntimeWarning, 1,
                 "Parent module '%.200s' not found "
-                "while handling absolute import", buf);
-            if (err_msg == NULL) {
-                return NULL;
-            }
-            if (!PyErr_WarnEx(PyExc_RuntimeWarning,
-                              PyBytes_AsString(err_msg), 1)) {
+                "while handling absolute import",
+                buf);
+            if (!err) {
                 *buf = '\0';
                 *p_buflen = 0;
                 parent = Py_None;
             }
-            Py_DECREF(err_msg);
         } else {
             PyErr_Format(PyExc_SystemError,
                 "Parent module '%.200s' not loaded, "


More information about the Python-checkins mailing list