GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — bpo-12887 and bpo-14345: Document the availability and uses of the SO_ constants. Document SOL_SOCKET by ammaraskar · Pull Request #3072 · python/cpython

Conversation

@ammaraskar

@ammaraskar ammaraskar commented Aug 11, 2017 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

The main thing I'm looking for feedback on is how to present information on the main OSs these options are available on. My current approach is to visualize it in a table that looks like this:

so_table

The alternative is to document it with each constant which looks like this:

setfib

However I think this starts to get very repetitive and doesn't look nearly as nice as the table. It does have the advantage that you don't have to scroll back up to the table to see if a certain option is available on your OS, you can see the availability as you read the documentation for the option. (I've commented those out until I make a decision on which approach is better, table or inline)

I've added fairly brief descriptions of each constant, they're ordered in the order they appear in https://github.com/python/cpython/blob/master/Modules/socketmodule.c
The descriptions have been written by consulting man pages and experimentation and any feedback on improving them would be appreciated.

Some of them have vary scant documentation online such as SO_PASSSEC so I've opted to keep those super brief. Hopefully they can be improved over time by the people who added them or otherwise.

https://bugs.python.org/issue12887

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@the-knights-who-say-ni the-knights-who-say-ni added the CLA signed label Aug 11, 2017
@ammaraskar
ammaraskar force-pushed the socket_docs branch from b72a80d to ebab99f Compare August 11, 2017 05:25
@ammaraskar
bpo-12887: Document the availability and uses of the SO_ constants.
73cc260
bpo-14345: Document SOL_SOCKET
@ammaraskar
ammaraskar force-pushed the socket_docs branch from ebab99f to 73cc260 Compare August 11, 2017 05:31
@ammaraskar ammaraskar mentioned this pull request Aug 11, 2017
Only run CI checks when appropriate files have changed python/core-workflow#14
Closed
vadmium
vadmium reviewed Aug 12, 2017
View reviewed changes
Comment thread Doc/library/socket.rst

When a socket is set to linger. A call to :meth:`socket.close` or
:meth:`socket.shutdown` will not return until all queued messages
for the socket have been successfully sent or the linger timeout

@vadmium vadmium Aug 12, 2017

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

First sentence does not make sense. Perhaps you should have a comma (,) instead of the full stop (.)?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread Doc/library/socket.rst Outdated
.. data:: SO_DONTROUTE

Indicates that packets sent through this socket should be routed
through the interface its bound to.

@vadmium vadmium Aug 12, 2017

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

It’s or it is

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread Doc/library/socket.rst Outdated

SO_RCVLOWAT sets the minimum number of bytes that must be present in
the socket's internal receive buffer before they are passed on to able
read call. SO_SNDLOWAT similiary sets the minimum bytes before data is

@vadmium vadmium Aug 12, 2017

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

I don’t understand “passed on to able read call”. Perhaps do you mean something like “before the socket becomes readable” or “before they are returned by a read”?

Spelling: similarly

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread Doc/library/socket.rst Outdated
SO_SNDLOWAT is read-only on Linux and SO_RCVLOWAT is read-only on
Linux versions below 2.4.

Both these values default to 1.

@vadmium vadmium Aug 12, 2017

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

According to Posix, the SNDLOWAT default depends on the implementation and protocol.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread Doc/library/socket.rst
SO_SNDTIMEO

Specifies the amount of time send and receive calls for this socket willl
block before timing out. The default timeout of zero means that operations

@vadmium vadmium Aug 12, 2017

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Extra l in will.

Is it worth clarifying that these timeouts (which raise EAGAIN etc) are independent of the settimeout mechanism (which raises socket.timeout for non-zero timeouts)?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread Doc/library/socket.rst Outdated
When bound, only packets received from that particular device are
processsed by the socket.

.. versionadded:: 3.1

@vadmium vadmium Aug 12, 2017

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Extra s in processed.

According to my patch at https://bugs.python.org/issue27409 this was only added in 3.3 (or did I make a mistake?). See also https://hg.python.org/cpython/rev/6159311f0f44.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@ammaraskar ammaraskar Aug 12, 2017

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Oh I came to the 3.1 number by going to https://github.com/python/cpython/blob/master/Modules/socketmodule.c

Clicking the blame button and checking the tag in the commit. The tag says 3.1 but it looks like that is incorrect, since the README at the time says: This is Python version 3.3 alpha 0

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread Doc/library/socket.rst Outdated

Passing ``SO_DOMAIN`` to :meth:`socket.getsockopt` allows for the retrival
of the ``family`` value as defined in the :func:`socket.socket` function.
``SO_PROTOCOL`` returns the ``proto`` value. Both these options are read only.

@vadmium vadmium Aug 12, 2017

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Actually it can return the real protocol, e.g. IPPROTO_TCP, even when proto is zero.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread Doc/library/socket.rst Outdated
``SO_PROTOCOL`` returns the ``proto`` value. Both these options are read only.

The value returned for the ``family`` is an integer and not one of the
friendly constants above like :const:`AF_INET`. In order to get a constant

@vadmium vadmium Aug 12, 2017

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

The integer value normally does equal one of those enumeration members; it is just that the integer object is never an enumeration member.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread Doc/library/socket.rst Outdated
SO_PEERCRED

Allows for the passing of SCM credentials over unix sockets.
See the end of :func:`socket.recvmsg` for details.

@vadmium vadmium Aug 12, 2017

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

I’m not familiar with either API, but it seems this only applies to PASSCRED, and PEERCRED returns credentials straight away.

There seems to be something missing with the reference to recvmsg. The end of that entry covers Python versions, platforms, and before that, receiving file descriptors with SCM_RIGHTS. But as I understand it, when using PASSCRED you would look for SCM_CREDENTIALS messages, with a different data structure. The first couple of paragraphs, about receiving anciliary messages in general, are more relevant.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@ammaraskar ammaraskar Aug 12, 2017

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Yeah I'm not familiar with this API either, I'm just going to add an easily searchable keyword of ancillary message and point to recvmsg

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread Doc/library/socket.rst Outdated

.. versionadded:: 3.6

.. Availability: Linux >= 2.6.13

@vadmium vadmium Aug 12, 2017

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Where does this come from? What I found was SO_PASSSEC added to Linux 2.6.18 (https://repo.or.cz/linux-2.6-linus.git/commitdiff/877ce7c), and SO_PEERSEC to 2.6.2 (schwabe/tglx-history@da6e57a).

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@ammaraskar ammaraskar Aug 12, 2017

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Aah that's my mistake. I think 2.6.2 is the right number to include here. I came up with 2.6.13 because I was looking at the blame for sock.c here: https://github.com/torvalds/linux/blame/add459aa1afe05472abc96f6a29aefd0c84e73d6/net/core/sock.c

I scrolled to

case SO_PEERSEC:
    return security_socket_getpeersec_stream(sock, optval, optlen, len);

and saw "Linux-2.6.12-rc2" in the commit message. But it looks like that was a big commit for when Linux switched from bitkeeper to git.

But if we look at the commit that adds

case SO_PASSSEC:
    v.val = test_bit(SOCK_PASSSEC, &sock->flags) ? 1 : 0;
    break;

the commit message says "[AF_UNIX]: Datagram getpeersec" and we can find the same commit in the 2.6.2 changelogs here: https://www.kernel.org/pub/linux/kernel/v2.6/ChangeLog-2.6.2

So I think its safe to say both of these options are available on >= 2.6.2

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
ammaraskar added 2 commits August 12, 2017 09:34
@ammaraskar
Fix simple issues found in code review
32ee0de
@ammaraskar
Fix Linux availability version for SO_PEERSEC
86cece8
@ammaraskar
ammaraskar force-pushed the socket_docs branch from 90ef897 to 86cece8 Compare August 12, 2017 20:54
@Mariatta Mariatta added needs backport to 3.6 docs Documentation in the Doc dir labels Aug 22, 2017
@pitrou

pitrou commented Aug 24, 2017 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

Hi @ammaraskar! I appreciate the level of work you've put into this. On the other hand, I wonder about the maintenance cost of updating this each time an OS gains support for one those options.
(also, the OS list by far not exhaustive, for example we would certainly want to add an OS X column -- or is it "macOS" these days?)

My personal take would be that people who use the (rather low-level) socket module should know how to check man pages and/or the MSDN documentation for the presence and meaning of specific options. Higher-level alternatives are the various networking frameworks (blocking or non-blocking) and the existing implementations of applicative protocols such as http.

Not sure what others think about this.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@ammaraskar

ammaraskar commented Aug 27, 2017

Copy link
Copy Markdown
Member Author

On the other hand, I wonder about the maintenance cost of updating this each time an OS gains support for one those options.

I think this will be far and in between, the core socket options are usually a fairly stable OS level API. With the current ones documented, it should hopefully encourage maintainers to document newly added ones and then check their availability accordingly.

As far as an OS gaining support, I think that will be fairly rare but in that case having the documentation being slightly outdated wouldn't be the worst of situations.

There is some older discussion about this on the bpo thread.

My personal take would be that people who use the (rather low-level) socket module should know how to check man pages and/or the MSDN documentation for the presence and meaning of specific options.

I agree, however there is no real documentation on what constants python exposes from the underlying socket library. At the very minimum I think documenting the list of options and brief descriptions of them would be a good idea. Short of doing dir(socket) there is no way to examine what is available.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@brettcannon brettcannon added the awaiting review label Jan 12, 2018
@matrixise

matrixise commented Jan 30, 2018

Copy link
Copy Markdown
Member

Hi,

@pitrou you are right about the list of the options, we have to check in the official documentation (man pages or MSDN site) but this table with some references can be useful for the beginners, and also for the maintainer, because we can check if an option is directly supported by Python or not.

@ammaraskar
Maybe you could add "This list is incomplete exhaustive and you should to read the documentation of your operating system" and also, add a column for macOS.
But don't forget that we support some operating systems.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@ammaraskar

ammaraskar commented Jan 30, 2018

Copy link
Copy Markdown
Member Author

At the very least, I think adding documentation for the list of options that socketmodule exports and getsockopt/setsockopt would be useful. I agree that the compatibility table is a bit of a pain to maintain.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@matrixise

matrixise commented Nov 4, 2018

Copy link
Copy Markdown
Member

@ammaraskar How did you find the entries for your table. I propose another solution, for the most used entries, we could keep them in the tables. Add a small paragraph with the link to the official documentation of macOS, Windows and for the man pages for POSIX systems.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@ammaraskar

ammaraskar commented Nov 4, 2018

Copy link
Copy Markdown
Member Author

I pretty much just went down the list here:

cpython/Modules/socketmodule.c

Line 7217 in e359bc2

#ifdef SOCK_RAW

Then I looked them up in the man page here for Linux: http://man7.org/linux/man-pages/man7/socket.7.html

FreeBSD here: https://www.freebsd.org/cgi/man.cgi?query=setsockopt&sektion=2&apropos=0&manpath=FreeBSD+7.0-RELEASE

and finally Windows here: https://docs.microsoft.com/en-us/windows/desktop/WinSock/sol-socket-socket-options

The availabilities took a bit more digging up but that was the basic idea.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@serhiy-storchaka serhiy-storchaka added the needs backport to 3.7 label Dec 10, 2018
@vstinner vstinner removed the needs backport to 3.6 label Jan 10, 2019
@vstinner

vstinner commented Jan 10, 2019

Copy link
Copy Markdown
Member

I removed the " needs backport to 3.6" label, the 3.6 branch no longer accept bugfixes (only security fixes are accepted): https://devguide.python.org/#status-of-python-branches

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@JulienPalard

JulienPalard commented Sep 10, 2019

Copy link
Copy Markdown
Member

At the very least, I think adding documentation for the list of options that socketmodule exports and getsockopt/setsockopt would be useful. I agree that the compatibility table is a bit of a pain to maintain.

I agree. In one hand I like the table (not having to search the info for an OS you don't know is nice), but it's probably too much work to maintain. Let's just start by documenting the exposed options. @ammaraskar can you remove the compatibility list and fix the previous reviews about?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@csabella csabella added awaiting changes needs backport to 3.8 and removed awaiting review labels Feb 4, 2020
@csabella

csabella commented Feb 4, 2020

Copy link
Copy Markdown
Contributor

@ammaraskar, please address the review comments.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@ned-deily ned-deily removed the needs backport to 3.7 label Jul 3, 2020
@ambv ambv removed the needs backport to 3.8 label May 4, 2021
@github-actions

github-actions Bot commented Feb 21, 2022

Copy link
Copy Markdown

This PR is stale because it has been open for 30 days with no activity.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@github-actions github-actions Bot added the stale Stale PR or inactive for long period of time. label Feb 21, 2022
@JelleZijlstra

JelleZijlstra commented May 8, 2022

Copy link
Copy Markdown
Member

Closing as it's been more than two years since changes were requested. Feel free to reopen or submit a new PR.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@JelleZijlstra JelleZijlstra closed this May 8, 2022
@ukarroum ukarroum mentioned this pull request Apr 20, 2025
gh-45048: Document the availability and uses of the SO_ constants #132752
Closed
@sandrotosi sandrotosi mentioned this pull request Aug 17, 2025
Documenting all SO_* constants in socket module #57096
Open
@techtonik techtonik mannequin mentioned this pull request Jul 3, 2026
Document socket.SOL_SOCKET #58553
Open
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@vadmium vadmium vadmium left review comments

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

awaiting changes docs Documentation in the Doc dir stale Stale PR or inactive for long period of time.

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

14 participants

@ammaraskar @pitrou @matrixise @vstinner @JulienPalard @csabella @JelleZijlstra @vadmium @brettcannon @ambv @serhiy-storchaka @ned-deily @Mariatta @the-knights-who-say-ni
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.