[New-bugs-announce] [issue18720] Switch suitable constants in the socket module to IntEnum

Eli Bendersky report at bugs.python.org
Mon Aug 12 23:10:49 CEST 2013

New submission from Eli Bendersky:

As part of original plan and since issue #18264 has been resolved, it's time to dust off some old patches I have for the socket.* module. The socket.AF_* and socket.SOCK_* constants are good candidates for IntEnum conversion.

I'm attaching an initial patch that handles socket.AF_* (as a proof-of-concept; socket.SOCK_* should get identical treatment); it only touches Lib/socket.py and all regrtest tests pass without changes. Result:

>>> import socket
>>> socket.AF_INET
<AddressFamily.AF_INET: 2>
>>> s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
>>> s.family
<AddressFamily.AF_INET: 2>
>>> '{}'.format(s.family)

The code in the patch is pretty well commented, and I also want to point out a couple of decision points that came up:

1. The underlying socketmodule.c has no idea of IntEnums, and neither IMHO it should. These constants are essentially one-way, going from Python into C. They are only exposed back through read-only accessors (e.g. s.family above), at which point the Python code can wrap them back into the enum. The alternative, making socketmodule.c use enums is probably way more complicated than really necessary.
2. As a followup to (1), the constants are actually wrapped into an IntEnum at the Python level and exposed back to the user. My hacking of globals() there may be a bit rough - suggestions for a better way are welcome.
3. A bunch of AF_* constants exported by Python built on my x64 Ubuntu are not documented in socket.rst; I'm still wrapping them all in enums; I basically went over all PyModule_AddIntMacro(m, AF_*) in PyInit__socket.


assignee: eli.bendersky
components: Library (Lib)
files: socket-intenum-af.1.patch
keywords: patch
messages: 195018
nosy: barry, eli.bendersky, ethan.furman, gvanrossum, ncoghlan, pitrou
priority: normal
severity: normal
stage: patch review
status: open
title: Switch suitable constants in the socket module to IntEnum
type: enhancement
versions: Python 3.4
Added file: http://bugs.python.org/file31261/socket-intenum-af.1.patch

Python tracker <report at bugs.python.org>

More information about the New-bugs-announce mailing list