[Python-bugs-list] [ python-Feature Requests-446502 ] Expanding symlinks, addition to os.path

noreply@sourceforge.net noreply@sourceforge.net
Wed, 13 Mar 2002 05:17:56 -0800


Feature Requests item #446502, was opened at 2001-07-31 14:43
You can respond by visiting: 
http://sourceforge.net/tracker/?func=detail&atid=355470&aid=446502&group_id=5470

Category: None
Group: None
>Status: Closed
Resolution: None
Priority: 5
Submitted By: Erik Demaine (edemaine)
Assigned to: Nobody/Anonymous (nobody)
Summary: Expanding symlinks, addition to os.path

Initial Comment:
I propose to add a new function expand_symlinks() to
os.path (perhaps) that allows the user to easily
resolve a path into a "real file," expanding all
symlink references.  Specifically:

    expand_symlinks(path, limit=100) - Resolve symbolic
links.
  
    Resolves all symbolic links for a given path, and
returns the resulting path that refers to a true file. 
To detect symlink loops, an IOError exception is thrown
if more than 'limit' expansions do not lead to a true
file.  To prevent this behavior, the 'limit' argument
can be set to None.

Rationale:  In many scripts I write, and I suspect that
others write, I want to know the directory in which the
script lives.  Often os.path.dirname(sys.argv[0])
suffices, but this does not work when the script is
executed by way of a symlink.  In this situation,
os.path.dirname(expand_symlinks(sys.argv[0])) is what I
want.

Common objection: expand_symlinks() is just a few
lines.

Response: It is tedious to write the necessary while
loop for every script that wants to know where it
lives.  And it is easy to get wrong, especially by
omitting a check for symlink loops (too many symlink
expansions).

Of course, this is a personal opinion, and I'm open to
suggestions.

Attached is a prototype implementation.

----------------------------------------------------------------------

>Comment By: Erik Demaine (edemaine)
Date: 2002-03-13 08:17

Message:
Logged In: YES 
user_id=265183

I think this bug should now be closed; Python 2.2 includes a
new os.path.realpath which is essentially the corrected
version of the original proposal (thanks loewis for pointing
this out!).  Whoever put it in Python 2.2, thanks! :-)

----------------------------------------------------------------------

Comment By: Martin v. Löwis (loewis)
Date: 2001-10-04 13:40

Message:
Logged In: YES 
user_id=21627

The proposed feature sounds good, but the implementation 
appears to be incorrect, if 'path' already goes through a 
symlink. Suppose you have the following structure

/usr/spool -> ../var/spool
/var/spool/mail -> ../mail

Now, I invoke expand_symlinks with /usr/spool/mail.
I should get /var/mail, but I do get /usr/mail, which does 
not exist.


----------------------------------------------------------------------

You can respond by visiting: 
http://sourceforge.net/tracker/?func=detail&atid=355470&aid=446502&group_id=5470