mirror of
				https://github.com/python/cpython.git
				synced 2025-10-31 13:41:24 +00:00 
			
		
		
		
	GH-127807: pathlib ABCs: remove PurePathBase._raw_paths (#127883)
				
					
				
			Remove the `PurePathBase` initializer, and make `with_segments()` and `__str__()` abstract. This allows us to drop the `_raw_paths` attribute, and also the `Parser.join()` protocol method.
This commit is contained in:
		
							parent
							
								
									2a66dd33df
								
							
						
					
					
						commit
						a959ea1b0a
					
				
					 5 changed files with 92 additions and 96 deletions
				
			
		|  | @ -53,7 +53,15 @@ def test_parser(self): | |||
| 
 | ||||
| 
 | ||||
| class DummyPurePath(PurePathBase): | ||||
|     __slots__ = () | ||||
|     __slots__ = ('_segments',) | ||||
| 
 | ||||
|     def __init__(self, *segments): | ||||
|         self._segments = segments | ||||
| 
 | ||||
|     def __str__(self): | ||||
|         if self._segments: | ||||
|             return self.parser.join(*self._segments) | ||||
|         return '' | ||||
| 
 | ||||
|     def __eq__(self, other): | ||||
|         if not isinstance(other, DummyPurePath): | ||||
|  | @ -66,6 +74,9 @@ def __hash__(self): | |||
|     def __repr__(self): | ||||
|         return "{}({!r})".format(self.__class__.__name__, self.as_posix()) | ||||
| 
 | ||||
|     def with_segments(self, *pathsegments): | ||||
|         return type(self)(*pathsegments) | ||||
| 
 | ||||
| 
 | ||||
| class DummyPurePathTest(unittest.TestCase): | ||||
|     cls = DummyPurePath | ||||
|  | @ -97,30 +108,11 @@ def test_constructor_common(self): | |||
|         P('a/b/c') | ||||
|         P('/a/b/c') | ||||
| 
 | ||||
|     def test_bytes(self): | ||||
|         P = self.cls | ||||
|         with self.assertRaises(TypeError): | ||||
|             P(b'a') | ||||
|         with self.assertRaises(TypeError): | ||||
|             P(b'a', 'b') | ||||
|         with self.assertRaises(TypeError): | ||||
|             P('a', b'b') | ||||
|         with self.assertRaises(TypeError): | ||||
|             P('a').joinpath(b'b') | ||||
|         with self.assertRaises(TypeError): | ||||
|             P('a') / b'b' | ||||
|         with self.assertRaises(TypeError): | ||||
|             b'a' / P('b') | ||||
|         with self.assertRaises(TypeError): | ||||
|             P('a').match(b'b') | ||||
|         with self.assertRaises(TypeError): | ||||
|             P('a').relative_to(b'b') | ||||
|         with self.assertRaises(TypeError): | ||||
|             P('a').with_name(b'b') | ||||
|         with self.assertRaises(TypeError): | ||||
|             P('a').with_stem(b'b') | ||||
|         with self.assertRaises(TypeError): | ||||
|             P('a').with_suffix(b'b') | ||||
|     def test_fspath_common(self): | ||||
|         self.assertRaises(TypeError, os.fspath, self.cls('')) | ||||
| 
 | ||||
|     def test_as_bytes_common(self): | ||||
|         self.assertRaises(TypeError, bytes, self.cls('')) | ||||
| 
 | ||||
|     def _check_str_subclass(self, *args): | ||||
|         # Issue #21127: it should be possible to construct a PurePath object | ||||
|  | @ -1286,36 +1278,6 @@ def test_is_absolute_windows(self): | |||
| # Tests for the virtual classes. | ||||
| # | ||||
| 
 | ||||
| class PathBaseTest(PurePathBaseTest): | ||||
|     cls = PathBase | ||||
| 
 | ||||
|     def test_not_implemented_error(self): | ||||
|         p = self.cls('') | ||||
|         e = NotImplementedError | ||||
|         self.assertRaises(e, p.stat) | ||||
|         self.assertRaises(e, p.exists) | ||||
|         self.assertRaises(e, p.is_dir) | ||||
|         self.assertRaises(e, p.is_file) | ||||
|         self.assertRaises(e, p.is_symlink) | ||||
|         self.assertRaises(e, p.open) | ||||
|         self.assertRaises(e, p.read_bytes) | ||||
|         self.assertRaises(e, p.read_text) | ||||
|         self.assertRaises(e, p.write_bytes, b'foo') | ||||
|         self.assertRaises(e, p.write_text, 'foo') | ||||
|         self.assertRaises(e, p.iterdir) | ||||
|         self.assertRaises(e, lambda: list(p.glob('*'))) | ||||
|         self.assertRaises(e, lambda: list(p.rglob('*'))) | ||||
|         self.assertRaises(e, lambda: list(p.walk())) | ||||
|         self.assertRaises(e, p.readlink) | ||||
|         self.assertRaises(e, p.symlink_to, 'foo') | ||||
|         self.assertRaises(e, p.mkdir) | ||||
| 
 | ||||
|     def test_fspath_common(self): | ||||
|         self.assertRaises(TypeError, os.fspath, self.cls('')) | ||||
| 
 | ||||
|     def test_as_bytes_common(self): | ||||
|         self.assertRaises(TypeError, bytes, self.cls('')) | ||||
| 
 | ||||
| 
 | ||||
| class DummyPathIO(io.BytesIO): | ||||
|     """ | ||||
|  | @ -1342,11 +1304,19 @@ class DummyPath(PathBase): | |||
|     Simple implementation of PathBase that keeps files and directories in | ||||
|     memory. | ||||
|     """ | ||||
|     __slots__ = () | ||||
|     __slots__ = ('_segments') | ||||
| 
 | ||||
|     _files = {} | ||||
|     _directories = {} | ||||
| 
 | ||||
|     def __init__(self, *segments): | ||||
|         self._segments = segments | ||||
| 
 | ||||
|     def __str__(self): | ||||
|         if self._segments: | ||||
|             return self.parser.join(*self._segments) | ||||
|         return '' | ||||
| 
 | ||||
|     def __eq__(self, other): | ||||
|         if not isinstance(other, DummyPath): | ||||
|             return NotImplemented | ||||
|  | @ -1358,6 +1328,9 @@ def __hash__(self): | |||
|     def __repr__(self): | ||||
|         return "{}({!r})".format(self.__class__.__name__, self.as_posix()) | ||||
| 
 | ||||
|     def with_segments(self, *pathsegments): | ||||
|         return type(self)(*pathsegments) | ||||
| 
 | ||||
|     def stat(self, *, follow_symlinks=True): | ||||
|         path = str(self).rstrip('/') | ||||
|         if path in self._files: | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Barney Gale
						Barney Gale