[Python-checkins] cpython (merge 3.4 -> default): (Merge 3.4) Issue #11453: asyncore: emit a ResourceWarning when an unclosed

victor.stinner python-checkins at python.org
Fri Jun 27 23:57:30 CEST 2014


http://hg.python.org/cpython/rev/7c9335d97628
changeset: 91451:7c9335d97628
parent: 91449:f1cd0aa1561a
parent: 91450:ae12a926e680
user: Victor Stinner <victor.stinner at gmail.com>
date: Fri Jun 27 23:57:19 2014 +0200
summary:
 (Merge 3.4) Issue #11453: asyncore: emit a ResourceWarning when an unclosed
file_wrapper object is destroyed. The destructor now closes the file if needed.
The close() method can now be called twice: the second call does nothing.
files:
 Lib/asyncore.py | 8 ++++++++
 Lib/test/test_asyncore.py | 16 ++++++++++++++++
 Misc/NEWS | 4 ++++
 3 files changed, 28 insertions(+), 0 deletions(-)
diff --git a/Lib/asyncore.py b/Lib/asyncore.py
--- a/Lib/asyncore.py
+++ b/Lib/asyncore.py
@@ -600,6 +600,11 @@
 def __init__(self, fd):
 self.fd = os.dup(fd)
 
+ def __del__(self):
+ if self.fd >= 0:
+ warnings.warn("unclosed file %r" % self, ResourceWarning)
+ self.close()
+
 def recv(self, *args):
 return os.read(self.fd, *args)
 
@@ -618,7 +623,10 @@
 write = send
 
 def close(self):
+ if self.fd < 0:
+ return
 os.close(self.fd)
+ self.fd = -1
 
 def fileno(self):
 return self.fd
diff --git a/Lib/test/test_asyncore.py b/Lib/test/test_asyncore.py
--- a/Lib/test/test_asyncore.py
+++ b/Lib/test/test_asyncore.py
@@ -413,6 +413,22 @@
 asyncore.loop(timeout=0.01, use_poll=True, count=2)
 self.assertEqual(b"".join(data), self.d)
 
+ def test_resource_warning(self):
+ # Issue #11453
+ fd = os.open(support.TESTFN, os.O_RDONLY)
+ f = asyncore.file_wrapper(fd)
+ with support.check_warnings(('', ResourceWarning)):
+ f = None
+ support.gc_collect()
+
+ def test_close_twice(self):
+ fd = os.open(support.TESTFN, os.O_RDONLY)
+ f = asyncore.file_wrapper(fd)
+ f.close()
+ self.assertEqual(f.fd, -1)
+ # calling close twice should not fail
+ f.close()
+
 
 class BaseTestHandler(asyncore.dispatcher):
 
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -103,6 +103,10 @@
 Library
 -------
 
+- Issue #11453: asyncore: emit a ResourceWarning when an unclosed file_wrapper
+ object is destroyed. The destructor now closes the file if needed. The
+ close() method can now be called twice: the second call does nothing.
+
 - Issue #21858: Better handling of Python exceptions in the sqlite3 module.
 
 - Issue #21476: Make sure the email.parser.BytesParser TextIOWrapper is
-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list

AltStyle によって変換されたページ (->オリジナル) /