summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_unicode.py
diff options
context:
space:
mode:
authorVictor Stinner <victor.stinner@haypocalc.com>2011-01-05 00:19:28 (GMT)
committerVictor Stinner <victor.stinner@haypocalc.com>2011-01-05 00:19:28 (GMT)
commitca1e7ec3444c372335c96f83d1550b16b0783557 (patch)
tree5cab60903900be4f45de13d8faaf6dd17ee83147 /Lib/test/test_unicode.py
parent1ec121d0d19b0ab55cead5974d126fb9b0f95938 (diff)
downloadcpython-ca1e7ec3444c372335c96f83d1550b16b0783557.zip
cpython-ca1e7ec3444c372335c96f83d1550b16b0783557.tar.gz
cpython-ca1e7ec3444c372335c96f83d1550b16b0783557.tar.bz2
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.
Diffstat (limited to 'Lib/test/test_unicode.py')
-rw-r--r--Lib/test/test_unicode.py26
1 files changed, 20 insertions, 6 deletions
diff --git a/Lib/test/test_unicode.py b/Lib/test/test_unicode.py
index 2de9e7f..7b130ca 100644
--- a/Lib/test/test_unicode.py
+++ b/Lib/test/test_unicode.py
@@ -1423,22 +1423,36 @@ class UnicodeTest(string_tests.CommonTest,
self.assertEqual("%s" % s, '__str__ overridden')
self.assertEqual("{}".format(s), '__str__ overridden')
+ # Test PyUnicode_FromFormat()
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
- 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')
- # non-ascii format, ascii argument: ensure that PyUnicode_FromFormat()
- # raises an error for a non-ascii format string.
+ # non-ascii format, ascii argument: ensure that PyUnicode_FromFormatV()
+ # raises an error
self.assertRaisesRegex(ValueError,
'^PyUnicode_FromFormatV\(\) expects an ASCII-encoded format '
'string, got a non-ASCII byte: 0xe9$',
- format_unicode, b'unicode\xe9=%s', 'ascii')
+ PyUnicode_FromFormat, b'unicode\xe9=%s', 'ascii')
# 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'")
# Test PyUnicode_AsWideChar()