[pypy-commit] pypy py3.5: Define SIZEOF_WCHAR_T in pyconfig.h and copy CPython logic for the related Py_UNICODE_XXX defines

rlamy pypy.commits at gmail.com
Tue Nov 14 16:43:25 EST 2017


Author: Ronan Lamy <ronan.lamy at gmail.com>
Branch: py3.5
Changeset: r93023:69a07b055bdd
Date: 2017-11-14 21:43 +0000
http://bitbucket.org/pypy/pypy/changeset/69a07b055bdd/

Log:	Define SIZEOF_WCHAR_T in pyconfig.h and copy CPython logic for the
	related Py_UNICODE_XXX defines

diff --git a/pypy/module/cpyext/include/pyconfig.h b/pypy/module/cpyext/include/pyconfig.h
--- a/pypy/module/cpyext/include/pyconfig.h
+++ b/pypy/module/cpyext/include/pyconfig.h
@@ -21,10 +21,9 @@
 /* PyPy supposes Py_UNICODE == wchar_t */
 #define HAVE_USABLE_WCHAR_T 1
 #ifndef _WIN32
-#define Py_UNICODE_SIZE 4
-#define Py_UNICODE_WIDE
+#define SIZEOF_WCHAR_T 4
 #else
-#define Py_UNICODE_SIZE 2
+#define SIZEOF_WCHAR_T 2
 #endif
 
 #ifndef _WIN32
diff --git a/pypy/module/cpyext/include/unicodeobject.h b/pypy/module/cpyext/include/unicodeobject.h
--- a/pypy/module/cpyext/include/unicodeobject.h
+++ b/pypy/module/cpyext/include/unicodeobject.h
@@ -1,6 +1,25 @@
 #ifndef Py_UNICODEOBJECT_H
 #define Py_UNICODEOBJECT_H
 
+#ifndef SIZEOF_WCHAR_T
+#error Must define SIZEOF_WCHAR_T
+#endif
+
+#define Py_UNICODE_SIZE SIZEOF_WCHAR_T
+
+/* If wchar_t can be used for UCS-4 storage, set Py_UNICODE_WIDE.
+   Otherwise, Unicode strings are stored as UCS-2 (with limited support
+   for UTF-16) */
+
+#if Py_UNICODE_SIZE >= 4
+#define Py_UNICODE_WIDE
+#endif
+
+/* Set these flags if the platform has "wchar.h" and the
+   wchar_t type is a 16-bit unsigned type */
+/* #define HAVE_WCHAR_H */
+/* #define HAVE_USABLE_WCHAR_T */
+
 #ifdef __cplusplus
 extern "C" {
 #endif
diff --git a/pypy/module/cpyext/test/test_unicodeobject.py b/pypy/module/cpyext/test/test_unicodeobject.py
--- a/pypy/module/cpyext/test/test_unicodeobject.py
+++ b/pypy/module/cpyext/test/test_unicodeobject.py
@@ -333,6 +333,29 @@
         raises(TypeError, module.from_object, b'abc')
         raises(TypeError, module.from_object, 42)
 
+    def test_widechar(self):
+        module = self.import_extension('foo', [
+            ("make_wide", "METH_NOARGS",
+             """
+            #if defined(SIZEOF_WCHAR_T) && (SIZEOF_WCHAR_T == 4)
+                const wchar_t wtext[2] = {(wchar_t)0x10ABCDu};
+                size_t wtextlen = 1;
+                const wchar_t invalid[1] = {(wchar_t)0x110000u};
+            #else
+                const wchar_t wtext[3] = {(wchar_t)0xDBEAu, (wchar_t)0xDFCDu};
+                size_t wtextlen = 2;
+            #endif
+                return PyUnicode_FromWideChar(wtext, wtextlen);
+             """),
+            ("make_utf8", "METH_NOARGS",
+             """
+            return PyUnicode_FromString("\\xf4\\x8a\\xaf\\x8d");
+             """)])
+        wide = module.make_wide()
+        utf8 = module.make_utf8()
+        print(repr(wide), repr(utf8))
+        assert wide == utf8
+
 
 class TestUnicode(BaseApiTest):
     def test_unicodeobject(self, space):


More information about the pypy-commit mailing list