Skip to content

Commit 97de1b4

Browse files
committed
Merge remote-tracking branch 'original/main' into develop
2 parents 3f8e7c0 + 44fc378 commit 97de1b4

File tree

189 files changed

+3350
-2239
lines changed

Some content is hidden

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

189 files changed

+3350
-2239
lines changed

Doc/c-api/dict.rst

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,9 @@ Dictionary Objects
7979
8080
.. c:function:: int PyDict_SetItemString(PyObject *p, const char *key, PyObject *val)
8181
82-
.. index:: single: PyUnicode_FromString()
83-
84-
Insert *val* into the dictionary *p* using *key* as a key. *key* should
85-
be a :c:expr:`const char*` UTF-8 encoded bytes string. The key object is created using
86-
``PyUnicode_FromString(key)``. Return ``0`` on success or ``-1`` on
87-
failure. This function *does not* steal a reference to *val*.
82+
This is the same as :c:func:`PyDict_SetItem`, but *key* is
83+
specified as a :c:expr:`const char*` UTF-8 encoded bytes string,
84+
rather than a :c:expr:`PyObject*`.
8885
8986
9087
.. c:function:: int PyDict_DelItem(PyObject *p, PyObject *key)
@@ -97,10 +94,9 @@ Dictionary Objects
9794
9895
.. c:function:: int PyDict_DelItemString(PyObject *p, const char *key)
9996
100-
Remove the entry in dictionary *p* which has a key specified by the UTF-8
101-
encoded bytes string *key*.
102-
If *key* is not in the dictionary, :exc:`KeyError` is raised.
103-
Return ``0`` on success or ``-1`` on failure.
97+
This is the same as :c:func:`PyDict_DelItem`, but *key* is
98+
specified as a :c:expr:`const char*` UTF-8 encoded bytes string,
99+
rather than a :c:expr:`PyObject*`.
104100
105101
106102
.. c:function:: int PyDict_GetItemRef(PyObject *p, PyObject *key, PyObject **result)

Doc/c-api/mapping.rst

