Skip to content

Commit 280e90e

Browse files
authored
Merge branch 'main' into tstrings
2 parents cf56234 + 5f50541 commit 280e90e

File tree

3 files changed

+64
-38
lines changed

3 files changed

+64
-38
lines changed

Doc/library/fcntl.rst

Lines changed: 46 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -82,65 +82,82 @@ descriptor.
8282
The module defines the following functions:
8383

8484

85-
.. function:: fcntl(fd, cmd, arg=0)
85+
.. function:: fcntl(fd, cmd, arg=0, /)
8686

8787
Perform the operation *cmd* on file descriptor *fd* (file objects providing
8888
a :meth:`~io.IOBase.fileno` method are accepted as well). The values used
8989
for *cmd* are operating system dependent, and are available as constants
9090
in the :mod:`fcntl` module, using the same names as used in the relevant C
91-
header files. The argument *arg* can either be an integer value, or a
92-
:class:`bytes` object. With an integer value, the return value of this
93-
function is the integer return value of the C :c:func:`fcntl` call. When
94-
the argument is bytes it represents a binary structure, e.g. created by
95-
:func:`struct.pack`. The binary data is copied to a buffer whose address is
91+
header files. The argument *arg* can either be an integer value, a
92+
:class:`bytes` object, or a string.
93+
The type and size of *arg* must match the type and size of
94+
the argument of the operation as specified in the relevant C documentation.
95+
96+
When *arg* is an integer, the function returns the integer
97+
return value of the C :c:func:`fcntl` call.
98+
99+
When the argument is bytes, it represents a binary structure,
100+
for example, created by :func:`struct.pack`.
101+
A string value is encoded to binary using the UTF-8 encoding.
102+
The binary data is copied to a buffer whose address is
96103
passed to the C :c:func:`fcntl` call. The return value after a successful
97104
call is the contents of the buffer, converted to a :class:`bytes` object.
98105
The length of the returned object will be the same as the length of the
99-
*arg* argument. This is limited to 1024 bytes. If the information returned
100-
in the buffer by the operating system is larger than 1024 bytes, this is
101-
most likely to result in a segmentation violation or a more subtle data
102-
corruption.
106+
*arg* argument. This is limited to 1024 bytes.
103107

104108
If the :c:func:`fcntl` call fails, an :exc:`OSError` is raised.
105109

110+
.. note::
111+
If the type or the size of *arg* does not match the type or size
112+
of the argument of the operation (for example, if an integer is
113+
passed when a pointer is expected, or the information returned in
114+
the buffer by the operating system is larger than 1024 bytes),
115+
this is most likely to result in a segmentation violation or
116+
a more subtle data corruption.
117+
106118
.. audit-event:: fcntl.fcntl fd,cmd,arg fcntl.fcntl
107119

108120

109-
.. function:: ioctl(fd, request, arg=0, mutate_flag=True)
121+
.. function:: ioctl(fd, request, arg=0, mutate_flag=True, /)
110122

111123
This function is identical to the :func:`~fcntl.fcntl` function, except
112124
that the argument handling is even more complicated.
113125

114-
The *request* parameter is limited to values that can fit in 32-bits.
126+
The *request* parameter is limited to values that can fit in 32-bits
127+
or 64-bits, depending on the platform.
115128
Additional constants of interest for use as the *request* argument can be
116129
found in the :mod:`termios` module, under the same names as used in
117130
the relevant C header files.
118131

119-
The parameter *arg* can be one of an integer, an object supporting the
120-
read-only buffer interface (like :class:`bytes`) or an object supporting
121-
the read-write buffer interface (like :class:`bytearray`).
132+
The parameter *arg* can be an integer, a :term:`bytes-like object`,
133+
or a string.
134+
The type and size of *arg* must match the type and size of
135+
the argument of the operation as specified in the relevant C documentation.
122136

123-
In all but the last case, behaviour is as for the :func:`~fcntl.fcntl`
137+
If *arg* does not support the read-write buffer interface or
138+
the *mutate_flag* is false, behavior is as for the :func:`~fcntl.fcntl`
124139
function.
125140

126-
If a mutable buffer is passed, then the behaviour is determined by the value of
127-
the *mutate_flag* parameter.
128-
129-
If it is false, the buffer's mutability is ignored and behaviour is as for a
130-
read-only buffer, except that the 1024 byte limit mentioned above is avoided --
131-
so long as the buffer you pass is at least as long as what the operating system
132-
wants to put there, things should work.
133-
134-
If *mutate_flag* is true (the default), then the buffer is (in effect) passed
135-
to the underlying :func:`ioctl` system call, the latter's return code is
141+
If *arg* supports the read-write buffer interface (like :class:`bytearray`)
142+
and *mutate_flag* is true (the default), then the buffer is (in effect) passed
143+
to the underlying :c:func:`!ioctl` system call, the latter's return code is
136144
passed back to the calling Python, and the buffer's new contents reflect the
137-
action of the :func:`ioctl`. This is a slight simplification, because if the
145+
action of the :c:func:`ioctl`. This is a slight simplification, because if the
138146
supplied buffer is less than 1024 bytes long it is first copied into a static
139147
buffer 1024 bytes long which is then passed to :func:`ioctl` and copied back
140148
into the supplied buffer.
141149

