Merged revisions 85987 via svnmerge from

svn+ssh://pythondev@svn.python.org/python/branches/py3k

........
  r85987 | brian.curtin | 2010-10-30 16:24:21 -0500 (Sat, 30 Oct 2010) | 2 lines

  Fix #10257. Clear resource warnings by using os.popen's context manager.
........
This commit is contained in:
Brian Curtin 2010-10-30 21:27:07 +00:00
parent cf9ca7f259
commit 32105f401d
1 changed files with 10 additions and 8 deletions

View File

@ -388,17 +388,19 @@ class EnvironTests(mapping_tests.BasicTestMappingProtocol):
os.environ.clear() os.environ.clear()
if os.path.exists("/bin/sh"): if os.path.exists("/bin/sh"):
os.environ.update(HELLO="World") os.environ.update(HELLO="World")
value = os.popen("/bin/sh -c 'echo $HELLO'").read().strip() with os.popen("/bin/sh -c 'echo $HELLO'") as popen:
self.assertEquals(value, "World") value = popen.read().strip()
self.assertEquals(value, "World")
def test_os_popen_iter(self): def test_os_popen_iter(self):
if os.path.exists("/bin/sh"): if os.path.exists("/bin/sh"):
popen = os.popen("/bin/sh -c 'echo \"line1\nline2\nline3\"'") with os.popen(
it = iter(popen) "/bin/sh -c 'echo \"line1\nline2\nline3\"'") as popen:
self.assertEquals(next(it), "line1\n") it = iter(popen)
self.assertEquals(next(it), "line2\n") self.assertEquals(next(it), "line1\n")
self.assertEquals(next(it), "line3\n") self.assertEquals(next(it), "line2\n")
self.assertRaises(StopIteration, next, it) self.assertEquals(next(it), "line3\n")
self.assertRaises(StopIteration, next, it)
# Verify environ keys and values from the OS are of the # Verify environ keys and values from the OS are of the
# correct str type. # correct str type.