From ecb7399b890513ef7be411d1d7fe6b56dfc680d8 Mon Sep 17 00:00:00 2001 From: A5rocks Date: Sat, 5 Sep 2026 13:32:54 -0400 Subject: [PATCH] Work around a CPython bug that broke things on Android <24 --- newsfragments/3493.bugfix.rst | 1 + src/trio/socket.py | 18 +++++++++++++----- 2 files changed, 14 insertions(+), 5 deletions(-) create mode 100644 newsfragments/3493.bugfix.rst 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 (