gh-132099: Harmonize Bluetooth address handling by serhiy-storchaka · Pull Request #132486 · python/cpython · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions Doc/library/socket.rst
40 changes: 31 additions & 9 deletions Lib/test/test_socket.py
Original file line number Diff line number Diff line change
Expand Up @@ -2681,6 +2681,8 @@ def testBadL2capAddr(self):
f.bind(socket.BDADDR_ANY)
with self.assertRaises(OSError):
f.bind((socket.BDADDR_ANY.encode(), 0x1001))
with self.assertRaises(OSError):
f.bind(('\ud812', 0x1001))

def testBindRfcommSocket(self):
with socket.socket(socket.AF_BLUETOOTH, socket.SOCK_STREAM, socket.BTPROTO_RFCOMM) as s:
Expand Down Expand Up @@ -2712,14 +2714,16 @@ def testBadRfcommAddr(self):
s.bind((socket.BDADDR_ANY, channel, 0))
with self.assertRaises(OSError):
s.bind((socket.BDADDR_ANY + '\0', channel))
with self.assertRaises(OSError):
s.bind('\ud812')
with self.assertRaises(OSError):
s.bind(('invalid', channel))

@unittest.skipUnless(hasattr(socket, 'BTPROTO_HCI'), 'Bluetooth HCI sockets required for this test')
def testBindHciSocket(self):
with socket.socket(socket.AF_BLUETOOTH, socket.SOCK_RAW, socket.BTPROTO_HCI) as s:
if sys.platform.startswith(('netbsd', 'dragonfly', 'freebsd')):
s.bind(socket.BDADDR_ANY.encode())
s.bind(socket.BDADDR_ANY)
addr = s.getsockname()
self.assertEqual(addr, socket.BDADDR_ANY)
else:
Expand All @@ -2738,14 +2742,17 @@ def testBadHciAddr(self):
with socket.socket(socket.AF_BLUETOOTH, socket.SOCK_RAW, socket.BTPROTO_HCI) as s:
if sys.platform.startswith(('netbsd', 'dragonfly', 'freebsd')):
with self.assertRaises(OSError):
s.bind(socket.BDADDR_ANY)
s.bind(socket.BDADDR_ANY.encode())
with self.assertRaises(OSError):
s.bind((socket.BDADDR_ANY.encode(),))
if sys.platform.startswith('freebsd'):
with self.assertRaises(ValueError):
s.bind(socket.BDADDR_ANY.encode() + b'\0')
with self.assertRaises(ValueError):
s.bind(socket.BDADDR_ANY.encode() + b' '*100)
s.bind((socket.BDADDR_ANY,))
with self.assertRaises(OSError):
s.bind(socket.BDADDR_ANY + '\0')
with self.assertRaises((ValueError, OSError)):
s.bind(socket.BDADDR_ANY + ' '*100)
with self.assertRaises(OSError):
s.bind('\ud812')
with self.assertRaises(OSError):
s.bind('invalid')
with self.assertRaises(OSError):
s.bind(b'invalid')
else:
Expand All @@ -2756,11 +2763,18 @@ def testBadHciAddr(self):
s.bind((dev, 0))
with self.assertRaises(OSError):
s.bind(dev)
with self.assertRaises(OSError):
s.bind(socket.BDADDR_ANY)
with self.assertRaises(OSError):
s.bind(socket.BDADDR_ANY.encode())

@unittest.skipUnless(hasattr(socket, 'BTPROTO_SCO'), 'Bluetooth SCO sockets required for this test')
def testBindScoSocket(self):
with socket.socket(socket.AF_BLUETOOTH, socket.SOCK_SEQPACKET, socket.BTPROTO_SCO) as s:
s.bind(socket.BDADDR_ANY)
addr = s.getsockname()
self.assertEqual(addr, socket.BDADDR_ANY)

with socket.socket(socket.AF_BLUETOOTH, socket.SOCK_SEQPACKET, socket.BTPROTO_SCO) as s:
s.bind(socket.BDADDR_ANY.encode())
addr = s.getsockname()
Expand All @@ -2770,9 +2784,17 @@ def testBindScoSocket(self):
def testBadScoAddr(self):
with socket.socket(socket.AF_BLUETOOTH, socket.SOCK_SEQPACKET, socket.BTPROTO_SCO) as s:
with self.assertRaises(OSError):
s.bind(socket.BDADDR_ANY)
s.bind((socket.BDADDR_ANY,))
with self.assertRaises(OSError):
s.bind((socket.BDADDR_ANY.encode(),))
with self.assertRaises(ValueError):
s.bind(socket.BDADDR_ANY + '\0')
with self.assertRaises(ValueError):
s.bind(socket.BDADDR_ANY.encode() + b'\0')
with self.assertRaises(UnicodeEncodeError):
s.bind('\ud812')
with self.assertRaises(OSError):
s.bind('invalid')
with self.assertRaises(OSError):
s.bind(b'invalid')

Expand Down
43 changes: 21 additions & 22 deletions Modules/socketmodule.c