diff options
author | Antoine Pitrou <solipsis@pitrou.net> | 2010-10-29 10:38:18 (GMT) |
---|---|---|
committer | Antoine Pitrou <solipsis@pitrou.net> | 2010-10-29 10:38:18 (GMT) |
commit | e033e06db077d5abcb4bc3729d03f8a4a09b2486 (patch) | |
tree | 04445ffa669d4d0df240d680249c7d7a7f661bd4 /Lib/test/test_io.py | |
parent | 9cbdd75ec5deda8f55edd7caab42dff65d009da2 (diff) | |
download | cpython-e033e06db077d5abcb4bc3729d03f8a4a09b2486.zip cpython-e033e06db077d5abcb4bc3729d03f8a4a09b2486.tar.gz cpython-e033e06db077d5abcb4bc3729d03f8a4a09b2486.tar.bz2 |
Issue #10093: ResourceWarnings are now issued when files and sockets are
deallocated without explicit closing. These warnings are silenced by
default, except in pydebug mode.
Diffstat (limited to 'Lib/test/test_io.py')
-rw-r--r-- | Lib/test/test_io.py | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/Lib/test/test_io.py b/Lib/test/test_io.py index 8784e34..7ce9753 100644 --- a/Lib/test/test_io.py +++ b/Lib/test/test_io.py @@ -29,6 +29,7 @@ import weakref import abc import signal import errno +import warnings from itertools import cycle, count from collections import deque from test import support @@ -2525,6 +2526,46 @@ class MiscIOTest(unittest.TestCase): # baseline "io" module. self._check_abc_inheritance(io) + def _check_warn_on_dealloc(self, *args, **kwargs): + f = open(*args, **kwargs) + r = repr(f) + with self.assertWarns(ResourceWarning) as cm: + f = None + support.gc_collect() + self.assertIn(r, str(cm.warning.args[0])) + + def test_warn_on_dealloc(self): + self._check_warn_on_dealloc(support.TESTFN, "wb", buffering=0) + self._check_warn_on_dealloc(support.TESTFN, "wb") + self._check_warn_on_dealloc(support.TESTFN, "w") + + def _check_warn_on_dealloc_fd(self, *args, **kwargs): + fds = [] + try: + r, w = os.pipe() + fds += r, w + self._check_warn_on_dealloc(r, *args, **kwargs) + # When using closefd=False, there's no warning + r, w = os.pipe() + fds += r, w + with warnings.catch_warnings(record=True) as recorded: + open(r, *args, closefd=False, **kwargs) + support.gc_collect() + self.assertEqual(recorded, []) + finally: + for fd in fds: + try: + os.close(fd) + except EnvironmentError as e: + if e.errno != errno.EBADF: + raise + + def test_warn_on_dealloc_fd(self): + self._check_warn_on_dealloc_fd("rb", buffering=0) + self._check_warn_on_dealloc_fd("rb") + self._check_warn_on_dealloc_fd("r") + + class CMiscIOTest(MiscIOTest): io = io |