Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0839863

Browse files
authored
gh-113028: Correctly memoize str in pickle when escapes added (GH-113436)
This fixes a divergence between the Python and C implementations of pickle for protocol 0, such that it pickle.py fails to re-use the first pickled representation of strings involving characters that have to be escaped.
1 parent 894f0e5 commit 0839863

File tree

3 files changed

+21
-7
lines changed

3 files changed

+21
-7
lines changed

Lib/pickle.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -857,13 +857,13 @@ def save_str(self, obj):
857857
else:
858858
self.write(BINUNICODE + pack("<I", n) + encoded)
859859
else:
860-
obj = obj.replace("\\", "\\u005c")
861-
obj = obj.replace("\0", "\\u0000")
862-
obj = obj.replace("\n", "\\u000a")
863-
obj = obj.replace("\r", "\\u000d")
864-
obj = obj.replace("\x1a", "\\u001a") # EOF on DOS
865-
self.write(UNICODE + obj.encode('raw-unicode-escape') +
866-
b'\n')
860+
# Escape what raw-unicode-escape doesn't, but memoize the original.
861+
tmp = obj.replace("\\", "\\u005c")
862+
tmp = tmp.replace("\0", "\\u0000")
863+
tmp = tmp.replace("\n", "\\u000a")
864+
tmp = tmp.replace("\r", "\\u000d")
865+
tmp = tmp.replace("\x1a", "\\u001a") # EOF on DOS
866+
self.write(UNICODE + tmp.encode('raw-unicode-escape') + b'\n')
867867
self.memoize(obj)
868868
dispatch[str] = save_str
869869

Lib/test/pickletester.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1825,6 +1825,14 @@ def test_unicode_high_plane(self):
18251825
t2 = self.loads(p)
18261826
self.assert_is_copy(t, t2)
18271827

1828+
def test_unicode_memoization(self):
1829+
# Repeated str is re-used (even when escapes added).
1830+
for proto in protocols:
1831+
for s in '', 'xyz', 'xyz\n', 'x\\yz', 'x\xa1yz\r':
1832+
p = self.dumps((s, s), proto)
1833+
s1, s2 = self.loads(p)
1834+
self.assertIs(s1, s2)
1835+
18281836
def test_bytes(self):
18291837
for proto in protocols:
18301838
for s in b'', b'xyz', b'xyz'*100:
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
When a second reference to a string appears in the input to :mod:`pickle`,
2+
and the Python implementation is in use,
3+
we are guaranteed that a single copy gets pickled
4+
and a single object is shared when reloaded.
5+
Previously, in protocol 0, when a string contained certain characters
6+
(e.g. newline) it resulted in duplicate objects.

0 commit comments

Comments
 (0)