mirror of
https://github.com/python/cpython.git
synced 2025-08-31 22:18:28 +00:00
GH-101362: Optimise PurePath(PurePath(...)) (GH-101667)
The previous `_parse_args()` method pulled the `_parts` out of any supplied `PurePath` objects; these were subsequently joined in `_from_parts()` using `os.path.join()`. This is actually a slower form of joining than calling `fspath()` on the path object, because it doesn't take advantage of the fact that the contents of `_parts` is normalized! This reduces the time taken to run `PurePath("foo", "bar")` by ~20%, and the time taken to run `PurePath(p, "cheese")`, where `p = PurePath("/foo", "bar", "baz")`, by ~40%. Automerge-Triggered-By: GH:AlexWaygood
This commit is contained in:
parent
3e60e0213e
commit
6716254e71
4 changed files with 45 additions and 27 deletions
|
@ -166,6 +166,33 @@ class _BasePurePathTest(object):
|
|||
self.assertEqual(P(P('a'), P('b')), P('a/b'))
|
||||
self.assertEqual(P(P('a'), P('b'), P('c')), P(FakePath("a/b/c")))
|
||||
|
||||
def test_bytes(self):
|
||||
P = self.cls
|
||||
message = (r"argument should be a str or an os\.PathLike object "
|
||||
r"where __fspath__ returns a str, not 'bytes'")
|
||||
with self.assertRaisesRegex(TypeError, message):
|
||||
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 _check_str_subclass(self, *args):
|
||||
# Issue #21127: it should be possible to construct a PurePath object
|
||||
# from a str subclass instance, and it then gets converted to
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue