diff --git a/newsfragments/3493.bugfix.rst b/newsfragments/3493.bugfix.rst new file mode 100644 index 0000000000..bd9293e1a0 --- /dev/null +++ b/newsfragments/3493.bugfix.rst @@ -0,0 +1 @@ +Avoid erroring when running in a CPython built for an Android API version less than 24. diff --git a/src/trio/socket.py b/src/trio/socket.py index 4aee184331..424f0f92e1 100644 --- a/src/trio/socket.py +++ b/src/trio/socket.py @@ -69,10 +69,18 @@ ) if sys.implementation.name == "cpython": - from socket import ( - if_indextoname as if_indextoname, - if_nametoindex as if_nametoindex, - ) + if sys.version_info >= (3, 16): # pragma: no branch + from socket import ( # pragma: no cover + if_indextoname as if_indextoname, + if_nametoindex as if_nametoindex, + ) + else: + # https://github.com/python/cpython/issues/156439 + with _suppress(ImportError): + from socket import ( + if_indextoname as if_indextoname, + if_nametoindex as if_nametoindex, + ) # For android devices, if_nameindex support was introduced in API 24, # so it doesn't exist for any version prior. @@ -82,7 +90,7 @@ ) -# not always available so expose only if +# not always available so expose only if possible if sys.platform != "win32" or not _t.TYPE_CHECKING: with _suppress(ImportError): from socket import (