Discussion:
[Python-checkins] cpython (3.4): Issue20386: SocketType is again socket.socket; the IntEnum SOCK constants are
ethan.furman
2014-10-15 01:58:20 UTC
Permalink
https://hg.python.org/cpython/rev/613c30ffd344
changeset: 93063:613c30ffd344
branch: 3.4
parent: 93061:94f8d65371b7
user: Ethan Furman <ethan at stoneleaf.us>
date: Tue Oct 14 18:56:53 2014 -0700
summary:
Issue20386: SocketType is again socket.socket; the IntEnum SOCK constants are SocketKind

files:
Doc/library/socket.rst | 5 +++++
Lib/socket.py | 12 +++++++-----
Lib/test/test_socket.py | 11 +++++++++--
3 files changed, 21 insertions(+), 7 deletions(-)


diff --git a/Doc/library/socket.rst b/Doc/library/socket.rst
--- a/Doc/library/socket.rst
+++ b/Doc/library/socket.rst
@@ -192,6 +192,11 @@
Constants
^^^^^^^^^

+ The AF_* and SOCK_* constants are now :class:`AddressFamily` and
+ :class:`SocketKind` :class:`.IntEnum` collections.
+
+ .. versionadded:: 3.4
+
.. data:: AF_UNIX
AF_INET
AF_INET6
diff --git a/Lib/socket.py b/Lib/socket.py
--- a/Lib/socket.py
+++ b/Lib/socket.py
@@ -35,11 +35,13 @@
error -- exception raised for I/O errors
has_ipv6 -- boolean value indicating if IPv6 is supported

-Integer constants:
+IntEnum constants:

AF_INET, AF_UNIX -- socket domains (first argument to socket() call)
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
the setsockopt() and getsockopt() methods.
"""
@@ -71,10 +73,10 @@
if name.isupper() and name.startswith('AF_')})
globals().update(AddressFamily.__members__)

-SocketType = IntEnum('SocketType',
+SocketKind = IntEnum('SocketKind',
{name: value for name, value in globals().items()
if name.isupper() and name.startswith('SOCK_')})
-globals().update(SocketType.__members__)
+globals().update(SocketKind.__members__)

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

if os.name == 'nt':
def get_inheritable(self):
@@ -530,6 +532,6 @@
for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
af, socktype, proto, canonname, sa = res
addrlist.append((_intenum_converter(af, AddressFamily),
- _intenum_converter(socktype, SocketType),
+ _intenum_converter(socktype, SocketKind),
proto, canonname, sa))
return addrlist
diff --git a/Lib/test/test_socket.py b/Lib/test/test_socket.py
--- a/Lib/test/test_socket.py
+++ b/Lib/test/test_socket.py
@@ -649,6 +649,13 @@

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):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
with s:
@@ -1224,7 +1231,7 @@
self.assertEqual(family, socket.AF_INET)
self.assertEqual(str(family), 'AddressFamily.AF_INET')
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)
for _, socktype, _, _, _ in infos:
self.assertEqual(socktype, socket.SOCK_STREAM)
@@ -1396,7 +1403,7 @@
# reprs.
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
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')
def test_uknown_socket_family_repr(self):
--
Repository URL: https://hg.python.org/cpython
Loading...