[Python-checkins] cpython (merge 3.4 -> default): Issue #22427: TemporaryDirectory no longer attempts to clean up twice when

serhiy.storchaka python-checkins at python.org
Wed Sep 24 12:31:13 CEST 2014


https://hg.python.org/cpython/rev/e9d4288c32de
changeset:   92560:e9d4288c32de
parent:      92557:de645efe6a9b
parent:      92559:7ea2153eae87
user:        Serhiy Storchaka <storchaka at gmail.com>
date:        Wed Sep 24 13:29:27 2014 +0300
summary:
  Issue #22427: TemporaryDirectory no longer attempts to clean up twice when
used in the with statement in generator.

files:
  Lib/tempfile.py           |  15 +++------------
  Lib/test/test_tempfile.py |  24 ++++++++++++++++++++++++
  Misc/NEWS                 |   3 +++
  3 files changed, 30 insertions(+), 12 deletions(-)


diff --git a/Lib/tempfile.py b/Lib/tempfile.py
--- a/Lib/tempfile.py
+++ b/Lib/tempfile.py
@@ -689,11 +689,6 @@
     in it are removed.
     """
 
-    # Handle mkdtemp raising an exception
-    name = None
-    _finalizer = None
-    _closed = False
-
     def __init__(self, suffix="", prefix=template, dir=None):
         self.name = mkdtemp(suffix, prefix, dir)
         self._finalizer = _weakref.finalize(
@@ -701,10 +696,9 @@
             warn_message="Implicitly cleaning up {!r}".format(self))
 
     @classmethod
-    def _cleanup(cls, name, warn_message=None):
+    def _cleanup(cls, name, warn_message):
         _shutil.rmtree(name)
-        if warn_message is not None:
-            _warnings.warn(warn_message, ResourceWarning)
+        _warnings.warn(warn_message, ResourceWarning)
 
 
     def __repr__(self):
@@ -717,8 +711,5 @@
         self.cleanup()
 
     def cleanup(self):
-        if self._finalizer is not None:
-            self._finalizer.detach()
-        if self.name is not None and not self._closed:
+        if self._finalizer.detach():
             _shutil.rmtree(self.name)
-            self._closed = True
diff --git a/Lib/test/test_tempfile.py b/Lib/test/test_tempfile.py
--- a/Lib/test/test_tempfile.py
+++ b/Lib/test/test_tempfile.py
@@ -1211,6 +1211,30 @@
                 self.assertNotIn("Exception ", err)
                 self.assertIn("ResourceWarning: Implicitly cleaning up", err)
 
+    def test_exit_on_shutdown(self):
+        # Issue #22427
+        with self.do_create() as dir:
+            code = """if True:
+                import sys
+                import tempfile
+                import warnings
+
+                def generator():
+                    with tempfile.TemporaryDirectory(dir={dir!r}) as tmp:
+                        yield tmp
+                g = generator()
+                sys.stdout.buffer.write(next(g).encode())
+
+                warnings.filterwarnings("always", category=ResourceWarning)
+                """.format(dir=dir)
+            rc, out, err = script_helper.assert_python_ok("-c", code)
+            tmp_name = out.decode().strip()
+            self.assertFalse(os.path.exists(tmp_name),
+                        "TemporaryDirectory %s exists after cleanup" % tmp_name)
+            err = err.decode('utf-8', 'backslashreplace')
+            self.assertNotIn("Exception ", err)
+            self.assertIn("ResourceWarning: Implicitly cleaning up", err)
+
     def test_warnings_on_cleanup(self):
         # ResourceWarning will be triggered by __del__
         with self.do_create() as dir:
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -137,6 +137,9 @@
 Library
 -------
 
+- Issue #22427: TemporaryDirectory no longer attempts to clean up twice when
+  used in the with statement in generator.
+
 - Issue #22362: Forbidden ambiguous octal escapes out of range 0-0o377 in
   regular expressions.
 

-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list