Skip to content

gh-135748: Use Argument Clinic for more socket functions - #155257

Open
serhiy-storchaka wants to merge 6 commits into
python:mainfrom
serhiy-storchaka:socket-clinic
Open

gh-135748: Use Argument Clinic for more socket functions#155257
serhiy-storchaka wants to merge 6 commits into
python:mainfrom
serhiy-storchaka:socket-clinic

Conversation

@serhiy-storchaka

@serhiy-storchaka serhiy-storchaka commented Aug 5, 2026

Copy link
Copy Markdown
Member

Convert all convertible functions and methods of the socket module to Argument Clinic. They now have signatures for introspection, and functions which take several arguments use METH_FASTCALL instead of METH_VARARGS.

The number of _socket callables for which inspect.signature() fails drops from 26 to 7. Five of those seven are deliberate: listen() (the default is Py_MIN(SOMAXCONN, 128)), getservbyname() and getservbyport() (the C default is NULL and None is not accepted), sendmsg() and sendmsg_afalg() (arguments which are absent rather than defaulted).

ioctl(), sendto() and setsockopt() are left as they are. Their behaviour depends on the number of the arguments or on the value of a preceding argument, which Argument Clinic cannot express:

  • sendto(data[, flags], address) -- the optional argument is in the middle, while optional groups only support [left] required [right].
  • setsockopt() distinguishes 3 from 4 arguments with PyTuple_Size(), and both resulting error messages are asserted by test_socket.
  • ioctl() parses its second argument differently depending on cmd.

The parameter names are taken from the existing docstrings. Seven of them differ from Doc/library/socket.rst (for example recv(buffersize) versus the documented bufsize); the documentation is left unchanged here.

Convert all convertible functions and methods of the socket module.
They now have signatures for introspection, and functions which take
several arguments use METH_FASTCALL instead of METH_VARARGS.

ioctl(), sendto() and setsockopt() are left as they are.  Their
behaviour depends on the number of the arguments or on the value of a
preceding argument, which Argument Clinic cannot express.
Comment thread Modules/socketmodule.c
PyMutex_Unlock(&netdb_lock);
#endif
finally:
PyMem_Free(ip_num);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The memory still needs to be freed right?

Comment thread Modules/socketmodule.c Outdated
if (iv.buf != NULL) {
PyBuffer_Release(&iv);
}
if (iv->buf != NULL) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

empty block

Comment thread Modules/socketmodule.c Outdated
/*[clinic input]
_socket.socket.setblocking
self as s: self(type="PySocketSockObject *")
flag as arg: object

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could use bool converter directly

Restore freeing the buffer allocated by the "et" format in
gethostbyaddr(), remove the empty block left after the Py_buffer
converter took over releasing the initialization vector in
sendmsg_afalg(), and use the bool converter in setblocking().
Restore the AF_INET default family of socketpair() on platforms which
do not define AF_UNIX, and the note about os.close() and os.dup() in
the docstrings of close() and dup().

Remove the preprocessor guards around the METHODDEF macros of the
functions generated by Argument Clinic: such macro is empty if the
function is not compiled.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants