Skip to content

Commit 5002f17

Browse files
authored
GH-119518: Stop interning strings in pathlib GH-123356)
Remove `sys.intern(str(x))` calls when normalizing a path in pathlib. This speeds up `str(Path('foo/bar'))` by about 10%.
1 parent 77a2fb4 commit 5002f17

File tree

3 files changed

+3
-11
lines changed

3 files changed

+3
-11
lines changed

Lib/pathlib/_local.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -272,8 +272,7 @@ def _parse_path(cls, path):
272272
elif len(drv_parts) == 6:
273273
# e.g. //?/unc/server/share
274274
root = sep
275-
parsed = [sys.intern(str(x)) for x in rel.split(sep) if x and x != '.']
276-
return drv, root, parsed
275+
return drv, root, [x for x in rel.split(sep) if x and x != '.']
277276

278277
@property
279278
def _raw_path(self):

Lib/test/test_pathlib/test_pathlib.py

-9
Original file line numberDiff line numberDiff line change
@@ -163,15 +163,6 @@ def test_empty_path(self):
163163
# Special case for the empty path.
164164
self._check_str('.', ('',))
165165

166-
def test_parts_interning(self):
167-
P = self.cls
168-
p = P('/usr/bin/foo')
169-
q = P('/usr/local/bin')
170-
# 'usr'
171-
self.assertIs(p.parts[1], q.parts[1])
172-
# 'bin'
173-
self.assertIs(p.parts[2], q.parts[3])
174-
175166
def test_join_nested(self):
176167
P = self.cls
177168
p = P('a/b').joinpath(P('c'))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Speed up normalization of :class:`pathlib.PurePath` and
2+
:class:`~pathlib.Path` objects by not interning string parts.

0 commit comments

Comments
 (0)