Issue20386: SocketType is again socket.socket; the IntEnum SOCK constants are SocketKind

This commit is contained in:
Ethan Furman 2014-10-14 18:57:58 -07:00
commit 41d31967c6
3 changed files with 21 additions and 7 deletions

View File

@ -192,6 +192,11 @@ Exceptions
Constants Constants
^^^^^^^^^ ^^^^^^^^^
The AF_* and SOCK_* constants are now :class:`AddressFamily` and
:class:`SocketKind` :class:`.IntEnum` collections.
.. versionadded:: 3.4
.. data:: AF_UNIX .. data:: AF_UNIX
AF_INET AF_INET
AF_INET6 AF_INET6

View File

@ -35,11 +35,13 @@ SocketType -- type object for socket objects
error -- exception raised for I/O errors error -- exception raised for I/O errors
has_ipv6 -- boolean value indicating if IPv6 is supported has_ipv6 -- boolean value indicating if IPv6 is supported
Integer constants: IntEnum constants:
AF_INET, AF_UNIX -- socket domains (first argument to socket() call) AF_INET, AF_UNIX -- socket domains (first argument to socket() call)
SOCK_STREAM, SOCK_DGRAM, SOCK_RAW -- socket types (second argument) SOCK_STREAM, SOCK_DGRAM, SOCK_RAW -- socket types (second argument)
Integer constants:
Many other constants may be defined; these may be used in calls to Many other constants may be defined; these may be used in calls to
the setsockopt() and getsockopt() methods. the setsockopt() and getsockopt() methods.
""" """
@ -71,10 +73,10 @@ AddressFamily = IntEnum('AddressFamily',
if name.isupper() and name.startswith('AF_')}) if name.isupper() and name.startswith('AF_')})
globals().update(AddressFamily.__members__) globals().update(AddressFamily.__members__)
SocketType = IntEnum('SocketType', SocketKind = IntEnum('SocketKind',
{name: value for name, value in globals().items() {name: value for name, value in globals().items()
if name.isupper() and name.startswith('SOCK_')}) if name.isupper() and name.startswith('SOCK_')})
globals().update(SocketType.__members__) globals().update(SocketKind.__members__)
_LOCALHOST = '127.0.0.1' _LOCALHOST = '127.0.0.1'
@ -420,7 +422,7 @@ class socket(_socket.socket):
def type(self): def type(self):
"""Read-only access to the socket type. """Read-only access to the socket type.
""" """
return _intenum_converter(super().type, SocketType) return _intenum_converter(super().type, SocketKind)
if os.name == 'nt': if os.name == 'nt':
def get_inheritable(self): def get_inheritable(self):
@ -727,6 +729,6 @@ def getaddrinfo(host, port, family=0, type=0, proto=0, flags=0):
for res in _socket.getaddrinfo(host, port, family, type, proto, flags): for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
af, socktype, proto, canonname, sa = res af, socktype, proto, canonname, sa = res
addrlist.append((_intenum_converter(af, AddressFamily), addrlist.append((_intenum_converter(af, AddressFamily),
_intenum_converter(socktype, SocketType), _intenum_converter(socktype, SocketKind),
proto, canonname, sa)) proto, canonname, sa))
return addrlist return addrlist

View File

@ -651,6 +651,13 @@ def requireSocket(*args):
class GeneralModuleTests(unittest.TestCase): class GeneralModuleTests(unittest.TestCase):
def test_SocketType_is_socketobject(self):
import _socket
self.assertTrue(socket.SocketType is _socket.socket)
s = socket.socket()
self.assertIsInstance(s, socket.SocketType)
s.close()
def test_repr(self): def test_repr(self):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
with s: with s:
@ -1226,7 +1233,7 @@ class GeneralModuleTests(unittest.TestCase):
self.assertEqual(family, socket.AF_INET) self.assertEqual(family, socket.AF_INET)
self.assertEqual(str(family), 'AddressFamily.AF_INET') self.assertEqual(str(family), 'AddressFamily.AF_INET')
self.assertEqual(type, socket.SOCK_STREAM) self.assertEqual(type, socket.SOCK_STREAM)
self.assertEqual(str(type), 'SocketType.SOCK_STREAM') self.assertEqual(str(type), 'SocketKind.SOCK_STREAM')
infos = socket.getaddrinfo(HOST, None, 0, socket.SOCK_STREAM) infos = socket.getaddrinfo(HOST, None, 0, socket.SOCK_STREAM)
for _, socktype, _, _, _ in infos: for _, socktype, _, _, _ in infos:
self.assertEqual(socktype, socket.SOCK_STREAM) self.assertEqual(socktype, socket.SOCK_STREAM)
@ -1401,7 +1408,7 @@ class GeneralModuleTests(unittest.TestCase):
# reprs. # reprs.
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
self.assertEqual(str(s.family), 'AddressFamily.AF_INET') self.assertEqual(str(s.family), 'AddressFamily.AF_INET')
self.assertEqual(str(s.type), 'SocketType.SOCK_STREAM') self.assertEqual(str(s.type), 'SocketKind.SOCK_STREAM')
@unittest.skipIf(os.name == 'nt', 'Will not work on Windows') @unittest.skipIf(os.name == 'nt', 'Will not work on Windows')
def test_uknown_socket_family_repr(self): def test_uknown_socket_family_repr(self):