Skip to content

Commit bf028b2

Browse files
authored
Merge branch 'python:main' into feat/docs-enhance
2 parents 8831e92 + 35e6138 commit bf028b2

File tree

19 files changed

+1138
-47
lines changed

19 files changed

+1138
-47
lines changed

Doc/c-api/bytes.rst

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,3 +219,162 @@ called with a non-bytes parameter.
219219
reallocation fails, the original bytes object at *\*bytes* is deallocated,
220220
*\*bytes* is set to ``NULL``, :exc:`MemoryError` is set, and ``-1`` is
221221
returned.
222+
223+
PyBytesWriter
224+
-------------
225+
226+
The :c:type:`PyBytesWriter` API can be used to create a Python :class:`bytes`
227+
object.
228+
229+
.. versionadded:: next
230+
231+
.. c:type:: PyBytesWriter
232+
233+
A bytes writer instance.
234+
235+
The API is **not thread safe**: a writer should only be used by a single
236+
thread at the same time.
237+
238+
The instance must be destroyed by :c:func:`PyBytesWriter_Finish` on
239+
success, or :c:func:`PyBytesWriter_Discard` on error.
240+
241+
242+
Create, Finish, Discard
243+
^^^^^^^^^^^^^^^^^^^^^^^
244+
245+
.. c:function:: PyBytesWriter* PyBytesWriter_Create(Py_ssize_t size)
246+
247+
Create a :c:type:`PyBytesWriter` to write *size* bytes.
248+
249+
If *size* is greater than zero, allocate *size* bytes, and set the
250+
writer size to *size*. The caller is responsible to write *size*
251+
bytes using :c:func:`PyBytesWriter_GetData`.
252+
253+
On error, set an exception and return NULL.
254+
255+
*size* must be positive or zero.
256+
257+
.. c:function:: PyObject* PyBytesWriter_Finish(PyBytesWriter *writer)
258+
259+
Finish a :c:type:`PyBytesWriter` created by
260+
:c:func:`PyBytesWriter_Create`.
261+
262+
On success, return a Python :class:`bytes` object.
263+
On error, set an exception and return ``NULL``.
264+
265+
The writer instance is invalid after the call in any case.
266+
No API can be called on the writer after :c:func:`PyBytesWriter_Finish`.
267+
268+
.. c:function:: PyObject* PyBytesWriter_FinishWithSize(PyBytesWriter *writer, Py_ssize_t size)
269+
270+
Similar to :c:func:`PyBytesWriter_Finish`, but resize the writer
271+
to *size* bytes before creating the :class:`bytes` object.
272+
273+
.. c:function:: PyObject* PyBytesWriter_FinishWithPointer(PyBytesWriter *writer, void *buf)
274+
275+
Similar to :c:func:`PyBytesWriter_Finish`, but resize the writer
276+
using *buf* pointer before creating the :class:`bytes` object.
277+
278+
Set an exception and return ``NULL`` if *buf* pointer is outside the
279+
internal buffer bounds.
280+
281+
Function pseudo-code::
282+
283+
Py_ssize_t size = (char*)buf - (char*)PyBytesWriter_GetData(writer);
284+
return PyBytesWriter_FinishWithSize(writer, size);
285+
286+
.. c:function:: void PyBytesWriter_Discard(PyBytesWriter *writer)
287+
288+
Discard a :c:type:`PyBytesWriter` created by :c:func:`PyBytesWriter_Create`.
289+
290+
Do nothing if *writer* is ``NULL``.
291+
292+
The writer instance is invalid after the call.
293+
No API can be called on the writer after :c:func:`PyBytesWriter_Discard`.
294+
295+
High-level API
296+
^^^^^^^^^^^^^^
297+
298+
.. c:function:: int PyBytesWriter_WriteBytes(PyBytesWriter *writer, const void *bytes, Py_ssize_t size)
299+
300+
Grow the *writer* internal buffer by *size* bytes,
301+
write *size* bytes of *bytes* at the *writer* end,
302+
and add *size* to the *writer* size.
303+
304+
If *size* is equal to ``-1``, call ``strlen(bytes)`` to get the
305+
string length.
306+
307+
On success, return ``0``.
308+
On error, set an exception and return ``-1``.
309+
310+
.. c:function:: int PyBytesWriter_Format(PyBytesWriter *writer, const char *format, ...)
311+
312+
Similar to :c:func:`PyBytes_FromFormat`, but write the output directly at
313+
the writer end. Grow the writer internal buffer on demand. Then add the
314+
written size to the writer size.
315+
316+
On success, return ``0``.
317+
On error, set an exception and return ``-1``.
318+
319+
320+
Getters
321+
^^^^^^^
322+
323+
.. c:function:: Py_ssize_t PyBytesWriter_GetSize(PyBytesWriter *writer)
324+
325+
Get the writer size.
326+
327+
.. c:function:: void* PyBytesWriter_GetData(PyBytesWriter *writer)
328+
329+
Get the writer data: start of the internal buffer.
330+
331+
The pointer is valid until :c:func:`PyBytesWriter_Finish` or
332+
:c:func:`PyBytesWriter_Discard` is called on *writer*.
333+
334+
335+
Low-level API
336+
^^^^^^^^^^^^^
337+
338+
.. c:function:: int PyBytesWriter_Resize(PyBytesWriter *writer, Py_ssize_t size)
339+
340+
Resize the writer to *size* bytes. It can be used to enlarge or to
341+
shrink the writer.
342+
343+
Newly allocated bytes are left uninitialized.
344+
345+
On success, return ``0``.
346+
On error, set an exception and return ``-1``.
347+
348+
*size* must be positive or zero.
349+
350+
.. c:function:: int PyBytesWriter_Grow(PyBytesWriter *writer, Py_ssize_t grow)
351+
352+
Resize the writer by adding *grow* bytes to the current writer size.
353+
354+
Newly allocated bytes are left uninitialized.
355+
356+
On success, return ``0``.
357+
On error, set an exception and return ``-1``.
358+
359+
*size* can be negative to shrink the writer.
360+
361+
.. c:function:: void* PyBytesWriter_GrowAndUpdatePointer(PyBytesWriter *writer, Py_ssize_t size, void *buf)
362+
363+
Similar to :c:func:`PyBytesWriter_Grow`, but update also the *buf*
364+
pointer.
365+
366+
The *buf* pointer is moved if the internal buffer is moved in memory.
367+
The *buf* relative position within the internal buffer is left
368+
unchanged.
369+
370+
On error, set an exception and return ``NULL``.
371+
372+
*buf* must not be ``NULL``.
373+
374+
Function pseudo-code::
375+
376+
Py_ssize_t pos = (char*)buf - (char*)PyBytesWriter_GetData(writer);
377+
if (PyBytesWriter_Grow(writer, size) < 0) {
378+
return NULL;
379+
}
380+
return (char*)PyBytesWriter_GetData(writer) + pos;

