Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7184bac

Browse files
committed
Issue20386: SocketType is again socket.socket; the IntEnum SOCK constants are SocketKind
1 parent 00bdce3 commit 7184bac

3 files changed

Lines changed: 21 additions & 7 deletions

File tree

Doc/library/socket.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,11 @@ Exceptions
192192
Constants
193193
^^^^^^^^^
194194

195+
The AF_* and SOCK_* constants are now :class:`AddressFamily` and
196+
:class:`SocketKind` :class:`.IntEnum` collections.
197+
198+
.. versionadded:: 3.4
199+
195200
.. data:: AF_UNIX
196201
AF_INET
197202
AF_INET6

Lib/socket.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,13 @@
3535
error -- exception raised for I/O errors
3636
has_ipv6 -- boolean value indicating if IPv6 is supported
3737
38-
Integer constants:
38+
IntEnum constants:
3939
4040
AF_INET, AF_UNIX -- socket domains (first argument to socket() call)
4141
SOCK_STREAM, SOCK_DGRAM, SOCK_RAW -- socket types (second argument)
4242
43+
Integer constants:
44+
4345
Many other constants may be defined; these may be used in calls to
4446
the setsockopt() and getsockopt() methods.
4547
"""
@@ -71,10 +73,10 @@
7173
if name.isupper() and name.startswith('AF_')})
7274
globals().update(AddressFamily.__members__)
7375

74-
SocketType = IntEnum('SocketType',
76+
SocketKind = IntEnum('SocketKind',
7577
{name: value for name, value in globals().items()
7678
if name.isupper() and name.startswith('SOCK_')})
77-
globals().update(SocketType.__members__)
79+
globals().update(SocketKind.__members__)
7880

7981
def _intenum_converter(value, enum_klass):
8082
"""Convert a numeric family value to an IntEnum member.
@@ -269,7 +271,7 @@ def family(self):
269271
def type(self):
270272
"""Read-only access to the socket type.
271273
"""
272-
return _intenum_converter(super().type, SocketType)
274+
return _intenum_converter(super().type, SocketKind)
273275

274276
if os.name == 'nt':
275277
def get_inheritable(self):
@@ -530,6 +532,6 @@ def getaddrinfo(host, port, family=0, type=0, proto=0, flags=0):
530532
for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
531533
af, socktype, proto, canonname, sa = res
532534
addrlist.append((_intenum_converter(af, AddressFamily),
533-
_intenum_converter(socktype, SocketType),
535+
_intenum_converter(socktype, SocketKind),
534536
proto, canonname, sa))
535537
return addrlist

Lib/test/test_socket.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -649,6 +649,13 @@ def requireSocket(*args):
649649

650650
class GeneralModuleTests(unittest.TestCase):
651651

652+
def test_SocketType_is_socketobject(self):
653+
import _socket
654+
self.assertTrue(socket.SocketType is _socket.socket)
655+
s = socket.socket()
656+
self.assertIsInstance(s, socket.SocketType)
657+
s.close()
658+
652659
def test_repr(self):
653660
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
654661
with s:
@@ -1224,7 +1231,7 @@ def testGetaddrinfo(self):
12241231
self.assertEqual(family, socket.AF_INET)
12251232
self.assertEqual(str(family), 'AddressFamily.AF_INET')
12261233
self.assertEqual(type, socket.SOCK_STREAM)
1227-
self.assertEqual(str(type), 'SocketType.SOCK_STREAM')
1234+
self.assertEqual(str(type), 'SocketKind.SOCK_STREAM')
12281235
infos = socket.getaddrinfo(HOST, None, 0, socket.SOCK_STREAM)
12291236
for _, socktype, _, _, _ in infos:
12301237
self.assertEqual(socktype, socket.SOCK_STREAM)
@@ -1396,7 +1403,7 @@ def test_str_for_enums(self):
13961403
# reprs.
13971404
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
13981405
self.assertEqual(str(s.family), 'AddressFamily.AF_INET')
1399-
self.assertEqual(str(s.type), 'SocketType.SOCK_STREAM')
1406+
self.assertEqual(str(s.type), 'SocketKind.SOCK_STREAM')
14001407

14011408
@unittest.skipIf(os.name == 'nt', 'Will not work on Windows')
14021409
def test_uknown_socket_family_repr(self):

0 commit comments

Comments
 (0)