Skip to content
Myr

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

1 file+40 added−37 removed

modified:httpx/concurrency/asyncio.py+40 added-37 removed
30 unmodified lines
3132 MonkeyPatch.write = _fixed_write333435async 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.4748 Adapted from this comment:4950 https://github.com/urllib3/urllib3/issues/1323#issuecomment-36249483951 """52 import asyncio.sslproto5354 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 )6566 transport.set_protocol(ssl_protocol)67 loop.call_soon(ssl_protocol.connection_made, transport)68 loop.call_soon(transport.resume_reading) # type: ignore6970 await waiter71 return ssl_protocol._app_transport727374class SocketStream(BaseSocketStream):75 def __init__(76 self, stream_reader: asyncio.StreamReader, stream_writer: asyncio.StreamWriter,77 ):
11 unmodified lines
89 stream_reader = asyncio.StreamReader()90 protocol = asyncio.StreamReaderProtocol(stream_reader)91 transport = self.stream_writer.transport9254 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)9458 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.sslproto7374 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 )8485 transport.set_protocol(ssl_protocol)86 loop.call_soon(ssl_protocol.connection_made, transport)87 loop.call_soon(transport.resume_reading) # type: ignore8889 await waiter90 return ssl_protocol._app_transport9195 transport = await asyncio.wait_for(96 loop_start_tls(97 transport=transport,98 protocol=protocol,
180 unmodified lines