-
-
Notifications
You must be signed in to change notification settings - Fork 30.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add context manager for temporary directory #49428
Comments
I noticed that it would be nice to have a temporary directory context I'm not sure if TemporaryDirectory is the best name. Also, perhaps it |
A __del__ method is definitely desirable (tempfile._TemporaryFileWrapper The new examples are good, but may give the misleading impression that There is also at least one existing contextlib based temp_dir context |
New version of the patch. Added a __del__ method that hopefully works |
If you look at the code for os.path.isdir, it uses other globals such as |
Argh, it's like pulling a sweater thread. The stat.S_ISDIR function Maybe there should be a special flag modules can set to make them get |
Or, perhaps we could remove the assignment to None and use a |
Unassigning for the moment - I'm still interested in the idea, but the I'll still try to get to it before the next 3.1 alpha in a couple of |
Building a collection of issues I want to take a look at for 2.7 |
Missed the boat for 2.7 I'm afraid. Definitely one to take another look at for 3.2 though. |
Ping... soon it's too late for 3.2 too :) |
Personally I would like to have a unique tempfile.mkdtemp which can be used as both a function and a context manager, as it happens for open() and others. |
Committed (with enhanced tests and a few fixes) in r85818 And credit where it's due to test___all__ for picking up a typo in my adjustment to tempfile.__all__ :) I created issue bpo-10188 to track the shutdown problem. I'm considering taking out the code that attempts to allow the __del__ method to work at shutdown though, since it isn't actually achieving anything (I wanted a record of it in the main repository, which is why I kept it for the initial checkin). |
Nick, could you comment about my last proposal? |
Merging the interfaces for mkdtemp and TemporaryDirectory isn't going to happen. mkstemp/mkdtemp are for when the user wants to control the lifecycle of the filesystem entries themselves. (Note that context management on a regular file-like object only closes the file, it doesn't delete it from the filesystem). They're also intended as relatively thin wrappers around the corresponding C standard library functionality. The other objects in tempfile (TemporaryFile, TemporaryDirectory, etc) are for when the user wants the lifecycle of the Python object to correspond with the lifecycle of the underlying filesystem element. That said, TD itself can be used to create the temporary directory without having to use it as a context manager (the underlying directory is created in __init__, not __enter__). |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: