Skip to content

Commit ed1494a

Browse files
authored
Fix AttributeError on windows: socket.EAI_ADDRFAMILY (#1252)
1 parent 90b8a96 commit ed1494a

File tree

1 file changed

+18
-8
lines changed

1 file changed

+18
-8
lines changed

src/neo4j/_async_compat/network/_util.py

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616

1717
import asyncio
18+
import contextlib
1819
import logging
1920
import socket
2021

@@ -41,12 +42,21 @@ def _resolved_addresses_from_info(info, host_name):
4142
yield ResolvedAddress(addr, host_name=host_name)
4243

4344

44-
_RETRYABLE_DNS_ERRNOS = {
45-
socket.EAI_ADDRFAMILY,
46-
socket.EAI_AGAIN,
47-
socket.EAI_MEMORY,
48-
socket.EAI_NODATA,
49-
}
45+
def _try_get_socket_attributes(*attrs):
46+
for attr in attrs:
47+
with contextlib.suppress(AttributeError):
48+
yield getattr(socket, attr)
49+
50+
51+
_RETRYABLE_DNS_ERRNOS = set(
52+
_try_get_socket_attributes(
53+
"EAI_ADDRFAMILY",
54+
"EAI_AGAIN",
55+
"EAI_MEMORY",
56+
"EAI_NODATA",
57+
)
58+
)
59+
_EAI_NONAME = set(_try_get_socket_attributes("EAI_NONAME"))
5060

5161

5262
class AsyncNetworkUtil:
@@ -79,7 +89,7 @@ async def _dns_resolver(address, family=0):
7989
)
8090
except OSError as e:
8191
if e.errno in _RETRYABLE_DNS_ERRNOS or (
82-
e.errno == socket.EAI_NONAME
92+
e.errno in _EAI_NONAME
8393
and (address.host is not None or address.port is not None)
8494
):
8595
raise ServiceUnavailable(
@@ -170,7 +180,7 @@ def _dns_resolver(address, family=0):
170180
)
171181
except OSError as e:
172182
if e.errno in _RETRYABLE_DNS_ERRNOS or (
173-
e.errno == socket.EAI_NONAME
183+
e.errno in _EAI_NONAME
174184
and (address.host is not None or address.port is not None)
175185
):
176186
raise ServiceUnavailable(

0 commit comments

Comments
 (0)