Skip to content

Commit 9d0369e

Browse files
committed
Merge branch 'main' into pythongh-104090-fix-leaked-semaphors-on-test_concurrent_futures-v2
2 parents a02fdbe + 4cb0b9c commit 9d0369e

Some content is hidden

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

42 files changed

+1252
-628
lines changed

Diff for: .github/CODEOWNERS

+1
Original file line numberDiff line numberDiff line change
@@ -177,3 +177,4 @@ Doc/c-api/stable.rst @encukou
177177
# Argument Clinic
178178
/Tools/clinic/** @erlend-aasland @AlexWaygood
179179
/Lib/test/test_clinic.py @erlend-aasland @AlexWaygood
180+
Doc/howto/clinic.rst @erlend-aasland

Diff for: Doc/c-api/module.rst

+31-34
Original file line numberDiff line numberDiff line change
@@ -486,12 +486,29 @@ state:
486486
.. versionadded:: 3.10
487487
488488
489+
.. c:function:: int PyModule_Add(PyObject *module, const char *name, PyObject *value)
490+
491+
Similar to :c:func:`PyModule_AddObjectRef`, but "steals" a reference
492+
to *value*.
493+
It can be called with a result of function that returns a new reference
494+
without bothering to check its result or even saving it to a variable.
495+
496+
Example usage::
497+
498+
if (PyModule_Add(module, "spam", PyBytes_FromString(value)) < 0) {
499+
goto error;
500+
}
501+
502+
.. versionadded:: 3.13
503+
504+
489505
.. c:function:: int PyModule_AddObject(PyObject *module, const char *name, PyObject *value)
490506
491507
Similar to :c:func:`PyModule_AddObjectRef`, but steals a reference to
492508
*value* on success (if it returns ``0``).
493509
494-
The new :c:func:`PyModule_AddObjectRef` function is recommended, since it is
510+
The new :c:func:`PyModule_Add` or :c:func:`PyModule_AddObjectRef`
511+
functions are recommended, since it is
495512
easy to introduce reference leaks by misusing the
496513
:c:func:`PyModule_AddObject` function.
497514
@@ -501,44 +518,24 @@ state:
501518
only decrements the reference count of *value* **on success**.
502519
503520
This means that its return value must be checked, and calling code must
504-
:c:func:`Py_DECREF` *value* manually on error.
521+
:c:func:`Py_XDECREF` *value* manually on error.
505522
506523
Example usage::
507524
508-
static int
509-
add_spam(PyObject *module, int value)
510-
{
511-
PyObject *obj = PyLong_FromLong(value);
512-
if (obj == NULL) {
513-
return -1;
514-
}
515-
if (PyModule_AddObject(module, "spam", obj) < 0) {
516-
Py_DECREF(obj);
517-
return -1;
518-
}
519-
// PyModule_AddObject() stole a reference to obj:
520-
// Py_DECREF(obj) is not needed here
521-
return 0;
522-
}
523-
524-
The example can also be written without checking explicitly if *obj* is
525-
``NULL``::
525+
PyObject *obj = PyBytes_FromString(value);
526+
if (PyModule_AddObject(module, "spam", obj) < 0) {
527+
// If 'obj' is not NULL and PyModule_AddObject() failed,
528+
// 'obj' strong reference must be deleted with Py_XDECREF().
529+
// If 'obj' is NULL, Py_XDECREF() does nothing.
530+
Py_XDECREF(obj);
531+
goto error;
532+
}
533+
// PyModule_AddObject() stole a reference to obj:
534+
// Py_XDECREF(obj) is not needed here.
526535
527-
static int
528-
add_spam(PyObject *module, int value)
529-
{
530-
PyObject *obj = PyLong_FromLong(value);
531-
if (PyModule_AddObject(module, "spam", obj) < 0) {
532-
Py_XDECREF(obj);
533-
return -1;
534-
}
535-
// PyModule_AddObject() stole a reference to obj:
536-
// Py_DECREF(obj) is not needed here
537-
return 0;
538-
}
536+
.. deprecated:: 3.13
539537
540-
Note that ``Py_XDECREF()`` should be used instead of ``Py_DECREF()`` in
541-
this case, since *obj* can be ``NULL``.
538+
:c:func:`PyModule_AddObject` is :term:`soft deprecated`.
542539
543540
544541
.. c:function:: int PyModule_AddIntConstant(PyObject *module, const char *name, long value)

Diff for: Doc/data/stable_abi.dat

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: Doc/howto/clinic.rst

+46-49
Original file line numberDiff line numberDiff line change
@@ -560,15 +560,12 @@ Let's dive in!
560560
Congratulations, you've ported your first function to work with Argument Clinic!
561561

562562

563-
Advanced topics
564-
===============
563+
How-to guides
564+
=============
565565

566-
Now that you've had some experience working with Argument Clinic, it's time
567-
for some advanced topics.
568566

569-
570-
Symbolic default values
571-
-----------------------
567+
How to use symbolic default values
568+
----------------------------------
572569

573570
The default value you provide for a parameter can't be any arbitrary
574571
expression. Currently the following are explicitly supported:
@@ -583,8 +580,8 @@ expression. Currently the following are explicitly supported:
583580
to allow full expressions like ``CONSTANT - 1``.)
584581

585582

586-
Renaming the C functions and variables generated by Argument Clinic
587-
-------------------------------------------------------------------
583+
How to to rename C functions and variables generated by Argument Clinic
584+
-----------------------------------------------------------------------
588585

589586
Argument Clinic automatically names the functions it generates for you.
590587
Occasionally this may cause a problem, if the generated name collides with
@@ -626,8 +623,8 @@ array) would be ``file``, but the C variable would be named ``file_obj``.
626623
You can use this to rename the ``self`` parameter too!
627624

628625

629-
Converting functions using PyArg_UnpackTuple
630-
--------------------------------------------
626+
How to convert functions using ``PyArg_UnpackTuple``
627+
----------------------------------------------------
631628

632629
To convert a function parsing its arguments with :c:func:`PyArg_UnpackTuple`,
633630
simply write out all the arguments, specifying each as an ``object``. You
@@ -639,8 +636,8 @@ Currently the generated code will use :c:func:`PyArg_ParseTuple`, but this
639636
will change soon.
640637

641638

642-
Optional groups
643-
---------------
639+
How to use optional groups
640+
--------------------------
644641

645642
Some legacy functions have a tricky approach to parsing their arguments:
646643
they count the number of positional arguments, then use a ``switch`` statement
@@ -732,8 +729,8 @@ Notes:
732729
use optional groups for new code.
733730

734731

735-
Using real Argument Clinic converters, instead of "legacy converters"
736-
---------------------------------------------------------------------
732+
How to use real Argument Clinic converters, instead of "legacy converters"
733+
--------------------------------------------------------------------------
737734

738735
To save time, and to minimize how much you need to learn
739736
to achieve your first port to Argument Clinic, the walkthrough above tells
@@ -903,17 +900,17 @@ it accepts, along with the default value for each parameter.
903900
Just run ``Tools/clinic/clinic.py --converters`` to see the full list.
904901

905902

906-
Py_buffer
907-
---------
903+
How to use the ``Py_buffer`` converter
904+
--------------------------------------
908905

909906
When using the ``Py_buffer`` converter
910907
(or the ``'s*'``, ``'w*'``, ``'*y'``, or ``'z*'`` legacy converters),
911908
you *must* not call :c:func:`PyBuffer_Release` on the provided buffer.
912909
Argument Clinic generates code that does it for you (in the parsing function).
913910

914911

915-
Advanced converters
916-
-------------------
912+
How to use advanced converters
913+
------------------------------
917914

918915
Remember those format units you skipped for your first
919916
time because they were advanced? Here's how to handle those too.
@@ -944,8 +941,8 @@ hard-coded encoding strings for parameters whose format units start with ``e``.
944941

945942
.. _default_values:
946943

947-
Parameter default values
948-
------------------------
944+
How to assign default values to parameter
945+
-----------------------------------------
949946

950947
Default values for parameters can be any of a number of values.
951948
At their simplest, they can be string, int, or float literals:
@@ -968,8 +965,8 @@ There's also special support for a default value of ``NULL``, and
968965
for simple expressions, documented in the following sections.
969966

970967

971-
The ``NULL`` default value
972-
--------------------------
968+
How to use the ``NULL`` default value
969+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
973970

974971
For string and object parameters, you can set them to ``None`` to indicate
975972
that there's no default. However, that means the C variable will be
@@ -979,8 +976,8 @@ behaves like a default value of ``None``, but the C variable is initialized
979976
with ``NULL``.
980977

981978

982-
Expressions specified as default values
983-
---------------------------------------
979+
How to use expressions as default values
980+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
984981

985982
The default value for a parameter can be more than just a literal value.
986983
It can be an entire expression, using math operators and looking up attributes
@@ -1036,8 +1033,8 @@ you're not permitted to use:
10361033
* Tuple/list/set/dict literals.
10371034

10381035

1039-
Using a return converter
1040-
------------------------
1036+
How to use return converters
1037+
----------------------------
10411038

10421039
By default, the impl function Argument Clinic generates for you returns
10431040
:c:type:`PyObject * <PyObject>`.
@@ -1106,8 +1103,8 @@ their parameters (if any),
11061103
just run ``Tools/clinic/clinic.py --converters`` for the full list.
11071104

11081105

1109-
Cloning existing functions
1110-
--------------------------
1106+
How to clone existing functions
1107+
-------------------------------
11111108

11121109
If you have a number of functions that look similar, you may be able to
11131110
use Clinic's "clone" feature. When you clone an existing function,
@@ -1150,8 +1147,8 @@ Also, the function you are cloning from must have been previously defined
11501147
in the current file.
11511148

11521149

1153-
Calling Python code
1154-
-------------------
1150+
How to call Python code
1151+
-----------------------
11551152

11561153
The rest of the advanced topics require you to write Python code
11571154
which lives inside your C file and modifies Argument Clinic's
@@ -1178,8 +1175,8 @@ variable to the C code::
11781175
/*[python checksum:...]*/
11791176

