bpo-28134: Auto-detect socket values from file descriptor (#1349) · python/cpython@b6e43af (original) (raw)
`@@ -20,6 +20,7 @@
`
20
20
`import pickle
`
21
21
`import struct
`
22
22
`import random
`
``
23
`+
import shutil
`
23
24
`import string
`
24
25
`import _thread as thread
`
25
26
`import threading
`
`@@ -1284,6 +1285,7 @@ def testSendAfterClose(self):
`
1284
1285
``
1285
1286
`def testCloseException(self):
`
1286
1287
`sock = socket.socket()
`
``
1288
`+
sock.bind((socket._LOCALHOST, 0))
`
1287
1289
`socket.socket(fileno=sock.fileno()).close()
`
1288
1290
`try:
`
1289
1291
`sock.close()
`
`@@ -1636,9 +1638,11 @@ def test_uknown_socket_family_repr(self):
`
1636
1638
` ) + 1
`
1637
1639
``
1638
1640
`with socket.socket(
`
1639
``
`-
family=unknown_family, type=unknown_type, fileno=fd) as s:
`
``
1641
`+
family=unknown_family, type=unknown_type, proto=23,
`
``
1642
`+
fileno=fd) as s:
`
1640
1643
`self.assertEqual(s.family, unknown_family)
`
1641
1644
`self.assertEqual(s.type, unknown_type)
`
``
1645
`+
self.assertEqual(s.proto, 23)
`
1642
1646
``
1643
1647
`@unittest.skipUnless(hasattr(os, 'sendfile'), 'test needs os.sendfile()')
`
1644
1648
`def test__sendfile_use_sendfile(self):
`
`@@ -1658,6 +1662,45 @@ def fileno(self):
`
1658
1662
`with self.assertRaises(TypeError):
`
1659
1663
`sock._sendfile_use_sendfile(File(None))
`
1660
1664
``
``
1665
`+
def _test_socket_fileno(self, s, family, stype):
`
``
1666
`+
self.assertEqual(s.family, family)
`
``
1667
`+
self.assertEqual(s.type, stype)
`
``
1668
+
``
1669
`+
fd = s.fileno()
`
``
1670
`+
s2 = socket.socket(fileno=fd)
`
``
1671
`+
self.addCleanup(s2.close)
`
``
1672
`+
detach old fd to avoid double close
`
``
1673
`+
s.detach()
`
``
1674
`+
self.assertEqual(s2.family, family)
`
``
1675
`+
self.assertEqual(s2.type, stype)
`
``
1676
`+
self.assertEqual(s2.fileno(), fd)
`
``
1677
+
``
1678
`+
def test_socket_fileno(self):
`
``
1679
`+
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
`
``
1680
`+
self.addCleanup(s.close)
`
``
1681
`+
s.bind((support.HOST, 0))
`
``
1682
`+
self._test_socket_fileno(s, socket.AF_INET, socket.SOCK_STREAM)
`
``
1683
+
``
1684
`+
if hasattr(socket, "SOCK_DGRAM"):
`
``
1685
`+
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
`
``
1686
`+
self.addCleanup(s.close)
`
``
1687
`+
s.bind((support.HOST, 0))
`
``
1688
`+
self._test_socket_fileno(s, socket.AF_INET, socket.SOCK_DGRAM)
`
``
1689
+
``
1690
`+
if support.IPV6_ENABLED:
`
``
1691
`+
s = socket.socket(socket.AF_INET6, socket.SOCK_STREAM)
`
``
1692
`+
self.addCleanup(s.close)
`
``
1693
`+
s.bind((support.HOSTv6, 0, 0, 0))
`
``
1694
`+
self._test_socket_fileno(s, socket.AF_INET6, socket.SOCK_STREAM)
`
``
1695
+
``
1696
`+
if hasattr(socket, "AF_UNIX"):
`
``
1697
`+
tmpdir = tempfile.mkdtemp()
`
``
1698
`+
self.addCleanup(shutil.rmtree, tmpdir)
`
``
1699
`+
s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
`
``
1700
`+
self.addCleanup(s.close)
`
``
1701
`+
s.bind(os.path.join(tmpdir, 'socket'))
`
``
1702
`+
self._test_socket_fileno(s, socket.AF_UNIX, socket.SOCK_STREAM)
`
``
1703
+
1661
1704
``
1662
1705
`@unittest.skipUnless(HAVE_SOCKET_CAN, 'SocketCan required for this test.')
`
1663
1706
`class BasicCANTest(unittest.TestCase):
`