From 38ab7d4721b422547f7b46b9d68968863fa70573 Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Fri, 31 May 2019 11:29:39 +0300 Subject: [PATCH] bpo-31829: Make protocol 0 pickles be loadable in text mode in Python 2. (GH-11859) Escape ``\r``, ``\0`` and ``\x1a`` (end-of-file on Windows) in Unicode strings. --- Lib/pickle.py | 3 +++ Lib/test/pickletester.py | 19 +++++++++++++++---- .../2017-10-21-12-07-56.bpo-31829.6IhP-O.rst | 3 +++ Modules/_pickle.c | 5 ++++- 4 files changed, 25 insertions(+), 5 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2017-10-21-12-07-56.bpo-31829.6IhP-O.rst diff --git a/Lib/pickle.py b/Lib/pickle.py index cb768b28586..a67ac7dd8b6 100644 --- a/Lib/pickle.py +++ b/Lib/pickle.py @@ -852,7 +852,10 @@ class _Pickler: self.write(BINUNICODE + pack("= 256 || ch == '\\' || ch == '\n') { + else if (ch >= 256 || + ch == '\\' || ch == 0 || ch == '\n' || ch == '\r' || + ch == 0x1a) + { /* -1: subtract 1 preallocated byte */ p = _PyBytesWriter_Prepare(&writer, p, 6-1); if (p == NULL)