Commit 6f6f8683 authored by Walter Dörwald's avatar Walter Dörwald

Change PyErr_Format() to generate a unicode string (by using

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