-
-
Notifications
You must be signed in to change notification settings - Fork 33.7k
bpo-38314: Add is_reading() method to asyncio _UnixReadPipeTransport #17755
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
Open
calluw
wants to merge
4
commits into
python:main
Choose a base branch
from
calluw:fix-issue-38314-rebase
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
a9ed2a4
Add is_reading() method to _UnixReadPipeTransport
dbcd035
Add new test case file for UNIX pipe transport functional tests
52e9ec8
📜🤖 Added by blurb_it.
blurb-it[bot] 981c452
Update Misc/NEWS.d/next/Library/2019-11-04-17-20-43.bpo-38314.zWz6_P.rst
calluw File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| """ | ||
| Functional tests for Unix transport pipes, designed around opening two sockets | ||
| on the localhost and sending information between | ||
| """ | ||
|
|
||
| import unittest | ||
| import sys | ||
| import os | ||
| import tempfile | ||
| import threading | ||
|
|
||
| if sys.platform == 'win32': | ||
| raise unittest.SkipTest('UNIX only') | ||
|
|
||
| import asyncio | ||
| from asyncio import unix_events | ||
|
|
||
|
|
||
| def tearDownModule(): | ||
| asyncio.set_event_loop_policy(None) | ||
|
|
||
|
|
||
| class UnixReadPipeTransportFuncTests(unittest.TestCase): | ||
| """ | ||
| Verify that transports on Unix can facilitate reading and have access to | ||
| all state methods: verify using class internals | ||
| """ | ||
|
|
||
| async def register_read_handle(self): | ||
| """ | ||
| Wait for read_handle and then register it to the loop | ||
| """ | ||
| self.transport, self.protocol = await self.loop.connect_read_pipe( | ||
| asyncio.BaseProtocol, | ||
| self.read_handle, | ||
| ) | ||
|
|
||
| def setup_read_handle(self): | ||
| """ | ||
| Open the read handle and record it in an attribute | ||
| """ | ||
| self.read_handle = open(self.pipe, "r") | ||
|
|
||
| def setup_write_handle(self): | ||
| """ | ||
| Open the write handle and record it in an attribute | ||
| """ | ||
| self.write_handle = open(self.pipe, "w") | ||
|
|
||
| def setUp(self): | ||
| """ | ||
| Create the UNIX pipe and register the read end to the loop, and connect | ||
| a write handle asynchronously | ||
| """ | ||
| self.loop = asyncio.get_event_loop() | ||
| self.temp_dir = tempfile.TemporaryDirectory(suffix="async_unix_events") | ||
| self.pipe = os.path.join(self.temp_dir.name, "unix_pipe") | ||
| os.mkfifo(self.pipe) | ||
|
|
||
| # Set the threads to open the handles going | ||
| r_handle_thread = threading.Thread(target=self.setup_read_handle) | ||
| r_handle_thread.start() | ||
| w_handle_thread = threading.Thread(target=self.setup_write_handle) | ||
| w_handle_thread.start() | ||
|
|
||
| # Wait for pipe pair to connect | ||
| r_handle_thread.join() | ||
| w_handle_thread.join() | ||
|
|
||
| # Once pipe is connected, get the read transport | ||
| self.loop.run_until_complete(self.register_read_handle()) | ||
|
|
||
| self.assertIsInstance(self.transport, | ||
| unix_events._UnixReadPipeTransport) | ||
|
|
||
| def tearDown(self): | ||
| """ | ||
| Destroy the read transport and the pipe | ||
| """ | ||
| self.transport.close() | ||
| self.write_handle.close() | ||
| self.read_handle.close() | ||
| self.loop._run_once() | ||
| os.unlink(self.pipe) | ||
| self.temp_dir.cleanup() | ||
| self.loop.close() | ||
|
|
||
| def test_is_reading(self): | ||
| """ | ||
| Verify that is_reading returns True unless transport is closed/closing | ||
| or paused | ||
| """ | ||
| self.assertTrue(self.transport.is_reading()) | ||
| self.transport.pause_reading() | ||
| self.assertFalse(self.transport.is_reading()) | ||
| self.transport.resume_reading() | ||
| self.assertTrue(self.transport.is_reading()) | ||
| self.transport.close() | ||
| self.assertFalse(self.transport.is_reading()) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() | ||
2 changes: 2 additions & 0 deletions
2
Misc/NEWS.d/next/Library/2019-11-04-17-20-43.bpo-38314.zWz6_P.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| The read pipe in :mod:`asyncio` UNIX transports now has access to the | ||
| `is_reading()` API. |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Style nit: As per PEP 257, within the standard library, we use the following format for all docstrings:
or
Rather than:
I was originally hesitant to point it out for a new test file that doesn't have an existing convention, but during this year's virtual core dev sprint (Oct. 19 - 23), I asked in the general chat and Guido commented that he was "unwaveringly in favor" of using the PEP 257 docstring conventions.