gh-132099: Accept an integer as the address for BTPROTO_HCI on Linux (GH-132525)

Previously only an integer packed in a tuple was accepted, while
getsockname() could return a raw integer.
Now the result of getsockname() is always acceptable as an address.
This commit is contained in:
Serhiy Storchaka 2025-04-16 13:02:51 +03:00 committed by GitHub
parent 82f74eb234
commit 8cb177d09b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 19 additions and 5 deletions

View file

@ -156,8 +156,9 @@ created. Socket addresses are represented as follows:
- :const:`BTPROTO_HCI` accepts a format that depends on your OS. - :const:`BTPROTO_HCI` accepts a format that depends on your OS.
- On Linux it accepts a tuple ``(device_id, [channel])`` where ``device_id`` - On Linux it accepts an integer ``device_id`` or a tuple
is an integer specifying the number of the Bluetooth device, ``(device_id, [channel])`` where ``device_id``
specifies the number of the Bluetooth device,
and ``channel`` is an optional integer specifying the HCI channel and ``channel`` is an optional integer specifying the HCI channel
(:const:`HCI_CHANNEL_RAW` by default). (:const:`HCI_CHANNEL_RAW` by default).
- On FreeBSD, NetBSD and DragonFly BSD it accepts ``bdaddr`` - On FreeBSD, NetBSD and DragonFly BSD it accepts ``bdaddr``
@ -171,6 +172,7 @@ created. Socket addresses are represented as follows:
.. versionchanged:: next .. versionchanged:: next
Added ``channel`` field. Added ``channel`` field.
``device_id`` not packed in a tuple is now accepted.
- :const:`BTPROTO_SCO` accepts ``bdaddr`` where ``bdaddr`` is - :const:`BTPROTO_SCO` accepts ``bdaddr`` where ``bdaddr`` is
the Bluetooth address as a string or a :class:`bytes` object. the Bluetooth address as a string or a :class:`bytes` object.

View file

@ -2745,6 +2745,12 @@ class BasicBluetoothTest(unittest.TestCase):
addr = s.getsockname() addr = s.getsockname()
self.assertEqual(addr, dev) self.assertEqual(addr, dev)
with (self.subTest('integer'),
socket.socket(socket.AF_BLUETOOTH, socket.SOCK_RAW, socket.BTPROTO_HCI) as s):
s.bind(dev)
addr = s.getsockname()
self.assertEqual(addr, dev)
with (self.subTest('channel=HCI_CHANNEL_RAW'), with (self.subTest('channel=HCI_CHANNEL_RAW'),
socket.socket(socket.AF_BLUETOOTH, socket.SOCK_RAW, socket.BTPROTO_HCI) as s): socket.socket(socket.AF_BLUETOOTH, socket.SOCK_RAW, socket.BTPROTO_HCI) as s):
channel = socket.HCI_CHANNEL_RAW channel = socket.HCI_CHANNEL_RAW
@ -2789,8 +2795,6 @@ class BasicBluetoothTest(unittest.TestCase):
s.bind(()) s.bind(())
with self.assertRaises(OSError): with self.assertRaises(OSError):
s.bind((dev, socket.HCI_CHANNEL_RAW, 0, 0)) s.bind((dev, socket.HCI_CHANNEL_RAW, 0, 0))
with self.assertRaises(OSError):
s.bind(dev)
with self.assertRaises(OSError): with self.assertRaises(OSError):
s.bind(socket.BDADDR_ANY) s.bind(socket.BDADDR_ANY)
with self.assertRaises(OSError): with self.assertRaises(OSError):

View file

@ -0,0 +1,3 @@
The Bluetooth socket with the :data:`~socket.BTPROTO_HCI` protocol on Linux
now accepts an address in the format of an integer ``device_id``, not only a
tuple ``(device_id,)``.

View file

@ -2147,7 +2147,12 @@ getsockaddrarg(PySocketSockObject *s, PyObject *args,
#if defined(HAVE_BLUETOOTH_BLUETOOTH_H) #if defined(HAVE_BLUETOOTH_BLUETOOTH_H)
unsigned short dev; unsigned short dev;
unsigned short channel = HCI_CHANNEL_RAW; unsigned short channel = HCI_CHANNEL_RAW;
if (!PyArg_ParseTuple(args, "H|H", &dev, &channel)) { if (PyLong_Check(args)) {
if (!PyArg_Parse(args, "H", &dev)) {
return 0;
}
}
else if (!PyArg_ParseTuple(args, "H|H", &dev, &channel)) {
PyErr_Format(PyExc_OSError, PyErr_Format(PyExc_OSError,
"%s(): wrong format", caller); "%s(): wrong format", caller);
return 0; return 0;