[pypy-svn] r15042 - pypy/dist/pypy/lib

rxe at codespeak.net rxe at codespeak.net
Mon Jul 25 17:23:52 CEST 2005


Author: rxe
Date: Mon Jul 25 17:23:51 2005
New Revision: 15042

Modified:
   pypy/dist/pypy/lib/_file.py
Log:

Added classmethod fdopen() and factored out the constructor for common code.
(hpk/rxe)
 


Modified: pypy/dist/pypy/lib/_file.py
==============================================================================
--- pypy/dist/pypy/lib/_file.py	(original)
+++ pypy/dist/pypy/lib/_file.py	Mon Jul 25 17:23:51 2005
@@ -79,12 +79,22 @@
     """
 
     def __init__(self, name, mode='r', bufsize=None):
-        self._mode = mode
+        self.fd = None
         self._name = name
-        self._closed = True   # Until the file is successfully opened
-        self.softspace = 0    # Required according to file object docs
-        self.encoding = None  # This is not used internally by file objects
-
+        self._inithelper(mode, bufsize)
+        
+    def fdopen(cls, fd, mode='r', bufsize=None):
+        f = cls.__new__(cls)
+
+        f.fd = fd
+        f._name = "<fdopen>"
+        f._inithelper(mode, bufsize)
+        return f
+
+    fdopen = classmethod(fdopen)
+        
+    def _inithelper(self, mode, bufsize):
+        self._mode = mode
         if not mode or mode[0] not in ['r', 'w', 'a', 'U']:
             raise IOError('invalid mode : %s' % mode)
 
@@ -110,19 +120,25 @@
         if binary or universal:
             flag |= O_BINARY
 
-        self.fd = os.open(name, flag)
+        if self.fd is None:
+            self.fd = os.open(self.name, flag)
         if basemode == 'a':
             try:
                 os.lseek(self.fd, 0, 2)
             except OSError:
                 pass
+            
+        reading = basemode == 'r' or plus
+        writing = basemode != 'r' or plus
 
+        self._closed = True   # Until the file is successfully opened
+
+        self.softspace = 0    # Required according to file object docs
+        self.encoding = None  # This is not used internally by file objects
+        
         self.stream = _sio.DiskFile(self.fd)
         self._closed = False
 
-        reading = basemode == 'r' or plus
-        writing = basemode != 'r' or plus
-
         if bufsize == 0:   # no buffering
             pass
         elif bufsize == 1:   # line-buffering



More information about the Pypy-commit mailing list