From 97e5f281a7dd4ce7ae0d5a967d8da0623e1e1d56 Mon Sep 17 00:00:00 2001 From: Amaury Forgeot d'Arc Date: Sat, 11 Jul 2009 09:35:13 +0000 Subject: #6358: Merge r73933: Add basic tests for the return value of os.popen().close(). And fix the implementation to make these tests pass with py3k --- Lib/os.py | 8 +++++++- Lib/test/test_popen.py | 7 +++++++ Misc/NEWS | 3 +++ 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/Lib/os.py b/Lib/os.py index 5dce0c1..a59c5df 100644 --- a/Lib/os.py +++ b/Lib/os.py @@ -643,7 +643,13 @@ class _wrap_close: self._proc = proc def close(self): self._stream.close() - return self._proc.wait() << 8 # Shift left to match old behavior + returncode = self._proc.wait() + if returncode == 0: + return None + if name == 'nt': + return returncode + else: + return returncode << 8 # Shift left to match old behavior def __getattr__(self, name): return getattr(self._stream, name) def __iter__(self): diff --git a/Lib/test/test_popen.py b/Lib/test/test_popen.py index d728792..99ad41d 100644 --- a/Lib/test/test_popen.py +++ b/Lib/test/test_popen.py @@ -42,6 +42,13 @@ class PopenTest(unittest.TestCase): ) support.reap_children() + def test_return_code(self): + self.assertEqual(os.popen("exit 0").close(), None) + if os.name == 'nt': + self.assertEqual(os.popen("exit 42").close(), 42) + else: + self.assertEqual(os.popen("exit 42").close(), 42 << 8) + def test_main(): support.run_unittest(PopenTest) diff --git a/Misc/NEWS b/Misc/NEWS index 8b29266..d0f155a 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -38,6 +38,9 @@ C-API Library ------- +- Issue #6358: The exit status of a command started with os.popen() was + reported differently than it did with python 2.x. + - Issue #6323: The pdb debugger did not exit when running a script with a syntax error. -- cgit v0.12