Skip to content

Commit 484aa9f

Browse files
committed
cleanup
1 parent 432380e commit 484aa9f

File tree

5 files changed

+52
-176
lines changed

5 files changed

+52
-176
lines changed

justfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
set shell := ["bash", "-c"]
33

44
# Commonly used command segments.
5-
uv_run := "uv run --isolated --frozen "
5+
uv_run := "uv run --frozen "
66
typing_run := uv_run + "--group typing --extra aws --extra encryption --extra ocsp --extra snappy --extra test --extra zstd"
77
docs_run := uv_run + "--extra docs"
88
doc_build := "./doc/_build"

pymongo/network_layer.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,8 @@
4141
from pymongo.socket_checker import _errno_from_exception
4242

4343
if TYPE_CHECKING:
44-
from ssl import SSLSocket
45-
4644
from pymongo.asynchronous.pool import AsyncBaseConnection
45+
from pymongo.pyopenssl_context import _sslConn
4746
from pymongo.synchronous.pool import BaseConnection
4847

4948
_UNPACK_HEADER = struct.Struct("<iiii").unpack
@@ -60,7 +59,7 @@
6059
)
6160

6261

63-
def sendall(sock: Union[socket.socket, SSLSocket], buf: bytes) -> None:
62+
def sendall(sock: Union[socket.socket, _sslConn], buf: bytes) -> None:
6463
sock.sendall(buf)
6564

6665

@@ -222,7 +221,7 @@ def sock(self) -> socket.socket:
222221

223222

224223
class NetworkingInterface(NetworkingInterfaceBase):
225-
def __init__(self, conn: Union[socket.socket, SSLSocket]):
224+
def __init__(self, conn: Union[socket.socket, _sslConn]):
226225
super().__init__(conn)
227226

228227
def gettimeout(self) -> float | None:
@@ -238,11 +237,11 @@ def is_closing(self) -> bool:
238237
return self.conn.is_closing()
239238

240239
@property
241-
def get_conn(self) -> Union[socket.socket, SSLSocket]:
240+
def get_conn(self) -> Union[socket.socket, _sslConn]:
242241
return self.conn
243242

244243
@property
245-
def sock(self) -> Union[socket.socket, SSLSocket]:
244+
def sock(self) -> Union[socket.socket, _sslConn]:
246245
return self.conn
247246

248247
def fileno(self) -> int:
@@ -553,7 +552,7 @@ def buffer_updated(self, nbytes: int) -> None:
553552
waiter = self._pending_listeners.popleft()
554553
waiter.set_result(data)
555554

556-
def _read(self, bytes_needed):
555+
def _read(self, bytes_needed: int) -> memoryview:
557556
"""Read bytes from the buffer."""
558557
# Send the bytes to the listener.
559558
if self._bytes_ready < bytes_needed:
@@ -581,7 +580,7 @@ def _read(self, bytes_needed):
581580
]
582581
out_index += buffer_remaining
583582
n_remaining -= buffer_remaining
584-
return output_buf
583+
return memoryview(output_buf)
585584

586585

587586
async def async_sendall(conn: PyMongoBaseProtocol, buf: bytes) -> None:

pymongo/pool_shared.py

Lines changed: 3 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
from __future__ import annotations
1717

1818
import asyncio
19-
import functools
2019
import socket
2120
import ssl
2221
import sys
@@ -25,7 +24,6 @@
2524
Any,
2625
NoReturn,
2726
Optional,
28-
Union,
2927
)
3028

3129
from pymongo import _csot
@@ -47,7 +45,6 @@
4745

4846
SSLErrors = (PYSSLError, SSLError)
4947
if TYPE_CHECKING:
50-
from pymongo.pyopenssl_context import _sslConn
5148
from pymongo.typings import _Address
5249

5350
try:
@@ -274,64 +271,10 @@ async def _async_create_connection(address: _Address, options: PoolOptions) -> s
274271
raise OSError("getaddrinfo failed")
275272

276273

