6161 INT_MAX = PY_SSIZE_T_MAX = sys .maxsize
6262
6363from test .support .script_helper import assert_python_ok
64- from test .support import unix_shell
64+ from test .support import unix_shell , FakePath
6565
6666
6767root_in_posix = False
@@ -85,21 +85,6 @@ def requires_os_func(name):
8585 return unittest .skipUnless (hasattr (os , name ), 'requires os.%s' % name )
8686
8787
88- class _PathLike (os .PathLike ):
89-
90- def __init__ (self , path = "" ):
91- self .path = path
92-
93- def __str__ (self ):
94- return str (self .path )
95-
96- def __fspath__ (self ):
97- if isinstance (self .path , BaseException ):
98- raise self .path
99- else :
100- return self .path
101-
102-
10388def create_file (filename , content = b'content' ):
10489 with open (filename , "xb" , 0 ) as fp :
10590 fp .write (content )
@@ -942,15 +927,14 @@ def test_walk_prune(self, walk_path=None):
942927 dirs .remove ('SUB1' )
943928
944929 self .assertEqual (len (all ), 2 )
945- self .assertEqual (all [0 ],
946- (str (walk_path ), ["SUB2" ], ["tmp1" ]))
930+ self .assertEqual (all [0 ], (self .walk_path , ["SUB2" ], ["tmp1" ]))
947931
948932 all [1 ][- 1 ].sort ()
949933 all [1 ][1 ].sort ()
950934 self .assertEqual (all [1 ], self .sub2_tree )
951935
952936 def test_file_like_path (self ):
953- self .test_walk_prune (_PathLike (self .walk_path ))
937+ self .test_walk_prune (FakePath (self .walk_path ))
954938
955939 def test_walk_bottom_up (self ):
956940 # Walk bottom-up.
@@ -2288,7 +2272,7 @@ def test_getppid(self):
22882272 def test_waitpid (self ):
22892273 args = [sys .executable , '-c' , 'pass' ]
22902274 # Add an implicit test for PyUnicode_FSConverter().
2291- pid = os .spawnv (os .P_NOWAIT , _PathLike (args [0 ]), args )
2275+ pid = os .spawnv (os .P_NOWAIT , FakePath (args [0 ]), args )
22922276 status = os .waitpid (pid , 0 )
22932277 self .assertEqual (status , (pid , 0 ))
22942278
@@ -3129,13 +3113,13 @@ def test_path_t_converter(self):
31293113 bytes_fspath = bytes_filename = None
31303114 else :
31313115 bytes_filename = support .TESTFN .encode ('ascii' )
3132- bytes_fspath = _PathLike (bytes_filename )
3133- fd = os .open (_PathLike (str_filename ), os .O_WRONLY | os .O_CREAT )
3116+ bytes_fspath = FakePath (bytes_filename )
3117+ fd = os .open (FakePath (str_filename ), os .O_WRONLY | os .O_CREAT )
31343118 self .addCleanup (support .unlink , support .TESTFN )
31353119 self .addCleanup (os .close , fd )
31363120
3137- int_fspath = _PathLike (fd )
3138- str_fspath = _PathLike (str_filename )
3121+ int_fspath = FakePath (fd )
3122+ str_fspath = FakePath (str_filename )
31393123
31403124 for name , allow_fd , extra_args , cleanup_fn in self .functions :
31413125 with self .subTest (name = name ):
@@ -3540,16 +3524,16 @@ def test_return_string(self):
35403524
35413525 def test_fsencode_fsdecode (self ):
35423526 for p in "path/like/object" , b"path/like/object" :
3543- pathlike = _PathLike (p )
3527+ pathlike = FakePath (p )
35443528
35453529 self .assertEqual (p , self .fspath (pathlike ))
35463530 self .assertEqual (b"path/like/object" , os .fsencode (pathlike ))
35473531 self .assertEqual ("path/like/object" , os .fsdecode (pathlike ))
35483532
35493533 def test_pathlike (self ):
3550- self .assertEqual ('#feelthegil' , self .fspath (_PathLike ('#feelthegil' )))
3551- self .assertTrue (issubclass (_PathLike , os .PathLike ))
3552- self .assertTrue (isinstance (_PathLike ( ), os .PathLike ))
3534+ self .assertEqual ('#feelthegil' , self .fspath (FakePath ('#feelthegil' )))
3535+ self .assertTrue (issubclass (FakePath , os .PathLike ))
3536+ self .assertTrue (isinstance (FakePath ( 'x' ), os .PathLike ))
35533537
35543538 def test_garbage_in_exception_out (self ):
35553539 vapor = type ('blah' , (), {})
@@ -3561,14 +3545,14 @@ def test_argument_required(self):
35613545
35623546 def test_bad_pathlike (self ):
35633547 # __fspath__ returns a value other than str or bytes.
3564- self .assertRaises (TypeError , self .fspath , _PathLike (42 ))
3548+ self .assertRaises (TypeError , self .fspath , FakePath (42 ))
35653549 # __fspath__ attribute that is not callable.
35663550 c = type ('foo' , (), {})
35673551 c .__fspath__ = 1
35683552 self .assertRaises (TypeError , self .fspath , c ())
35693553 # __fspath__ raises an exception.
35703554 self .assertRaises (ZeroDivisionError , self .fspath ,
3571- _PathLike (ZeroDivisionError ()))
3555+ FakePath (ZeroDivisionError ()))
35723556
35733557
35743558class TimesTests (unittest .TestCase ):
0 commit comments