[pypy-commit] cffi wchar_t: Another test.

arigo noreply at buildbot.pypy.org
Wed Jun 27 10:16:47 CEST 2012


Author: Armin Rigo <arigo at tunes.org>
Branch: wchar_t
Changeset: r534:1bf9eced10c8
Date: 2012-06-27 10:16 +0200
http://bitbucket.org/cffi/cffi/changeset/1bf9eced10c8/

Log:	Another test.

diff --git a/c/test_c.py b/c/test_c.py
--- a/c/test_c.py
+++ b/c/test_c.py
@@ -1144,3 +1144,74 @@
     newp(BStructPtr, [cast(BCharP, 0)])
     py.test.raises(TypeError, newp, BStructPtr, [cast(BIntP, 0)])
     py.test.raises(TypeError, newp, BStructPtr, [cast(BFunc2, 0)])
+
+def test_wchar():
+    BWChar = new_primitive_type("wchar_t")
+    pyuni4 = {1: True, 2: False}[len(u'\U00012345')]
+    wchar4 = {2: False, 4: True}[sizeof(BWChar)]
+    #
+    BWCharP = new_pointer_type(BWChar)
+    BStruct = new_struct_type("foo_s")
+    BStructPtr = new_pointer_type(BStruct)
+    complete_struct_or_union(BStruct, [('a1', BWChar, -1),
+                                       ('a2', BWCharP, -1)])
+    s = newp(BStructPtr)
+    s.a1 = '\x00'
+    assert s.a1 == u'\x00'
+    py.test.raises(UnicodeDecodeError, "s.a1 = '\xFF'")
+    s.a1 = u'\u1234'
+    assert s.a1 == u'\u1234'
+    if pyuni4:
+        assert wchar4
+        s.a1 = u'\U00012345'
+        assert s.a1 == u'\U00012345'
+    elif wchar4:
+        s.a1 = cast(BWChar, 0x12345)
+        assert s.a1 == u'\ud808\udf45'
+        s.a1 = u'\ud807\udf44'
+        assert s.a1 == u'\U00011f44'
+    else:
+        py.test.raises(ValueError, "s.a1 = u'\U00012345'")
+    #
+    a = new_array_type(BWCharP, u'hello \u1234 world')
+    assert len(a) == 14   # including the final null
+    assert unicode(a) == u'hello \u1234 world'
+    py.test.raises(UnicodeEncodeError, str, a)
+    assert a[6] == u'\u1234'
+    a[6] = '-'
+    assert str(a) == 'hello - world'
+    #
+    w = cast(BWChar, 'a')
+    assert repr(w) == "<cdata 'wchar_t' u'a'>"
+    assert str(w) == 'a'
+    assert unicode(w) == u'a'
+    w = cast(BWChar, 0x1234)
+    assert repr(w) == "<cdata 'wchar_t' u'\u1234'>"
+    py.test.raises(UnicodeEncodeError, str, w)
+    assert unicode(w) == u'\u1234'
+    assert int(w) == 0x1234
+    #
+    p = cast(BWCharP, a)
+    assert str(p) == 'hello - world'
+    assert unicode(p) == u'hello - world'
+    p[6] = u'\u2345'
+    py.test.raises(UnicodeEncodeError, str, p)
+    assert unicode(p) == u'hello \u2345 world'
+    #
+    s = newp(BStructPtr, [u'\u1234', p])
+    assert s.a1 == u'\u1234'
+    assert s.a2 == p
+    py.test.raises(UnicodeEncodeError, str, s.a2)
+    assert unicode(s.a2) == u'hello \u2345 world'
+    #
+    q = cast(BWCharP, 0)
+    py.test.raises(RuntimeError, str, q)
+    py.test.raises(RuntimeError, unicode, q)
+    #
+    BInt = new_primitive_type("int")
+    def cb(p):
+        assert repr(p).startswith("<cdata 'wchar_t *' 0x")
+        return len(unicode(p))
+    BFunc = new_function_type((BWCharP,), BInt, False)
+    f = callback(BFunc, cb, -42)
+    assert f(u'a\u1234b') == 3


More information about the pypy-commit mailing list