Skip to content

Commit da15ec5

Browse files
Merge branch 'main' into curses-extended-color
# Conflicts: # Modules/clinic/_cursesmodule.c.h
2 parents d84b27f + b35c379 commit da15ec5

42 files changed

Lines changed: 1710 additions & 211 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

Doc/c-api/extension-modules.rst

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,11 +100,35 @@ For example, a module called ``spam`` would be defined like this::
100100
The export hook is typically the only non-\ ``static``
101101
item defined in the module's C source.
102102

103-
The hook should be kept short -- ideally, one line as above.
104-
If you do need to use Python C API in this function, it is recommended to call
105-
``PyABIInfo_Check(&abi_info, "modulename")`` first to raise an exception,
106-
rather than crash, in common cases of ABI mismatch.
103+
.. _pymodexport-api-caveats:
107104

105+
The hook should be kept short.
106+
If it does more than ``return`` a static array, several caveats apply:
107+
108+
- If you need to use any Python C API, it is recommended to call
109+
:c:func:`PyABIInfo_Check` first to raise an exception,
110+
rather than crash, in common cases of ABI mismatch.
111+
- Code in the export hook must never rely on the :term:`GIL`:
112+
:term:`free-threaded builds <free-threaded build>` of Python can only check
113+
the :c:macro:`Py_mod_gil` slot (or the lack of it) after the hook returns,
114+
- Similarly, the hook may be called in any subinterpreter, since the
115+
:c:macro:`Py_mod_multiple_interpreters` slot (or lack of it)
116+
is only checked after the hook returns.
117+
118+
For example::
119+
120+
PyMODEXPORT_FUNC
121+
PyModExport_modulename(void)
122+
{
123+
if (PyABIInfo_Check(&abi_info, "modulename") < 0) {
124+
/* ABI mismatch. It's not safe to examine the raised exception. */
125+
return NULL;
126+
}
127+
128+
/* use Python API (as little as possible); don't rely on GIL */
129+
130+
return modulename_slots;
131+
}
108132

109133
.. note::
110134

Doc/c-api/import.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,11 @@ Importing Modules
304304
305305
Initialization function for a module built into the interpreter.
306306
307+
Note that the inittab uses "``PyInit``"
308+
:ref:`initialization functions <extension-pyinit>`;
309+
there is currently no way to include "``PyModExport_``"
310+
:ref:`export hooks <extension-export-hook>`.
311+
307312
308313
.. c:function:: int PyImport_ExtendInittab(struct _inittab *newtab)
309314

Doc/howto/abi3t-migration.rst

Lines changed: 101 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,8 @@ versions you support.
210210
This will ensure that nothing breaks as you are porting.
211211

212212

213+
.. _abi3t-howto-modexport:
214+
213215
Module export hook
214216
==================
215217

@@ -290,6 +292,104 @@ and substitute your own values.
290292
See the :c:type:`PySlot` and :c:ref:`export hook <extension-export-hook>`
291293
documentation for details on this API.
292294

295+
As in the example, your ``PyModExport_`` function should *only* return a
296+
pointer to static data.
297+
If you cannot avoid additional code, refer to the
298+
:ref:`caveats in PyModExport documentation <pymodexport-api-caveats>`.
299+
300+
301+
Existing slots
302+
--------------
303+
304+
If you have a ``Py_mod_slots`` slot, check the array it refers to.
305+
It should be a :c:type:`PyModuleDef_Slot` array like the following:
306+
307+
.. code-block::
308+
:class: bad
309+
310+
static PyObject *create_module(PyObject *spec, PyModuleDef *def) { ... }
311+
static int my_first_module_exec(PyObject *module) { ... }
312+
static int my_second_module_exec(PyObject *module) { ... }
313+
314+
static PyModuleDef_Slot my_slots[] = {
315+
{Py_mod_gil, Py_MOD_GIL_NOT_USED},
316+
{Py_mod_multiple_interpreters, Py_MOD_PER_INTERPRETER_GIL_SUPPORTED},
317+
{Py_mod_create, my_module_create},
318+
{Py_mod_exec, my_first_module_exec},
319+
{Py_mod_exec, my_second_module_exec},
320+
{0, NULL}
321+
};
322+
323+
``py_mod_create``
324+
.................
325+
326+
327+
If you have a :c:macro:`Py_mod_create` entry, make sure the function can be
328+
called with ``NULL`` as its second argument (instead of the
329+
:c:type:`PyModuleDef`, which you are removing).
330+
Often, this argument isn't used at all; you can check by renaming it:
331+
332+
.. code-block::
333+
:class: good
334+
335+
static PyObject *create_module(PyObject *spec, PyModuleDef *_unused) { ... }
336+
337+
If the argument is used, find a different way to pass in the data.
338+
Commonly, the information is static and you can refer to it directly.
339+
(If you're reusing a single function for several different modules, consider
340+
defining several functions instead.)
341+
342+
343+
Multiple ``py_mod_exec``
344+
........................
345+
346+
If you have *more than one* :c:macro:`Py_mod_exec` entry, consolidate them:
347+
create a new function that calls the others, and replace existing slots
348+
with it.
349+
350+
.. code-block::
351+
:class: good
352+
353+
static int my_module_exec(PyObject *module) {
354+
if (my_first_module_exec(module) < 0) return -1;
355+
if (my_second_module_exec(module) < 0) return -1;
356+
}
357+
358+
static PyModuleDef_Slot my_slots[] = {
359+
...
360+
/* (remove other Py_mod_exec slots) */
361+
...
362+
{Py_mod_exec, my_module_exec},
363+
{0, NULL}
364+
};
365+
366+
If the functions aren't used elsewhere, you can combine their bodies instead.
367+
368+
369+
Merging slot arrays
370+
...................
371+
372+
Optionally, when you break compatibility with Python 3.14, you may clean up
373+
the code by moving slots into the :c:type:`PySlot` array, and converting the
374+
definitions to :c:macro:`PySlot_DATA` and :c:macro:`PySlot_FUNC`:
375+
376+
.. code-block::
377+
:class: good
378+
379+
static PySlot my_slot_array[] = {
380+
...
381+
PySlot_DATA(Py_mod_gil, Py_MOD_GIL_NOT_USED),
382+
PySlot_DATA(Py_mod_multiple_interpreters,
383+
Py_MOD_PER_INTERPRETER_GIL_SUPPORTED)
384+
PySlot_FUNC(Py_mod_create, my_module_create),
385+
PySlot_FUNC(Py_mod_exec, my_module_exec),
386+
PySlot_END
387+
};
388+
389+
If you do this, delete the original :c:type:`PyModuleDef_Slot` array and
390+
its ``Py_mod_slots`` entry.
391+
392+
293393
Associated ``PyModuleDef``
294394
--------------------------
295395