277-
async def _async_configured_socket(
278-
address: _Address, options: PoolOptions
279-
) -> Union[socket.socket, _sslConn]:
280-
"""Given (host, port) and PoolOptions, return a raw configured socket.
281-
282-
Can raise socket.error, ConnectionFailure, or _CertificateError.
283-
284-
Sets socket's SSL and timeout options.
285-
"""
286-
sock = await _async_create_connection(address, options)
287-
ssl_context = options._ssl_context
288-
289-
if ssl_context is None:
290-
sock.settimeout(options.socket_timeout)
291-
return sock
292-
293-
host = address[0]
294-
try:
295-
# We have to pass hostname / ip address to wrap_socket
296-
# to use SSLContext.check_hostname.
297-
if _has_sni(False):
298-
loop = asyncio.get_running_loop()
299-
ssl_sock = await loop.run_in_executor(
300-
None,
301-
functools.partial(ssl_context.wrap_socket, sock, server_hostname=host), # type: ignore[assignment, misc, unused-ignore]
302-
)
303-
else:
304-
loop = asyncio.get_running_loop()
305-
ssl_sock = await loop.run_in_executor(None, ssl_context.wrap_socket, sock) # type: ignore[assignment, misc, unused-ignore]
306-
except _CertificateError:
307-
sock.close()
308-
# Raise _CertificateError directly like we do after match_hostname
309-
# below.
310-
raise
311-
except (OSError, *SSLErrors) as exc:
312-
sock.close()
313-
# We raise AutoReconnect for transient and permanent SSL handshake
314-
# failures alike. Permanent handshake failures, like protocol
315-
# mismatch, will be turned into ServerSelectionTimeoutErrors later.
316-
details = _get_timeout_details(options)
317-
_raise_connection_failure(address, exc, "SSL handshake failed: ", timeout_details=details)
318-
if (
319-
ssl_context.verify_mode
320-
and not ssl_context.check_hostname
321-
and not options.tls_allow_invalid_hostnames
322-
):
323-
try:
324-
ssl.match_hostname(ssl_sock.getpeercert(), hostname=host) # type:ignore[attr-defined, unused-ignore]
325-
except _CertificateError:
326-
ssl_sock.close()
327-
raise
328-
329-
ssl_sock.settimeout(options.socket_timeout)
330-
return ssl_sock
331-
332-
333274
async def _configured_protocol_interface(
334-
address: _Address, options: PoolOptions, protocol_kls: PyMongoBaseProtocol = PyMongoProtocol
275+
address: _Address,
276+
options: PoolOptions,
277+
protocol_kls: type[PyMongoBaseProtocol] = PyMongoProtocol,
335278
) -> AsyncNetworkingInterface:
336279
"""Given (host, port) and PoolOptions, return a configured AsyncNetworkingInterface.
337280
@@ -455,55 +398,6 @@ def _create_connection(address: _Address, options: PoolOptions) -> socket.socket
455398
raise OSError("getaddrinfo failed")
456399

457400

458-
def _configured_socket(address: _Address, options: PoolOptions) -> Union[socket.socket, _sslConn]:
459-
"""Given (host, port) and PoolOptions, return a raw configured socket.
460-
461-
Can raise socket.error, ConnectionFailure, or _CertificateError.
462-
463-
Sets socket's SSL and timeout options.
464-
"""
465-
sock = _create_connection(address, options)
466-
ssl_context = options._ssl_context
467-
468-
if ssl_context is None:
469-
sock.settimeout(options.socket_timeout)
470-
return sock
471-
472-
host = address[0]
473-
try:
474-
# We have to pass hostname / ip address to wrap_socket
475-
# to use SSLContext.check_hostname.
476-
if _has_sni(True):
477-
ssl_sock = ssl_context.wrap_socket(sock, server_hostname=host) # type: ignore[assignment, misc, unused-ignore]
478-
else:
479-
ssl_sock = ssl_context.wrap_socket(sock) # type: ignore[assignment, misc, unused-ignore]
480-
except _CertificateError:
481-
sock.close()
482-
# Raise _CertificateError directly like we do after match_hostname
483-
# below.
484-
raise
485-
except (OSError, *SSLErrors) as exc:
486-
sock.close()
487-
# We raise AutoReconnect for transient and permanent SSL handshake
488-
# failures alike. Permanent handshake failures, like protocol
489-
# mismatch, will be turned into ServerSelectionTimeoutErrors later.
490-
details = _get_timeout_details(options)
491-
_raise_connection_failure(address, exc, "SSL handshake failed: ", timeout_details=details)
492-
if (
493-
ssl_context.verify_mode
494-
and not ssl_context.check_hostname
495-
and not options.tls_allow_invalid_hostnames
496-
):
497-
try:
498-
ssl.match_hostname(ssl_sock.getpeercert(), hostname=host) # type:ignore[attr-defined, unused-ignore]
499-
except _CertificateError:
500-
ssl_sock.close()
501-
raise
502-
503-
ssl_sock.settimeout(options.socket_timeout)
504-
return ssl_sock
505-
506-
507401
def _configured_socket_interface(
508402
address: _Address, options: PoolOptions, *args: Any
509403
) -> NetworkingInterface:

tools/synchro.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,6 @@
123123
"async_receive_kms": "receive_kms",
124124
"AsyncNetworkingInterface": "NetworkingInterface",
125125
"_configured_protocol_interface": "_configured_socket_interface",
126-
"_async_configured_socket": "_configured_socket",
127126
"SpecRunnerTask": "SpecRunnerThread",
128127
"AsyncMockConnection": "MockConnection",
129128
"AsyncMockPool": "MockPool",

0 commit comments

Comments
 (0)