Django

Code

Ticket #5690 (closed: worksforme)

Opened 7 months ago

Last modified 7 months ago

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

Reported by: Thomas Güttler <hv@tbz-pariv.de> Assigned to: nobody
Component: django-admin.py Version: SVN
Keywords: Cc:
Triage Stage: Unreviewed Has patch: 1
Needs documentation: 0 Needs tests: 0
Patch needs improvement: 0

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

use_dirname_not_dot_dot.diff (1.3 kB) - added by Thomas Güttler <hv@tbz-pariv.de> on 10/05/07 02:44:29.

Change History

10/05/07 02:44:29 changed by Thomas Güttler <hv@tbz-pariv.de>

  • attachment use_dirname_not_dot_dot.diff added.

10/05/07 20:40:07 changed by gwilson

  • needs_better_patch changed.
  • component changed from Uncategorized to django-admin.py.
  • description changed.
  • needs_tests changed.
  • needs_docs changed.

fixed ticket formatting

10/05/07 20:55:16 changed 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'

10/05/07 20:57:15 changed 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 '..'.

10/05/07 21:24:00 changed by gwilson

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

(follow-up: ↓ 6 ) 10/07/07 13:39:51 changed by Thomas Guettler (Home)

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

(in reply to: ↑ 5 ) 10/07/07 13:57:24 changed 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.

10/08/07 02:16:15 changed by Thomas Güttler <hv@tbz-pariv.de>

  • status changed from new to closed.
  • resolution set to worksforme.

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/Change #5690 (Use os.path.dirname() not os.path.join(foo, '..'))




Change Properties
Action