cpython: 50aa9e3ab9a4 (original) (raw)

Mercurial > cpython

changeset 88771:50aa9e3ab9a4 3.3

Issue #19077: tempfile.TemporaryDirectory cleanup is now most likely successful when called during nulling out of modules during shutdown. Misleading exception no longer raised when resource warning is emitted during shutdown. [#19077]

Serhiy Storchaka storchaka@gmail.com
date Mon, 27 Jan 2014 11🔞27 +0200
parents 791b69f9f96d
children d792eb3afa58 f4377699fd47
files Lib/tempfile.py Lib/test/test_tempfile.py Misc/NEWS
diffstat 3 files changed, 88 insertions(+), 96 deletions(-)[+] [-] Lib/tempfile.py 95 Lib/test/test_tempfile.py 84 Misc/NEWS 5

line wrap: on

line diff

--- a/Lib/tempfile.py +++ b/Lib/tempfile.py @@ -27,11 +27,12 @@ This module also provides some data item

Imports.

+import atexit as _atexit import functools as _functools import warnings as _warnings -import sys as _sys import io as _io import os as _os +import shutil as _shutil import errno as _errno from random import Random as _Random @@ -355,10 +356,13 @@ class _TemporaryFileCloser: underlying file object, without adding a del method to the temporary file."""

+ def init(self, file, name, delete=True): self.file = file self.name = name

# NT provides delete-on-close as a primitive, so we don't need @@ -370,14 +374,13 @@ class _TemporaryFileCloser: # that this must be referenced as self.unlink, because the # name TemporaryFileWrapper may also get None'd out before # del is called.

# Need to ensure the file is deleted on del def del(self): @@ -677,9 +680,11 @@ class TemporaryDirectory(object): in it are removed. """

+ def init(self, suffix="", prefix=template, dir=None):

def repr(self): @@ -688,23 +693,24 @@ class TemporaryDirectory(object): def enter(self): return self.name

def exit(self, exc, value, tb): self.cleanup() @@ -713,36 +719,27 @@ class TemporaryDirectory(object): # Issue a ResourceWarning if implicit cleanup needed self.cleanup(_warn=True)

-

+ +_is_running = True + +def _on_shutdown():

+ +_atexit.register(_on_shutdown)

--- a/Lib/test/test_tempfile.py +++ b/Lib/test/test_tempfile.py @@ -11,7 +11,7 @@ import contextlib import weakref import unittest -from test import support +from test import support, script_helper if hasattr(os, 'stat'): @@ -1073,7 +1073,8 @@ class TestTemporaryDirectory(BaseTestCas self.nameCheck(tmp.name, dir, pre, suf) # Create a subdirectory and some files if recurse:

@@ -1105,7 +1106,7 @@ class TestTemporaryDirectory(BaseTestCas def test_cleanup_with_symlink_to_a_directory(self): # cleanup() should not follow symlinks to directories (issue #12464) d1 = self.do_create()

# Symlink d1/foo -> d2 os.symlink(d2.name, os.path.join(d1.name, "foo")) @@ -1135,60 +1136,49 @@ class TestTemporaryDirectory(BaseTestCas finally: os.rmdir(dir)

+

+

+

+

def test_warnings_on_cleanup(self):

-

# Check for the resource warning with support.check_warnings(('Implicitly', ResourceWarning), quiet=False): warnings.filterwarnings("always", category=ResourceWarning)

def test_multiple_close(self): # Can be cleaned-up many times without error

--- a/Misc/NEWS +++ b/Misc/NEWS @@ -50,6 +50,11 @@ Core and Builtins Library ------- +- Issue #19077: tempfile.TemporaryDirectory cleanup is now most likely