Re: [Python-Dev] cpython (3.2): Fix #11583. Changed os.path.isdir to use GetFileAttributes instead of os.stat.
On 06/09/11 02:00, brian.curtin wrote:
http://hg.python.org/cpython/rev/88e318166eaf changeset: 70713:88e318166eaf branch: 3.2 parent: 70700:0aa3064d1cef user: Brian Curtin <brian@python.org> date: Wed Jun 08 18:17:18 2011 -0500 summary: Fix #11583. Changed os.path.isdir to use GetFileAttributes instead of os.stat.
By changing to the Windows GetFileAttributes API in nt._isdir we can figure out if the path is a directory without opening the file via os.stat. This has the minor benefit of speeding up os.path.isdir by at least 2x for regular files and 10-15x improvements were seen on symbolic links (which opened the file multiple times during os.stat). Since os.path.isdir is used in several places on interpreter startup, we get a minor speedup in startup time.
files: Lib/ntpath.py | 13 ++++++++++ Misc/NEWS | 3 ++ Modules/posixmodule.c | 37 +++++++++++++++++++++++++++++++ 3 files changed, 53 insertions(+), 0 deletions(-)
diff --git a/Lib/ntpath.py b/Lib/ntpath.py --- a/Lib/ntpath.py +++ b/Lib/ntpath.py @@ -672,3 +672,16 @@ def sameopenfile(f1, f2): """Test whether two file objects reference the same file""" return _getfileinformation(f1) == _getfileinformation(f2) + + +try: + # The genericpath.isdir implementation uses os.stat and checks the mode + # attribute to tell whether or not the path is a directory. + # This is overkill on Windows - just pass the path to GetFileAttributes + # and check the attribute from there. + from nt import _isdir +except ImportError: + from genericpath import isdir as _isdir + +def isdir(path): + return _isdir(path)
Not that it matters, but ISTM that this would be faster as try: from nt import _isdir as isdir except ImportError: pass Georg
Le jeudi 09 juin 2011 à 08:16 +0200, Georg Brandl a écrit :
On 06/09/11 02:00, brian.curtin wrote:
http://hg.python.org/cpython/rev/88e318166eaf changeset: 70713:88e318166eaf branch: 3.2 parent: 70700:0aa3064d1cef user: Brian Curtin <brian@python.org> date: Wed Jun 08 18:17:18 2011 -0500 summary: Fix #11583. Changed os.path.isdir to use GetFileAttributes instead of os.stat.
By changing to the Windows GetFileAttributes API in nt._isdir we can figure out if the path is a directory without opening the file via os.stat. This has the minor benefit of speeding up os.path.isdir by at least 2x for regular files and 10-15x improvements were seen on symbolic links (which opened the file multiple times during os.stat). Since os.path.isdir is used in several places on interpreter startup, we get a minor speedup in startup time.
files: Lib/ntpath.py | 13 ++++++++++ Misc/NEWS | 3 ++ Modules/posixmodule.c | 37 +++++++++++++++++++++++++++++++ 3 files changed, 53 insertions(+), 0 deletions(-)
diff --git a/Lib/ntpath.py b/Lib/ntpath.py --- a/Lib/ntpath.py +++ b/Lib/ntpath.py @@ -672,3 +672,16 @@ def sameopenfile(f1, f2): """Test whether two file objects reference the same file""" return _getfileinformation(f1) == _getfileinformation(f2) + + +try: + # The genericpath.isdir implementation uses os.stat and checks the mode + # attribute to tell whether or not the path is a directory. + # This is overkill on Windows - just pass the path to GetFileAttributes + # and check the attribute from there. + from nt import _isdir +except ImportError: + from genericpath import isdir as _isdir + +def isdir(path): + return _isdir(path)
Not that it matters, but ISTM that this would be faster as
try: from nt import _isdir as isdir except ImportError: pass
I would matter if _isdir() had a docstring, but it doesn't :-) genericpath.isdir() has the following doc: def isdir(s): """Return true if the pathname refers to an existing directory.""" Victor
On Thu, Jun 9, 2011 at 04:05, Victor Stinner <victor.stinner@haypocalc.com>wrote:
On 06/09/11 02:00, brian.curtin wrote:
http://hg.python.org/cpython/rev/88e318166eaf changeset: 70713:88e318166eaf branch: 3.2 parent: 70700:0aa3064d1cef user: Brian Curtin <brian@python.org> date: Wed Jun 08 18:17:18 2011 -0500 summary: Fix #11583. Changed os.path.isdir to use GetFileAttributes instead of os.stat.
By changing to the Windows GetFileAttributes API in nt._isdir we can
out if the path is a directory without opening the file via os.stat. This has the minor benefit of speeding up os.path.isdir by at least 2x for regular files and 10-15x improvements were seen on symbolic links (which opened
Le jeudi 09 juin 2011 à 08:16 +0200, Georg Brandl a écrit : figure the
file multiple times during os.stat). Since os.path.isdir is used in several places on interpreter startup, we get a minor speedup in startup time.
files: Lib/ntpath.py | 13 ++++++++++ Misc/NEWS | 3 ++ Modules/posixmodule.c | 37 +++++++++++++++++++++++++++++++ 3 files changed, 53 insertions(+), 0 deletions(-)
diff --git a/Lib/ntpath.py b/Lib/ntpath.py --- a/Lib/ntpath.py +++ b/Lib/ntpath.py @@ -672,3 +672,16 @@ def sameopenfile(f1, f2): """Test whether two file objects reference the same file""" return _getfileinformation(f1) == _getfileinformation(f2) + + +try: + # The genericpath.isdir implementation uses os.stat and checks the mode + # attribute to tell whether or not the path is a directory. + # This is overkill on Windows - just pass the path to GetFileAttributes + # and check the attribute from there. + from nt import _isdir +except ImportError: + from genericpath import isdir as _isdir + +def isdir(path): + return _isdir(path)
Not that it matters, but ISTM that this would be faster as
try: from nt import _isdir as isdir except ImportError: pass
I would matter if _isdir() had a docstring, but it doesn't :-) genericpath.isdir() has the following doc:
def isdir(s): """Return true if the pathname refers to an existing directory."""
http://hg.python.org/lookup/d40609dd01e0 adds the docstring back in and redoes the imports as Georg mentioned, which is better. Thanks for having a look.
participants (3)
-
Brian Curtin
-
Georg Brandl
-
Victor Stinner