Skip to content

Commit

Permalink
Restore part interning, tests changes.
Browse files Browse the repository at this point in the history
  • Loading branch information
barneygale committed Dec 18, 2023
1 parent 0799d7a commit 9720628
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 14 deletions.
3 changes: 2 additions & 1 deletion Lib/pathlib/_abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ def _parse_path(cls, path):
elif len(drv_parts) == 6:
# e.g. //?/unc/server/share
root = sep
parsed = [x for x in rel.split(sep) if x and x != '.']
parsed = [sys.intern(str(x)) for x in rel.split(sep) if x and x != '.']
return drv, root, parsed

def _load_parts(self):
Expand Down Expand Up @@ -531,6 +531,7 @@ def match(self, path_pattern, *, case_sensitive=None):
return match(str(self)) is not None



class PathBase(PurePathBase):
"""Base class for concrete path objects.
Expand Down
21 changes: 8 additions & 13 deletions Lib/test/test_pathlib/test_pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,19 +60,14 @@ def test_div_nested(self):

def test_pickling_common(self):
P = self.cls
paths = [
P('a'), P('a', 'b'), P('a/b'), P('a', 'b', 'c'), P('a/b/c'),
P('/'), P('/a', 'b'), P('/a/b'), P('/a', 'b', 'c'), P('/a/b/c'),
]
for p in paths:
with self.subTest(path=p):
for proto in range(0, pickle.HIGHEST_PROTOCOL + 1):
dumped = pickle.dumps(p, proto)
pp = pickle.loads(dumped)
self.assertIs(pp.__class__, p.__class__)
self.assertEqual(pp, p)
self.assertEqual(hash(pp), hash(p))
self.assertEqual(str(pp), str(p))
p = P('/a/b')
for proto in range(0, pickle.HIGHEST_PROTOCOL + 1):
dumped = pickle.dumps(p, proto)
pp = pickle.loads(dumped)
self.assertIs(pp.__class__, p.__class__)
self.assertEqual(pp, p)
self.assertEqual(hash(pp), hash(p))
self.assertEqual(str(pp), str(p))

def test_fspath_common(self):
P = self.cls
Expand Down
7 changes: 7 additions & 0 deletions Lib/test/test_pathlib/test_pathlib_abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -1655,6 +1655,13 @@ def test_is_char_device_false(self):
self.assertIs((P / 'fileA\udfff').is_char_device(), False)
self.assertIs((P / 'fileA\x00').is_char_device(), False)

def test_pickling_common(self):
p = self.cls(self.base, 'fileA')
for proto in range(0, pickle.HIGHEST_PROTOCOL + 1):
dumped = pickle.dumps(p, proto)
pp = pickle.loads(dumped)
self.assertEqual(pp.stat(), p.stat())

def test_parts_interning(self):
P = self.cls
p = P('/usr/bin/foo')
Expand Down

0 comments on commit 9720628

Please sign in to comment.