diff options
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/pickle.py | 3 | ||||
-rw-r--r-- | Lib/test/pickletester.py | 19 |
2 files changed, 18 insertions, 4 deletions
diff --git a/Lib/pickle.py b/Lib/pickle.py index cb768b2..a67ac7d 100644 --- a/Lib/pickle.py +++ b/Lib/pickle.py @@ -852,7 +852,10 @@ class _Pickler: 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) diff --git a/Lib/test/pickletester.py b/Lib/test/pickletester.py index f6fda9e..f8f3bc9 100644 --- a/Lib/test/pickletester.py +++ b/Lib/test/pickletester.py @@ -3067,22 +3067,20 @@ class BadGetattr: 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() @@ -3106,6 +3104,19 @@ class AbstractPickleModuleTests(unittest.TestCase): 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. |