142150
If the :c:func:`ioctl` call fails, an :exc:`OSError` exception is raised.
143151

152+
.. note::
153+
If the type or size of *arg* does not match the type or size
154+
of the operation's argument (for example, if an integer is
155+
passed when a pointer is expected, or the information returned in
156+
the buffer by the operating system is larger than 1024 bytes,
157+
or the size of the mutable bytes-like object is too small),
158+
this is most likely to result in a segmentation violation or
159+
a more subtle data corruption.
160+
144161
An example::
145162

146163
>>> import array, fcntl, struct, termios, os
@@ -157,7 +174,7 @@ The module defines the following functions:
157174
.. audit-event:: fcntl.ioctl fd,request,arg fcntl.ioctl
158175

159176

160-
.. function:: flock(fd, operation)
177+
.. function:: flock(fd, operation, /)
161178

162179
Perform the lock operation *operation* on file descriptor *fd* (file objects providing
163180
a :meth:`~io.IOBase.fileno` method are accepted as well). See the Unix manual
@@ -169,7 +186,7 @@ The module defines the following functions:
169186
.. audit-event:: fcntl.flock fd,operation fcntl.flock
170187

171188

172-
.. function:: lockf(fd, cmd, len=0, start=0, whence=0)
189+
.. function:: lockf(fd, cmd, len=0, start=0, whence=0, /)
173190

174191
This is essentially a wrapper around the :func:`~fcntl.fcntl` locking calls.
175192
*fd* is the file descriptor (file objects providing a :meth:`~io.IOBase.fileno`

Lib/test/test_socket.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2647,14 +2647,13 @@ def testBluetoothConstants(self):
26472647
socket.BT_POWER
26482648
socket.BT_CHANNEL_POLICY
26492649
socket.BT_CHANNEL_POLICY_BREDR_ONLY
2650-
socket.BT_PHY
2651-
socket.BT_PHY_BR_1M_1SLOT
2652-
socket.BT_MODE
2653-
socket.BT_MODE_BASIC
2654-
socket.BT_VOICE
2655-
socket.BT_VOICE_TRANSPARENT
2656-
socket.BT_VOICE_CVSD_16BIT
2657-
socket.BT_CODEC
2650+
if hasattr(socket, 'BT_PHY'):
2651+
socket.BT_PHY_BR_1M_1SLOT
2652+
if hasattr(socket, 'BT_MODE'):
2653+
socket.BT_MODE_BASIC
2654+
if hasattr(socket, 'BT_VOICE'):
2655+
socket.BT_VOICE_TRANSPARENT
2656+
socket.BT_VOICE_CVSD_16BIT
26582657
socket.L2CAP_LM
26592658
socket.L2CAP_LM_MASTER
26602659
socket.L2CAP_LM_AUTH

Modules/socketmodule.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ shutdown(how) -- shut down traffic in one or both directions\n\
208208
# include <sys/ioctl.h>
209209
#endif
210210

211-
#ifdef HAVE_BLUETOOTH_H
211+
#if defined(HAVE_BLUETOOTH_H) && !defined(__FreeBSD__)
212212
# include <netbt/l2cap.h>
213213
# include <netbt/rfcomm.h>
214214
# include <netbt/hci.h>
@@ -8014,6 +8014,7 @@ socket_exec(PyObject *m)
80148014
#endif
80158015
ADD_INT_MACRO(m, BT_SNDMTU);
80168016
ADD_INT_MACRO(m, BT_RCVMTU);
8017+
#ifdef BT_PHY
80178018
ADD_INT_MACRO(m, BT_PHY);
80188019
ADD_INT_MACRO(m, BT_PHY_BR_1M_1SLOT);
80198020
ADD_INT_MACRO(m, BT_PHY_BR_1M_3SLOT);
@@ -8030,15 +8031,24 @@ socket_exec(PyObject *m)
80308031
ADD_INT_MACRO(m, BT_PHY_LE_2M_RX);
80318032
ADD_INT_MACRO(m, BT_PHY_LE_CODED_TX);
80328033
ADD_INT_MACRO(m, BT_PHY_LE_CODED_RX);
8034+
#endif
8035+
#ifdef BT_MODE
80338036
ADD_INT_MACRO(m, BT_MODE);
80348037
ADD_INT_MACRO(m, BT_MODE_BASIC);
80358038
ADD_INT_MACRO(m, BT_MODE_ERTM);
80368039
ADD_INT_MACRO(m, BT_MODE_STREAMING);
80378040
ADD_INT_MACRO(m, BT_MODE_LE_FLOWCTL);
80388041
ADD_INT_MACRO(m, BT_MODE_EXT_FLOWCTL);
8042+
#endif
8043+
#ifdef BT_PKT_STATUS
80398044
ADD_INT_MACRO(m, BT_PKT_STATUS);
8045+
#endif
8046+
#ifdef BT_ISO_QOS
80408047
ADD_INT_MACRO(m, BT_ISO_QOS);
8048+
#endif
8049+
#ifdef BT_CODEC
80418050
ADD_INT_MACRO(m, BT_CODEC);
8051+
#endif
80428052
#endif /* HAVE_BLUETOOTH_BLUETOOTH_H */
80438053
#endif /* USE_BLUETOOTH */
80448054

0 commit comments

Comments
 (0)