summaryrefslogtreecommitdiff
path: root/Lib/ctypes/__init__.py
diff options
context:
space:
mode:
Diffstat (limited to 'Lib/ctypes/__init__.py')
-rw-r--r--Lib/ctypes/__init__.py10
1 files changed, 9 insertions, 1 deletions
diff --git a/Lib/ctypes/__init__.py b/Lib/ctypes/__init__.py
index 6146773988..dae408a867 100644
--- a/Lib/ctypes/__init__.py
+++ b/Lib/ctypes/__init__.py
@@ -279,7 +279,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