
Author: brian.curtin Date: Sat Oct 30 23:24:21 2010 New Revision: 85987 Log: Fix #10257. Clear resource warnings by using os.popen's context manager. Modified: python/branches/py3k/Lib/test/test_os.py Modified: python/branches/py3k/Lib/test/test_os.py ============================================================================== --- python/branches/py3k/Lib/test/test_os.py (original) +++ python/branches/py3k/Lib/test/test_os.py Sat Oct 30 23:24:21 2010 @@ -406,17 +406,19 @@ os.environ.clear() if os.path.exists("/bin/sh"): os.environ.update(HELLO="World") - value = os.popen("/bin/sh -c 'echo $HELLO'").read().strip() - self.assertEquals(value, "World") + with os.popen("/bin/sh -c 'echo $HELLO'") as popen: + value = popen.read().strip() + self.assertEquals(value, "World") def test_os_popen_iter(self): if os.path.exists("/bin/sh"): - popen = os.popen("/bin/sh -c 'echo \"line1\nline2\nline3\"'") - it = iter(popen) - self.assertEquals(next(it), "line1\n") - self.assertEquals(next(it), "line2\n") - self.assertEquals(next(it), "line3\n") - self.assertRaises(StopIteration, next, it) + with os.popen( + "/bin/sh -c 'echo \"line1\nline2\nline3\"'") as popen: + it = iter(popen) + self.assertEquals(next(it), "line1\n") + self.assertEquals(next(it), "line2\n") + self.assertEquals(next(it), "line3\n") + self.assertRaises(StopIteration, next, it) # Verify environ keys and values from the OS are of the # correct str type.

On 2010/10/31 6:24, brian.curtin wrote:
Author: brian.curtin Date: Sat Oct 30 23:24:21 2010 New Revision: 85987
Log: Fix #10257. Clear resource warnings by using os.popen's context manager.
Modified: python/branches/py3k/Lib/test/test_os.py
Modified: python/branches/py3k/Lib/test/test_os.py ============================================================================== --- python/branches/py3k/Lib/test/test_os.py (original) +++ python/branches/py3k/Lib/test/test_os.py Sat Oct 30 23:24:21 2010 @@ -406,17 +406,19 @@ os.environ.clear() if os.path.exists("/bin/sh"): os.environ.update(HELLO="World") - value = os.popen("/bin/sh -c 'echo $HELLO'").read().strip() - self.assertEquals(value, "World") + with os.popen("/bin/sh -c 'echo $HELLO'") as popen: + value = popen.read().strip() + self.assertEquals(value, "World")
Does this really cause resource warning? I think os.popen instance won't be into traceback because it's not declared as variable. So I suppose it will be deleted by reference count == 0 even when exception occurs.

On Tue, Nov 2, 2010 at 2:10 AM, Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp> wrote:
Does this really cause resource warning? I think os.popen instance won't be into traceback because it's not declared as variable. So I suppose it will be deleted by reference count == 0 even when exception occurs.
Any time __del__ has to close the resource triggers ResourceWarning, regardless of whether that is due to the cyclic garbage collector or the refcount naturally falling to zero. In the past dealing with this was clumsy, so it made sense to rely on CPython's refcounting to do the work. However, we have better tools for deterministic resource management now (in the form of context managers), so these updates help make the standard library and its test suite more suitable for use with non-refcounting Python implementations (such as PyPy, Jython and IronPython). Cheers, Nick. -- Nick Coghlan | ncoghlan@gmail.com | Brisbane, Australia

On 2010/11/02 1:30, Nick Coghlan wrote:
On Tue, Nov 2, 2010 at 2:10 AM, Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp> wrote:
Does this really cause resource warning? I think os.popen instance won't be into traceback because it's not declared as variable. So I suppose it will be deleted by reference count == 0 even when exception occurs.
Any time __del__ has to close the resource triggers ResourceWarning, regardless of whether that is due to the cyclic garbage collector or the refcount naturally falling to zero. In the past dealing with this was clumsy, so it made sense to rely on CPython's refcounting to do the work. However, we have better tools for deterministic resource management now (in the form of context managers), so these updates help make the standard library and its test suite more suitable for use with non-refcounting Python implementations (such as PyPy, Jython and IronPython).
Cheers, Nick.
Thank you for reply. Probably this is difficult problem. I often use with statement, but it's also true sometimes I feel this warning is a bit noisy. Is there a way to turn this off? C:\Documents and Settings\Ocean>py3k Python 3.2a3+ (py3k, Nov 3 2010, 00:27:28) [MSC v.1200 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information.
open("a.py").read() __main__:1: ResourceWarning: unclosed file <_io.TextIOWrapper name='a.py' encodi ng='cp932'> '\nimport timeit\n\nt = timeit.Timer("""\nos.stat("e:/voltest/lnk")\n""", """\ni mport os\n""")\n\nprint(t.timeit(1000))\n\n' [49593 refs]
participants (3)
-
brian.curtin
-
Hirokazu Yamamoto
-
Nick Coghlan