11
11
import tempfile
12
12
import unittest
13
13
from unittest import mock
14
+ import warnings
14
15
15
16
from test .support import import_helper
16
17
from test .support import is_emscripten , is_wasi
@@ -1749,39 +1750,42 @@ def test_iterdir_nodir(self):
1749
1750
errno .ENOENT , errno .EINVAL ))
1750
1751
1751
1752
def test_glob_common (self ):
1752
- def _check (glob , expected ):
1753
- self .assertEqual (set (glob ), { P (BASE , q ) for q in expected })
1753
+ def _check (path , glob , expected ):
1754
+ with self .assertWarns (DeprecationWarning ):
1755
+ actual = {q for q in path .glob (glob )}
1756
+ self .assertEqual (actual , { P (BASE , q ) for q in expected })
1754
1757
P = self .cls
1755
1758
p = P (BASE )
1756
- it = p .glob ("fileA" )
1757
- self .assertIsInstance (it , collections .abc .Iterator )
1758
- _check (it , ["fileA" ])
1759
- _check (p .glob ("fileB" ), [])
1760
- _check (p .glob ("dir*/file*" ), ["dirB/fileB" , "dirC/fileC" ])
1759
+ with self .assertWarns (DeprecationWarning ):
1760
+ it = p .glob ("fileA" )
1761
+ self .assertIsInstance (it , collections .abc .Iterator )
1762
+ self .assertEqual (set (it ), { P (BASE , "fileA" ) })
1763
+ _check (p , "fileB" , [])
1764
+ _check (p , "dir*/file*" , ["dirB/fileB" , "dirC/fileC" ])
1761
1765
if not os_helper .can_symlink ():
1762
- _check (p . glob ( "*A" ) , ['dirA' , 'fileA' ])
1766
+ _check (p , "*A" , ['dirA' , 'fileA' ])
1763
1767
else :
1764
- _check (p . glob ( "*A" ) , ['dirA' , 'fileA' , 'linkA' ])
1768
+ _check (p , "*A" , ['dirA' , 'fileA' , 'linkA' ])
1765
1769
if not os_helper .can_symlink ():
1766
- _check (p . glob ( "*B/*" ) , ['dirB/fileB' ])
1770
+ _check (p , "*B/*" , ['dirB/fileB' ])
1767
1771
else :
1768
- _check (p . glob ( "*B/*" ) , ['dirB/fileB' , 'dirB/linkD' ,
1772
+ _check (p , "*B/*" , ['dirB/fileB' , 'dirB/linkD' ,
1769
1773
'linkB/fileB' , 'linkB/linkD' ])
1770
1774
if not os_helper .can_symlink ():
1771
- _check (p . glob ( "*/fileB" ) , ['dirB/fileB' ])
1775
+ _check (p , "*/fileB" , ['dirB/fileB' ])
1772
1776
else :
1773
- _check (p . glob ( "*/fileB" ) , ['dirB/fileB' , 'linkB/fileB' ])
1777
+ _check (p , "*/fileB" , ['dirB/fileB' , 'linkB/fileB' ])
1774
1778
1775
1779
if not os_helper .can_symlink ():
1776
- _check (p . glob ( "*/" ) , ["dirA" , "dirB" , "dirC" , "dirE" ])
1780
+ _check (p , "*/" , ["dirA" , "dirB" , "dirC" , "dirE" ])
1777
1781
else :
1778
- _check (p . glob ( "*/" ) , ["dirA" , "dirB" , "dirC" , "dirE" , "linkB" ])
1782
+ _check (p , "*/" , ["dirA" , "dirB" , "dirC" , "dirE" , "linkB" ])
1779
1783
1780
1784
@os_helper .skip_unless_symlink
1781
1785
def test_glob_follow_symlinks_common (self ):
1782
1786
def _check (path , glob , expected ):
1783
- actual = {path for path in path .glob (glob , follow_symlinks = True )
1784
- if "linkD" not in path .parent .parts } # exclude symlink loop.
1787
+ actual = {q for q in path .glob (glob , follow_symlinks = True )
1788
+ if "linkD" not in q .parent .parts } # exclude symlink loop.
1785
1789
self .assertEqual (actual , { P (BASE , q ) for q in expected })
1786
1790
P = self .cls
1787
1791
p = P (BASE )
@@ -1795,7 +1799,7 @@ def _check(path, glob, expected):
1795
1799
@os_helper .skip_unless_symlink
1796
1800
def test_glob_no_follow_symlinks_common (self ):
1797
1801
def _check (path , glob , expected ):
1798
- actual = {path for path in path .glob (glob , follow_symlinks = False )}
1802
+ actual = {q for q in path .glob (glob , follow_symlinks = False )}
1799
1803
self .assertEqual (actual , { P (BASE , q ) for q in expected })
1800
1804
P = self .cls
1801
1805
p = P (BASE )
@@ -1807,43 +1811,46 @@ def _check(path, glob, expected):
1807
1811
_check (p , "*/" , ["dirA" , "dirB" , "dirC" , "dirE" ])
1808
1812
1809
1813
def test_rglob_common (self ):
1810
- def _check (glob , expected ):
1811
- self .assertEqual (set (glob ), { P (BASE , q ) for q in expected })
1814
+ def _check (path , glob , expected ):
1815
+ with self .assertWarns (DeprecationWarning ):
1816
+ actual = {q for q in path .rglob (glob )}
1817
+ self .assertEqual (actual , { P (BASE , q ) for q in expected })
1812
1818
P = self .cls
1813
1819
p = P (BASE )
1814
- it = p .rglob ("fileA" )
1815
- self .assertIsInstance (it , collections .abc .Iterator )
1816
- _check (it , ["fileA" ])
1817
- _check (p .rglob ("fileB" ), ["dirB/fileB" ])
1818
- _check (p .rglob ("*/fileA" ), [])
1820
+ with self .assertWarns (DeprecationWarning ):
1821
+ it = p .rglob ("fileA" )
1822
+ self .assertIsInstance (it , collections .abc .Iterator )
1823
+ self .assertEqual (set (it ), { P (BASE , "fileA" ) })
1824
+ _check (p , "fileB" , ["dirB/fileB" ])
1825
+ _check (p , "*/fileA" , [])
1819
1826
if not os_helper .can_symlink ():
1820
- _check (p . rglob ( "*/fileB" ) , ["dirB/fileB" ])
1827
+ _check (p , "*/fileB" , ["dirB/fileB" ])
1821
1828
else :
1822
- _check (p . rglob ( "*/fileB" ) , ["dirB/fileB" , "dirB/linkD/fileB" ,
1829
+ _check (p , "*/fileB" , ["dirB/fileB" , "dirB/linkD/fileB" ,
1823
1830
"linkB/fileB" , "dirA/linkC/fileB" ])
1824
- _check (p . rglob ( "file*" ) , ["fileA" , "dirB/fileB" ,
1831
+ _check (p , "file*" , ["fileA" , "dirB/fileB" ,
1825
1832
"dirC/fileC" , "dirC/dirD/fileD" ])
1826
1833
if not os_helper .can_symlink ():
1827
- _check (p . rglob ( "*/" ) , [
1834
+ _check (p , "*/" , [
1828
1835
"dirA" , "dirB" , "dirC" , "dirC/dirD" , "dirE" ,
1829
1836
])
1830
1837
else :
1831
- _check (p . rglob ( "*/" ) , [
1838
+ _check (p , "*/" , [
1832
1839
"dirA" , "dirA/linkC" , "dirB" , "dirB/linkD" , "dirC" ,
1833
1840
"dirC/dirD" , "dirE" , "linkB" ,
1834
1841
])
1835
- _check (p . rglob ( "" ) , ["" , "dirA" , "dirB" , "dirC" , "dirE" , "dirC/dirD" ])
1842
+ _check (p , "" , ["" , "dirA" , "dirB" , "dirC" , "dirE" , "dirC/dirD" ])
1836
1843
1837
1844
p = P (BASE , "dirC" )
1838
- _check (p . rglob ( "*" ) , ["dirC/fileC" , "dirC/novel.txt" ,
1845
+ _check (p , "*" , ["dirC/fileC" , "dirC/novel.txt" ,
1839
1846
"dirC/dirD" , "dirC/dirD/fileD" ])
1840
- _check (p . rglob ( "file*" ) , ["dirC/fileC" , "dirC/dirD/fileD" ])
1841
- _check (p . rglob ( "*/*" ) , ["dirC/dirD/fileD" ])
1842
- _check (p . rglob ( "*/" ) , ["dirC/dirD" ])
1843
- _check (p . rglob ( "" ) , ["dirC" , "dirC/dirD" ])
1847
+ _check (p , "file*" , ["dirC/fileC" , "dirC/dirD/fileD" ])
1848
+ _check (p , "*/*" , ["dirC/dirD/fileD" ])
1849
+ _check (p , "*/" , ["dirC/dirD" ])
1850
+ _check (p , "" , ["dirC" , "dirC/dirD" ])
1844
1851
# gh-91616, a re module regression
1845
- _check (p . rglob ( "*.txt" ) , ["dirC/novel.txt" ])
1846
- _check (p . rglob ( "*.*" ) , ["dirC/novel.txt" ])
1852
+ _check (p , "*.txt" , ["dirC/novel.txt" ])
1853
+ _check (p , "*.*" , ["dirC/novel.txt" ])
1847
1854
1848
1855
@os_helper .skip_unless_symlink
1849
1856
def test_rglob_follow_symlinks_common (self ):
@@ -1904,7 +1911,7 @@ def test_rglob_symlink_loop(self):
1904
1911
# Don't get fooled by symlink loops (Issue #26012).
1905
1912
P = self .cls
1906
1913
p = P (BASE )
1907
- given = set (p .rglob ('*' ))
1914
+ given = set (p .rglob ('*' , follow_symlinks = False ))
1908
1915
expect = {'brokenLink' ,
1909
1916
'dirA' , 'dirA/linkC' ,
1910
1917
'dirB' , 'dirB/fileB' , 'dirB/linkD' ,
@@ -1925,10 +1932,10 @@ def test_glob_many_open_files(self):
1925
1932
p = P (base , * (['d' ]* depth ))
1926
1933
p .mkdir (parents = True )
1927
1934
pattern = '/' .join (['*' ] * depth )
1928
- iters = [base .glob (pattern ) for j in range (100 )]
1935
+ iters = [base .glob (pattern , follow_symlinks = True ) for j in range (100 )]
1929
1936
for it in iters :
1930
1937
self .assertEqual (next (it ), p )
1931
- iters = [base .rglob ('d' ) for j in range (100 )]
1938
+ iters = [base .rglob ('d' , follow_symlinks = True ) for j in range (100 )]
1932
1939
p = base
1933
1940
for i in range (depth ):
1934
1941
p = p / 'd'
@@ -1939,9 +1946,9 @@ def test_glob_dotdot(self):
1939
1946
# ".." is not special in globs.
1940
1947
P = self .cls
1941
1948
p = P (BASE )
1942
- self .assertEqual (set (p .glob (".." )), { P (BASE , ".." ) })
1943
- self .assertEqual (set (p .glob ("dirA/../file*" )), { P (BASE , "dirA/../fileA" ) })
1944
- self .assertEqual (set (p .glob ("../xyzzy" )), set ())
1949
+ self .assertEqual (set (p .glob (".." , follow_symlinks = True )), { P (BASE , ".." ) })
1950
+ self .assertEqual (set (p .glob ("dirA/../file*" , follow_symlinks = True )), { P (BASE , "dirA/../fileA" ) })
1951
+ self .assertEqual (set (p .glob ("../xyzzy" , follow_symlinks = True )), set ())
1945
1952
1946
1953
@os_helper .skip_unless_symlink
1947
1954
def test_glob_permissions (self ):
@@ -1971,11 +1978,11 @@ def my_scandir(path):
1971
1978
return contextlib .nullcontext (entries )
1972
1979
1973
1980
with mock .patch ("os.scandir" , my_scandir ):
1974
- self .assertEqual (len (set (base .glob ("*" ))), 3 )
1981
+ self .assertEqual (len (set (base .glob ("*" , follow_symlinks = True ))), 3 )
1975
1982
subdir .mkdir ()
1976
- self .assertEqual (len (set (base .glob ("*" ))), 4 )
1983
+ self .assertEqual (len (set (base .glob ("*" , follow_symlinks = True ))), 4 )
1977
1984
subdir .chmod (000 )
1978
- self .assertEqual (len (set (base .glob ("*" ))), 4 )
1985
+ self .assertEqual (len (set (base .glob ("*" , follow_symlinks = True ))), 4 )
1979
1986
1980
1987
def _check_resolve (self , p , expected , strict = True ):
1981
1988
q = p .resolve (strict )
@@ -2894,7 +2901,7 @@ def test_unsupported_flavour(self):
2894
2901
def test_glob_empty_pattern (self ):
2895
2902
p = self .cls ()
2896
2903
with self .assertRaisesRegex (ValueError , 'Unacceptable pattern' ):
2897
- list (p .glob ('' ))
2904
+ list (p .glob ('' , follow_symlinks = True ))
2898
2905
2899
2906
2900
2907
@only_posix
@@ -2987,18 +2994,18 @@ def test_resolve_loop(self):
2987
2994
def test_glob (self ):
2988
2995
P = self .cls
2989
2996
p = P (BASE )
2990
- given = set (p .glob ("FILEa" ))
2997
+ given = set (p .glob ("FILEa" , follow_symlinks = True ))
2991
2998
expect = set () if not os_helper .fs_is_case_insensitive (BASE ) else given
2992
2999
self .assertEqual (given , expect )
2993
- self .assertEqual (set (p .glob ("FILEa*" )), set ())
3000
+ self .assertEqual (set (p .glob ("FILEa*" , follow_symlinks = True )), set ())
2994
3001
2995
3002
def test_rglob (self ):
2996
3003
P = self .cls
2997
3004
p = P (BASE , "dirC" )
2998
- given = set (p .rglob ("FILEd" ))
3005
+ given = set (p .rglob ("FILEd" , follow_symlinks = True ))
2999
3006
expect = set () if not os_helper .fs_is_case_insensitive (BASE ) else given
3000
3007
self .assertEqual (given , expect )
3001
- self .assertEqual (set (p .rglob ("FILEd*" )), set ())
3008
+ self .assertEqual (set (p .rglob ("FILEd*" , follow_symlinks = True )), set ())
3002
3009
3003
3010
@unittest .skipUnless (hasattr (pwd , 'getpwall' ),
3004
3011
'pwd module does not expose getpwall()' )
@@ -3061,7 +3068,7 @@ def test_expanduser(self):
3061
3068
"Bad file descriptor in /dev/fd affects only macOS" )
3062
3069
def test_handling_bad_descriptor (self ):
3063
3070
try :
3064
- file_descriptors = list (pathlib .Path ('/dev/fd' ).rglob ("*" ))[3 :]
3071
+ file_descriptors = list (pathlib .Path ('/dev/fd' ).rglob ("*" , follow_symlinks = True ))[3 :]
3065
3072
if not file_descriptors :
3066
3073
self .skipTest ("no file descriptors - issue was not reproduced" )
3067
3074
# Checking all file descriptors because there is no guarantee
@@ -3133,18 +3140,18 @@ def test_absolute(self):
3133
3140
def test_glob (self ):
3134
3141
P = self .cls
3135
3142
p = P (BASE )
3136
- self .assertEqual (set (p .glob ("FILEa" )), { P (BASE , "fileA" ) })
3137
- self .assertEqual (set (p .glob ("*a\\ " )), { P (BASE , "dirA" ) })
3138
- self .assertEqual (set (p .glob ("F*a" )), { P (BASE , "fileA" ) })
3139
- self .assertEqual (set (map (str , p .glob ("FILEa" ))), {f"{ p } \\ fileA" })
3140
- self .assertEqual (set (map (str , p .glob ("F*a" ))), {f"{ p } \\ fileA" })
3143
+ self .assertEqual (set (p .glob ("FILEa" , follow_symlinks = True )), { P (BASE , "fileA" ) })
3144
+ self .assertEqual (set (p .glob ("*a\\ " , follow_symlinks = True )), { P (BASE , "dirA" ) })
3145
+ self .assertEqual (set (p .glob ("F*a" , follow_symlinks = True )), { P (BASE , "fileA" ) })
3146
+ self .assertEqual (set (map (str , p .glob ("FILEa" , follow_symlinks = True ))), {f"{ p } \\ fileA" })
3147
+ self .assertEqual (set (map (str , p .glob ("F*a" , follow_symlinks = True ))), {f"{ p } \\ fileA" })
3141
3148
3142
3149
def test_rglob (self ):
3143
3150
P = self .cls
3144
3151
p = P (BASE , "dirC" )
3145
- self .assertEqual (set (p .rglob ("FILEd" )), { P (BASE , "dirC/dirD/fileD" ) })
3146
- self .assertEqual (set (p .rglob ("*\\ " )), { P (BASE , "dirC/dirD" ) })
3147
- self .assertEqual (set (map (str , p .rglob ("FILEd" ))), {f"{ p } \\ dirD\\ fileD" })
3152
+ self .assertEqual (set (p .rglob ("FILEd" , follow_symlinks = True )), { P (BASE , "dirC/dirD/fileD" ) })
3153
+ self .assertEqual (set (p .rglob ("*\\ " , follow_symlinks = True )), { P (BASE , "dirC/dirD" ) })
3154
+ self .assertEqual (set (map (str , p .rglob ("FILEd" , follow_symlinks = True ))), {f"{ p } \\ dirD\\ fileD" })
3148
3155
3149
3156
def test_expanduser (self ):
3150
3157
P = self .cls
0 commit comments