Skip to content

bpo-31829: Make protocol 0 pickles be loadable in text mode in Python 2. #11859

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 31, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Lib/pickle.py
Original file line number Diff line number Diff line change
Expand Up @@ -745,7 +745,10 @@ def save_str(self, obj):
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')
self.memoize(obj)
Expand Down
19 changes: 15 additions & 4 deletions Lib/test/pickletester.py
Original file line number Diff line number Diff line change
Expand Up @@ -2644,22 +2644,20 @@ def __getattr__(self, key):
class AbstractPickleModuleTests(unittest.TestCase):

def test_dump_closed_file(self):
import os
f = open(TESTFN, "wb")
try:
f.close()
self.assertRaises(ValueError, self.dump, 123, f)
finally:
os.remove(TESTFN)
support.unlink(TESTFN)

def test_load_closed_file(self):
import os
f = open(TESTFN, "wb")
try:
f.close()
self.assertRaises(ValueError, self.dump, 123, f)
finally:
os.remove(TESTFN)
support.unlink(TESTFN)

def test_load_from_and_dump_to_file(self):
stream = io.BytesIO()
Expand All @@ -2683,6 +2681,19 @@ def test_callapi(self):
self.Pickler(f, -1)
self.Pickler(f, protocol=-1)

def test_dump_text_file(self):
f = open(TESTFN, "w")
try:
for proto in protocols:
self.assertRaises(TypeError, self.dump, 123, f, proto)
finally:
f.close()
support.unlink(TESTFN)

def test_incomplete_input(self):
s = io.BytesIO(b"X''.")
self.assertRaises((EOFError, struct.error, pickle.UnpicklingError), self.load, s)

def test_bad_init(self):
# Test issue3664 (pickle can segfault from a badly initialized Pickler).
# Override initialization without calling __init__() of the superclass.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
``\r``, ``\0`` and ``\x1a`` (end-of-file on Windows) are now escaped in
protocol 0 pickles of Unicode strings. This allows to load them without loss
from files open in text mode in Python 2.
5 changes: 4 additions & 1 deletion Modules/_pickle.c
Original file line number Diff line number Diff line change
Expand Up @@ -2336,7 +2336,10 @@ raw_unicode_escape(PyObject *obj)
*p++ = Py_hexdigits[ch & 15];
}
/* Map 16-bit characters, '\\' and '\n' to '\uxxxx' */
else if (ch >= 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)
Expand Down