Doc/faq/python-video-icon.png

-2.82 KB
Binary file not shown.

Doc/howto/remote_debugging.rst

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,78 @@
33
Remote debugging attachment protocol
44
====================================
55

6+
This protocol enables external tools to attach to a running CPython process and
7+
execute Python code remotely.
8+
9+
Most platforms require elevated privileges to attach to another Python process.
10+
11+
.. _permission-requirements:
12+
13+
Permission requirements
14+
=======================
15+
16+
Attaching to a running Python process for remote debugging requires elevated
17+
privileges on most platforms. The specific requirements and troubleshooting
18+
steps depend on your operating system:
19+
20+
.. rubric:: Linux
21+
22+
The tracer process must have the ``CAP_SYS_PTRACE`` capability or equivalent
23+
privileges. You can only trace processes you own and can signal. Tracing may
24+
fail if the process is already being traced, or if it is running with
25+
set-user-ID or set-group-ID. Security modules like Yama may further restrict
26+
tracing.
27+
28+
To temporarily relax ptrace restrictions (until reboot), run:
29+
30+
``echo 0 | sudo tee /proc/sys/kernel/yama/ptrace_scope``
31+
32+
.. note::
33+
34+
Disabling ``ptrace_scope`` reduces system hardening and should only be done
35+
in trusted environments.
36+
37+
If running inside a container, use ``--cap-add=SYS_PTRACE`` or
38+
``--privileged``, and run as root if needed.
39+
40+
Try re-running the command with elevated privileges:
41+
42+
``sudo -E !!``
43+
44+
45+
.. rubric:: macOS
46+
47+
To attach to another process, you typically need to run your debugging tool
48+
with elevated privileges. This can be done by using ``sudo`` or running as
49+
root.
50+
51+
Even when attaching to processes you own, macOS may block debugging unless
52+
the debugger is run with root privileges due to system security restrictions.
53+
54+
55+
.. rubric:: Windows
56+
57+
To attach to another process, you usually need to run your debugging tool
58+
with administrative privileges. Start the command prompt or terminal as
59+
Administrator.
60+
61+
Some processes may still be inaccessible even with Administrator rights,
62+
unless you have the ``SeDebugPrivilege`` privilege enabled.
63+
64+
To resolve file or folder access issues, adjust the security permissions:
65+
66+
1. Right-click the file or folder and select **Properties**.
67+
2. Go to the **Security** tab to view users and groups with access.
68+
3. Click **Edit** to modify permissions.
69+
4. Select your user account.
70+
5. In **Permissions**, check **Read** or **Full control** as needed.
71+
6. Click **Apply**, then **OK** to confirm.
72+
73+
74+
.. note::
75+
76+
Ensure you've satisfied all :ref:`permission-requirements` before proceeding.
77+
678
This section describes the low-level protocol that enables external tools to
779
inject and execute a Python script within a running CPython process.
880