11801177

1181-
Using a "self converter"
1182-
------------------------
1178+
How to use the "self converter"
1179+
-------------------------------
11831180

11841181
Argument Clinic automatically adds a "self" parameter for you
11851182
using a default converter. It automatically sets the ``type``
@@ -1230,8 +1227,8 @@ type for ``self``, it's best to create your own converter, subclassing
12301227
[clinic start generated code]*/
12311228

12321229

1233-
Using a "defining class" converter
1234-
----------------------------------
1230+
How to use the "defining class" converter
1231+
-----------------------------------------
12351232

12361233
Argument Clinic facilitates gaining access to the defining class of a method.
12371234
This is useful for :ref:`heap type <heap-types>` methods that need to fetch
@@ -1293,8 +1290,8 @@ state. Example from the ``setattro`` slot method in
12931290
See also :pep:`573`.
12941291

12951292

1296-
Writing a custom converter
1297-
--------------------------
1293+
How to write a custom converter
1294+
-------------------------------
12981295

12991296
As we hinted at in the previous section... you can write your own converters!
13001297
A converter is simply a Python class that inherits from ``CConverter``.
@@ -1385,8 +1382,8 @@ You can see more examples of custom converters in the CPython
13851382
source tree; grep the C files for the string ``CConverter``.
13861383

