Commit ca1e7ec3 authored by Victor Stinner's avatar Victor Stinner

test_unicode: use ctypes to test PyUnicode_FromFormat()

Instead of _testcapi.format_unicode() because it has a limited API: it requires
exactly one argument of type unicode.
parent 1ec121d0
...@@ -1423,22 +1423,36 @@ class UnicodeTest(string_tests.CommonTest, ...@@ -1423,22 +1423,36 @@ class UnicodeTest(string_tests.CommonTest,
self.assertEqual("%s" % s, '__str__ overridden') self.assertEqual("%s" % s, '__str__ overridden')
self.assertEqual("{}".format(s), '__str__ overridden') self.assertEqual("{}".format(s), '__str__ overridden')
# Test PyUnicode_FromFormat()
def test_from_format(self): def test_from_format(self):
from _testcapi import format_unicode support.import_module('ctypes')
from ctypes import pythonapi, py_object
if sys.maxunicode == 65535:
name = "PyUnicodeUCS2_FromFormat"
else:
name = "PyUnicodeUCS4_FromFormat"
_PyUnicode_FromFormat = getattr(pythonapi, name)
_PyUnicode_FromFormat.restype = py_object
def PyUnicode_FromFormat(format, *args):
cargs = tuple(
py_object(arg) if isinstance(arg, str) else arg
for arg in args)
return _PyUnicode_FromFormat(format, *cargs)
# ascii format, non-ascii argument # ascii format, non-ascii argument
text = format_unicode(b'ascii\x7f=%U', 'unicode\xe9') text = PyUnicode_FromFormat(b'ascii\x7f=%U', 'unicode\xe9')
self.assertEqual(text, 'ascii\x7f=unicode\xe9') self.assertEqual(text, 'ascii\x7f=unicode\xe9')
# non-ascii format, ascii argument: ensure that PyUnicode_FromFormat() # non-ascii format, ascii argument: ensure that PyUnicode_FromFormatV()
# raises an error for a non-ascii format string. # raises an error
self.assertRaisesRegex(ValueError, self.assertRaisesRegex(ValueError,
'^PyUnicode_FromFormatV\(\) expects an ASCII-encoded format ' '^PyUnicode_FromFormatV\(\) expects an ASCII-encoded format '
'string, got a non-ASCII byte: 0xe9$', 'string, got a non-ASCII byte: 0xe9$',
format_unicode, b'unicode\xe9=%s', 'ascii') PyUnicode_FromFormat, b'unicode\xe9=%s', 'ascii')
# other tests # other tests
text = format_unicode(b'%%A:%A', 'abc\xe9\uabcd\U0010ffff') text = PyUnicode_FromFormat(b'%%A:%A', 'abc\xe9\uabcd\U0010ffff')
self.assertEqual(text, r"%A:'abc\xe9\uabcd\U0010ffff'") self.assertEqual(text, r"%A:'abc\xe9\uabcd\U0010ffff'")
# Test PyUnicode_AsWideChar() # Test PyUnicode_AsWideChar()
......
...@@ -2246,17 +2246,6 @@ crash_no_current_thread(PyObject *self) ...@@ -2246,17 +2246,6 @@ crash_no_current_thread(PyObject *self)
return NULL; 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[] = { static PyMethodDef TestMethods[] = {
{"raise_exception", raise_exception, METH_VARARGS}, {"raise_exception", raise_exception, METH_VARARGS},
{"raise_memoryerror", (PyCFunction)raise_memoryerror, METH_NOARGS}, {"raise_memoryerror", (PyCFunction)raise_memoryerror, METH_NOARGS},
...@@ -2338,7 +2327,6 @@ static PyMethodDef TestMethods[] = { ...@@ -2338,7 +2327,6 @@ static PyMethodDef TestMethods[] = {
{"make_exception_with_doc", (PyCFunction)make_exception_with_doc, {"make_exception_with_doc", (PyCFunction)make_exception_with_doc,
METH_VARARGS | METH_KEYWORDS}, METH_VARARGS | METH_KEYWORDS},
{"crash_no_current_thread", (PyCFunction)crash_no_current_thread, METH_NOARGS}, {"crash_no_current_thread", (PyCFunction)crash_no_current_thread, METH_NOARGS},
{"format_unicode", format_unicode, METH_VARARGS},
{NULL, NULL} /* sentinel */ {NULL, NULL} /* sentinel */
}; };
......
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