Red Hat Bugzilla – Bug 436560
socket module SO_* constants incomplete
Last modified: 2008-03-08 10:07:53 EST
The socket module via the _socket .so extension (Python/Modules/socketmodule.c)
exports a number of constants imported from <sys/socket.h>. Many of those
constants are in turn imported from <asm/socket.h>, in particular the SO_*
constants for use with getsockopt(). Because these constants are defined in an
asm header file they are architecture dependent.
If a SO_* constant is missing from the socket module one cannot hardcode the
value in your python program because it will vary depending on architecture. The
only way for a python program to correctly know the proper constant is if the
constant was obtained via compilation on the target system and then exported via
a CPython module.
We ran across this problem when trying to use SO_PEERCRED with getsockopt(),
it's not exported by the socket module so we hardcoded it to 17, which worked
for many architectures, but not all.
It's easy to export constants. I compiled a list of SO_ constants not currently
exported by the socket module, they are:
and prepared a patch to socketmodule.c to export these missing constants. It's
important for the socket module to export them because there is no other way for
a noarch python program to get the arch specific value.
Created attachment 297256 [details]
patch to export missing SO_* constants
FYI: bug #436564 has a patch showing what has to be done if the SO_PEERCRED
constant is not exported. One has to determine all the arch specific values
(gathered from the kernel source code), get the arch from uname() and then
hardcode the constant based on the arch.
Note that I just added these to Fedora, so I can't guarantee that they'll even
get in upstream (and they certainly won't be in upstream for a while, at least).
But then hasattr() is your friend :).
I've tested python and random apps. I use ... and socket.SO_PEERCRED etc. is
there. Any bugs, you know where to find me :).
Thanks James for the quick response.