bpo-32034: Make IncompleteReadError & LimitOverrunError pickleable #4409

This commit is contained in:
Yury Selivanov 2017-11-15 17:14:28 -05:00 committed by GitHub
parent 4bd41c9b52
commit 43605e6bfa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 25 additions and 0 deletions

View File

@ -35,6 +35,9 @@ class IncompleteReadError(EOFError):
self.partial = partial self.partial = partial
self.expected = expected self.expected = expected
def __reduce__(self):
return type(self), (self.partial, self.expected)
class LimitOverrunError(Exception): class LimitOverrunError(Exception):
"""Reached the buffer limit while looking for a separator. """Reached the buffer limit while looking for a separator.
@ -46,6 +49,9 @@ class LimitOverrunError(Exception):
super().__init__(message) super().__init__(message)
self.consumed = consumed self.consumed = consumed
def __reduce__(self):
return type(self), (self.args[0], self.consumed)
@coroutine @coroutine
def open_connection(host=None, port=None, *, def open_connection(host=None, port=None, *,

View File

@ -3,6 +3,7 @@
import gc import gc
import os import os
import queue import queue
import pickle
import socket import socket
import sys import sys
import threading import threading
@ -845,6 +846,23 @@ os.close(fd)
stream._transport.__repr__.return_value = "<Transport>" stream._transport.__repr__.return_value = "<Transport>"
self.assertEqual("<StreamReader t=<Transport>>", repr(stream)) self.assertEqual("<StreamReader t=<Transport>>", 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__': if __name__ == '__main__':
unittest.main() unittest.main()

View File

@ -0,0 +1 @@
Make asyncio.IncompleteReadError and LimitOverrunError pickleable.