Author: Armin Rigo <armin.r...@gmail.com>
Branch: 
Changeset: r69710:7532271a94d7
Date: 2014-03-05 10:16 +0100
http://bitbucket.org/pypy/pypy/changeset/7532271a94d7/

Log:    Merged in krono/pypy/popen-pclose (pull request #206)

        Provide an exit status for popen'ed RFiles via pclose

diff --git a/rpython/rlib/rfile.py b/rpython/rlib/rfile.py
--- a/rpython/rlib/rfile.py
+++ b/rpython/rlib/rfile.py
@@ -125,7 +125,15 @@
             rffi.free_nonmovingbuffer(value, ll_value)
 
     def close(self):
+        """Closes the described file.
+
+        Attention! Unlike Python semantics, `close' does not return `None' upon
+        success but `0', to be able to return an exit code for popen'ed files.
+
+        The actual return value may be determined with os.WEXITSTATUS.
+        """
         ll_f = self.ll_file
+        res = 0
         if ll_f:
             # double close is allowed
             self.ll_file = lltype.nullptr(FILE)
@@ -133,6 +141,7 @@
             if res == -1:
                 errno = rposix.get_errno()
                 raise OSError(errno, os.strerror(errno))
+        return res
 
     _do_close = staticmethod(c_close)    # overridden in RPopenFile
 
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
@@ -186,7 +186,7 @@
             f.close()
 
 
-class TestPopen:
+class TestPopen(object):
     def setup_class(cls):
         if sys.platform == 'win32':
             py.test.skip("not for win32")
@@ -196,3 +196,42 @@
         s = f.read()
         f.close()
         assert s == '42\n'
+
+    def test_pclose(self):
+        retval = 32
+        printval = 42
+        cmd = "python -c 'import sys; print %s; sys.exit(%s)'" % (
+            printval, retval)
+        f = rfile.create_popen_file(cmd, "r")
+        s = f.read()
+        r = f.close()
+        assert s == "%s\n" % printval
+        assert os.WEXITSTATUS(r) == retval
+
+class TestPopenR(BaseRtypingTest):
+    def setup_class(cls):
+        if sys.platform == 'win32':
+            py.test.skip("not for win32")
+
+    def test_popen(self):
+        printval = 42
+        cmd = "python -c 'print %s'" % printval
+        def f():
+            f = rfile.create_popen_file(cmd, "r")
+            s = f.read()
+            f.close()
+            assert s == "%s\n" % printval
+        self.interpret(f, [])
+
+    def test_pclose(self):
+        printval = 42
+        retval = 32
+        cmd = "python -c 'import sys; print %s; sys.exit(%s)'" % (
+            printval, retval)
+        def f():
+            f = rfile.create_popen_file(cmd, "r")
+            s = f.read()
+            assert s == "%s\n" % printval
+            return f.close()
+        r = self.interpret(f, [])
+        assert os.WEXITSTATUS(r) == retval
_______________________________________________
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to