diff --git a/Misc/NEWS.d/next/Library/2026-04-20-00-20-01.gh-issue-148783.ZRKJt4.rst b/Misc/NEWS.d/next/Library/2026-04-20-00-20-01.gh-issue-148783.ZRKJt4.rst new file mode 100644 index 00000000000000..dbfda29fd9a93f --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-04-20-00-20-01.gh-issue-148783.ZRKJt4.rst @@ -0,0 +1,2 @@ +Fix :class:`socket.socket` to respect caller-specified proto over +auto-detection, which is not supported on platforms such as macOS. diff --git a/Modules/socketmodule.c b/Modules/socketmodule.c index f1a55db229e115..25123b8a7314ff 100644 --- a/Modules/socketmodule.c +++ b/Modules/socketmodule.c @@ -5746,8 +5746,8 @@ sock_initobj_impl(PySocketSockObject *self, int family, int type, int proto, #else type = SOCK_STREAM; #endif -#ifdef SO_PROTOCOL if (proto == -1) { +#ifdef SO_PROTOCOL int tmp; socklen_t slen = sizeof(tmp); if (getsockopt(fd, SOL_SOCKET, SO_PROTOCOL, @@ -5758,10 +5758,10 @@ sock_initobj_impl(PySocketSockObject *self, int family, int type, int proto, set_error(); return -1; } - } #else proto = 0; #endif + } } } else {