Doc/whatsnew/3.15.rst

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -707,6 +707,23 @@ New features
707707
and :c:data:`Py_mod_abi`.
708708
(Contributed by Petr Viktorin in :gh:`137210`.)
709709

710+
* Implement :pep:`782`, the :c:type:`PyBytesWriter` API. Add functions:
711+
712+
* :c:func:`PyBytesWriter_Create`
713+
* :c:func:`PyBytesWriter_Discard`
714+
* :c:func:`PyBytesWriter_FinishWithPointer`
715+
* :c:func:`PyBytesWriter_FinishWithSize`
716+
* :c:func:`PyBytesWriter_Finish`
717+
* :c:func:`PyBytesWriter_Format`
718+
* :c:func:`PyBytesWriter_GetData`
719+
* :c:func:`PyBytesWriter_GetSize`
720+
* :c:func:`PyBytesWriter_GrowAndUpdatePointer`
721+
* :c:func:`PyBytesWriter_Grow`
722+
* :c:func:`PyBytesWriter_Resize`
723+
* :c:func:`PyBytesWriter_WriteBytes`
724+
725+
(Contributed by Victor Stinner in :gh:`129813`.)
726+
710727

711728
Porting to Python 3.15
712729
----------------------

Include/cpython/bytesobject.h

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,46 @@ _PyBytes_Join(PyObject *sep, PyObject *iterable)
4040
{
4141
return PyBytes_Join(sep, iterable);
4242
}
43+
44+
45+
// --- PyBytesWriter API -----------------------------------------------------
46+
47+
typedef struct PyBytesWriter PyBytesWriter;
48+
49+
PyAPI_FUNC(PyBytesWriter *) PyBytesWriter_Create(
50+
Py_ssize_t size);
51+
PyAPI_FUNC(void) PyBytesWriter_Discard(
52+
PyBytesWriter *writer);
53+
PyAPI_FUNC(PyObject*) PyBytesWriter_Finish(
54+
PyBytesWriter *writer);
55+
PyAPI_FUNC(PyObject*) PyBytesWriter_FinishWithSize(
56+
PyBytesWriter *writer,
57+
Py_ssize_t size);
58+
PyAPI_FUNC(PyObject*) PyBytesWriter_FinishWithPointer(
59+
PyBytesWriter *writer,
60+
void *buf);
61+
62+
PyAPI_FUNC(void*) PyBytesWriter_GetData(
63+
PyBytesWriter *writer);
64+
PyAPI_FUNC(Py_ssize_t) PyBytesWriter_GetSize(
65+
PyBytesWriter *writer);
66+
67+
PyAPI_FUNC(int) PyBytesWriter_WriteBytes(
68+
PyBytesWriter *writer,
69+
const void *bytes,
70+
Py_ssize_t size);
71+
PyAPI_FUNC(int) PyBytesWriter_Format(
72+
PyBytesWriter *writer,
73+
const char *format,
74+
...);
75+
76+
PyAPI_FUNC(int) PyBytesWriter_Resize(
77+
PyBytesWriter *writer,
78+
Py_ssize_t size);
79+
PyAPI_FUNC(int) PyBytesWriter_Grow(
80+
PyBytesWriter *writer,
81+
Py_ssize_t size);
82+
PyAPI_FUNC(void*) PyBytesWriter_GrowAndUpdatePointer(
83+
PyBytesWriter *writer,
84+
Py_ssize_t size,
85+
void *buf);

