| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -247,6 +247,10 @@ typedef Py_ssize_t Py_ssize_clean_t; | |||
| 247 | 247 | #define S_ISCHR(x) (((x) & S_IFMT) == S_IFCHR) | |
| 248 | 248 | #endif | |
| 249 | 249 | ||
| 250 | + #ifndef S_ISLNK | ||
| 251 | + #define S_ISLNK(x) (((x) & S_IFMT) == S_IFLNK) | ||
| 252 | + #endif | ||
| 253 | + | ||
| 250 | 254 | #ifdef __cplusplus | |
| 251 | 255 | /* Move this down here since some C++ #include's don't like to be included | |
| 252 | 256 | inside an extern "C" */ | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -7,7 +7,7 @@ | |||
| 7 | 7 | import stat | |
| 8 | 8 | ||
| 9 | 9 | __all__ = ['commonprefix', 'exists', 'getatime', 'getctime', 'getmtime', | |
| 10 | - 'getsize', 'isdir', 'isfile', 'samefile', 'sameopenfile', | ||
| 10 | + 'getsize', 'isdir', 'isfile', 'islink', 'samefile', 'sameopenfile', | ||
| 11 | 11 | 'samestat'] | |
| 12 | 12 | ||
| 13 | 13 | ||
@@ -45,6 +45,18 @@ def isdir(s): | |||
| 45 | 45 | return stat.S_ISDIR(st.st_mode) | |
| 46 | 46 | ||
| 47 | 47 | ||
| 48 | + # Is a path a symbolic link? | ||
| 49 | + # This will always return false on systems where os.lstat doesn't exist. | ||
| 50 | + | ||
| 51 | + def islink(path): | ||
| 52 | + """Test whether a path is a symbolic link""" | ||
| 53 | + try: | ||
| 54 | + st = os.lstat(path) | ||
| 55 | + except (OSError, ValueError, AttributeError): | ||
| 56 | + return False | ||
| 57 | + return stat.S_ISLNK(st.st_mode) | ||
| 58 | + | ||
| 59 | + | ||
| 48 | 60 | def getsize(filename): | |
| 49 | 61 | """Return the size of a file, reported by os.stat().""" | |
| 50 | 62 | return os.stat(filename).st_size | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -276,19 +276,6 @@ def dirname(p): | |||
| 276 | 276 | """Returns the directory component of a pathname""" | |
| 277 | 277 | return split(p)[0] | |
| 278 | 278 | ||
| 279 | - # Is a path a symbolic link? | ||
| 280 | - # This will always return false on systems where os.lstat doesn't exist. | ||
| 281 | - | ||
| 282 | - def islink(path): | ||
| 283 | - """Test whether a path is a symbolic link. | ||
| 284 | - This will always return false for Windows prior to 6.0. | ||
| 285 | - """ | ||
| 286 | - try: | ||
| 287 | - st = os.lstat(path) | ||
| 288 | - except (OSError, ValueError, AttributeError): | ||
| 289 | - return False | ||
| 290 | - return stat.S_ISLNK(st.st_mode) | ||
| 291 | - | ||
| 292 | 279 | ||
| 293 | 280 | # Is a path a junction? | |
| 294 | 281 | ||
@@ -870,11 +857,13 @@ def commonpath(paths): | |||
| 870 | 857 | ||
| 871 | 858 | ||
| 872 | 859 | try: | |
| 873 | - # The genericpath.isdir implementation uses os.stat and checks the mode | ||
| 874 | - # attribute to tell whether or not the path is a directory. | ||
| 875 | - # This is overkill on Windows - just pass the path to GetFileAttributes | ||
| 876 | - # and check the attribute from there. | ||
| 877 | - from nt import _isdir as isdir | ||
| 860 | + # The isdir(), isfile(), islink() and exists() implementations in | ||
| 861 | + # genericpath use os.stat(). This is overkill on Windows. Use simpler | ||
| 862 | + # builtin functions if they are available. | ||
| 863 | + from nt import _path_isdir as isdir | ||
| 864 | + from nt import _path_isfile as isfile | ||
| 865 | + from nt import _path_islink as islink | ||
| 866 | + from nt import _path_exists as exists | ||
| 878 | 867 | except ImportError: | |
| 879 | - # Use genericpath.isdir as imported above. | ||
| 868 | + # Use genericpath.* as imported above | ||
| 880 | 869 | pass | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -187,18 +187,6 @@ def dirname(p): | |||
| 187 | 187 | return head | |
| 188 | 188 | ||
| 189 | 189 | ||
| 190 | - # Is a path a symbolic link? | ||
| 191 | - # This will always return false on systems where os.lstat doesn't exist. | ||
| 192 | - | ||
| 193 | - def islink(path): | ||
| 194 | - """Test whether a path is a symbolic link""" | ||
| 195 | - try: | ||
| 196 | - st = os.lstat(path) | ||
| 197 | - except (OSError, ValueError, AttributeError): | ||
| 198 | - return False | ||
| 199 | - return stat.S_ISLNK(st.st_mode) | ||
| 200 | - | ||
| 201 | - | ||
| 202 | 190 | # Is a path a junction? | |
| 203 | 191 | ||
| 204 | 192 | def isjunction(path): | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,9 +1,10 @@ | |||
| 1 | + import inspect | ||
| 1 | 2 | import ntpath | |
| 2 | 3 | import os | |
| 3 | 4 | import sys | |
| 4 | 5 | import unittest | |
| 5 | 6 | import warnings | |
| 6 | - from test.support import os_helper | ||
| 7 | + from test.support import cpython_only, os_helper | ||
| 7 | 8 | from test.support import TestFailed, is_emscripten | |
| 8 | 9 | from test.support.os_helper import FakePath | |
| 9 | 10 | from test import test_genericpath | |
@@ -938,6 +939,35 @@ def test_isjunction(self): | |||
| 938 | 939 | self.assertFalse(ntpath.isjunction('tmpdir')) | |
| 939 | 940 | self.assertPathEqual(ntpath.realpath('testjunc'), ntpath.realpath('tmpdir')) | |
| 940 | 941 | ||
| 942 | + @unittest.skipIf(sys.platform != 'win32', "drive letters are a windows concept") | ||
| 943 | + def test_isfile_driveletter(self): | ||
| 944 | + drive = os.environ.get('SystemDrive') | ||
| 945 | + if drive is None or len(drive) != 2 or drive[1] != ':': | ||
| 946 | + raise unittest.SkipTest('SystemDrive is not defined or malformed') | ||
| 947 | + self.assertFalse(os.path.isfile('\\\\.\\' + drive)) | ||
| 948 | + | ||
| 949 | + @unittest.skipIf(sys.platform != 'win32', "windows only") | ||
| 950 | + def test_con_device(self): | ||
| 951 | + self.assertFalse(os.path.isfile(r"\\.\CON")) | ||
| 952 | + self.assertFalse(os.path.isdir(r"\\.\CON")) | ||
| 953 | + self.assertFalse(os.path.islink(r"\\.\CON")) | ||
| 954 | + self.assertTrue(os.path.exists(r"\\.\CON")) | ||
| 955 | + | ||
| 956 | + @unittest.skipIf(sys.platform != 'win32', "Fast paths are only for win32") | ||
| 957 | + @cpython_only | ||
| 958 | + def test_fast_paths_in_use(self): | ||
| 959 | + # There are fast paths of these functions implemented in posixmodule.c. | ||
| 960 | + # Confirm that they are being used, and not the Python fallbacks in | ||
| 961 | + # genericpath.py. | ||
| 962 | + self.assertTrue(os.path.isdir is nt._path_isdir) | ||
| 963 | + self.assertFalse(inspect.isfunction(os.path.isdir)) | ||
| 964 | + self.assertTrue(os.path.isfile is nt._path_isfile) | ||
| 965 | + self.assertFalse(inspect.isfunction(os.path.isfile)) | ||
| 966 | + self.assertTrue(os.path.islink is nt._path_islink) | ||
| 967 | + self.assertFalse(inspect.isfunction(os.path.islink)) | ||
| 968 | + self.assertTrue(os.path.exists is nt._path_exists) | ||
| 969 | + self.assertFalse(inspect.isfunction(os.path.exists)) | ||
| 970 | + | ||
| 941 | 971 | ||
| 942 | 972 | class NtCommonTest(test_genericpath.CommonTest, unittest.TestCase): | |
| 943 | 973 | pathmodule = ntpath | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -742,6 +742,7 @@ def test_access_denied(self): | |||
| 742 | 742 | ) | |
| 743 | 743 | result = os.stat(fname) | |
| 744 | 744 | self.assertNotEqual(result.st_size, 0) | |
| 745 | + self.assertTrue(os.path.isfile(fname)) | ||
| 745 | 746 | ||
| 746 | 747 | @unittest.skipUnless(sys.platform == "win32", "Win32 specific tests") | |
| 747 | 748 | def test_stat_block_device(self): | |
@@ -2860,6 +2861,7 @@ def test_appexeclink(self): | |||
| 2860 | 2861 | self.assertEqual(st, os.stat(alias)) | |
| 2861 | 2862 | self.assertFalse(stat.S_ISLNK(st.st_mode)) | |
| 2862 | 2863 | self.assertEqual(st.st_reparse_tag, stat.IO_REPARSE_TAG_APPEXECLINK) | |
| 2864 | + self.assertTrue(os.path.isfile(alias)) | ||
| 2863 | 2865 | # testing the first one we see is sufficient | |
| 2864 | 2866 | break | |
| 2865 | 2867 | else: | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,3 @@ | |||
| 1 | + The functions ``os.path.isdir``, ``os.path.isfile``, ``os.path.islink`` and | ||
| 2 | + ``os.path.exists`` are now 13% to 28% faster on Windows, by making fewer Win32 | ||
| 3 | + API calls. | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments