summaryrefslogtreecommitdiffstats
path: root/Lib/pickle.py
diff options
context:
space:
mode:
authorJeff Allen <ja.py@farowl.co.uk>2023-12-24 09:43:44 (GMT)
committerGitHub <noreply@github.com>2023-12-24 09:43:44 (GMT)
commit08398631a0298dcf785ee7bd0e26c7844823ce59 (patch)
treef669f700f6f6b5b91d59825045c00f7dbae4e408 /Lib/pickle.py
parent894f0e573d9eb49cd5864c44328f10a731852dab (diff)
downloadcpython-08398631a0298dcf785ee7bd0e26c7844823ce59.zip
cpython-08398631a0298dcf785ee7bd0e26c7844823ce59.tar.gz
cpython-08398631a0298dcf785ee7bd0e26c7844823ce59.tar.bz2
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.
Diffstat (limited to 'Lib/pickle.py')
-rw-r--r--Lib/pickle.py14
1 files changed, 7 insertions, 7 deletions
diff --git a/Lib/pickle.py b/Lib/pickle.py
index 4f5ad5b..988c088 100644
--- a/Lib/pickle.py
+++ b/Lib/pickle.py
@@ -857,13 +857,13 @@ class _Pickler:
else:
self.write(BINUNICODE + pack("<I", n) + encoded)
else:
- obj = obj.replace("\\", "\\u005c")
- obj = obj.replace("\0", "\\u0000")
- obj = obj.replace("\n", "\\u000a")
- obj = obj.replace("\r", "\\u000d")
- obj = obj.replace("\x1a", "\\u001a") # EOF on DOS
- self.write(UNICODE + obj.encode('raw-unicode-escape') +
- b'\n')
+ # Escape what raw-unicode-escape doesn't, but memoize the original.
+ tmp = obj.replace("\\", "\\u005c")
+ tmp = tmp.replace("\0", "\\u0000")
+ tmp = tmp.replace("\n", "\\u000a")
+ tmp = tmp.replace("\r", "\\u000d")
+ tmp = tmp.replace("\x1a", "\\u001a") # EOF on DOS
+ self.write(UNICODE + tmp.encode('raw-unicode-escape') + b'\n')
self.memoize(obj)
dispatch[str] = save_str