Skip to content

Commit 336d445

Browse files
[3.11] gh-113028: Correctly memoize str in pickle when escapes added (GH-113436) (GH-113449)
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. (cherry picked from commit 0839863) Co-authored-by: Jeff Allen <[email protected]>
1 parent b60bddb commit 336d445

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
@@ -861,13 +861,13 @@ def save_str(self, obj):
861861
else:
862862
self.write(BINUNICODE + pack("<I", n) + encoded)
863863
else:
864-
obj = obj.replace("\\", "\\u005c")
865-
obj = obj.replace("\0", "\\u0000")
866-
obj = obj.replace("\n", "\\u000a")
867-
obj = obj.replace("\r", "\\u000d")
868-
obj = obj.replace("\x1a", "\\u001a") # EOF on DOS
869-
self.write(UNICODE + obj.encode('raw-unicode-escape') +
870-
b'\n')
864+
# Escape what raw-unicode-escape doesn't, but memoize the original.
865+
tmp = obj.replace("\\", "\\u005c")
866+
tmp = tmp.replace("\0", "\\u0000")
867+
tmp = tmp.replace("\n", "\\u000a")
868+
tmp = tmp.replace("\r", "\\u000d")
869+
tmp = tmp.replace("\x1a", "\\u001a") # EOF on DOS
870+
self.write(UNICODE + tmp.encode('raw-unicode-escape') + b'\n')
871871
self.memoize(obj)
872872
dispatch[str] = save_str
873873

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)