From: Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> Date: Thu, 16 Nov 2017 00:28:25 +0000 (-0800) Subject: bpo-32034: Make IncompleteReadError & LimitOverrunError pickleable GH-4409 (#4411) X-Git-Tag: v3.6.4rc1~64 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=f35076a002b958f991d180d6f945344cc5ab3900;p=python bpo-32034: Make IncompleteReadError & LimitOverrunError pickleable GH-4409 (#4411) (cherry picked from commit 43605e6bfa8d49612df4a38460d063d6ba781906) --- diff --git a/Lib/asyncio/streams.py b/Lib/asyncio/streams.py index a82cc79aca..4089b0e00a 100644 --- a/Lib/asyncio/streams.py +++ b/Lib/asyncio/streams.py @@ -35,6 +35,9 @@ class IncompleteReadError(EOFError): self.partial = partial self.expected = expected + def __reduce__(self): + return type(self), (self.partial, self.expected) + class LimitOverrunError(Exception): """Reached the buffer limit while looking for a separator. @@ -46,6 +49,9 @@ class LimitOverrunError(Exception): super().__init__(message) self.consumed = consumed + def __reduce__(self): + return type(self), (self.args[0], self.consumed) + @coroutine def open_connection(host=None, port=None, *, diff --git a/Lib/test/test_asyncio/test_streams.py b/Lib/test/test_asyncio/test_streams.py index b47433a4cf..6d16d20079 100644 --- a/Lib/test/test_asyncio/test_streams.py +++ b/Lib/test/test_asyncio/test_streams.py @@ -3,6 +3,7 @@ import gc import os import queue +import pickle import socket import sys import threading @@ -845,6 +846,23 @@ os.close(fd) stream._transport.__repr__.return_value = "" self.assertEqual(">", repr(stream)) + def test_IncompleteReadError_pickleable(self): + e = asyncio.IncompleteReadError(b'abc', 10) + for proto in range(pickle.HIGHEST_PROTOCOL + 1): + with self.subTest(pickle_protocol=proto): + e2 = pickle.loads(pickle.dumps(e, protocol=proto)) + self.assertEqual(str(e), str(e2)) + self.assertEqual(e.partial, e2.partial) + self.assertEqual(e.expected, e2.expected) + + def test_LimitOverrunError_pickleable(self): + e = asyncio.LimitOverrunError('message', 10) + for proto in range(pickle.HIGHEST_PROTOCOL + 1): + with self.subTest(pickle_protocol=proto): + e2 = pickle.loads(pickle.dumps(e, protocol=proto)) + self.assertEqual(str(e), str(e2)) + self.assertEqual(e.consumed, e2.consumed) + if __name__ == '__main__': unittest.main() diff --git a/Misc/NEWS.d/next/Library/2017-11-15-13-44-28.bpo-32034.uHAOmu.rst b/Misc/NEWS.d/next/Library/2017-11-15-13-44-28.bpo-32034.uHAOmu.rst new file mode 100644 index 0000000000..828e8cde57 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2017-11-15-13-44-28.bpo-32034.uHAOmu.rst @@ -0,0 +1 @@ +Make asyncio.IncompleteReadError and LimitOverrunError pickleable.