Author: Brian Kearns <bdkea...@gmail.com>
Branch: use-file-star-for-file
Changeset: r73141:d882ed7c35d1
Date: 2014-08-28 23:27 -0400
http://bitbucket.org/pypy/pypy/changeset/d882ed7c35d1/

Log:    merge default

diff --git a/rpython/rlib/rfile.py b/rpython/rlib/rfile.py
--- a/rpython/rlib/rfile.py
+++ b/rpython/rlib/rfile.py
@@ -21,8 +21,6 @@
     fileno = '_fileno'
 eci = ExternalCompilationInfo(includes=includes)
 
-def llexternal(*args, **kwargs):
-    return rffi.llexternal(*args, compilation_info=eci, **kwargs)
 
 class CConfig(object):
     _compilation_info_ = eci
@@ -36,13 +34,20 @@
 
 config = platform.configure(CConfig)
 
+FILEP = rffi.COpaquePtr("FILE")
 OFF_T = config['off_t']
-FILEP = rffi.COpaquePtr("FILE")
 _IONBF = config['_IONBF']
 _IOLBF = config['_IOLBF']
 _IOFBF = config['_IOFBF']
 BUFSIZ = config['BUFSIZ']
 
+BASE_BUF_SIZE = 4096
+BASE_LINE_SIZE = 100
+
+
+def llexternal(*args, **kwargs):
+    return rffi.llexternal(*args, compilation_info=eci, **kwargs)
+
 c_fopen = llexternal('fopen', [rffi.CCHARP, rffi.CCHARP], FILEP)
 c_fclose = llexternal('fclose', [FILEP], rffi.INT, releasegil=False)
 c_fwrite = llexternal('fwrite', [rffi.CCHARP, rffi.SIZE_T, rffi.SIZE_T,
@@ -69,9 +74,6 @@
 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
-
 
 def _error(ll_file):
     errno = c_ferror(ll_file)
@@ -80,8 +82,6 @@
 
 
 def create_file(filename, mode="r", buffering=-1):
-    assert filename is not None
-    assert mode is not None
     ll_name = rffi.str2charp(filename)
     try:
         ll_mode = rffi.str2charp(mode)
@@ -111,8 +111,8 @@
         raise OSError(errno, os.strerror(errno))
     return RFile(res)
 
+
 def create_fdopen_rfile(fd, mode="r"):
-    assert mode is not None
     ll_mode = rffi.str2charp(mode)
     try:
         ll_f = c_fdopen(rffi.cast(rffi.INT, fd), ll_mode)
@@ -123,6 +123,7 @@
         lltype.free(ll_mode, flavor='raw')
     return RFile(ll_f)
 
+
 def create_popen_file(command, type):
     ll_command = rffi.str2charp(command)
     try:
@@ -143,18 +144,18 @@
     def __init__(self, ll_file):
         self.ll_file = ll_file
 
+    def _check_closed(self):
+        if not self.ll_file:
+            raise ValueError("I/O operation on closed file")
+
     def write(self, value):
-        assert value is not None
-        ll_file = self.ll_file
-        if not ll_file:
-            raise ValueError("I/O operation on closed file")
-        assert value is not None
+        self._check_closed()
         ll_value = rffi.get_nonmovingbuffer(value)
         try:
             # note that since we got a nonmoving buffer, it is either raw
             # or already cannot move, so the arithmetics below are fine
             length = len(value)
-            bytes = c_fwrite(ll_value, 1, length, ll_file)
+            bytes = c_fwrite(ll_value, 1, length, self.ll_file)
             if bytes != length:
                 errno = rposix.get_errno()
                 raise OSError(errno, os.strerror(errno))
@@ -170,11 +171,11 @@
         The actual return value may be determined with os.WEXITSTATUS.
         """
         res = 0
-        ll_f = self.ll_file
-        if ll_f:
+        ll_file = self.ll_file
+        if ll_file:
             # double close is allowed
             self.ll_file = lltype.nullptr(FILEP.TO)
-            res = self._do_close(ll_f)
+            res = self._do_close(ll_file)
             if res == -1:
                 errno = rposix.get_errno()
                 raise OSError(errno, os.strerror(errno))
@@ -184,9 +185,8 @@
 
     def read(self, size=-1):
         # XXX CPython uses a more delicate logic here
+        self._check_closed()
         ll_file = self.ll_file
-        if not ll_file:
-            raise ValueError("I/O operation on closed file")
         if size < 0:
             # read the entire contents
             buf = lltype.malloc(rffi.CCHARP.TO, BASE_BUF_SIZE, flavor='raw')
@@ -214,58 +214,51 @@
             return s
 
     def seek(self, pos, whence=0):
-        ll_file = self.ll_file
-        if not ll_file:
-            raise ValueError("I/O operation on closed file")
-        res = c_fseek(ll_file, pos, whence)
+        self._check_closed()
+        res = c_fseek(self.ll_file, pos, whence)
         if res == -1:
             errno = rposix.get_errno()
             raise OSError(errno, os.strerror(errno))
 
     def fileno(self):
-        if self.ll_file:
-            return intmask(c_fileno(self.ll_file))
-        raise ValueError("I/O operation on closed file")
+        self._check_closed()
+        return intmask(c_fileno(self.ll_file))
 
     def tell(self):
-        if self.ll_file:
-            res = intmask(c_ftell(self.ll_file))
-            if res == -1:
-                errno = rposix.get_errno()
-                raise OSError(errno, os.strerror(errno))
-            return res
-        raise ValueError("I/O operation on closed file")
+        self._check_closed()
+        res = intmask(c_ftell(self.ll_file))
+        if res == -1:
+            errno = rposix.get_errno()
+            raise OSError(errno, os.strerror(errno))
+        return res
 
     def flush(self):
-        if self.ll_file:
-            res = c_fflush(self.ll_file)
-            if res != 0:
-                errno = rposix.get_errno()
-                raise OSError(errno, os.strerror(errno))
-            return
-        raise ValueError("I/O operation on closed file")
+        self._check_closed()
+        res = c_fflush(self.ll_file)
+        if res != 0:
+            errno = rposix.get_errno()
+            raise OSError(errno, os.strerror(errno))
 
     def truncate(self, arg=-1):
-        if self.ll_file:
-            if arg == -1:
-                arg = self.tell()
-            self.flush()
-            res = c_ftruncate(self.fileno(), arg)
-            if res == -1:
-                errno = rposix.get_errno()
-                raise OSError(errno, os.strerror(errno))
-            return
-        raise ValueError("I/O operation on closed file")
+        self._check_closed()
+        if arg == -1:
+            arg = self.tell()
+        self.flush()
+        res = c_ftruncate(self.fileno(), arg)
+        if res == -1:
+            errno = rposix.get_errno()
+            raise OSError(errno, os.strerror(errno))
 
     def __del__(self):
         self.close()
 
     def _readline1(self, raw_buf):
-        result = c_fgets(raw_buf, BASE_LINE_SIZE, self.ll_file)
+        ll_file = self.ll_file
+        result = c_fgets(raw_buf, BASE_LINE_SIZE, ll_file)
         if not result:
-            if c_feof(self.ll_file):   # ok
+            if c_feof(ll_file):   # ok
                 return 0
-            raise _error(self.ll_file)
+            raise _error(ll_file)
         #
         # Assume that fgets() works as documented, and additionally
         # never writes beyond the final \0, which the CPython
@@ -283,23 +276,22 @@
         return strlen
 
     def readline(self):
-        if self.ll_file:
-            with rffi.scoped_alloc_buffer(BASE_LINE_SIZE) as buf:
+        self._check_closed()
+        with rffi.scoped_alloc_buffer(BASE_LINE_SIZE) as buf:
+            c = self._readline1(buf.raw)
+            if c >= 0:
+                return buf.str(c)
+            #
+            # this is the rare case: the line is longer than BASE_LINE_SIZE
+            s = StringBuilder()
+            while True:
+                s.append_charpsize(buf.raw, BASE_LINE_SIZE - 1)
                 c = self._readline1(buf.raw)
                 if c >= 0:
-                    return buf.str(c)
-                #
-                # this is the rare case: the line is longer than BASE_LINE_SIZE
-                s = StringBuilder()
-                while True:
-                    s.append_charpsize(buf.raw, BASE_LINE_SIZE - 1)
-                    c = self._readline1(buf.raw)
-                    if c >= 0:
-                        break
-                #
-                s.append_charpsize(buf.raw, c)
-                return s.build()
-        raise ValueError("I/O operation on closed file")
+                    break
+            #
+            s.append_charpsize(buf.raw, c)
+            return s.build()
 
 
 class RPopenFile(RFile):
_______________________________________________
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to