Tweak backport_start_tls implementation, and add 'nocover'. (#622)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Tom Christie · committed by GitHub · parent 02d16bf · Browse files · 1 file·+40 added−37 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+40 added−37 removed
- other:+40 added−37 removed
Provenance
- Commit
66e0f4b- Tree
2b67a82- Author
Tom Christie <tom@tomchristie.com>- Committer
GitHub <noreply@github.com>- Parent
02d16bf
1 file changed+40 added−37 removed
modified:httpx/concurrency/asyncio.py+40 added-37 removed
30 unmodified lines31 32 MonkeyPatch.write = _fixed_write33 34 35+async def backport_start_tls(36+ transport: asyncio.BaseTransport,37+ protocol: asyncio.BaseProtocol,38+ sslcontext: ssl.SSLContext = None,39+ *,40+ server_side: bool = False,41+ server_hostname: str = None,42+ ssl_handshake_timeout: float = None,43+) -> asyncio.Transport: # pragma: nocover (Since it's not used on all Python versions.)44+ """45+ Python 3.6 asyncio doesn't have a start_tls() method on the loop46+ so we use this function in place of the loop's start_tls() method.47+48+ Adapted from this comment:49+50+ https://github.com/urllib3/urllib3/issues/1323#issuecomment-36249483951+ """52+ import asyncio.sslproto53+54+ loop = asyncio.get_event_loop()55+ waiter = loop.create_future()56+ ssl_protocol = asyncio.sslproto.SSLProtocol(57+ loop,58+ protocol,59+ sslcontext,60+ waiter,61+ server_side=False,62+ server_hostname=server_hostname,63+ call_connection_made=False,64+ )65+66+ transport.set_protocol(ssl_protocol)67+ loop.call_soon(ssl_protocol.connection_made, transport)68+ loop.call_soon(transport.resume_reading) # type: ignore69+70+ await waiter71+ return ssl_protocol._app_transport72+73+74 class SocketStream(BaseSocketStream):75 def __init__(76 self, stream_reader: asyncio.StreamReader, stream_writer: asyncio.StreamWriter,77 ):11 unmodified lines89 stream_reader = asyncio.StreamReader()90 protocol = asyncio.StreamReaderProtocol(stream_reader)91 transport = self.stream_writer.transport92 54- if hasattr(loop, "start_tls"):55- loop_start_tls = loop.start_tls # type: ignore56- else:93+ loop_start_tls = getattr(loop, "start_tls", backport_start_tls)94 58- async def loop_start_tls(59- transport: asyncio.BaseTransport,60- protocol: asyncio.BaseProtocol,61- sslcontext: ssl.SSLContext = None,62- *,63- server_side: bool = False,64- server_hostname: str = None,65- ssl_handshake_timeout: float = None,66- ) -> asyncio.Transport:67- """Python 3.6 asyncio doesn't have a start_tls() method on the loop68- so we use this function in place of the loop's start_tls() method.69- Adapted from this comment:70- https://github.com/urllib3/urllib3/issues/1323#issuecomment-36249483971- """72- import asyncio.sslproto73-74- waiter = loop.create_future()75- ssl_protocol = asyncio.sslproto.SSLProtocol(76- loop,77- protocol,78- sslcontext,79- waiter,80- server_side=False,81- server_hostname=server_hostname,82- call_connection_made=False,83- )84-85- transport.set_protocol(ssl_protocol)86- loop.call_soon(ssl_protocol.connection_made, transport)87- loop.call_soon(transport.resume_reading) # type: ignore88-89- await waiter90- return ssl_protocol._app_transport91-95 transport = await asyncio.wait_for(96 loop_start_tls(97 transport=transport,98 protocol=protocol,180 unmodified lines