Skip to content

bpo-32034: Make asyncio.IncompleteReadError pickleable. #4409

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 3 commits into from
Nov 15, 2017
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
6 changes: 6 additions & 0 deletions Lib/asyncio/streams.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ def __init__(self, partial, expected):
self.partial = partial
self.expected = expected

def __reduce__(self):
return type(self), (self.partial, self.expected)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is other problem with this solution -- it loses all additional attributes.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

asyncio doesn't set any additional attributes, so I think it's OK

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if somebody will create a subclass?

Adding self.__dict__ as the third component will save all attributes. But not slots.

bpo-26579 could help to support general cases. But pickling exceptions is a headache in any case.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if somebody will create a subclass?

Well, they then can handle pickling on their own. I mean in asyncio we control both exceptions and don't subclass them. If someone outside of asyncio decides to subclass or otherwise reuse these exceptions, it's entirely up to them.

I don't want to introduce any extra overhead here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then everything is fine. Except misleading repr, but this is another story.



class LimitOverrunError(Exception):
"""Reached the buffer limit while looking for a separator.
Expand All @@ -46,6 +49,9 @@ def __init__(self, message, consumed):
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, *,
Expand Down
18 changes: 18 additions & 0 deletions Lib/test/test_asyncio/test_streams.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import gc
import os
import queue
import pickle
import socket
import sys
import threading
Expand Down Expand Up @@ -845,6 +846,23 @@ def test___repr__transport(self):
stream._transport.__repr__.return_value = "<Transport>"
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__':
unittest.main()
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Make asyncio.IncompleteReadError and LimitOverrunError pickleable.