Opened 17 years ago

Closed 17 years ago

#5690 closed (worksforme)

Use os.path.dirname() not os.path.join(foo, '..')

Reported by: Thomas Güttler <hv@…> Owned by: nobody
Component: Core (Management commands) Version: dev
Severity: Keywords:
Cc: Triage Stage: Unreviewed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Gary Wilson)

If you use symlinks to directories using ".." will lead you
to the wrong directory.

Here is an example:

mkdir -p /tmp/dotdot/two
ln -s  /tmp/dotdot/two/ /tmp/symlink
cd /tmp/symlink
ls ..
 --> two
 Content of /tmp/symlink, not /tmp!

Attachments (1)

use_dirname_not_dot_dot.diff (1.3 KB ) - added by Thomas Güttler <hv@…> 17 years ago.

Download all attachments as: .zip

Change History (8)

by Thomas Güttler <hv@…>, 17 years ago

comment:1 by Gary Wilson, 17 years ago

Component: Uncategorizeddjango-admin.py
Description: modified (diff)

fixed ticket formatting

comment:2 by Gary Wilson, 17 years ago

Using dirname() returns the same thing as what's currently there if there is no trailing slash:

>>> directory = os.getcwd()
>>> directory
'/tmp/dotdot/two'
>>> project_dir = os.path.normpath(os.path.join(directory, '..'))
>>> project_dir
'/tmp/dotdot'
>>> parent_dir = os.path.basename(project_dir)
>>> parent_dir
'dotdot'

and

>>> directory = os.getcwd()
>>> directory
'/tmp/dotdot/two'
>>> project_dir = os.path.normpath(os.path.dirname(directory))
>>> project_dir
'/tmp/dotdot'
>>> parent_dir = os.path.basename(project_dir)
>>> parent_dir
'dotdot'

However, using dirname() returns different results if directory ends with a slash:

>>> directory = '/tmp/dotdot/two/'
>>> project_dir = os.path.normpath(os.path.join(directory, '..'))
>>> project_dir
'/tmp/dotdot'
>>> parent_dir = os.path.basename(project_dir)
>>> parent_dir
'dotdot'

and

>>> directory = '/tmp/dotdot/two/'
>>> project_dir = os.path.normpath(os.path.dirname(directory))
>>> project_dir
'/tmp/dotdot/two'
>>> parent_dir = os.path.basename(project_dir)
>>> parent_dir
'two'

comment:3 by Gary Wilson, 17 years ago

So I'm not seeing how the using of '..' is leading to different results as you have mentioned. However, I do believe we should at least be using os.pardir there instead of '..'.

comment:4 by Gary Wilson, 17 years ago

(In [6456]) Refs #5690 -- Changed path joining to use os.pardir instead of '..'.

comment:5 by Thomas Guettler (Home), 17 years ago

In django/core/management/init.py normpath() is not used. Please add it. This would
fix my problem:

in reply to:  5 comment:6 by Malcolm Tredinnick, 17 years ago

Replying to Thomas Guettler (Home):

In django/core/management/init.py normpath() is not used. Please add it. This would
fix my problem:

We aren't going to add anything without an explanation of what the real problem is. Gary has already mentioned that he doesn't see what would be causing the problem here. Please give an explanation of what you are trying to fix so that we can ensure we make the correct fix.

comment:7 by Thomas Güttler <hv@…>, 17 years ago

Resolution: worksforme
Status: newclosed

My problem was, that the import failed, because the directory was a symlink.
I changed my setup and can't reproduce the problem. Using ".." (without
normpath) is a bad way to get the upper directory, since it can fail. See
shell example above. Nevertheless I close this ticket. Sorry for the noise.

Note: See TracTickets for help on using tickets.
Back to Top