summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorInada Naoki <songofacandy@gmail.com>2020-03-14 03:43:18 (GMT)
committerGitHub <noreply@github.com>2020-03-14 03:43:18 (GMT)
commitc7ad974d341d3edb6b9d2a2dcae4d3d4794ada6b (patch)
tree2026fd46b762fb2deaf9091e4d7e09dc198bc2d3 /Lib
parent8fb02b6e1942811c8d81041e7df3f5f1f4b1d410 (diff)
downloadcpython-c7ad974d341d3edb6b9d2a2dcae4d3d4794ada6b.zip
cpython-c7ad974d341d3edb6b9d2a2dcae4d3d4794ada6b.tar.gz
cpython-c7ad974d341d3edb6b9d2a2dcae4d3d4794ada6b.tar.bz2
bpo-39087: Add _PyUnicode_GetUTF8Buffer() (GH-17659)
Co-authored-by: Victor Stinner <vstinner@python.org>
Diffstat (limited to 'Lib')
-rw-r--r--Lib/test/test_unicode.py22
1 files changed, 22 insertions, 0 deletions
diff --git a/Lib/test/test_unicode.py b/Lib/test/test_unicode.py
index 2839889..0522513 100644
--- a/Lib/test/test_unicode.py
+++ b/Lib/test/test_unicode.py
@@ -2830,6 +2830,28 @@ class CAPITest(unittest.TestCase):
self.assertEqual(unicode_asucs4(s, len(s), True), s+'\0')
self.assertEqual(unicode_asucs4(s, len(s), False), s+'\uffff')
+ # Test _PyUnicode_GetUTF8Buffer()
+ @support.cpython_only
+ def test_getutf8buffer(self):
+ from _testcapi import unicode_getutf8buffer, unicode_test_getutf8buffer
+
+ # Run tests wrtten in C. Raise an error when test failed.
+ unicode_test_getutf8buffer()
+
+ ascii_ = "foo"
+ bmp = '\u0100'
+ bmp2 = '\uffff'
+ nonbmp = chr(0x10ffff)
+ surrogates = 'a\ud800b\udfffc'
+
+ self.assertEqual(unicode_getutf8buffer(ascii_), b'foo')
+ self.assertEqual(unicode_getutf8buffer(bmp), b'\xc4\x80')
+ self.assertEqual(unicode_getutf8buffer(bmp2), b'\xef\xbf\xbf')
+ self.assertEqual(unicode_getutf8buffer(nonbmp), b'\xf4\x8f\xbf\xbf')
+ self.assertRaises(UnicodeEncodeError, unicode_getutf8buffer, surrogates)
+ self.assertEqual(unicode_getutf8buffer(surrogates, "surrogatepass"),
+ b'a\xed\xa0\x80b\xed\xbf\xbfc')
+
# Test PyUnicode_AsUTF8()
@support.cpython_only
def test_asutf8(self):