]> granicus.if.org Git - python/commitdiff
Change PyErr_Format() to generate a unicode string (by using
authorWalter Dörwald <walter@livinglogic.de>
Fri, 25 May 2007 15:46:59 +0000 (15:46 +0000)
committerWalter Dörwald <walter@livinglogic.de>
Fri, 25 May 2007 15:46:59 +0000 (15:46 +0000)
PyUnicode_FromFormatV() instead of PyString_FromFormatV()).

Change calls to PyErr_Format() to benefit from the new format
specifiers: Using %S, object instead of %s, PyString_AS_STRING(object)
with will work with unicode objects too.

Include/pyerrors.h
Python/ceval.c
Python/codecs.c
Python/compile.c
Python/errors.c

index c72a5bd1b11913470932675c37e2d7bc9dc1ebde..1f7b2ca06b5c833b0a3421ee9acb45369052f053 100644 (file)
@@ -173,8 +173,7 @@ PyAPI_FUNC(PyObject *) PyErr_SetFromErrnoWithUnicodeFilename(
        PyObject *, Py_UNICODE *);
 #endif /* Py_WIN_WIDE_FILENAMES */
 
-PyAPI_FUNC(PyObject *) PyErr_Format(PyObject *, const char *, ...)
-                       Py_GCC_ATTRIBUTE((format(printf, 2, 3)));
+PyAPI_FUNC(PyObject *) PyErr_Format(PyObject *, const char *, ...);
 
 #ifdef MS_WINDOWS
 PyAPI_FUNC(PyObject *) PyErr_SetFromWindowsErrWithFilenameObject(
index d777a3a13591f947aa5014cd9db0013c3e76e1d1..37659f8f72abfa8dc2a58e9f0bce73d6e020b2d4 100644 (file)
@@ -2615,9 +2615,9 @@ PyEval_EvalCodeEx(PyCodeObject *co, PyObject *globals, PyObject *locals,
                if (argcount > co->co_argcount) {
                        if (!(co->co_flags & CO_VARARGS)) {
                                PyErr_Format(PyExc_TypeError,
-                                   "%.200s() takes %s %d "
+                                   "%S() takes %s %d "
                                    "%spositional argument%s (%d given)",
-                                   PyString_AsString(co->co_name),
+                                   co->co_name,
                                    defcount ? "at most" : "exactly",
                                    co->co_argcount,
                                    kwcount ? "non-keyword " : "",
@@ -2649,8 +2649,8 @@ PyEval_EvalCodeEx(PyCodeObject *co, PyObject *globals, PyObject *locals,
                        int j;
                        if (keyword == NULL || !(PyString_Check(keyword) || PyUnicode_Check(keyword))) {
                                PyErr_Format(PyExc_TypeError,
-                                   "%.200s() keywords must be strings",
-                                   PyString_AsString(co->co_name));
+                                   "%S() keywords must be strings",
+                                   co->co_name);
                                goto fail;
                        }
                        /* XXX slow -- speed up using dictionary? */
@@ -2672,10 +2672,10 @@ PyEval_EvalCodeEx(PyCodeObject *co, PyObject *globals, PyObject *locals,
                        if (j >= co->co_argcount + co->co_kwonlyargcount) {
                                if (kwdict == NULL) {
                                        PyErr_Format(PyExc_TypeError,
-                                           "%.200s() got an unexpected "
-                                           "keyword argument '%.400s'",
-                                           PyString_AsString(co->co_name),
-                                           PyString_AsString(keyword));
+                                           "%S() got an unexpected "
+                                           "keyword argument '%S'",
+                                           co->co_name,
+                                           keyword);
                                        goto fail;
                                }
                                PyDict_SetItem(kwdict, keyword, value);
@@ -2683,11 +2683,11 @@ PyEval_EvalCodeEx(PyCodeObject *co, PyObject *globals, PyObject *locals,
                        else {
                                if (GETLOCAL(j) != NULL) {
                                        PyErr_Format(PyExc_TypeError,
-                                            "%.200s() got multiple "
+                                            "%S() got multiple "
                                             "values for keyword "
-                                            "argument '%.400s'",
-                                            PyString_AsString(co->co_name),
-                                            PyString_AsString(keyword));
+                                            "argument '%S'",
+                                            co->co_name,
+                                            keyword);
                                        goto fail;
                                }
                                Py_INCREF(value);
@@ -2711,10 +2711,8 @@ PyEval_EvalCodeEx(PyCodeObject *co, PyObject *globals, PyObject *locals,
                                        continue;
                                }
                                PyErr_Format(PyExc_TypeError,
-                                       "%.200s() needs "
-                                       "keyword-only argument %s",
-                                       PyString_AsString(co->co_name),
-                                       PyString_AsString(name));
+                                       "%S() needs keyword-only argument %S",
+                                       co->co_name, name);
                                goto fail;
                        }
                }
@@ -2723,10 +2721,10 @@ PyEval_EvalCodeEx(PyCodeObject *co, PyObject *globals, PyObject *locals,
                        for (i = argcount; i < m; i++) {
                                if (GETLOCAL(i) == NULL) {
                                        PyErr_Format(PyExc_TypeError,
-                                           "%.200s() takes %s %d "
+                                           "%S() takes %s %d "
                                            "%spositional argument%s "
                                            "(%d given)",
-                                           PyString_AsString(co->co_name),
+                                           co->co_name,
                                            ((co->co_flags & CO_VARARGS) ||
                                             defcount) ? "at least"
                                                       : "exactly",
@@ -2751,8 +2749,8 @@ PyEval_EvalCodeEx(PyCodeObject *co, PyObject *globals, PyObject *locals,
        else {
                if (argcount > 0 || kwcount > 0) {
                        PyErr_Format(PyExc_TypeError,
-                                    "%.200s() takes no arguments (%d given)",
-                                    PyString_AsString(co->co_name),
+                                    "%S() takes no arguments (%d given)",
+                                    co->co_name,
                                     argcount + kwcount);
                        goto fail;
                }
@@ -4021,9 +4019,7 @@ import_from(PyObject *v, PyObject *name)
 
        x = PyObject_GetAttr(v, name);
        if (x == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
-               PyErr_Format(PyExc_ImportError,
-                            "cannot import name %.230s",
-                            PyString_AsString(name));
+               PyErr_Format(PyExc_ImportError, "cannot import name %S", name);
        }
        return x;
 }
index ddd19359ac927d51c7e248b51364bd63b830f496..3aa1f386e7be9aebc948ac4e20ef3b8f8c6ca6b2 100644 (file)
@@ -443,18 +443,13 @@ static void wrong_exception_type(PyObject *exc)
 {
     PyObject *type = PyObject_GetAttrString(exc, "__class__");
     if (type != NULL) {
-       PyObject *name = PyObject_GetAttrString(type, "__name__");
-       Py_DECREF(type);
-       if (name != NULL) {
-           PyObject *string = PyObject_Str(name);
-           Py_DECREF(name);
-           if (string != NULL) {
-               PyErr_Format(PyExc_TypeError,
-                   "don't know how to handle %.400s in error callback",
-                   PyString_AS_STRING(string));
-               Py_DECREF(string);
-           }
-       }
+        PyObject *name = PyObject_GetAttrString(type, "__name__");
+        Py_DECREF(type);
+        if (name != NULL) {
+            PyErr_Format(PyExc_TypeError,
+                         "don't know how to handle %S in error callback", name);
+            Py_DECREF(name);
+        }
     }
 }
 
index ffca8307e5224f78db58b4d7062d5bcf7797233c..359de587c5971ea12b89e5ea28eb85dd75931116 100644 (file)
@@ -2467,9 +2467,9 @@ mangled = _Py_Mangle(c->u->u_private, name);
                        break;
                case Del:
                        PyErr_Format(PyExc_SyntaxError,
-                                    "can not delete variable '%s' referenced "
+                                    "can not delete variable '%S' referenced "
                                     "in nested scope",
-                                    PyString_AS_STRING(name));
+                                    name);
                        Py_DECREF(mangled);
                        return 0;
                case Param:
index 4ef491fe1bc6ec5319422e283bde873f33b283da..2a84c8d30dc56324b24fcf696b55074cbc379ff2 100644 (file)
@@ -54,11 +54,9 @@ PyErr_SetObject(PyObject *exception, PyObject *value)
 {
        if (exception != NULL &&
            !PyExceptionClass_Check(exception)) {
-               PyObject *excstr = PyObject_ReprStr8(exception);
                PyErr_Format(PyExc_SystemError,
-                            "exception %s not a BaseException subclass",
-                            PyString_AS_STRING(excstr));
-               Py_DECREF(excstr);
+                            "exception %R not a BaseException subclass",
+                            exception);
                return;
        }
        Py_XINCREF(exception);
@@ -75,7 +73,7 @@ PyErr_SetNone(PyObject *exception)
 void
 PyErr_SetString(PyObject *exception, const char *string)
 {
-       PyObject *value = PyString_FromString(string);
+       PyObject *value = PyUnicode_FromString(string);
        PyErr_SetObject(exception, value);
        Py_XDECREF(value);
 }
@@ -528,7 +526,7 @@ PyErr_Format(PyObject *exception, const char *format, ...)
        va_start(vargs);
 #endif
 
-       string = PyString_FromFormatV(format, vargs);
+       string = PyUnicode_FromFormatV(format, vargs);
        PyErr_SetObject(exception, string);
        Py_XDECREF(string);
        va_end(vargs);