summaryrefslogtreecommitdiff
path: root/Lib/test/test_socket.py
diff options
context:
space:
mode:
Diffstat (limited to 'Lib/test/test_socket.py')
-rw-r--r--Lib/test/test_socket.py41
1 files changed, 35 insertions, 6 deletions
diff --git a/Lib/test/test_socket.py b/Lib/test/test_socket.py
index 5b4c5f9f8c..43688eacf0 100644
--- a/Lib/test/test_socket.py
+++ b/Lib/test/test_socket.py
@@ -1577,6 +1577,22 @@ class GeneralModuleTests(unittest.TestCase):
self.assertEqual(str(s.family), 'AddressFamily.AF_INET')
self.assertEqual(str(s.type), 'SocketKind.SOCK_STREAM')
+ def test_socket_consistent_sock_type(self):
+ SOCK_NONBLOCK = getattr(socket, 'SOCK_NONBLOCK', 0)
+ SOCK_CLOEXEC = getattr(socket, 'SOCK_CLOEXEC', 0)
+ sock_type = socket.SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC
+
+ with socket.socket(socket.AF_INET, sock_type) as s:
+ self.assertEqual(s.type, socket.SOCK_STREAM)
+ s.settimeout(1)
+ self.assertEqual(s.type, socket.SOCK_STREAM)
+ s.settimeout(0)
+ self.assertEqual(s.type, socket.SOCK_STREAM)
+ s.setblocking(True)
+ self.assertEqual(s.type, socket.SOCK_STREAM)
+ s.setblocking(False)
+ self.assertEqual(s.type, socket.SOCK_STREAM)
+
@unittest.skipIf(os.name == 'nt', 'Will not work on Windows')
def test_uknown_socket_family_repr(self):
# Test that when created with a family that's not one of the known
@@ -1589,9 +1605,18 @@ class GeneralModuleTests(unittest.TestCase):
# On Windows this trick won't work, so the test is skipped.
fd, path = tempfile.mkstemp()
self.addCleanup(os.unlink, path)
- with socket.socket(family=42424, type=13331, fileno=fd) as s:
- self.assertEqual(s.family, 42424)
- self.assertEqual(s.type, 13331)
+ unknown_family = max(socket.AddressFamily.__members__.values()) + 1
+
+ unknown_type = max(
+ kind
+ for name, kind in socket.SocketKind.__members__.items()
+ if name not in {'SOCK_NONBLOCK', 'SOCK_CLOEXEC'}
+ ) + 1
+
+ with socket.socket(
+ family=unknown_family, type=unknown_type, fileno=fd) as s:
+ self.assertEqual(s.family, unknown_family)
+ self.assertEqual(s.type, unknown_type)
@unittest.skipUnless(hasattr(os, 'sendfile'), 'test needs os.sendfile()')
def test__sendfile_use_sendfile(self):
@@ -5084,7 +5109,7 @@ class InheritanceTest(unittest.TestCase):
def test_SOCK_CLOEXEC(self):
with socket.socket(socket.AF_INET,
socket.SOCK_STREAM | socket.SOCK_CLOEXEC) as s:
- self.assertTrue(s.type & socket.SOCK_CLOEXEC)
+ self.assertEqual(s.type, socket.SOCK_STREAM)
self.assertFalse(s.get_inheritable())
def test_default_inheritable(self):
@@ -5149,11 +5174,15 @@ class InheritanceTest(unittest.TestCase):
class NonblockConstantTest(unittest.TestCase):
def checkNonblock(self, s, nonblock=True, timeout=0.0):
if nonblock:
- self.assertTrue(s.type & socket.SOCK_NONBLOCK)
+ self.assertEqual(s.type, socket.SOCK_STREAM)
self.assertEqual(s.gettimeout(), timeout)
+ self.assertTrue(
+ fcntl.fcntl(s, fcntl.F_GETFL, os.O_NONBLOCK) & os.O_NONBLOCK)
else:
- self.assertFalse(s.type & socket.SOCK_NONBLOCK)
+ self.assertEqual(s.type, socket.SOCK_STREAM)
self.assertEqual(s.gettimeout(), None)
+ self.assertFalse(
+ fcntl.fcntl(s, fcntl.F_GETFL, os.O_NONBLOCK) & os.O_NONBLOCK)
@support.requires_linux_version(2, 6, 28)
def test_SOCK_NONBLOCK(self):