@@ -53,7 +53,15 @@ def test_parser(self):
5353
5454
5555class DummyPurePath (PurePathBase ):
56- __slots__ = ()
56+ __slots__ = ('_segments' ,)
57+
58+ def __init__ (self , * segments ):
59+ self ._segments = segments
60+
61+ def __str__ (self ):
62+ if self ._segments :
63+ return self .parser .join (* self ._segments )
64+ return ''
5765
5866 def __eq__ (self , other ):
5967 if not isinstance (other , DummyPurePath ):
@@ -66,6 +74,9 @@ def __hash__(self):
6674 def __repr__ (self ):
6775 return "{}({!r})" .format (self .__class__ .__name__ , self .as_posix ())
6876
77+ def with_segments (self , * pathsegments ):
78+ return type (self )(* pathsegments )
79+
6980
7081class DummyPurePathTest (unittest .TestCase ):
7182 cls = DummyPurePath
@@ -97,30 +108,11 @@ def test_constructor_common(self):
97108 P ('a/b/c' )
98109 P ('/a/b/c' )
99110
100- def test_bytes (self ):
101- P = self .cls
102- with self .assertRaises (TypeError ):
103- P (b'a' )
104- with self .assertRaises (TypeError ):
105- P (b'a' , 'b' )
106- with self .assertRaises (TypeError ):
107- P ('a' , b'b' )
108- with self .assertRaises (TypeError ):
109- P ('a' ).joinpath (b'b' )
110- with self .assertRaises (TypeError ):
111- P ('a' ) / b'b'
112- with self .assertRaises (TypeError ):
113- b'a' / P ('b' )
114- with self .assertRaises (TypeError ):
115- P ('a' ).match (b'b' )
116- with self .assertRaises (TypeError ):
117- P ('a' ).relative_to (b'b' )
118- with self .assertRaises (TypeError ):
119- P ('a' ).with_name (b'b' )
120- with self .assertRaises (TypeError ):
121- P ('a' ).with_stem (b'b' )
122- with self .assertRaises (TypeError ):
123- P ('a' ).with_suffix (b'b' )
111+ def test_fspath_common (self ):
112+ self .assertRaises (TypeError , os .fspath , self .cls ('' ))
113+
114+ def test_as_bytes_common (self ):
115+ self .assertRaises (TypeError , bytes , self .cls ('' ))
124116
125117 def _check_str_subclass (self , * args ):
126118 # Issue #21127: it should be possible to construct a PurePath object
@@ -1286,36 +1278,6 @@ def test_is_absolute_windows(self):
12861278# Tests for the virtual classes.
12871279#
12881280
1289- class PathBaseTest (PurePathBaseTest ):
1290- cls = PathBase
1291-
1292- def test_not_implemented_error (self ):
1293- p = self .cls ('' )
1294- e = NotImplementedError
1295- self .assertRaises (e , p .stat )
1296- self .assertRaises (e , p .exists )
1297- self .assertRaises (e , p .is_dir )
1298- self .assertRaises (e , p .is_file )
1299- self .assertRaises (e , p .is_symlink )
1300- self .assertRaises (e , p .open )
1301- self .assertRaises (e , p .read_bytes )
1302- self .assertRaises (e , p .read_text )
1303- self .assertRaises (e , p .write_bytes , b'foo' )
1304- self .assertRaises (e , p .write_text , 'foo' )
1305- self .assertRaises (e , p .iterdir )
1306- self .assertRaises (e , lambda : list (p .glob ('*' )))
1307- self .assertRaises (e , lambda : list (p .rglob ('*' )))
1308- self .assertRaises (e , lambda : list (p .walk ()))
1309- self .assertRaises (e , p .readlink )
1310- self .assertRaises (e , p .symlink_to , 'foo' )
1311- self .assertRaises (e , p .mkdir )
1312-
1313- def test_fspath_common (self ):
1314- self .assertRaises (TypeError , os .fspath , self .cls ('' ))
1315-
1316- def test_as_bytes_common (self ):
1317- self .assertRaises (TypeError , bytes , self .cls ('' ))
1318-
13191281
13201282class DummyPathIO (io .BytesIO ):
13211283 """
@@ -1342,11 +1304,19 @@ class DummyPath(PathBase):
13421304 Simple implementation of PathBase that keeps files and directories in
13431305 memory.
13441306 """
1345- __slots__ = ()
1307+ __slots__ = ('_segments' )
13461308
13471309 _files = {}
13481310 _directories = {}
13491311
1312+ def __init__ (self , * segments ):
1313+ self ._segments = segments
1314+
1315+ def __str__ (self ):
1316+ if self ._segments :
1317+ return self .parser .join (* self ._segments )
1318+ return ''
1319+
13501320 def __eq__ (self , other ):
13511321 if not isinstance (other , DummyPath ):
13521322 return NotImplemented
@@ -1358,6 +1328,9 @@ def __hash__(self):
13581328 def __repr__ (self ):
13591329 return "{}({!r})" .format (self .__class__ .__name__ , self .as_posix ())
13601330
1331+ def with_segments (self , * pathsegments ):
1332+ return type (self )(* pathsegments )
1333+
13611334 def stat (self , * , follow_symlinks = True ):
13621335 path = str (self ).rstrip ('/' )
13631336 if path in self ._files :
0 commit comments