[pypy-commit] pypy default: merge

fijal noreply at buildbot.pypy.org
Wed Oct 31 15:11:05 CET 2012


Author: Maciej Fijalkowski <fijall at gmail.com>
Branch: 
Changeset: r58639:1fa8fb721755
Date: 2012-10-31 16:10 +0200
http://bitbucket.org/pypy/pypy/changeset/1fa8fb721755/

Log:	merge

diff --git a/pypy/module/_cffi_backend/ctypeptr.py b/pypy/module/_cffi_backend/ctypeptr.py
--- a/pypy/module/_cffi_backend/ctypeptr.py
+++ b/pypy/module/_cffi_backend/ctypeptr.py
@@ -323,6 +323,7 @@
 
 
 rffi_fdopen = rffi.llexternal("fdopen", [rffi.INT, rffi.CCHARP], rffi.CCHARP)
+rffi_setbuf = rffi.llexternal("setbuf", [rffi.CCHARP,rffi.CCHARP], lltype.Void)
 rffi_fclose = rffi.llexternal("fclose", [rffi.CCHARP], rffi.INT)
 
 class CffiFileObj(object):
@@ -331,6 +332,7 @@
         self.llf = rffi_fdopen(fd, mode)
         if not self.llf:
             raise OSError(rposix.get_errno(), "fdopen failed")
+        rffi_setbuf(self.llf, lltype.nullptr(rffi.CCHARP.TO))
     def close(self):
         rffi_fclose(self.llf)
 
diff --git a/pypy/module/_cffi_backend/test/_backend_test_c.py b/pypy/module/_cffi_backend/test/_backend_test_c.py
--- a/pypy/module/_cffi_backend/test/_backend_test_c.py
+++ b/pypy/module/_cffi_backend/test/_backend_test_c.py
@@ -6,6 +6,7 @@
     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')
@@ -19,6 +20,7 @@
     mandatory_b_prefix = 'b'
     mandatory_u_prefix = ''
     bytechr = lambda n: bytes([n])
+    bitem2bchr = bytechr
     u = ""
 
 def size_of_int():
@@ -1804,7 +1806,10 @@
     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()
@@ -1826,7 +1831,7 @@
         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):
@@ -1835,33 +1840,34 @@
         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):
@@ -1869,10 +1875,10 @@
             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():
@@ -2245,6 +2251,11 @@
     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")
@@ -2262,19 +2273,20 @@
     #
     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)
     assert res >= 0
-    fw1.close()
+    fw1.flush()     # should not be needed
     #
     p = newp(new_array_type(BCharP, 100), None)
     res = fscanf(fr1, b"%s\n", p)
     assert res == 1
     assert string(p) == b"Xhello"
     fr1.close()
+    fw1.close()
 
 def test_FILE_only_for_FILE_arg():
     if sys.platform == "win32":
@@ -2295,8 +2307,9 @@
     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":
@@ -2323,7 +2336,7 @@
     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)


More information about the pypy-commit mailing list