Author: Brian Kearns <bdkea...@gmail.com>
Branch: use-file-star-for-file
Changeset: r73125:be91fae57988
Date: 2014-08-28 18:05 -0400
http://bitbucket.org/pypy/pypy/changeset/be91fae57988/

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
@@ -32,6 +32,7 @@
     _IONBF = platform.DefinedConstantInteger('_IONBF')
     _IOLBF = platform.DefinedConstantInteger('_IOLBF')
     _IOFBF = platform.DefinedConstantInteger('_IOFBF')
+    BUFSIZ = platform.DefinedConstantInteger('BUFSIZ')
 
 config = platform.configure(CConfig)
 
@@ -40,6 +41,7 @@
 _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)
@@ -65,7 +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], 
lltype.Void)
+c_setvbuf = llexternal('setvbuf', [FILEP, rffi.CCHARP, rffi.INT, rffi.SIZE_T], 
rffi.INT)
 
 BASE_BUF_SIZE = 4096
 BASE_LINE_SIZE = 100
@@ -92,8 +94,13 @@
             lltype.free(ll_mode, flavor='raw')
     finally:
         lltype.free(ll_name, flavor='raw')
-    if buffering != -1:
-        c_setvbuf(ll_f, lltype.nullptr(rffi.CCHARP.TO), _IOFBF, buffering)
+    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,18 +20,36 @@
         self.interpret(f, [])
         assert open(fname, "r").read() == "dupa"
 
-    def test_open_buffering(self):
+    def test_open_buffering_line(self):
         fname = str(self.tmpdir.join('file_1a'))
 
         def f():
-            f = open(fname, 'w', 3)
+            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()
-            f2 = open(fname, 'r')
-            assert f2.read() == 'dupa'
+            assert f2.read() != ''
+            f2.close()
 
+        f()
         self.interpret(f, [])
 
     def test_read_write(self):
_______________________________________________
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to