[pypy-commit] pypy default: test/fix rfile buffering

bdkearns noreply at buildbot.pypy.org
Fri Aug 29 03:41:18 CEST 2014


Author: Brian Kearns <bdkearns at gmail.com>
Branch: 
Changeset: r73135:7445b0d7890d
Date: 2014-08-28 18:05 -0400
http://bitbucket.org/pypy/pypy/changeset/7445b0d7890d/

Log:	test/fix rfile buffering

diff --git a/rpython/rlib/rfile.py b/rpython/rlib/rfile.py
--- a/rpython/rlib/rfile.py
+++ b/rpython/rlib/rfile.py
@@ -29,10 +29,19 @@
 
     off_t = platform.SimpleType('off_t')
 
+    _IONBF = platform.DefinedConstantInteger('_IONBF')
+    _IOLBF = platform.DefinedConstantInteger('_IOLBF')
+    _IOFBF = platform.DefinedConstantInteger('_IOFBF')
+    BUFSIZ = platform.DefinedConstantInteger('BUFSIZ')
+
 config = platform.configure(CConfig)
 
 OFF_T = config['off_t']
 FILEP = rffi.COpaquePtr("FILE")
+_IONBF = config['_IONBF']
+_IOLBF = config['_IOLBF']
+_IOFBF = config['_IOFBF']
+BUFSIZ = config['BUFSIZ']
 
 c_fopen = llexternal('fopen', [rffi.CCHARP, rffi.CCHARP], FILEP)
 c_fclose = llexternal('fclose', [FILEP], rffi.INT, releasegil=False)
@@ -58,6 +67,7 @@
 
 c_popen = llexternal('popen', [rffi.CCHARP, rffi.CCHARP], FILEP)
 c_pclose = llexternal('pclose', [FILEP], rffi.INT, releasegil=False)
+c_setvbuf = llexternal('setvbuf', [FILEP, rffi.CCHARP, rffi.INT, rffi.SIZE_T], rffi.INT)
 
 BASE_BUF_SIZE = 4096
 BASE_LINE_SIZE = 100
@@ -70,7 +80,6 @@
 
 
 def create_file(filename, mode="r", buffering=-1):
-    assert buffering == -1
     assert filename is not None
     assert mode is not None
     ll_name = rffi.str2charp(filename)
@@ -85,6 +94,13 @@
             lltype.free(ll_mode, flavor='raw')
     finally:
         lltype.free(ll_name, flavor='raw')
+    if buffering >= 0:
+        if buffering == 0:
+            c_setvbuf(ll_f, lltype.nullptr(rffi.CCHARP.TO), _IONBF, 0)
+        elif buffering == 1:
+            c_setvbuf(ll_f, lltype.nullptr(rffi.CCHARP.TO), _IOLBF, BUFSIZ)
+        else:
+            c_setvbuf(ll_f, lltype.nullptr(rffi.CCHARP.TO), _IOFBF, buffering)
     return RFile(ll_f)
 
 
diff --git a/rpython/rlib/test/test_rfile.py b/rpython/rlib/test/test_rfile.py
--- a/rpython/rlib/test/test_rfile.py
+++ b/rpython/rlib/test/test_rfile.py
@@ -20,6 +20,38 @@
         self.interpret(f, [])
         assert open(fname, "r").read() == "dupa"
 
+    def test_open_buffering_line(self):
+        fname = str(self.tmpdir.join('file_1a'))
+
+        def f():
+            f = open(fname, 'w', 1)
+            f.write('dupa\ndupb')
+            f2 = open(fname, 'r')
+            assert f2.read() == 'dupa\n'
+            f.close()
+            assert f2.read() == 'dupb'
+            f2.close()
+
+        f()
+        self.interpret(f, [])
+
+    def test_open_buffering_full(self):
+        fname = str(self.tmpdir.join('file_1b'))
+
+        def f():
+            f = open(fname, 'w', 128)
+            f.write('dupa')
+            f2 = open(fname, 'r')
+            assert f2.read() == ''
+            f.write('z' * 5000)
+            assert f2.read() != ''
+            f.close()
+            assert f2.read() != ''
+            f2.close()
+
+        f()
+        self.interpret(f, [])
+
     def test_read_write(self):
         fname = str(self.tmpdir.join('file_2'))
 


More information about the pypy-commit mailing list