Skip to content

Commit 3d85ae2

Browse files
committed
pythonGH-119518: Stop interning strings in pathlib
Remove `sys.intern(str(x))` calls when normalizing a path in pathlib. This speeds up `str(Path('foo/bar'))` by about 10%.
1 parent 7bd6ebf commit 3d85ae2

File tree

3 files changed

+3
-11
lines changed

3 files changed

+3
-11
lines changed

Lib/pathlib/_local.py

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

279278
@property
280279
def _raw_path(self):

Lib/test/test_pathlib/test_pathlib.py

Lines changed: 0 additions & 9 deletions
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'))
Lines changed: 2 additions & 0 deletions
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)