[pypy-commit] pypy use-file-star-for-file: use fstat for sizing rfile read buffer when possible

bdkearns noreply at buildbot.pypy.org
Thu Sep 11 11:15:13 CEST 2014


Author: Brian Kearns <bdkearns at gmail.com>
Branch: use-file-star-for-file
Changeset: r73454:02830ee6a82d
Date: 2014-09-11 04:54 -0400
http://bitbucket.org/pypy/pypy/changeset/02830ee6a82d/

Log:	use fstat for sizing rfile read buffer when possible

diff --git a/rpython/rlib/rfile.py b/rpython/rlib/rfile.py
--- a/rpython/rlib/rfile.py
+++ b/rpython/rlib/rfile.py
@@ -346,7 +346,22 @@
         return i
 
     def _new_buffersize(self, currentsize):
-        # XXX use fstat if possible
+        ll_file = self._ll_file
+        try:
+            st = os.fstat(c_fileno(ll_file))
+        except OSError:
+            pass
+        else:
+            end = st.st_size
+            try:
+                pos = os.lseek(c_fileno(ll_file), 0, os.SEEK_CUR)
+            except OSError:
+                c_clearerr(ll_file)
+            else:
+                pos = c_ftell(ll_file)
+                if end > pos and pos >= 0:
+                    return currentsize + end - pos + 1
+        # fstat didn't work
         return currentsize + (currentsize >> 3) + 6
 
     def read(self, size=-1):
@@ -367,7 +382,7 @@
             while True:
                 if bytesrequested >= 0:
                     buffersize = bytesrequested - bytesread
-                    assert buffersize >= 0
+                assert buffersize >= 0
                 chunksize = intmask(self._fread(buf, buffersize, ll_file))
                 interrupted = (c_ferror(ll_file) and
                                rposix.get_errno() == errno.EINTR)


More information about the pypy-commit mailing list