Skip to content

_overlapped_BindLocal_impl() exception message is not helpful #144493

@dlech

Description

@dlech

Bug report

Bug description:

When calling loop.sock_connect() on Windows, it calls into _overlapped_BindLocal_impl().

static PyObject *
_overlapped_BindLocal_impl(PyObject *module, HANDLE Socket, int Family)
/*[clinic end generated code: output=edb93862697aed9c input=a0e7b5c2f541170c]*/
{
BOOL ret;
if (Family == AF_INET) {
struct sockaddr_in addr;
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = 0;
addr.sin_addr.S_un.S_addr = INADDR_ANY;
ret = bind((SOCKET)Socket, (SOCKADDR*)&addr, sizeof(addr))
!= SOCKET_ERROR;
} else if (Family == AF_INET6) {
struct sockaddr_in6 addr;
memset(&addr, 0, sizeof(addr));
addr.sin6_family = AF_INET6;
addr.sin6_port = 0;
addr.sin6_addr = in6addr_any;
ret = bind((SOCKET)Socket, (SOCKADDR*)&addr, sizeof(addr))
!= SOCKET_ERROR;
} else {
PyErr_SetString(PyExc_ValueError, "expected tuple of length 2 or 4");
return NULL;
}
if (!ret)
return SetFromWindowsErr(WSAGetLastError());
Py_RETURN_NONE;
}

If the socket is not an AF_INET or AF_INET6 socket (for example an AF_BLUETOOTH socket), a ValueError is raised with the message: "expected tuple of length 2 or 4". This makes no sense. The actual problem is that the function only supports the two IP families. The error message should explain this.

CPython versions tested on:

3.14

Operating systems tested on:

Windows

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    extension-modulesC modules in the Modules dirtype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions