Skip to content

Commit 297bf9c

Browse files
committed
getaddrinfo helper method
1 parent 26fe6e1 commit 297bf9c

File tree

6 files changed

+50
-52
lines changed

6 files changed

+50
-52
lines changed

pymongo/asynchronous/auth.py

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
"""Authentication helpers."""
1616
from __future__ import annotations
1717

18-
import asyncio
1918
import functools
2019
import hashlib
2120
import hmac
@@ -39,6 +38,7 @@
3938
_authenticate_oidc,
4039
_get_authenticator,
4140
)
41+
from pymongo.asynchronous.helpers import getaddrinfo
4242
from pymongo.auth_shared import (
4343
MongoCredential,
4444
_authenticate_scram_start,
@@ -184,22 +184,16 @@ async def _canonicalize_hostname(hostname: str, option: str | bool) -> str:
184184
if option in [False, "none"]:
185185
return hostname
186186

187-
if not _IS_SYNC:
188-
loop = asyncio.get_running_loop()
189-
af, socktype, proto, canonname, sockaddr = (
190-
await loop.getaddrinfo(
191-
hostname,
192-
None,
193-
family=0,
194-
type=0,
195-
proto=socket.IPPROTO_TCP,
196-
flags=socket.AI_CANONNAME,
197-
)
198-
)[0] # type: ignore[index]
199-
else:
200-
af, socktype, proto, canonname, sockaddr = socket.getaddrinfo(
201-
hostname, None, 0, 0, socket.IPPROTO_TCP, socket.AI_CANONNAME
202-
)[0]
187+
af, socktype, proto, canonname, sockaddr = (
188+
await getaddrinfo(
189+
hostname,
190+
None,
191+
family=0,
192+
type=0,
193+
proto=socket.IPPROTO_TCP,
194+
flags=socket.AI_CANONNAME,
195+
)
196+
)[0] # type: ignore[index]
203197

204198
# For forward just to resolve the cname as dns.lookup() will not return it.
205199
if option == "forward":

pymongo/asynchronous/helpers.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
"""Miscellaneous pieces that need to be synchronized."""
1616
from __future__ import annotations
1717

18+
import asyncio
1819
import builtins
20+
import socket
1921
import sys
2022
from typing import (
2123
Any,
@@ -68,6 +70,16 @@ async def inner(*args: Any, **kwargs: Any) -> Any:
6870
return cast(F, inner)
6971

7072

73+
async def getaddrinfo(host, port, **kwargs):
74+
if not _IS_SYNC:
75+
loop = asyncio.get_running_loop()
76+
return await loop.getaddrinfo( # type: ignore[assignment]
77+
host, port, **kwargs
78+
)
79+
else:
80+
return socket.getaddrinfo(host, port, **kwargs) # type: ignore[assignment]
81+
82+
7183
if sys.version_info >= (3, 10):
7284
anext = builtins.anext
7385
aiter = builtins.aiter

pymongo/asynchronous/pool.py

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
from pymongo import _csot, helpers_shared
4242
from pymongo._asyncio_executor import _PYMONGO_EXECUTOR
4343
from pymongo.asynchronous.client_session import _validate_session_write_concern
44-
from pymongo.asynchronous.helpers import _handle_reauth
44+
from pymongo.asynchronous.helpers import _handle_reauth, getaddrinfo
4545
from pymongo.asynchronous.network import command, receive_message
4646
from pymongo.common import (
4747
MAX_BSON_SIZE,
@@ -815,14 +815,7 @@ async def _create_connection(address: _Address, options: PoolOptions) -> socket.
815815
family = socket.AF_UNSPEC
816816

817817
err = None
818-
if not _IS_SYNC:
819-
loop = asyncio.get_running_loop()
820-
results = await loop.getaddrinfo( # type: ignore[assignment]
821-
host, port, family=family, type=socket.SOCK_STREAM
822-
)
823-
else:
824-
results = socket.getaddrinfo(host, port, family, socket.SOCK_STREAM) # type: ignore[assignment]
825-
for res in results: # type: ignore[attr-defined]
818+
for res in await getaddrinfo(host, port, family=family, type=socket.SOCK_STREAM): # type: ignore[attr-defined]
826819
af, socktype, proto, dummy, sa = res
827820
# SOCK_CLOEXEC was new in CPython 3.2, and only available on a limited
828821
# number of platforms (newer Linux and *BSD). Starting with CPython 3.4

pymongo/synchronous/auth.py

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
"""Authentication helpers."""
1616
from __future__ import annotations
1717

18-
import asyncio
1918
import functools
2019
import hashlib
2120
import hmac
@@ -46,6 +45,7 @@
4645
_authenticate_oidc,
4746
_get_authenticator,
4847
)
48+
from pymongo.synchronous.helpers import getaddrinfo
4949

5050
if TYPE_CHECKING:
5151
from pymongo.hello import Hello
@@ -181,22 +181,16 @@ def _canonicalize_hostname(hostname: str, option: str | bool) -> str:
181181
if option in [False, "none"]:
182182
return hostname
183183

184-
if not _IS_SYNC:
185-
loop = asyncio.get_running_loop()
186-
af, socktype, proto, canonname, sockaddr = (
187-
loop.getaddrinfo(
188-
hostname,
189-
None,
190-
family=0,
191-
type=0,
192-
proto=socket.IPPROTO_TCP,
193-
flags=socket.AI_CANONNAME,
194-
)
195-
)[0] # type: ignore[index]
196-
else:
197-
af, socktype, proto, canonname, sockaddr = socket.getaddrinfo(
198-
hostname, None, 0, 0, socket.IPPROTO_TCP, socket.AI_CANONNAME
199-
)[0]
184+
af, socktype, proto, canonname, sockaddr = (
185+
getaddrinfo(
186+
hostname,
187+
None,
188+
family=0,
189+
type=0,
190+
proto=socket.IPPROTO_TCP,
191+
flags=socket.AI_CANONNAME,
192+
)
193+
)[0] # type: ignore[index]
200194

201195
# For forward just to resolve the cname as dns.lookup() will not return it.
202196
if option == "forward":

pymongo/synchronous/helpers.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
"""Miscellaneous pieces that need to be synchronized."""
1616
from __future__ import annotations
1717

18+
import asyncio
1819
import builtins
20+
import socket
1921
import sys
2022
from typing import (
2123
Any,
@@ -68,6 +70,16 @@ def inner(*args: Any, **kwargs: Any) -> Any:
6870
return cast(F, inner)
6971

7072

73+
def getaddrinfo(host, port, **kwargs):
74+
if not _IS_SYNC:
75+
loop = asyncio.get_running_loop()
76+
return loop.getaddrinfo( # type: ignore[assignment]
77+
host, port, **kwargs
78+
)
79+
else:
80+
return socket.getaddrinfo(host, port, **kwargs) # type: ignore[assignment]
81+
82+
7183
if sys.version_info >= (3, 10):
7284
next = builtins.next
7385
iter = builtins.iter

pymongo/synchronous/pool.py

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@
8585
from pymongo.socket_checker import SocketChecker
8686
from pymongo.ssl_support import HAS_SNI, SSLError
8787
from pymongo.synchronous.client_session import _validate_session_write_concern
88-
from pymongo.synchronous.helpers import _handle_reauth
88+
from pymongo.synchronous.helpers import _handle_reauth, getaddrinfo
8989
from pymongo.synchronous.network import command, receive_message
9090

9191
if TYPE_CHECKING:
@@ -813,14 +813,7 @@ def _create_connection(address: _Address, options: PoolOptions) -> socket.socket
813813
family = socket.AF_UNSPEC
814814

815815
err = None
816-
if not _IS_SYNC:
817-
loop = asyncio.get_running_loop()
818-
results = loop.getaddrinfo( # type: ignore[assignment]
819-
host, port, family=family, type=socket.SOCK_STREAM
820-
)
821-
else:
822-
results = socket.getaddrinfo(host, port, family, socket.SOCK_STREAM) # type: ignore[assignment]
823-
for res in results: # type: ignore[attr-defined]
816+
for res in getaddrinfo(host, port, family=family, type=socket.SOCK_STREAM): # type: ignore[attr-defined]
824817
af, socktype, proto, dummy, sa = res
825818
# SOCK_CLOEXEC was new in CPython 3.2, and only available on a limited
826819
# number of platforms (newer Linux and *BSD). Starting with CPython 3.4

0 commit comments

Comments
 (0)