Adding 'lexists()' to os.path

I was just reviewing the patch at http://python.org/sf/941486, which proposes adding a new 'lexists()' function to os.path. This function would return True for dangling symbolic links, unlike os.path.exists() which returns False. One could use 'os.path.islink(path) or os.path.exists()' as well, but that's two stat calls instead of one. This function is useful and efficient for fixing a bug in glob.py (which the patch does as well) and it seems like it could be useful in tarfile.py and tempfile.py as well. Also, a high-quality patch is already available. So, any objections to adding this to os.path? Johannes

Johannes Gijsbers wrote:
This function is useful and efficient for fixing a bug in glob.py (which the patch does as well) and it seems like it could be useful in tarfile.py and tempfile.py as well. Also, a high-quality patch is already available. So, any objections to adding this to os.path?
Go ahead. "Equivallent" contains a typo, though. Martin

Johannes Gijsbers <jlgijsbers@planet.nl> writes:
I was just reviewing the patch at http://python.org/sf/941486, which proposes adding a new 'lexists()' function to os.path. This function would return True for dangling symbolic links, unlike os.path.exists() which returns False. One could use 'os.path.islink(path) or os.path.exists()' as well, but that's two stat calls instead of one.
As an alternative, would there be any value in cacheing the last stat result? In a sequence like this, with multiple calls for the same argument, many stat calls would be replaced by one. And it's likely that the checking overhead on cache misses would be minimal compared to the cost of the stat call, so it's an overall win. Of course, actual tests beat idle speculation like this... Paul. -- Any sufficiently advanced technology is indistinguishable from magic. -- Arthur C. Clarke

On Sun, Aug 22, 2004 at 02:17:00PM +0100, Paul Moore wrote:
As an alternative, would there be any value in cacheing the last stat result?
Well, code like this would break: def wait_for_change(filename, delay=.1): stamp = os.stat(filename).st_mtime while 1: time.sleep(delay) if os.stat(filename).st_mtime != stamp: break Hm ...
import statcache /usr/lib/python2.3/statcache.py:8: DeprecationWarning: The statcache module is obsolete. Use os.stat() instead.
Jeff

Paul Moore wrote:
I was just reviewing the patch at http://python.org/sf/941486, which proposes adding a new 'lexists()' function to os.path. This function would return True for dangling symbolic links, unlike os.path.exists() which returns False. One could use 'os.path.islink(path) or os.path.exists()' as well, but that's two stat calls instead of one.
As an alternative, would there be any value in cacheing the last stat result? In a sequence like this, with multiple calls for the same argument, many stat calls would be replaced by one. And it's likely that the checking overhead on cache misses would be minimal compared to the cost of the stat call, so it's an overall win.
Hi Paul, you will never know how long the cached result is true. Files on disk can change outside your python script.... I doubt that we can find a good expire time for the caching? Someone will come up with an application in which this caching will give wrong results... If someone knows what he does and likes efficient programming he can cache the result of os.stat() himself and work with that. (Like this lexists() function is programmed internally...) My personal opinion: +0.5 for os.lexists() [it's useful, but we can also live without it...] -1 for a stat cache in os.*. Cheers, Tino

Tino Lange <tino.lange@isg.de> writes:
Paul Moore wrote:
As an alternative, would there be any value in cacheing the last stat result? In a sequence like this, with multiple calls for the same argument, many stat calls would be replaced by one. And it's likely that the checking overhead on cache misses would be minimal compared to the cost of the stat call, so it's an overall win.
you will never know how long the cached result is true. Files on disk can change outside your python script....
Doh. Sorry about that - I wasn't thinking hard enough before posting. Back to sleep... Paul. -- Instant gratification takes too long -- Carrie Fisher

I was just reviewing the patch at http://python.org/sf/941486, which proposes adding a new 'lexists()' function to os.path. This function would return True for dangling symbolic links, unlike os.path.exists() which returns False. One could use 'os.path.islink(path) or os.path.exists()' as well, but that's two stat calls instead of one.
This function is useful and efficient for fixing a bug in glob.py (which the patch does as well) and it seems like it could be useful in tarfile.py and tempfile.py as well. Also, a high-quality patch is already available. So, any objections to adding this to os.path?
Looks like you already got the go-ahead. Just make sure with any API changes/additions to os.path, that there are many individual *path modules that need to be updated -- if a platform doesn't support symlinks, lexists() should probably just be an alias for exists(). posixpath.py, ntpath.py, macpath.py, os2emxpath.py. --Guido van Rossum (home page: http://www.python.org/~guido/)

Looks like you already got the go-ahead. Just make sure with any API changes/additions to os.path, that there are many individual *path modules that need to be updated -- if a platform doesn't support symlinks, lexists() should probably just be an alias for exists().
posixpath.py, ntpath.py, macpath.py, os2emxpath.py.
Yes, cben's patch takes cares of that. I've checked it in just now. Johannes
participants (6)
-
"Martin v. Löwis"
-
Guido van Rossum
-
Jeff Epler
-
Johannes Gijsbers
-
Paul Moore
-
Tino Lange