-
-
Notifications
You must be signed in to change notification settings - Fork 31.9k
tempfile.mkdtemp() does not return absolute pathname when relative dir is specified #51574
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
Comments
The docs (http://www.python.org/doc/2.5.1/lib/module-tempfile.html) specify that $ python
Python 2.5.1 (r251:54863, Oct 17 2008, 14:39:09)
[GCC 3.4.6 20060404 (Red Hat 3.4.6-10)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import tempfile
>>> tempfile.mkdtemp(dir='.')
'./tmpHk1pBD' similar results were obtained on: Python 2.5.1 (r251:54863, Feb 6 2009, 19:02:12) Note that mkstemp() gets it right: >>> tempfile.mkdtemp(dir='.')
'./tmpoPXdL7'
>>> tempfile.mkstemp(dir='.')
(3, '/Users/roy2/tmpwTGZ2y')
>>> |
This is true on trunk and py3k as well. 2.5 is in security fix only Since mkstemp does return in the absolute path in this case, I think |
It's a border case for backporting. The docs do say it should be returning My vote is to backport. Otherwise the docs should be updated for 2.6/3.1 |
I think we should be conservative. People may be relying on it *not* |
I have created a patch for this for Python 3.1 and included an update to the unit tests. The tests were never checking for a relative path and if they did would pas it even when it would have failed due to liberal use of os.path.abspath() The test fix is probably not as clean as it could be by I did not want to severely alter its state. The change to tempfile.mkdtemp() was clear and followed the current method tempfile.mkstemp() uses. |
As a side note, this was done mostly as an exercise for myself and as a learning experience. Any feedback would be appreciated regardless of any decision on the status of this bug. |
First of all, thanks for working on this. Now for some feedback :) It is most helpful if you provide patches against trunk (which is 2.7 alpha right now). We then forward port them to py3k. (This will change after the release of 2.7, when py3k will become trunk.) Isn't the assert you added to nameCheck redundant? It seems to me it would be better just to remove the abspath call on ndir from the existing assert. I haven't applied and tested the patch, but the other changes to the tests look odd to me. Why are you obtaining the abspath of the current directory? And why do those tests need to be changed at all? Your additional test case looks like it might be some exploration code you forgot to delete before generating the patch, maybe those other changes are as well? |
I agree, I do not feel like the precise changes to the tests feel completely ideal. I feel that this problem stems from the fact that the nameCheck function as originally written doesn't seem to completely serve its originally intended purpose. The original issue that caused the modifications of the tests were as follows:
I am quite confident there is a much better way to accomplish this but I did not wish to change _too_ much of the test on my first stab at this. I appreciate your feedback very much. I will work on setting up the 2.7 environment for working on issues that span the 2x/3x gap. |
One other thing that crossed my mind while I was thinking over this today. Instead of just relative pathing to '.' I should probably change my path to gettempdir() and then relative path to it since the location of the python executable may not be writable. Any thoughts on how to go about testing this in a way that is most likely to succeed in all cases and respect OS permissions? |
I made a new patch off of the 2.7 trunk version. I think I have handled some of the issues more cleanly. Please see Py(27)7325.diff I addressed the issue of using a relative path in the tempdir to achieve the 'guaranteed' ability to write rather than assuming the location of the python executable was writable. |
Tried this on windows against 2.7 don't see why it can't go forward. |
Thomas, I think the weirdness you were sensing when trying to adapt the nameCheck calls in test__RandomNameSequence after adding the abspath check is that those test cases really don't need nameCheck. For example, test_get_six_char_str should be testing that _RandomNameSequence returns something that is both a string and 6 characters, not properties of some path manufactured around it. I've attached a patch based on Thomas's that factors out the string check in nameCheck and has the tests in test__RandomNameSequence just use the string check. The patch is against release27-maint. |
Hi people, thanks. |
Looks good to me, with one exception: if temp_dir_abspath.startswith('./'): Wouldn’t this be better: if not _os.path.isabs(temp_dir_abspath) (P.S. file is not a builtin anymore in 3.x, it’s used for example as an argument to the print function.) |
You is right, i put this command on test and not the test. |
os.path.abspath actually checks for isabs first, so I think you can leave the test out and always call abspath. |
I think we should resolve this one line change. Jessica’s patch looks just fine, so I tend to apply it. However, I’d like to document the current behavior in 2.7, 3.2, 3.3 and 3.4. Am I missing anything? |
bump? see also bpo-20267 |
It's still the same in 3.11. I don't know whether this is something to fix in the code or in the docs. >> import tempfile
>>> tempfile.mkdtemp(dir='.')
'./tmplvkt14za'
>>> tempfile.mkstemp(dir='.')
(3, '/Users/iritkatriel/src/cpython/tmp86ljh34k') |
@kj7rrv made a pull request but that doesn’t send a notification to people here. The PR needs tests and docs (with versionchanged note); I think some of the original patches here contained these, so you can take inspiration from them. |
@merwok Oh, I didn't see there is already a PR for this. |
No, I was referring to your PR! |
The PR is ready for a final review. I think it should not be backported, but a doc PR for older branches could be created. |
Co-authored-by: Éric <[email protected]> Co-authored-by: AlexWaygood <[email protected]>
The PR has now been merged, fixing the issue for 3.12 and newer. It won't be backported. All that is left to do now is to file a docs PR to the 3.11 branch to clarify the behaviour for 3.11 and older. @mblahay, would you be interested in doing that, maybe? |
I realised that getting the wording right here was quite tricky, since we don't usually "document bugs", so I went ahead and made a PR myself: #103844. Reviews are welcome! :) |
…ython#94612) Co-authored-by: Éric <[email protected]> Co-authored-by: AlexWaygood <[email protected]>
The docs PR for 3.11 has been merged. Closing this issue as completed! |
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:
Linked PRs
mkdtemp
on 3.11 and lower #103844The text was updated successfully, but these errors were encountered: