@@ -53,7 +53,15 @@ def test_parser(self):
53
53
54
54
55
55
class 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 ''
57
65
58
66
def __eq__ (self , other ):
59
67
if not isinstance (other , DummyPurePath ):
@@ -66,6 +74,9 @@ def __hash__(self):
66
74
def __repr__ (self ):
67
75
return "{}({!r})" .format (self .__class__ .__name__ , self .as_posix ())
68
76
77
+ def with_segments (self , * pathsegments ):
78
+ return type (self )(* pathsegments )
79
+
69
80
70
81
class DummyPurePathTest (unittest .TestCase ):
71
82
cls = DummyPurePath
@@ -97,30 +108,11 @@ def test_constructor_common(self):
97
108
P ('a/b/c' )
98
109
P ('/a/b/c' )
99
110
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 ('' ))
124
116
125
117
def _check_str_subclass (self , * args ):
126
118
# Issue #21127: it should be possible to construct a PurePath object
@@ -1286,36 +1278,6 @@ def test_is_absolute_windows(self):
1286
1278
# Tests for the virtual classes.
1287
1279
#
1288
1280
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
-
1319
1281
1320
1282
class DummyPathIO (io .BytesIO ):
1321
1283
"""
@@ -1342,11 +1304,19 @@ class DummyPath(PathBase):
1342
1304
Simple implementation of PathBase that keeps files and directories in
1343
1305
memory.
1344
1306
"""
1345
- __slots__ = ()
1307
+ __slots__ = ('_segments' )
1346
1308
1347
1309
_files = {}
1348
1310
_directories = {}
1349
1311
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
+
1350
1320
def __eq__ (self , other ):
1351
1321
if not isinstance (other , DummyPath ):
1352
1322
return NotImplemented
@@ -1358,6 +1328,9 @@ def __hash__(self):
1358
1328
def __repr__ (self ):
1359
1329
return "{}({!r})" .format (self .__class__ .__name__ , self .as_posix ())
1360
1330
1331
+ def with_segments (self , * pathsegments ):
1332
+ return type (self )(* pathsegments )
1333
+
1361
1334
def stat (self , * , follow_symlinks = True ):
1362
1335
path = str (self ).rstrip ('/' )
1363
1336
if path in self ._files :
0 commit comments