Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some socket constants are not enums #65605

Closed
pitrou opened this issue May 1, 2014 · 7 comments
Closed

Some socket constants are not enums #65605

pitrou opened this issue May 1, 2014 · 7 comments
Labels
type-feature A feature request or enhancement

Comments

@pitrou
Copy link
Member

pitrou commented May 1, 2014

BPO 21406
Nosy @warsaw, @pitrou, @bitdancer, @ethanfurman

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = <Date 2015-07-21.08:04:46.847>
created_at = <Date 2014-05-01.12:17:46.891>
labels = ['type-feature']
title = 'Some socket constants are not enums'
updated_at = <Date 2015-07-21.08:04:46.846>
user = 'https://github.com/pitrou'

bugs.python.org fields:

activity = <Date 2015-07-21.08:04:46.846>
actor = 'ethan.furman'
assignee = 'none'
closed = True
closed_date = <Date 2015-07-21.08:04:46.847>
closer = 'ethan.furman'
components = []
creation = <Date 2014-05-01.12:17:46.891>
creator = 'pitrou'
dependencies = []
files = []
hgrepos = []
issue_num = 21406
keywords = []
message_count = 7.0
messages = ['217691', '217712', '217714', '217715', '217716', '217717', '217718']
nosy_count = 6.0
nosy_names = ['barry', 'pitrou', 'r.david.murray', 'eli.bendersky', 'neologix', 'ethan.furman']
pr_nums = []
priority = 'low'
resolution = 'rejected'
stage = None
status = 'closed'
superseder = None
type = 'enhancement'
url = 'https://bugs.python.org/issue21406'
versions = ['Python 3.5']

@pitrou
Copy link
Member Author

pitrou commented May 1, 2014

Many constants in the socket module, are not int enums. Examples are socket.CAN_BCM, socket.BTPROTO_RFCOMM, etc.

For example when creating a bluetooth socket, you may get the following repr():

>>> socket.socket(socket.AF_BLUETOOTH, socket.SOCK_STREAM, socket.BTPROTO_RFCOMM)
<socket.socket fd=3, family=AddressFamily.AF_BLUETOOTH, type=SocketType.SOCK_STREAM, proto=3, laddr=('00:00:00:00:00:00', 0), raddr=('00:00:00:00:00:00', 0)>

(notice the integer "proto")

@pitrou pitrou added the type-feature A feature request or enhancement label May 1, 2014
@neologix
Copy link
Mannequin

neologix mannequin commented May 1, 2014

Enum are for, well, enumerated values, so for values within a finite
and known range (like days, cards, etc).
OTOH, I'm not sure all socket constants could be categorized like this.
It makes sense for address families, especially since they're used so
much, but when it comes to e.g. SO_REUSEADDR or BTPROTO_RFCOMM, I'm
not sure how we could categorize them.
Unless would declare them all in a "SocketConstant" Enum?

@bitdancer
Copy link
Member

This is why we should have had named constants and not Enums :)

But no, nothing in the python Enum implementation restricts it to a value *range*. It is really a collection of named constants.

@neologix
Copy link
Mannequin

neologix mannequin commented May 1, 2014

But no, nothing in the python Enum implementation restricts it to a value *range*. It is really a collection of named constants.

I didn't say in the implementation, I said in spirit.
Would you describe all possible Unix PIDs are a Enum?

Also, the problem is that many such constant can have identical values
(because they can be passed at different syscalls/argument offset),
and in this case the IntEnum equality isn't wanted:
cf@neobox:~/python/hg/default$ cat /tmp/test.py
from enum import IntEnum

class Const(IntEnum):

    AF_INET = 1
    SO_REUSEADDR = 1

print(Const.AF_INET == Const.SO_REUSEADDR)
cf@neobox:~/python/hg/default$ ./python /tmp/test.py
True

Really?

@pitrou
Copy link
Member Author

pitrou commented May 1, 2014

It makes sense for address families, especially since they're used so
much, but when it comes to e.g. SO_REUSEADDR or BTPROTO_RFCOMM,

Hmm, I was thinking mostly about protocol numbers. All the BTPROTO_* constants should be part of a given enum (BlueToothProtocol?), the CAN_* constants part of another one.

@neologix
Copy link
Mannequin

neologix mannequin commented May 1, 2014

To put it slightly differently:
AF_XXX constant actually whome belong to the same namespace, the
socket address family namespace.
So we put them all in AddressFamily Enum.

Now, for many constants defined in system header files, it's not so
clear, e.g. BTPROTO_RFCOMM, TIPC_ADDR_ID, SCM_CREDENTIALS: in which
Enum would you declare them?

I'm not saying it's a bad idea: it actually probably makes sense for
e.g. socket-level options (SO_REUSEADDR & Co), but it don't see any
generic classification scheme that would make sense for all of them.

@pitrou
Copy link
Member Author

pitrou commented May 1, 2014

Ah, I missed the fact that the "family" and "type" properties are re-computed on the fly; I thought the enum values where stored on the socket object.

Then it makes it harder to do the same for "proto", since there are family-specific namespaces with colliding values, indeed.

>>> socket.IPPROTO_ICMP
1
>>> socket.BTPROTO_HCI
1

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

3 participants