[ python-Feature Requests-1154351 ] add get_current_dir_name() to os module

SourceForge.net noreply at sourceforge.net
Mon Mar 14 20:17:13 CET 2005


Feature Requests item #1154351, was opened at 2005-03-01 17:26
Message generated for change (Comment added) made by loewis
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=355470&aid=1154351&group_id=5470

Category: Python Library
Group: None
Status: Open
Resolution: None
Priority: 5
Submitted By: Michael Hoffman (hoffmanm)
Assigned to: Nobody/Anonymous (nobody)
Summary: add get_current_dir_name() to os module

Initial Comment:
os.getcwd() does not use the contents of the PWD
environment variable, much as the glibc getcwd() does
not. This means that if a user sets the path using a
symbolic link, it will be dereferenced before being
passed by os.getcwd().

I propose that get_current_dir_name() be added, either
as a call to the glibc get_current_dir_name(), which
uses the PWD environment variable and therefore will
not dereference symbolic links in the path, or as a
fallback to this Pure Python function:

def get_current_dir_name():
    cwd = os.getcwd()
    
    try:
        pwd = os.environ["PWD"]
    except KeyError:
        return cwd

    cwd_stat, pwd_stat = map(os.stat, [cwd, pwd])

    if cwd_stat.st_dev == pwd_stat.st_dev and
cwd_stat.st_ino == pwd_stat.st_ino:
        return pwd
    else:
        return cwd


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

>Comment By: Martin v. Löwis (loewis)
Date: 2005-03-14 20:17

Message:
Logged In: YES 
user_id=21627

Equalling get_current_dir_name and getcwd for non-POSIX
systems seems like a conservative approach, so I'm for it.
Alternatively, one might think that PWD won't be set on
non-POSIX systems.

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

Comment By: Michael Hoffman (hoffmanm)
Date: 2005-03-14 10:03

Message:
Logged In: YES 
user_id=987664

Thanks for your comments. I agree that a better description
of the difference here is necessary.

I just checked the glibc source and this is almost exactly
what it does. It actually does an os.stat on "." and only
calls __getcwd() if necessary. It's in
glibc-2.3.4/io/getdirname.c if you're curious.

I'll make that change and add the patch to my list of things
to do...

Since st_dev and st_ino don't work outside Posix, should I
just return os.getcwd() on other platforms?

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

Comment By: Martin v. Löwis (loewis)
Date: 2005-03-14 00:00

Message:
Logged In: YES 
user_id=21627

I was going to say "what is the advantage of using the PWD
variable?", but, thinking about it three times, I found that
it gives you what the user typed in the last cd(1),
independent of symbolic links. So even though you wrote what
it does, and how it differs from getcwd, its not at all
obvious that this is a good thing (even though I now agree
it is a good thing)

Would you like to work on a patch? A pure Python
implementation sounds reasonable, if your code is what glibc
does as well. It seems to me that the documentation patch is
really crucial here, or else users will wonder "what the
heck...".

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

Comment By: Michael Hoffman (hoffmanm)
Date: 2005-03-01 17:27

Message:
Logged In: YES 
user_id=987664

Hmmm... my indentation seems to have gone by the wayside.
Still you can probably figure out what the code is supposed
to do <wink>

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

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


More information about the Python-bugs-list mailing list