6464 INT_MAX = PY_SSIZE_T_MAX = sys .maxsize
6565
6666from test .support .script_helper import assert_python_ok
67- from test .support import unix_shell
67+ from test .support import unix_shell , FakePath
6868
6969
7070root_in_posix = False
@@ -94,21 +94,6 @@ def requires_os_func(name):
9494 return unittest .skipUnless (hasattr (os , name ), 'requires os.%s' % name )
9595
9696
97- class _PathLike (os .PathLike ):
98-
99- def __init__ (self , path = "" ):
100- self .path = path
101-
102- def __str__ (self ):
103- return str (self .path )
104-
105- def __fspath__ (self ):
106- if isinstance (self .path , BaseException ):
107- raise self .path
108- else :
109- return self .path
110-
111-
11297def create_file (filename , content = b'content' ):
11398 with open (filename , "xb" , 0 ) as fp :
11499 fp .write (content )
@@ -970,15 +955,14 @@ def test_walk_prune(self, walk_path=None):
970955 dirs .remove ('SUB1' )
971956
972957 self .assertEqual (len (all ), 2 )
973- self .assertEqual (all [0 ],
974- (str (walk_path ), ["SUB2" ], ["tmp1" ]))
958+ self .assertEqual (all [0 ], (self .walk_path , ["SUB2" ], ["tmp1" ]))
975959
976960 all [1 ][- 1 ].sort ()
977961 all [1 ][1 ].sort ()
978962 self .assertEqual (all [1 ], self .sub2_tree )
979963
980964 def test_file_like_path (self ):
981- self .test_walk_prune (_PathLike (self .walk_path ))
965+ self .test_walk_prune (FakePath (self .walk_path ))
982966
983967 def test_walk_bottom_up (self ):
984968 # Walk bottom-up.
@@ -2294,7 +2278,7 @@ def test_getppid(self):
22942278 def test_waitpid (self ):
22952279 args = [sys .executable , '-c' , 'pass' ]
22962280 # Add an implicit test for PyUnicode_FSConverter().
2297- pid = os .spawnv (os .P_NOWAIT , _PathLike (args [0 ]), args )
2281+ pid = os .spawnv (os .P_NOWAIT , FakePath (args [0 ]), args )
22982282 status = os .waitpid (pid , 0 )
22992283 self .assertEqual (status , (pid , 0 ))
23002284
@@ -3140,13 +3124,13 @@ def test_path_t_converter(self):
31403124 bytes_fspath = bytes_filename = None
31413125 else :
31423126 bytes_filename = support .TESTFN .encode ('ascii' )
3143- bytes_fspath = _PathLike (bytes_filename )
3144- fd = os .open (_PathLike (str_filename ), os .O_WRONLY | os .O_CREAT )
3127+ bytes_fspath = FakePath (bytes_filename )
3128+ fd = os .open (FakePath (str_filename ), os .O_WRONLY | os .O_CREAT )
31453129 self .addCleanup (support .unlink , support .TESTFN )
31463130 self .addCleanup (os .close , fd )
31473131
3148- int_fspath = _PathLike (fd )
3149- str_fspath = _PathLike (str_filename )
3132+ int_fspath = FakePath (fd )
3133+ str_fspath = FakePath (str_filename )
31503134
31513135 for name , allow_fd , extra_args , cleanup_fn in self .functions :
31523136 with self .subTest (name = name ):
@@ -3519,16 +3503,16 @@ def test_return_string(self):
35193503
35203504 def test_fsencode_fsdecode (self ):
35213505 for p in "path/like/object" , b"path/like/object" :
3522- pathlike = _PathLike (p )
3506+ pathlike = FakePath (p )
35233507
35243508 self .assertEqual (p , self .fspath (pathlike ))
35253509 self .assertEqual (b"path/like/object" , os .fsencode (pathlike ))
35263510 self .assertEqual ("path/like/object" , os .fsdecode (pathlike ))
35273511
35283512 def test_pathlike (self ):
3529- self .assertEqual ('#feelthegil' , self .fspath (_PathLike ('#feelthegil' )))
3530- self .assertTrue (issubclass (_PathLike , os .PathLike ))
3531- self .assertTrue (isinstance (_PathLike ( ), os .PathLike ))
3513+ self .assertEqual ('#feelthegil' , self .fspath (FakePath ('#feelthegil' )))
3514+ self .assertTrue (issubclass (FakePath , os .PathLike ))
3515+ self .assertTrue (isinstance (FakePath ( 'x' ), os .PathLike ))
35323516
35333517 def test_garbage_in_exception_out (self ):
35343518 vapor = type ('blah' , (), {})
@@ -3540,14 +3524,14 @@ def test_argument_required(self):
35403524
35413525 def test_bad_pathlike (self ):
35423526 # __fspath__ returns a value other than str or bytes.
3543- self .assertRaises (TypeError , self .fspath , _PathLike (42 ))
3527+ self .assertRaises (TypeError , self .fspath , FakePath (42 ))
35443528 # __fspath__ attribute that is not callable.
35453529 c = type ('foo' , (), {})
35463530 c .__fspath__ = 1
35473531 self .assertRaises (TypeError , self .fspath , c ())
35483532 # __fspath__ raises an exception.
35493533 self .assertRaises (ZeroDivisionError , self .fspath ,
3550- _PathLike (ZeroDivisionError ()))
3534+ FakePath (ZeroDivisionError ()))
35513535
35523536# Only test if the C version is provided, otherwise TestPEP519 already tested
35533537# the pure Python implementation.
0 commit comments