summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>2019-06-14 16:43:22 (GMT)
committerGitHub <noreply@github.com>2019-06-14 16:43:22 (GMT)
commitb0f6fa8d7d4c6d8263094124df9ef9cf816bbed6 (patch)
treef63bc97e394dbe4e8bde7febd91c32347781e24c
parent0c2eb6d21013d77e1160250d3cf69ca80215d064 (diff)
downloadcpython-b0f6fa8d7d4c6d8263094124df9ef9cf816bbed6.zip
cpython-b0f6fa8d7d4c6d8263094124df9ef9cf816bbed6.tar.gz
cpython-b0f6fa8d7d4c6d8263094124df9ef9cf816bbed6.tar.bz2
bpo-19865: ctypes.create_unicode_buffer() supports non-BMP strings on Windows (GH-14081)
(cherry picked from commit 9765efcb39fc03d5b1abec3924388974470a8bd5) Co-authored-by: Zackery Spytz <zspytz@gmail.com>
-rw-r--r--Lib/ctypes/__init__.py10
-rw-r--r--Lib/ctypes/test/test_buffers.py9
-rw-r--r--Misc/NEWS.d/next/Library/2019-06-14-08-30-16.bpo-19865.FRGH4I.rst2
3 files changed, 20 insertions, 1 deletions
diff --git a/Lib/ctypes/__init__.py b/Lib/ctypes/__init__.py
index 4107db3..128155d 100644
--- a/Lib/ctypes/__init__.py
+++ b/Lib/ctypes/__init__.py
@@ -274,7 +274,15 @@ def create_unicode_buffer(init, size=None):
"""
if isinstance(init, str):
if size is None:
- size = len(init)+1
+ if sizeof(c_wchar) == 2:
+ # UTF-16 requires a surrogate pair (2 wchar_t) for non-BMP
+ # characters (outside [U+0000; U+FFFF] range). +1 for trailing
+ # NUL character.
+ size = sum(2 if ord(c) > 0xFFFF else 1 for c in init) + 1
+ else:
+ # 32-bit wchar_t (1 wchar_t per Unicode character). +1 for
+ # trailing NUL character.
+ size = len(init) + 1
buftype = c_wchar * size
buf = buftype()
buf.value = init
diff --git a/Lib/ctypes/test/test_buffers.py b/Lib/ctypes/test/test_buffers.py
index 166faaf..15782be 100644
--- a/Lib/ctypes/test/test_buffers.py
+++ b/Lib/ctypes/test/test_buffers.py
@@ -60,5 +60,14 @@ class StringBufferTestCase(unittest.TestCase):
self.assertEqual(b[::2], "ac")
self.assertEqual(b[::5], "a")
+ @need_symbol('c_wchar')
+ def test_create_unicode_buffer_non_bmp(self):
+ expected = 5 if sizeof(c_wchar) == 2 else 3
+ for s in '\U00010000\U00100000', '\U00010000\U0010ffff':
+ b = create_unicode_buffer(s)
+ self.assertEqual(len(b), expected)
+ self.assertEqual(b[-1], '\0')
+
+
if __name__ == "__main__":
unittest.main()
diff --git a/Misc/NEWS.d/next/Library/2019-06-14-08-30-16.bpo-19865.FRGH4I.rst b/Misc/NEWS.d/next/Library/2019-06-14-08-30-16.bpo-19865.FRGH4I.rst
new file mode 100644
index 0000000..efd1f55
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2019-06-14-08-30-16.bpo-19865.FRGH4I.rst
@@ -0,0 +1,2 @@
+:func:`ctypes.create_unicode_buffer()` now also supports non-BMP characters
+on platforms with 16-bit :c:type:`wchar_t` (for example, Windows and AIX).