Author: Brian Kearns <bdkea...@gmail.com>
Branch: 
Changeset: r73425:500069cd3e5f
Date: 2014-09-10 18:57 -0400
http://bitbucket.org/pypy/pypy/changeset/500069cd3e5f/

Log:    support stdin/stdout/stderr in rfile

diff --git a/rpython/rlib/rfile.py b/rpython/rlib/rfile.py
--- a/rpython/rlib/rfile.py
+++ b/rpython/rlib/rfile.py
@@ -38,6 +38,7 @@
 
 FILEP = rffi.COpaquePtr("FILE")
 OFF_T = config['off_t']
+
 _IONBF = config['_IONBF']
 _IOLBF = config['_IOLBF']
 _IOFBF = config['_IOFBF']
@@ -95,6 +96,10 @@
 c_ferror = llexternal('ferror', [FILEP], rffi.INT)
 c_clearerr = llexternal('clearerr', [FILEP], lltype.Void)
 
+c_stdin = rffi.CExternVariable(FILEP, 'stdin', eci, c_type='FILE*')[0]
+c_stdout = rffi.CExternVariable(FILEP, 'stdout', eci, c_type='FILE*')[0]
+c_stderr = rffi.CExternVariable(FILEP, 'stderr', eci, c_type='FILE*')[0]
+
 
 def _error(ll_file):
     err = c_ferror(ll_file)
@@ -199,6 +204,14 @@
     return RFile(ll_file, close2=_pclose2)
 
 
+def create_stdio():
+    close2 = [None, None]
+    stdin = RFile(c_stdin(), close2=close2)
+    stdout = RFile(c_stdout(), close2=close2)
+    stderr = RFile(c_stderr(), close2=close2)
+    return stdin, stdout, stderr
+
+
 class RFile(object):
     _univ_newline = False
     _newlinetypes = NEWLINE_UNKNOWN
@@ -218,7 +231,8 @@
         ll_file = self._ll_file
         if ll_file:
             do_close = self._close2[1]
-            do_close(ll_file)       # return value ignored
+            if do_close:
+                do_close(ll_file)       # return value ignored
 
     def _cleanup_(self):
         self._ll_file = lltype.nullptr(FILEP.TO)
@@ -237,10 +251,11 @@
             # double close is allowed
             self._ll_file = lltype.nullptr(FILEP.TO)
             do_close = self._close2[0]
-            res = do_close(ll_file)
-            if res == -1:
-                errno = rposix.get_errno()
-                raise IOError(errno, os.strerror(errno))
+            if do_close:
+                res = do_close(ll_file)
+                if res == -1:
+                    errno = rposix.get_errno()
+                    raise IOError(errno, os.strerror(errno))
         return res
 
     def _check_closed(self):
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
@@ -351,6 +351,13 @@
         cls.tmpdir = udir.join('test_rfile_direct')
         cls.tmpdir.ensure(dir=True)
 
+    def test_stdio(self):
+        i, o, e = rfile.create_stdio()
+        o.write("test\n")
+        i.close()
+        o.close()
+        e.close()
+
     def test_auto_close(self):
         fname = str(self.tmpdir.join('file_auto_close'))
         f = rfile.create_file(fname, 'w')
_______________________________________________
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to