Lines changed: 28 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ See also :c:func:`PyObject_GetItem`, :c:func:`PyObject_SetItem` and
2828
2929
.. c:function:: PyObject* PyMapping_GetItemString(PyObject *o, const char *key)
3030
31-
Return element of *o* corresponding to the string *key* or ``NULL`` on failure.
32-
This is the equivalent of the Python expression ``o[key]``.
33-
See also :c:func:`PyObject_GetItem`.
31+
This is the same as :c:func:`PyObject_GetItem`, but *key* is
32+
specified as a :c:expr:`const char*` UTF-8 encoded bytes string,
33+
rather than a :c:expr:`PyObject*`.
3434
3535
3636
.. c:function:: int PyMapping_GetOptionalItem(PyObject *obj, PyObject *key, PyObject **result)
@@ -50,38 +50,30 @@ See also :c:func:`PyObject_GetItem`, :c:func:`PyObject_SetItem` and
5050
5151
.. c:function:: int PyMapping_GetOptionalItemString(PyObject *obj, const char *key, PyObject **result)
5252
53-
Variant of :c:func:`PyMapping_GetItemString` which doesn't raise
54-
:exc:`KeyError` if the key is not found.
55-
56-
If the key is found, return ``1`` and set *\*result* to a new
57-
:term:`strong reference` to the corresponding value.
58-
If the key is not found, return ``0`` and set *\*result* to ``NULL``;
59-
the :exc:`KeyError` is silenced.
60-
If an error other than :exc:`KeyError` is raised, return ``-1`` and
61-
set *\*result* to ``NULL``.
53+
This is the same as :c:func:`PyMapping_GetOptionalItem`, but *key* is
54+
specified as a :c:expr:`const char*` UTF-8 encoded bytes string,
55+
rather than a :c:expr:`PyObject*`.
6256
6357
.. versionadded:: 3.13
6458
6559
6660
.. c:function:: int PyMapping_SetItemString(PyObject *o, const char *key, PyObject *v)
6761
68-
Map the string *key* to the value *v* in object *o*. Returns ``-1`` on
69-
failure. This is the equivalent of the Python statement ``o[key] = v``.
70-
See also :c:func:`PyObject_SetItem`. This function *does not* steal a
71-
reference to *v*.
62+
This is the same as :c:func:`PyObject_SetItem`, but *key* is
63+
specified as a :c:expr:`const char*` UTF-8 encoded bytes string,
64+
rather than a :c:expr:`PyObject*`.
7265
7366
7467
.. c:function:: int PyMapping_DelItem(PyObject *o, PyObject *key)
7568
76-
Remove the mapping for the object *key* from the object *o*. Return ``-1``
77-
on failure. This is equivalent to the Python statement ``del o[key]``.
7869
This is an alias of :c:func:`PyObject_DelItem`.
7970
8071
8172
.. c:function:: int PyMapping_DelItemString(PyObject *o, const char *key)
8273
83-
Remove the mapping for the string *key* from the object *o*. Return ``-1``
84-
on failure. This is equivalent to the Python statement ``del o[key]``.
74+
This is the same as :c:func:`PyObject_DelItem`, but *key* is
75+
specified as a :c:expr:`const char*` UTF-8 encoded bytes string,
76+
rather than a :c:expr:`PyObject*`.
8577
8678
8779
.. c:function:: int PyMapping_HasKey(PyObject *o, PyObject *key)
@@ -90,20 +82,27 @@ See also :c:func:`PyObject_GetItem`, :c:func:`PyObject_SetItem` and
9082
This is equivalent to the Python expression ``key in o``.
9183
This function always succeeds.
9284
93-
Note that exceptions which occur while calling the :meth:`~object.__getitem__`
94-
method will get suppressed.
95-
To get error reporting use :c:func:`PyObject_GetItem()` instead.
85+
.. note::
86+
87+
Exceptions which occur when this calls :meth:`~object.__getitem__`
88+
method are silently ignored.
89+
For proper error handling, use :c:func:`PyMapping_GetOptionalItem` or
90+
:c:func:`PyObject_GetItem()` instead.
9691
9792
9893
.. c:function:: int PyMapping_HasKeyString(PyObject *o, const char *key)
9994
100-
Return ``1`` if the mapping object has the key *key* and ``0`` otherwise.
101-
This is equivalent to the Python expression ``key in o``.
102-
This function always succeeds.
95+
This is the same as :c:func:`PyMapping_HasKey`, but *key* is
96+
specified as a :c:expr:`const char*` UTF-8 encoded bytes string,
97+
rather than a :c:expr:`PyObject*`.
98+
99+
.. note::
103100
104-
Note that exceptions which occur while calling the :meth:`~object.__getitem__`
105-
method and creating a temporary string object will get suppressed.
106-
To get error reporting use :c:func:`PyMapping_GetItemString()` instead.
101+
Exceptions that occur when this calls :meth:`~object.__getitem__`
102+
method or while creating the temporary :class:`str`
103+
object are silently ignored.
104+
For proper error handling, use :c:func:`PyMapping_GetOptionalItemString` or
105+
:c:func:`PyMapping_GetItemString` instead.
107106
108107
109108
.. c:function:: PyObject* PyMapping_Keys(PyObject *o)

