a non-ASCII byte in the format string.
Document also the encoding.
.. cfunction:: PyObject* PyErr_Format(PyObject *exception, const char *format, ...)
This function sets the error indicator and returns *NULL*. *exception* should be
- a Python exception (class, not an instance). *format* should be a string,
+ a Python exception (class, not an instance). *format* should be an ASCII-encoded string,
containing format codes, similar to :cfunc:`printf`. The ``width.precision``
before a format code is parsed, but the width part is ignored.
arguments, calculate the size of the resulting Python unicode string and return
a string with the values formatted into it. The variable arguments must be C
types and must correspond exactly to the format characters in the *format*
- string. The following format characters are allowed:
+ ASCII-encoded string. The following format characters are allowed:
.. % This should be exactly the same as the table in PyErr_Format.
.. % The descriptions for %zd and %zu are wrong, but the truth is complicated
PyObject *, const Py_UNICODE *);
#endif /* MS_WINDOWS */
-PyAPI_FUNC(PyObject *) PyErr_Format(PyObject *, const char *, ...);
+PyAPI_FUNC(PyObject *) PyErr_Format(
+ PyObject *exception,
+ const char *format, /* ASCII-encoded string */
+ ...
+ );
#ifdef MS_WINDOWS
PyAPI_FUNC(PyObject *) PyErr_SetFromWindowsErrWithFilenameObject(
register PyObject *obj /* Object */
);
-PyAPI_FUNC(PyObject *) PyUnicode_FromFormatV(const char*, va_list);
-PyAPI_FUNC(PyObject *) PyUnicode_FromFormat(const char*, ...);
+PyAPI_FUNC(PyObject *) PyUnicode_FromFormatV(
+ const char *format, /* ASCII-encoded string */
+ va_list vargs
+ );
+PyAPI_FUNC(PyObject *) PyUnicode_FromFormat(
+ const char *format, /* ASCII-encoded string */
+ ...
+ );
/* Format the object based on the format_spec, as defined in PEP 3101
(Advanced String Formatting). */
self.assertEquals("%s" % s, '__str__ overridden')
self.assertEquals("{}".format(s), '__str__ overridden')
+ def test_from_format(self):
+ # Ensure that PyUnicode_FromFormat() raises an error for a non-ascii
+ # format string.
+ from _testcapi import format_unicode
+
+ # ascii format, non-ascii argument
+ text = format_unicode(b'ascii\x7f=%U', 'unicode\xe9')
+ self.assertEqual(text, 'ascii\x7f=unicode\xe9')
+
+ # non-ascii format, ascii argument
+ self.assertRaisesRegexp(ValueError,
+ '^PyUnicode_FromFormatV\(\) expects an ASCII-encoded format '
+ 'string, got a non-ascii byte: 0xe9$',
+ format_unicode, b'unicode\xe9=%s', 'ascii')
def test_main():
support.run_unittest(__name__)
Core and Builtins
-----------------
+- Issue #9738: PyUnicode_FromFormat() and PyErr_Format() raise an error on
+ a non-ASCII byte in the format string.
+
- Issue #4617: Previously it was illegal to delete a name from the local
namespace if it occurs as a free variable in a nested block. This limitation
of the compiler has been lifted, and a new opcode introduced (DELETE_DEREF).
return NULL;
}
+static PyObject *
+format_unicode(PyObject *self, PyObject *args)
+{
+ const char *format;
+ PyObject *arg;
+ if (!PyArg_ParseTuple(args, "yU", &format, &arg))
+ return NULL;
+ return PyUnicode_FromFormat(format, arg);
+
+}
+
static PyMethodDef TestMethods[] = {
{"raise_exception", raise_exception, METH_VARARGS},
{"raise_memoryerror", (PyCFunction)raise_memoryerror, METH_NOARGS},
{"make_exception_with_doc", (PyCFunction)make_exception_with_doc,
METH_VARARGS | METH_KEYWORDS},
{"crash_no_current_thread", (PyCFunction)crash_no_current_thread, METH_NOARGS},
+ {"format_unicode", format_unicode, METH_VARARGS},
{NULL, NULL} /* sentinel */
};
appendstring(p);
goto end;
}
- } else
+ }
+ else if (128 <= (unsigned char)*f) {
+ PyErr_Format(PyExc_ValueError,
+ "PyUnicode_FromFormatV() expects an ASCII-encoded format "
+ "string, got a non-ascii byte: 0x%02x",
+ (unsigned char)*f);
+ goto fail;
+ }
+ else
*s++ = *f;
}