You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
TemporaryDirectory does not resolve path when created using a relative path #64466
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:
assignee=Noneclosed_at=Nonecreated_at=<Date2014-01-15.04:48:28.375>labels= ['library']
title='TemporaryDirectory does not resolve path when created using a relative path'updated_at=<Date2014-10-05.00:20:00.718>user='https://github.com/anntzer'
A second version of the patch (tempfile_02), fixing more tempfile functions to properly support relative paths. Please review.
anntzer commented on Sep 26, 2014
anntzermannequin
MannequinAuthor
More actions
This looks reasonable. Note that the output of gettempdir is always passed as first argument to os.path.join (possibly via _mkstemp_inner), so perhaps you should rather define something like
def_absolute_child_of_parent_or_tmpdir(parent, *args):
"""Return the absolute child of parent, or gettempdir() if parent is None, given by *args. """ifparentisNone:
parent=<_sanitize_dir># inline the code herereturn_os.path.join(parent, *args)
and use that function instead.
This factorizes the code a little bit more and makes intent clearer (I don't think _sanitize_dir is a very clear name).
Antony, I agree regarding the poor naming of '_sanitize_dir()' helper. As for your other suggestion, I think such a refactoring will actually make code harder to follow (+ it's more invasive). Generally, I'm in favour of transforming parameters like 'dir' closer to the beginning of the method's code, so that it's immediately obvious what's going on, and is also easier to put debug code [like 'print("mkdtemp call for: ", dir)'].
anntzer commented on Sep 26, 2014
anntzermannequin
MannequinAuthor
More actions
I don't feel strongly about this, so either way is fine.
The change would be backwards-incompatible but also mimics the behavior of NamedTemporaryFile (which also fails to delete the file if the containing folder has been renamed -- this is easy to verify manually).
I guess the other option would be to use fd-based semantics? (but it'd be preferable if the behavior was kept the same between TemporaryDirectory and NamedTemporaryFile).
Activity
anntzer commentedon Jan 15, 2014
Essentially, the following fails:
because t.name is a relative path. Resolving the "dir" argument when the directory is created should(?) fix the issue.
1st1 commentedon Jan 20, 2014
This looks like a bug to me (or we should complain if a relative 'dir' was passed).
I'm attaching a patch that fixes this, although I'm not sure if the unittest I wrote will work on windows.
Moreover, 'mkstemp' and 'mktemp' have the same bug (separate issue for them?)
1st1 commentedon Jan 20, 2014
The patch is ok for windows.
anntzer commentedon Feb 8, 2014
Thanks for the fix. The same fix seems should also work for mkstemp and mktemp -- is it really worth creating a new issue for them?
1st1 commentedon Feb 8, 2014
No, no new issue is necessary. I think I'll update the patch and merge it in 3.5, as it's too late for 3.4
1st1 commentedon Sep 26, 2014
A second version of the patch (tempfile_02), fixing more tempfile functions to properly support relative paths. Please review.
anntzer commentedon Sep 26, 2014
This looks reasonable. Note that the output of gettempdir is always passed as first argument to os.path.join (possibly via _mkstemp_inner), so perhaps you should rather define something like
and use that function instead.
This factorizes the code a little bit more and makes intent clearer (I don't think _sanitize_dir is a very clear name).
1st1 commentedon Sep 26, 2014
Antony, I agree regarding the poor naming of '_sanitize_dir()' helper. As for your other suggestion, I think such a refactoring will actually make code harder to follow (+ it's more invasive). Generally, I'm in favour of transforming parameters like 'dir' closer to the beginning of the method's code, so that it's immediately obvious what's going on, and is also easier to put debug code [like 'print("mkdtemp call for: ", dir)'].
anntzer commentedon Sep 26, 2014
I don't feel strongly about this, so either way is fine.
serhiy-storchaka commentedon Sep 27, 2014
Note that abspath() can return incorrect result in case of symbolic links to directories and pardir components. I.e. abspath('symlink/..').
1st1 commentedon Sep 27, 2014
Good catch.. Should I use os.path.realpath?
serhiy-storchaka commentedon Oct 4, 2014
I don't see other way.
But there is other issue. Following code works now, but will fail with the patch:
anntzer commentedon Oct 5, 2014
The change would be backwards-incompatible but also mimics the behavior of NamedTemporaryFile (which also fails to delete the file if the containing folder has been renamed -- this is easy to verify manually).
I guess the other option would be to use fd-based semantics? (but it'd be preferable if the behavior was kept the same between TemporaryDirectory and NamedTemporaryFile).