Doc/c-api/object.rst

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,15 @@ Object Protocol
4343
4444
.. c:function:: int PyObject_HasAttrString(PyObject *o, const char *attr_name)
4545
46-
Returns ``1`` if *o* has the attribute *attr_name*, and ``0`` otherwise. This
47-
is equivalent to the Python expression ``hasattr(o, attr_name)``. This function
48-
always succeeds.
46+
This is the same as :c:func:`PyObject_HasAttr`, but *attr_name* is
47+
specified as a :c:expr:`const char*` UTF-8 encoded bytes string,
48+
rather than a :c:expr:`PyObject*`.
4949
5050
.. note::
5151
5252
Exceptions that occur when this calls :meth:`~object.__getattr__` and
53-
:meth:`~object.__getattribute__` methods or while creating the temporary :class:`str`
54-
object are silently ignored.
53+
:meth:`~object.__getattribute__` methods or while creating the temporary
54+
:class:`str` object are silently ignored.
5555
For proper error handling, use :c:func:`PyObject_GetOptionalAttrString`
5656
or :c:func:`PyObject_GetAttrString` instead.
5757
@@ -68,9 +68,9 @@ Object Protocol
6868
6969
.. c:function:: PyObject* PyObject_GetAttrString(PyObject *o, const char *attr_name)
7070
71-
Retrieve an attribute named *attr_name* from object *o*. Returns the attribute
72-
value on success, or ``NULL`` on failure. This is the equivalent of the Python
73-
expression ``o.attr_name``.
71+
This is the same as :c:func:`PyObject_GetAttr`, but *attr_name* is
72+
specified as a :c:expr:`const char*` UTF-8 encoded bytes string,
73+
rather than a :c:expr:`PyObject*`.
7474
7575
If the missing attribute should not be treated as a failure, you can use
7676
:c:func:`PyObject_GetOptionalAttrString` instead.
@@ -93,15 +93,9 @@ Object Protocol
9393
9494
.. c:function:: int PyObject_GetOptionalAttrString(PyObject *obj, const char *attr_name, PyObject **result);
9595
96-
Variant of :c:func:`PyObject_GetAttrString` which doesn't raise
97-
:exc:`AttributeError` if the attribute is not found.
98-
99-
If the attribute is found, return ``1`` and set *\*result* to a new
100-
:term:`strong reference` to the attribute.
101-
If the attribute is not found, return ``0`` and set *\*result* to ``NULL``;
102-
the :exc:`AttributeError` is silenced.
103-
If an error other than :exc:`AttributeError` is raised, return ``-1`` and
104-
set *\*result* to ``NULL``.
96+
This is the same as :c:func:`PyObject_GetOptionalAttr`, but *attr_name* is
97+
specified as a :c:expr:`const char*` UTF-8 encoded bytes string,
98+
rather than a :c:expr:`PyObject*`.
10599
106100
.. versionadded:: 3.13
107101
@@ -129,10 +123,9 @@ Object Protocol
129123
130124
.. c:function:: int PyObject_SetAttrString(PyObject *o, const char *attr_name, PyObject *v)
131125
132-
Set the value of the attribute named *attr_name*, for object *o*, to the value
133-
*v*. Raise an exception and return ``-1`` on failure;
134-
return ``0`` on success. This is the equivalent of the Python statement
135-
``o.attr_name = v``.
126+
This is the same as :c:func:`PyObject_SetAttr`, but *attr_name* is
127+
specified as a :c:expr:`const char*` UTF-8 encoded bytes string,
128+
rather than a :c:expr:`PyObject*`.
136129
137130
If *v* is ``NULL``, the attribute is deleted, but this feature is
138131
deprecated in favour of using :c:func:`PyObject_DelAttrString`.
@@ -158,8 +151,9 @@ Object Protocol
158151
159152
.. c:function:: int PyObject_DelAttrString(PyObject *o, const char *attr_name)
160153
161-
Delete attribute named *attr_name*, for object *o*. Returns ``-1`` on failure.
162-
This is the equivalent of the Python statement ``del o.attr_name``.
154+
This is the same as :c:func:`PyObject_DelAttr`, but *attr_name* is
155+
specified as a :c:expr:`const char*` UTF-8 encoded bytes string,
156+
rather than a :c:expr:`PyObject*`.
163157
164158
165159
.. c:function:: PyObject* PyObject_GenericGetDict(PyObject *o, void *context)

Doc/library/datetime.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ on efficient attribute extraction for output formatting and manipulation.
3737
Package `dateutil <https://dateutil.readthedocs.io/en/stable/>`_
3838
Third-party library with expanded time zone and parsing support.
3939

40+
Package `DateType <https://pypi.org/project/datetype/>`_
41+
Third-party library that introduces distinct static types to e.g. allow static type checkers
42+
to differentiate between naive and aware datetimes.
43+
4044
.. _datetime-naive-aware:
4145

4246
Aware and Naive Objects

Doc/library/doctest.rst

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1409,6 +1409,27 @@ DocTestParser objects
14091409
identifying this string, and is only used for error messages.
14101410

14111411

1412+
TestResults objects
1413+
^^^^^^^^^^^^^^^^^^^
1414+
1415+
1416+
.. class:: TestResults(failed, attempted)
1417+
1418+
.. attribute:: failed
1419+
1420+
Number of failed tests.
1421+
1422+
.. attribute:: attempted
1423+
1424+
Number of attempted tests.
1425+
1426+
.. attribute:: skipped
1427+
1428+
Number of skipped tests.
1429+
1430+
.. versionadded:: 3.13
1431+
1432+
14121433
.. _doctest-doctestrunner:
14131434

14141435
DocTestRunner objects
@@ -1427,7 +1448,7 @@ DocTestRunner objects
14271448
passing a subclass of :class:`OutputChecker` to the constructor.
14281449

14291450
The test runner's display output can be controlled in two ways. First, an output
1430-
function can be passed to :meth:`TestRunner.run`; this function will be called
1451+
function can be passed to :meth:`run`; this function will be called
14311452
with strings that should be displayed. It defaults to ``sys.stdout.write``. If
14321453
capturing the output is not sufficient, then the display output can be also
14331454
customized by subclassing DocTestRunner, and overriding the methods
@@ -1448,6 +1469,10 @@ DocTestRunner objects
14481469
runner compares expected output to actual output, and how it displays failures.
14491470
For more information, see section :ref:`doctest-options`.
14501471

