Issue1154351
Created on 2005-03-01 16:26 by hoffman, last changed 2010-08-09 03:23 by terry.reedy.
| Messages (6) | |||
|---|---|---|---|
| msg61185 - (view) | Author: Michael Hoffman (hoffman) | Date: 2005-03-01 16:26 | |
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
|
|||
| msg61186 - (view) | Author: Michael Hoffman (hoffman) | Date: 2005-03-01 16:27 | |
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> |
|||
| msg61187 - (view) | Author: Martin v. Löwis (loewis) * ![]() |
Date: 2005-03-13 23:00 | |
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...". |
|||
| msg61188 - (view) | Author: Michael Hoffman (hoffman) | Date: 2005-03-14 09:03 | |
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? |
|||
| msg61189 - (view) | Author: Martin v. Löwis (loewis) * ![]() |
Date: 2005-03-14 19:17 | |
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. |
|||
| msg61190 - (view) | Author: Michael Hoffman (hoffman) | Date: 2005-03-14 19:45 | |
Logged In: YES user_id=987664 It might if they were using a ported UNIX shell. But I think trusting PWD without checking it is a Bad Thing. |
|||
| History | |||
|---|---|---|---|
| Date | User | Action | Args |
| 2010-08-09 03:23:15 | terry.reedy | set | versions: + Python 3.2, - Python 3.1, Python 2.7 |
| 2009-02-15 23:56:34 | ajaksu2 | set | stage: test needed versions: + Python 3.1, Python 2.7 |
| 2005-03-01 16:26:00 | hoffmanm.historic | create | |
