99from test import test_genericpath
1010from test .support import import_helper
1111from test .support import os_helper
12- from test .support .os_helper import FakePath
12+ from test .support .os_helper import FakePath , TESTFN
1313from unittest import mock
1414
1515try :
2121# An absolute path to a temporary filename for testing. We can't rely on TESTFN
2222# being an absolute path, so we need this.
2323
24- ABSTFN = abspath (os_helper . TESTFN )
24+ ABSTFN = abspath (TESTFN )
2525
2626def skip_if_ABSTFN_contains_backslash (test ):
2727 """
@@ -33,21 +33,11 @@ def skip_if_ABSTFN_contains_backslash(test):
3333 msg = "ABSTFN is not a posix path - tests fail"
3434 return [test , unittest .skip (msg )(test )][found_backslash ]
3535
36- def safe_rmdir (dirname ):
37- try :
38- os .rmdir (dirname )
39- except OSError :
40- pass
41-
4236class PosixPathTest (unittest .TestCase ):
4337
4438 def setUp (self ):
45- self .tearDown ()
46-
47- def tearDown (self ):
4839 for suffix in ["" , "1" , "2" ]:
49- os_helper .unlink (os_helper .TESTFN + suffix )
50- safe_rmdir (os_helper .TESTFN + suffix )
40+ self .assertFalse (posixpath .lexists (ABSTFN + suffix ))
5141
5242 def test_join (self ):
5343 fn = posixpath .join
@@ -194,25 +184,28 @@ def test_dirname(self):
194184 self .assertEqual (posixpath .dirname (b"//foo//bar" ), b"//foo" )
195185
196186 def test_islink (self ):
197- self .assertIs (posixpath .islink (os_helper . TESTFN + "1" ), False )
198- self .assertIs (posixpath .lexists (os_helper . TESTFN + "2" ), False )
187+ self .assertIs (posixpath .islink (TESTFN + "1" ), False )
188+ self .assertIs (posixpath .lexists (TESTFN + "2" ), False )
199189
200- with open (os_helper .TESTFN + "1" , "wb" ) as f :
190+ self .addCleanup (os_helper .unlink , TESTFN + "1" )
191+ with open (TESTFN + "1" , "wb" ) as f :
201192 f .write (b"foo" )
202- self .assertIs (posixpath .islink (os_helper . TESTFN + "1" ), False )
193+ self .assertIs (posixpath .islink (TESTFN + "1" ), False )
203194
204195 if os_helper .can_symlink ():
205- os .symlink (os_helper .TESTFN + "1" , os_helper .TESTFN + "2" )
206- self .assertIs (posixpath .islink (os_helper .TESTFN + "2" ), True )
207- os .remove (os_helper .TESTFN + "1" )
208- self .assertIs (posixpath .islink (os_helper .TESTFN + "2" ), True )
209- self .assertIs (posixpath .exists (os_helper .TESTFN + "2" ), False )
210- self .assertIs (posixpath .lexists (os_helper .TESTFN + "2" ), True )
211-
212- self .assertIs (posixpath .islink (os_helper .TESTFN + "\udfff " ), False )
213- self .assertIs (posixpath .islink (os .fsencode (os_helper .TESTFN ) + b"\xff " ), False )
214- self .assertIs (posixpath .islink (os_helper .TESTFN + "\x00 " ), False )
215- self .assertIs (posixpath .islink (os .fsencode (os_helper .TESTFN ) + b"\x00 " ), False )
196+ self .addCleanup (os_helper .unlink , TESTFN + "2" )
197+ os .symlink (TESTFN + "1" , TESTFN + "2" )
198+ self .assertIs (posixpath .islink (TESTFN + "2" ), True )
199+ os .remove (TESTFN + "1" )
200+ self .assertIs (posixpath .islink (TESTFN + "2" ), True )
201+ self .assertIs (posixpath .exists (TESTFN + "2" ), False )
202+ self .assertIs (posixpath .lexists (TESTFN + "2" ), True )
203+
204+ def test_islink_invalid_paths (self ):
205+ self .assertIs (posixpath .islink (TESTFN + "\udfff " ), False )
206+ self .assertIs (posixpath .islink (os .fsencode (TESTFN ) + b"\xff " ), False )
207+ self .assertIs (posixpath .islink (TESTFN + "\x00 " ), False )
208+ self .assertIs (posixpath .islink (os .fsencode (TESTFN ) + b"\x00 " ), False )
216209
217210 def test_ismount (self ):
218211 self .assertIs (posixpath .ismount ("/" ), True )
@@ -227,7 +220,7 @@ def test_ismount_non_existent(self):
227220 os .mkdir (ABSTFN )
228221 self .assertIs (posixpath .ismount (ABSTFN ), False )
229222 finally :
230- safe_rmdir (ABSTFN )
223+ os_helper . rmdir (ABSTFN )
231224
232225 self .assertIs (posixpath .ismount ('/\udfff ' ), False )
233226 self .assertIs (posixpath .ismount (b'/\xff ' ), False )
@@ -241,7 +234,7 @@ def test_ismount_symlinks(self):
241234 os .symlink ("/" , ABSTFN )
242235 self .assertIs (posixpath .ismount (ABSTFN ), False )
243236 finally :
244- os .unlink (ABSTFN )
237+ os_helper .unlink (ABSTFN )
245238
246239 @unittest .skipIf (posix is None , "Test requires posix module" )
247240 def test_ismount_different_device (self ):
@@ -502,10 +495,10 @@ def test_realpath_relative(self):
502495 @skip_if_ABSTFN_contains_backslash
503496 def test_realpath_missing_pardir (self ):
504497 try :
505- os .symlink (os_helper . TESTFN + "1" , os_helper . TESTFN )
506- self .assertEqual (realpath ("nonexistent/../" + os_helper . TESTFN ), ABSTFN + "1" )
498+ os .symlink (TESTFN + "1" , TESTFN )
499+ self .assertEqual (realpath ("nonexistent/../" + TESTFN ), ABSTFN + "1" )
507500 finally :
508- os_helper .unlink (os_helper . TESTFN )
501+ os_helper .unlink (TESTFN )
509502
510503 @os_helper .skip_unless_symlink
511504 @skip_if_ABSTFN_contains_backslash
@@ -601,7 +594,7 @@ def test_realpath_repeated_indirect_symlinks(self):
601594 finally :
602595 os_helper .unlink (ABSTFN + '/self' )
603596 os_helper .unlink (ABSTFN + '/link' )
604- safe_rmdir (ABSTFN )
597+ os_helper . rmdir (ABSTFN )
605598
606599 @os_helper .skip_unless_symlink
607600 @skip_if_ABSTFN_contains_backslash
@@ -620,7 +613,7 @@ def test_realpath_deep_recursion(self):
620613 finally :
621614 for i in range (depth + 1 ):
622615 os_helper .unlink (ABSTFN + '/%d' % i )
623- safe_rmdir (ABSTFN )
616+ os_helper . rmdir (ABSTFN )
624617
625618 @os_helper .skip_unless_symlink
626619 @skip_if_ABSTFN_contains_backslash
@@ -638,8 +631,8 @@ def test_realpath_resolve_parents(self):
638631 self .assertEqual (realpath ("a" ), ABSTFN + "/y/a" )
639632 finally :
640633 os_helper .unlink (ABSTFN + "/k" )
641- safe_rmdir (ABSTFN + "/y" )
642- safe_rmdir (ABSTFN )
634+ os_helper . rmdir (ABSTFN + "/y" )
635+ os_helper . rmdir (ABSTFN )
643636
644637 @os_helper .skip_unless_symlink
645638 @skip_if_ABSTFN_contains_backslash
@@ -665,9 +658,9 @@ def test_realpath_resolve_before_normalizing(self):
665658 ABSTFN + "/k" )
666659 finally :
667660 os_helper .unlink (ABSTFN + "/link-y" )
668- safe_rmdir (ABSTFN + "/k/y" )
669- safe_rmdir (ABSTFN + "/k" )
670- safe_rmdir (ABSTFN )
661+ os_helper . rmdir (ABSTFN + "/k/y" )
662+ os_helper . rmdir (ABSTFN + "/k" )
663+ os_helper . rmdir (ABSTFN )
671664
672665 @os_helper .skip_unless_symlink
673666 @skip_if_ABSTFN_contains_backslash
@@ -685,8 +678,8 @@ def test_realpath_resolve_first(self):
685678 self .assertEqual (realpath (base + "link/k" ), ABSTFN + "/k" )
686679 finally :
687680 os_helper .unlink (ABSTFN + "link" )
688- safe_rmdir (ABSTFN + "/k" )
689- safe_rmdir (ABSTFN )
681+ os_helper . rmdir (ABSTFN + "/k" )
682+ os_helper . rmdir (ABSTFN )
690683
691684 @os_helper .skip_unless_symlink
692685 @skip_if_ABSTFN_contains_backslash
@@ -704,7 +697,7 @@ def test_realpath_unreadable_symlink(self):
704697 realpath (ABSTFN , strict = True )
705698 finally :
706699 os .chmod (ABSTFN , 0o755 , follow_symlinks = False )
707- os .unlink (ABSTFN )
700+ os_helper .unlink (ABSTFN )
708701
709702 @skip_if_ABSTFN_contains_backslash
710703 def test_realpath_nonterminal_file (self ):
@@ -743,6 +736,7 @@ def test_realpath_nonterminal_symlink_to_file(self):
743736 self .assertRaises (NotADirectoryError , realpath , ABSTFN + "/subdir" , strict = True )
744737 finally :
745738 os_helper .unlink (ABSTFN )
739+ os_helper .unlink (ABSTFN + "1" )
746740
747741 @os_helper .skip_unless_symlink
748742 @skip_if_ABSTFN_contains_backslash
@@ -764,6 +758,8 @@ def test_realpath_nonterminal_symlink_to_symlinks_to_file(self):
764758 self .assertRaises (NotADirectoryError , realpath , ABSTFN + "/subdir" , strict = True )
765759 finally :
766760 os_helper .unlink (ABSTFN )
761+ os_helper .unlink (ABSTFN + "1" )
762+ os_helper .unlink (ABSTFN + "2" )
767763
768764 def test_relpath (self ):
769765 (real_getcwd , os .getcwd ) = (os .getcwd , lambda : r"/home/user/bar" )
@@ -889,8 +885,8 @@ class PathLikeTests(unittest.TestCase):
889885 path = posixpath
890886
891887 def setUp (self ):
892- self .file_name = os_helper . TESTFN
893- self .file_path = FakePath (os_helper . TESTFN )
888+ self .file_name = TESTFN
889+ self .file_path = FakePath (TESTFN )
894890 self .addCleanup (os_helper .unlink , self .file_name )
895891 with open (self .file_name , 'xb' , 0 ) as file :
896892 file .write (b"test_posixpath.PathLikeTests" )
0 commit comments