1472+
The test runner accumulates statistics. The aggregated number of attempted,
1473+
failed and skipped examples is also available via the :attr:`tries`,
1474+
:attr:`failures` and :attr:`skips` attributes. The :meth:`run` and
1475+
:meth:`summarize` methods return a :class:`TestResults` instance.
14511476

14521477
:class:`DocTestParser` defines the following methods:
14531478

@@ -1500,7 +1525,8 @@ DocTestRunner objects
15001525
.. method:: run(test, compileflags=None, out=None, clear_globs=True)
15011526

15021527
Run the examples in *test* (a :class:`DocTest` object), and display the
1503-
results using the writer function *out*.
1528+
results using the writer function *out*. Return a :class:`TestResults`
1529+
instance.
15041530

15051531
The examples are run in the namespace ``test.globs``. If *clear_globs* is
15061532
true (the default), then this namespace will be cleared after the test runs,
@@ -1519,12 +1545,29 @@ DocTestRunner objects
15191545
.. method:: summarize(verbose=None)
15201546

15211547
Print a summary of all the test cases that have been run by this DocTestRunner,
1522-
and return a :term:`named tuple` ``TestResults(failed, attempted)``.
1548+
and return a :class:`TestResults` instance.
15231549

15241550
The optional *verbose* argument controls how detailed the summary is. If the
15251551
verbosity is not specified, then the :class:`DocTestRunner`'s verbosity is
15261552
used.
15271553

1554+
:class:`DocTestParser` has the following attributes:
1555+
1556+
.. attribute:: tries
1557+
1558+
Number of attempted examples.
1559+
1560+
.. attribute:: failures
1561+
1562+
Number of failed examples.
1563+
1564+
.. attribute:: skips
1565+
1566+
Number of skipped examples.
1567+
1568+
.. versionadded:: 3.13
1569+
1570+
15281571
.. _doctest-outputchecker:
15291572

15301573
OutputChecker objects

Doc/library/itertools.rst

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -865,26 +865,22 @@ which incur interpreter overhead.
865865
# first_true([a,b], x, f) --> a if f(a) else b if f(b) else x
866866
return next(filter(pred, iterable), default)
867867

868-
def iter_index(iterable, value, start=0):
868+
def iter_index(iterable, value, start=0, stop=None):
869869
"Return indices where a value occurs in a sequence or iterable."
870870
# iter_index('AABCADEAF', 'A') --> 0 1 4 7
871-
try:
872-
seq_index = iterable.index
873-
except AttributeError:
871+
seq_index = getattr(iterable, 'index', None)
872+
if seq_index is None:
874873
# Slow path for general iterables
875-
it = islice(iterable, start, None)
876-
i = start - 1
877-
try:
878-
while True:
879-
yield (i := i + operator.indexOf(it, value) + 1)
880-
except ValueError:
881-
pass
874+
it = islice(iterable, start, stop)
875+
for i, element in enumerate(it, start):
876+
if element is value or element == value:
877+
yield i
882878
else:
883879
# Fast path for sequences
884880
i = start - 1
885881
try:
886882
while True:
887-
yield (i := seq_index(value, i+1))
883+
yield (i := seq_index(value, i+1, stop))
888884
except ValueError:
889885
pass
890886

@@ -1331,6 +1327,21 @@ The following recipes have a more mathematical flavor:
13311327
[]
13321328
>>> list(iter_index(iter('AABCADEAF'), 'A', 10))
13331329
[]
1330+
>>> list(iter_index('AABCADEAF', 'A', 1, 7))
1331+
[1, 4]
1332+
>>> list(iter_index(iter('AABCADEAF'), 'A', 1, 7))
1333+
[1, 4]
1334+
>>> # Verify that ValueErrors not swallowed (gh-107208)
1335+
>>> def assert_no_value(iterable, forbidden_value):
1336+
... for item in iterable:
1337+
... if item == forbidden_value:
1338+
... raise ValueError
1339+
... yield item
1340+
...
1341+
>>> list(iter_index(assert_no_value('AABCADEAF', 'B'), 'A'))
1342+
Traceback (most recent call last):
1343+
...
1344+
ValueError
13341345

13351346
>>> list(sieve(30))
13361347
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29]

0 commit comments

Comments
 (0)