bpo-32331: Fix socket.type when SOCK_NONBLOCK is available (#4877) · python/cpython@9818142 (original) (raw)

`@@ -1577,6 +1577,22 @@ def test_str_for_enums(self):

`

1577

1577

`self.assertEqual(str(s.family), 'AddressFamily.AF_INET')

`

1578

1578

`self.assertEqual(str(s.type), 'SocketKind.SOCK_STREAM')

`

1579

1579

``

``

1580

`+

def test_socket_consistent_sock_type(self):

`

``

1581

`+

SOCK_NONBLOCK = getattr(socket, 'SOCK_NONBLOCK', 0)

`

``

1582

`+

SOCK_CLOEXEC = getattr(socket, 'SOCK_CLOEXEC', 0)

`

``

1583

`+

sock_type = socket.SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC

`

``

1584

+

``

1585

`+

with socket.socket(socket.AF_INET, sock_type) as s:

`

``

1586

`+

self.assertEqual(s.type, socket.SOCK_STREAM)

`

``

1587

`+

s.settimeout(1)

`

``

1588

`+

self.assertEqual(s.type, socket.SOCK_STREAM)

`

``

1589

`+

s.settimeout(0)

`

``

1590

`+

self.assertEqual(s.type, socket.SOCK_STREAM)

`

``

1591

`+

s.setblocking(True)

`

``

1592

`+

self.assertEqual(s.type, socket.SOCK_STREAM)

`

``

1593

`+

s.setblocking(False)

`

``

1594

`+

self.assertEqual(s.type, socket.SOCK_STREAM)

`

``

1595

+

1580

1596

`@unittest.skipIf(os.name == 'nt', 'Will not work on Windows')

`

1581

1597

`def test_uknown_socket_family_repr(self):

`

1582

1598

`# Test that when created with a family that's not one of the known

`

`@@ -1589,9 +1605,18 @@ def test_uknown_socket_family_repr(self):

`

1589

1605

`# On Windows this trick won't work, so the test is skipped.

`

1590

1606

`fd, path = tempfile.mkstemp()

`

1591

1607

`self.addCleanup(os.unlink, path)

`

1592

``

`-

with socket.socket(family=42424, type=13331, fileno=fd) as s:

`

1593

``

`-

self.assertEqual(s.family, 42424)

`

1594

``

`-

self.assertEqual(s.type, 13331)

`

``

1608

`+

unknown_family = max(socket.AddressFamily.members.values()) + 1

`

``

1609

+

``

1610

`+

unknown_type = max(

`

``

1611

`+

kind

`

``

1612

`+

for name, kind in socket.SocketKind.members.items()

`

``

1613

`+

if name not in {'SOCK_NONBLOCK', 'SOCK_CLOEXEC'}

`

``

1614

`+

) + 1

`

``

1615

+

``

1616

`+

with socket.socket(

`

``

1617

`+

family=unknown_family, type=unknown_type, fileno=fd) as s:

`

``

1618

`+

self.assertEqual(s.family, unknown_family)

`

``

1619

`+

self.assertEqual(s.type, unknown_type)

`

1595

1620

``

1596

1621

`@unittest.skipUnless(hasattr(os, 'sendfile'), 'test needs os.sendfile()')

`

1597

1622

`def test__sendfile_use_sendfile(self):

`

`@@ -5084,7 +5109,7 @@ class InheritanceTest(unittest.TestCase):

`

5084

5109

`def test_SOCK_CLOEXEC(self):

`

5085

5110

`with socket.socket(socket.AF_INET,

`

5086

5111

`socket.SOCK_STREAM | socket.SOCK_CLOEXEC) as s:

`

5087

``

`-

self.assertTrue(s.type & socket.SOCK_CLOEXEC)

`

``

5112

`+

self.assertEqual(s.type, socket.SOCK_STREAM)

`

5088

5113

`self.assertFalse(s.get_inheritable())

`

5089

5114

``

5090

5115

`def test_default_inheritable(self):

`

`@@ -5149,11 +5174,15 @@ def test_socketpair(self):

`

5149

5174

`class NonblockConstantTest(unittest.TestCase):

`

5150

5175

`def checkNonblock(self, s, nonblock=True, timeout=0.0):

`

5151

5176

`if nonblock:

`

5152

``

`-

self.assertTrue(s.type & socket.SOCK_NONBLOCK)

`

``

5177

`+

self.assertEqual(s.type, socket.SOCK_STREAM)

`

5153

5178

`self.assertEqual(s.gettimeout(), timeout)

`

``

5179

`+

self.assertTrue(

`

``

5180

`+

fcntl.fcntl(s, fcntl.F_GETFL, os.O_NONBLOCK) & os.O_NONBLOCK)

`

5154

5181

`else:

`

5155

``

`-

self.assertFalse(s.type & socket.SOCK_NONBLOCK)

`

``

5182

`+

self.assertEqual(s.type, socket.SOCK_STREAM)

`

5156

5183

`self.assertEqual(s.gettimeout(), None)

`

``

5184

`+

self.assertFalse(

`

``

5185

`+

fcntl.fcntl(s, fcntl.F_GETFL, os.O_NONBLOCK) & os.O_NONBLOCK)

`

5157

5186

``

5158

5187

`@support.requires_linux_version(2, 6, 28)

`

5159

5188

`def test_SOCK_NONBLOCK(self):

`