Code

Opened 7 years ago

Closed 7 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: master
Severity: Keywords:
Cc: Triage Stage: Unreviewed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description (last modified by gwilson)

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@…> 7 years ago.

Download all attachments as: .zip

Change History (8)

Changed 7 years ago by Thomas Güttler <hv@…>

comment:1 Changed 7 years ago by gwilson

  • Component changed from Uncategorized to django-admin.py
  • Description modified (diff)
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

fixed ticket formatting

comment:2 Changed 7 years ago by gwilson

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 Changed 7 years ago by gwilson

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 Changed 7 years ago by gwilson

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

comment:5 follow-up: Changed 7 years ago by Thomas Guettler (Home)

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

comment:6 in reply to: ↑ 5 Changed 7 years ago by mtredinnick

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 Changed 7 years ago by Thomas Güttler <hv@…>

  • Resolution set to worksforme
  • Status changed from new to closed

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.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.