13871384

1388-
Writing a custom return converter
1389-
---------------------------------
1385+
How to write a custom return converter
1386+
--------------------------------------
13901387

13911388
Writing a custom return converter is much like writing
13921389
a custom converter. Except it's somewhat simpler, because return
@@ -1400,8 +1397,8 @@ specifically the implementation of ``CReturnConverter`` and
14001397
all its subclasses.
14011398

14021399

1403-
METH_O and METH_NOARGS
1404-
----------------------
1400+
How to convert ``METH_O`` and ``METH_NOARGS`` functions
1401+
-------------------------------------------------------
14051402

14061403
To convert a function using ``METH_O``, make sure the function's
14071404
single argument is using the ``object`` converter, and mark the
@@ -1422,8 +1419,8 @@ You can still use a self converter, a return converter, and specify
14221419
a ``type`` argument to the object converter for ``METH_O``.
14231420

14241421

1425-
tp_new and tp_init functions
1426-
----------------------------
1422+
How to convert ``tp_new`` and ``tp_init`` functions
1423+
---------------------------------------------------
14271424

14281425
You can convert ``tp_new`` and ``tp_init`` functions. Just name
14291426
them ``__new__`` or ``__init__`` as appropriate. Notes:
@@ -1445,8 +1442,8 @@ them ``__new__`` or ``__init__`` as appropriate. Notes:
14451442
generated will throw an exception if it receives any.)
14461443

14471444

1448-
Changing and redirecting Clinic's output
1449-
----------------------------------------
1445+
How to change and redirect Clinic's output
1446+
------------------------------------------
14501447

14511448
It can be inconvenient to have Clinic's output interspersed with
14521449
your conventional hand-edited C code. Luckily, Clinic is configurable:
@@ -1728,8 +1725,8 @@ it in a Clinic block lets Clinic use its existing checksum functionality to ensu
17281725
the file was not modified by hand before it gets overwritten.
17291726

17301727

1731-
The #ifdef trick
1732-
----------------
1728+
How to use the ``#ifdef`` trick
1729+
-------------------------------
17331730

17341731
If you're converting a function that isn't available on all platforms,
17351732
there's a trick you can use to make life a little easier. The existing
@@ -1809,8 +1806,8 @@ Argument Clinic added to your file (it'll be at the very bottom), then
18091806
move it above the ``PyMethodDef`` structure where that macro is used.
18101807

18111808

1812-
Using Argument Clinic in Python files
1813-
-------------------------------------
1809+
How to use Argument Clinic in Python files
1810+
------------------------------------------
18141811

18151812
It's actually possible to use Argument Clinic to preprocess Python files.
18161813
There's no point to using Argument Clinic blocks, of course, as the output

Diff for: Doc/whatsnew/3.13.rst

+5
Original file line numberDiff line numberDiff line change
@@ -774,6 +774,11 @@ New Features
774774
If the assertion fails, make sure that the size is set before.
775775
(Contributed by Victor Stinner in :gh:`106168`.)
776776

777+
* Add :c:func:`PyModule_Add` function: similar to
778+
:c:func:`PyModule_AddObjectRef` and :c:func:`PyModule_AddObject` but
779+
always steals a reference to the value.
780+
(Contributed by Serhiy Storchaka in :gh:`86493`.)
781+
777782
Porting to Python 3.13
778783
----------------------
779784

0 commit comments

Comments
 (0)