Commits

Armin Rigo  committed 8a242b9

Import from cffi/c7af62d13de6.

  • Participants
  • Parent commits 4fe5af4

Comments (0)

Files changed (1)

File pypy/module/_cffi_backend/test/_backend_test_c.py

     mandatory_b_prefix = ''
     mandatory_u_prefix = 'u'
     bytechr = chr
+    bitem2bchr = lambda x: x
     class U(object):
         def __add__(self, other):
             return eval('u'+repr(other).replace(r'\\u', r'\u')
     mandatory_b_prefix = 'b'
     mandatory_u_prefix = ''
     bytechr = lambda n: bytes([n])
+    bitem2bchr = bytechr
     u = ""
 
 def size_of_int():
     assert (p < s) ^ (p > s)
 
 def test_buffer():
-    import __builtin__
+    try:
+        import __builtin__
+    except ImportError:
+        import builtins as __builtin__
     BShort = new_primitive_type("short")
     s = newp(new_pointer_type(BShort), 100)
     assert sizeof(s) == size_of_ptr()
         except IndexError:
             py.test.raises(IndexError, "buf[i]")
         else:
-            assert buf[i] == expected
+            assert buf[i] == bitem2bchr(expected)
     # --mb_slice--
     assert buf[:] == b"hi there\x00"
     for i in range(-12, 12):
         for j in range(-12, 12):
             assert buf[i:j] == b"hi there\x00"[i:j]
     # --misc--
-    assert list(buf) == list(b"hi there\x00")
+    assert list(buf) == list(map(bitem2bchr, b"hi there\x00"))
     # --mb_as_buffer--
-    py.test.raises(TypeError, __builtin__.buffer, c)
-    bf1 = __builtin__.buffer(buf)
-    assert len(bf1) == len(buf) and bf1[3] == "t"
+    if hasattr(__builtin__, 'buffer'):          # Python <= 2.7
+        py.test.raises(TypeError, __builtin__.buffer, c)
+        bf1 = __builtin__.buffer(buf)
+        assert len(bf1) == len(buf) and bf1[3] == "t"
     if hasattr(__builtin__, 'memoryview'):      # Python >= 2.7
         py.test.raises(TypeError, memoryview, c)
         mv1 = memoryview(buf)
-        assert len(mv1) == len(buf) and mv1[3] == "t"
+        assert len(mv1) == len(buf) and mv1[3] in (b"t", ord(b"t"))
     # --mb_ass_item--
-    expected = list(b"hi there\x00")
+    expected = list(map(bitem2bchr, b"hi there\x00"))
     for i in range(-12, 12):
         try:
-            expected[i] = chr(i & 0xff)
+            expected[i] = bytechr(i & 0xff)
         except IndexError:
-            py.test.raises(IndexError, "buf[i] = chr(i & 0xff)")
+            py.test.raises(IndexError, "buf[i] = bytechr(i & 0xff)")
         else:
-            buf[i] = chr(i & 0xff)
+            buf[i] = bytechr(i & 0xff)
         assert list(buf) == expected
     # --mb_ass_slice--
     buf[:] = b"hi there\x00"
-    assert list(buf) == list(c) == list(b"hi there\x00")
+    assert list(buf) == list(c) == list(map(bitem2bchr, b"hi there\x00"))
     py.test.raises(ValueError, 'buf[:] = b"shorter"')
     py.test.raises(ValueError, 'buf[:] = b"this is much too long!"')
     buf[4:2] = b""   # no effect, but should work
     assert buf[:] == b"hi there\x00"
-    expected = list(b"hi there\x00")
+    expected = list(map(bitem2bchr, b"hi there\x00"))
     x = 0
     for i in range(-12, 12):
         for j in range(-12, 12):
             stop  = j if j >= 0 else j + len(buf)
             start = max(0, min(len(buf), start))
             stop  = max(0, min(len(buf), stop))
-            sample = chr(x & 0xff) * (stop - start)
+            sample = bytechr(x & 0xff) * (stop - start)
             x += 1
             buf[i:j] = sample
-            expected[i:j] = sample
+            expected[i:j] = map(bitem2bchr, sample)
             assert list(buf) == expected
 
 def test_getcname():
     assert len(p) == 4
     assert list(p) == [b"f", b"o", b"o", b"\x00"]
 
+# XXX hack
+if sys.version_info >= (3,):
+    import posix, io
+    posix.fdopen = io.open
+
 def test_FILE():
     if sys.platform == "win32":
         py.test.skip("testing FILE not implemented")
     #
     import posix
     fdr, fdw = posix.pipe()
-    fr1 = posix.fdopen(fdr, 'r', 256)
-    fw1 = posix.fdopen(fdw, 'w', 256)
+    fr1 = posix.fdopen(fdr, 'rb', 256)
+    fw1 = posix.fdopen(fdw, 'wb', 256)
     #
     fw1.write(b"X")
     res = fputs(b"hello world\n", fw1)
     fw1 = posix.fdopen(fdw, 'w')
     #
     e = py.test.raises(TypeError, fputs, b"hello world\n", fw1)
-    assert str(e.value) == ("initializer for ctype 'struct NOT_FILE *' must "
-                            "be a cdata pointer, not file")
+    assert str(e.value).startswith(
+        "initializer for ctype 'struct NOT_FILE *' must "
+        "be a cdata pointer, not ")
 
 def test_FILE_object():
     if sys.platform == "win32":
     res = fputs(b"hello\n", fw1p)
     assert res >= 0
     res = fileno(fw1p)
-    assert res == fdw
+    assert (res == fdw) == (sys.version_info < (3,))
     fw1.close()
     #
     data = posix.read(fdr, 256)