Include/internal/pycore_bytesobject.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,10 @@ PyAPI_FUNC(void*) _PyBytesWriter_WriteBytes(_PyBytesWriter *writer,
143143
const void *bytes,
144144
Py_ssize_t size);
145145

146+
// Export for '_testcapi' shared extension.
147+
PyAPI_FUNC(PyBytesWriter*) _PyBytesWriter_CreateByteArray(
148+
Py_ssize_t size);
149+
146150
#ifdef __cplusplus
147151
}
148152
#endif

Include/internal/pycore_code.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,8 @@ extern void _PyLineTable_InitAddressRange(
274274
/** API for traversing the line number table. */
275275
extern int _PyLineTable_NextAddressRange(PyCodeAddressRange *range);
276276
extern int _PyLineTable_PreviousAddressRange(PyCodeAddressRange *range);
277+
// This is used in dump_frame() in traceback.c without an attached tstate.
278+
extern int _PyCode_Addr2LineNoTstate(PyCodeObject *co, int addr);
277279

278280
/** API for executors */
279281
extern void _PyCode_Clear_Executors(PyCodeObject *code);

Include/internal/pycore_freelist_state.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ extern "C" {
2727
# define Py_futureiters_MAXFREELIST 255
2828
# define Py_object_stack_chunks_MAXFREELIST 4
2929
# define Py_unicode_writers_MAXFREELIST 1
30+
# define Py_bytes_writers_MAXFREELIST 1
3031
# define Py_pycfunctionobject_MAXFREELIST 16
3132
# define Py_pycmethodobject_MAXFREELIST 16
3233
# define Py_pymethodobjects_MAXFREELIST 20
@@ -61,6 +62,7 @@ struct _Py_freelists {
6162
struct _Py_freelist futureiters;
6263
struct _Py_freelist object_stack_chunks;
6364
struct _Py_freelist unicode_writers;
65+
struct _Py_freelist bytes_writers;
6466
struct _Py_freelist pycfunctionobject;
6567
struct _Py_freelist pycmethodobject;
6668
struct _Py_freelist pymethodobjects;

Lib/asyncio/tools.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,20 @@ def _print_cycle_exception(exception: CycleFoundException):
222222
print(f"cycle: {inames}", file=sys.stderr)
223223

224224

225+
def exit_with_permission_help_text():
226+
"""
227+
Prints a message pointing to platform-specific permission help text and exits the program.
228+
This function is called when a PermissionError is encountered while trying
229+
to attach to a process.
230+
"""
231+
print(
232+
"Error: The specified process cannot be attached to due to insufficient permissions.\n"
233+
"See the Python documentation for details on required privileges and troubleshooting:\n"
234+
"https://docs.python.org/3.14/howto/remote_debugging.html#permission-requirements\n"
235+
)
236+
sys.exit(1)
237+
238+
225239
def _get_awaited_by_tasks(pid: int) -> list:
226240
try:
227241
return get_all_awaited_by(pid)
@@ -230,6 +244,8 @@ def _get_awaited_by_tasks(pid: int) -> list:
230244
e = e.__context__
231245
print(f"Error retrieving tasks: {e}")
232246
sys.exit(1)
247+
except PermissionError as e:
248+
exit_with_permission_help_text()
233249

234250

235251
def display_awaited_by_tasks_table(pid: int) -> None:

0 commit comments

Comments
 (0)