cpython: 038543d34166 (original) (raw)

Mercurial > cpython

changeset 85481:038543d34166

Switch the AF_* and SOCK_* constants in the socket module to IntEnum. Closes #18720. [#18720]

Eli Bendersky eliben@gmail.com
date Sat, 31 Aug 2013 15:13:30 -0700
parents 5600e9a5c35d
children 4d604f1f0219
files Lib/socket.py Lib/test/test_socket.py
diffstat 2 files changed, 91 insertions(+), 3 deletions(-)[+] [-] Lib/socket.py 66 Lib/test/test_socket.py 28

line wrap: on

line diff

--- a/Lib/socket.py +++ b/Lib/socket.py @@ -48,6 +48,7 @@ import _socket from _socket import import os, sys, io +from enum import IntEnum try: import errno @@ -60,6 +61,30 @@ EWOULDBLOCK = getattr(errno, 'EWOULDBLOC all = ["getfqdn", "create_connection"] all.extend(os._get_exports_list(socket)) +# Set up the socket.AF socket.SOCK_* constants as members of IntEnums for +# nicer string representations. +# Note that _socket only knows about the integer values. The public interface +# in this module understands the enums and translates them back from integers +# where needed (e.g. .family property of a socket object). +AddressFamily = IntEnum('AddressFamily',

+globals().update(AddressFamily.members) + +SocketType = IntEnum('SocketType',

+globals().update(SocketType.members) + +def _intenum_converter(value, enum_klass):

+

_realsocket = socket @@ -91,6 +116,10 @@ class socket(_socket.socket): slots = ["weakref", "_io_refs", "_closed"] def init(self, family=AF_INET, type=SOCK_STREAM, proto=0, fileno=None):

@@ -230,6 +259,18 @@ class socket(_socket.socket): self._closed = True return super().detach()

+

+ if os.name == 'nt': def get_inheritable(self): return os.get_handle_inheritable(self.fileno()) @@ -243,7 +284,6 @@ class socket(socket.socket): get_inheritable.doc = "Get the inheritable flag of the socket" set_inheritable.doc = "Set the inheritable flag of the socket" - def fromfd(fd, family, type, proto=0): """ fromfd(fd, family, type[, proto]) -> socket object @@ -469,3 +509,27 @@ def create_connection(address, timeout= raise err else: raise error("getaddrinfo returns an empty list") + +def getaddrinfo(host, port, family=0, type=0, proto=0, flags=0):

+

+

--- a/Lib/test/test_socket.py +++ b/Lib/test/test_socket.py @@ -1161,9 +1161,12 @@ class GeneralModuleTests(unittest.TestCa socket.getaddrinfo(HOST, 80) socket.getaddrinfo(HOST, None) # test family and socktype filters

@@ -1321,6 +1324,27 @@ class GeneralModuleTests(unittest.TestCa with socket.socket(socket.AF_INET6, socket.SOCK_STREAM) as s: self.assertRaises(OverflowError, s.bind, (support.HOSTv6, 0, -10))

+

@unittest.skipUnless(HAVE_SOCKET_CAN, 'SocketCan required for this test.') class BasicCANTest(unittest.TestCase):