@@ -483,7 +583,7 @@ For example, if a user makes a subclass like this:
483583
class Sub(YourCustomClass):
484584
__slots__ = ('a', 'b')
485585
486-
then ``Py_TYPE(obj)`` is ``YourCustomClass``, and the underlying memory may
586+
then ``Py_TYPE(obj)`` is ``Sub``, and the underlying memory may
487587
look like this:
488588

489589
.. code-block:: text

Doc/library/curses.panel.rst

Lines changed: 43 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,14 @@ displayed. Panels can be added, moved up or down in the stack, and removed.
1616
Functions
1717
---------
1818

19+
The module :mod:`!curses.panel` defines the following exception:
20+
21+
22+
.. exception:: error
23+
24+
Exception raised when a curses panel library function returns an error.
25+
26+
1927
The module :mod:`!curses.panel` defines the following functions:
2028

2129

@@ -48,73 +56,91 @@ The module :mod:`!curses.panel` defines the following functions:
4856
Panel objects
4957
-------------
5058

51-
Panel objects, as returned by :func:`new_panel` above, are windows with a
52-
stacking order. There's always a window associated with a panel which determines
53-
the content, while the panel methods are responsible for the window's depth in
54-
the panel stack.
59+
.. raw:: html
60+
61+
<!-- Keep the old URL fragments working (see gh-89554) -->
62+
<span id='curses.panel.Panel.above'></span>
63+
<span id='curses.panel.Panel.below'></span>
64+
<span id='curses.panel.Panel.bottom'></span>
65+
<span id='curses.panel.Panel.hidden'></span>
66+
<span id='curses.panel.Panel.hide'></span>
67+
<span id='curses.panel.Panel.move'></span>
68+
<span id='curses.panel.Panel.replace'></span>
69+
<span id='curses.panel.Panel.set_userptr'></span>
70+
<span id='curses.panel.Panel.show'></span>
71+
<span id='curses.panel.Panel.top'></span>
72+
<span id='curses.panel.Panel.userptr'></span>
73+
<span id='curses.panel.Panel.window'></span>
74+
75+
.. class:: panel
76+
77+
Panel objects, as returned by :func:`new_panel` above, are windows with a
78+
stacking order. There's always a window associated with a panel which
79+
determines the content, while the panel methods are responsible for the
80+
window's depth in the panel stack.
5581

56-
Panel objects have the following methods:
82+
Panel objects have the following methods:
5783

5884

59-
.. method:: Panel.above()
85+
.. method:: panel.above()
6086

6187
Returns the panel above the current panel.
6288

6389

64-
.. method:: Panel.below()
90+
.. method:: panel.below()
6591

6692
Returns the panel below the current panel.
6793

6894

69-
.. method:: Panel.bottom()
95+
.. method:: panel.bottom()
7096

7197
Push the panel to the bottom of the stack.
7298

7399

74-
.. method:: Panel.hidden()
100+
.. method:: panel.hidden()
75101

76102
Returns ``True`` if the panel is hidden (not visible), ``False`` otherwise.
77103

78104

79-
.. method:: Panel.hide()
105+
.. method:: panel.hide()
80106

81107
Hide the panel. This does not delete the object, it just makes the window on
82108
screen invisible.
83109

84110

85-
.. method:: Panel.move(y, x)
111+
.. method:: panel.move(y, x)
86112

87113
Move the panel to the screen coordinates ``(y, x)``.
88114

89115

90-
.. method:: Panel.replace(win)
116+
.. method:: panel.replace(win)
91117

92118
Change the window associated with the panel to the window *win*.
93119

94120

95-
.. method:: Panel.set_userptr(obj)
121+
.. method:: panel.set_userptr(obj)
96122

97123
Set the panel's user pointer to *obj*. This is used to associate an arbitrary
98124
piece of data with the panel, and can be any Python object.
99125

100126

101-
.. method:: Panel.show()
127+
.. method:: panel.show()
102128

103129
Display the panel (which might have been hidden), placing it on top of
104130
the panel stack.
105131

106132

107-
.. method:: Panel.top()
133+
.. method:: panel.top()
108134

109135
Push panel to the top of the stack.
110136

111137

112-
.. method:: Panel.userptr()
138+
.. method:: panel.userptr()
113139

114140
Returns the user pointer for the panel. This might be any Python object.
115141

116142

117-
.. method:: Panel.window()
143+
.. method:: panel.window()
118144

119145
Returns the window object associated with the panel.
120146

0 commit comments

Comments
 (0)