Skip to content

Commit 531bf38

Browse files
Merge branch 'main' into gh-134786-managed-weakref
2 parents 3608747 + 48d0d0d commit 531bf38

Some content is hidden

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

48 files changed

+2682
-143
lines changed

Doc/library/collections.rst

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -367,9 +367,11 @@ Several mathematical operations are provided for combining :class:`Counter`
367367
objects to produce multisets (counters that have counts greater than zero).
368368
Addition and subtraction combine counters by adding or subtracting the counts
369369
of corresponding elements. Intersection and union return the minimum and
370-
maximum of corresponding counts. Equality and inclusion compare
371-
corresponding counts. Each operation can accept inputs with signed
372-
counts, but the output will exclude results with counts of zero or less.
370+
maximum of corresponding counts. Symmetric difference returns the difference
371+
between the maximum and minimum of the corresponding counts. Equality and
372+
inclusion compare corresponding counts. Each operation can accept inputs
373+
with signed counts, but the output will exclude results with counts of zero
374+
or below.
373375

374376
.. doctest::
375377

@@ -383,6 +385,8 @@ counts, but the output will exclude results with counts of zero or less.
383385
Counter({'a': 1, 'b': 1})
384386
>>> c | d # union: max(c[x], d[x])
385387
Counter({'a': 3, 'b': 2})
388+
>>> c ^ d # max(c[x], d[x]) - min(c[x], d[x])
389+
Counter({'a': 2, 'b': 1})
386390
>>> c == d # equality: c[x] == d[x]
387391
False
388392
>>> c <= d # inclusion: c[x] <= d[x]
@@ -400,6 +404,9 @@ or subtracting from an empty counter.
400404
.. versionadded:: 3.3
401405
Added support for unary plus, unary minus, and in-place multiset operations.
402406

407+
.. versionadded:: next
408+
Added support for the symmetric difference multiset operation, ``c ^ d``.
409+
403410
.. note::
404411

405412
Counters were primarily designed to work with positive integers to represent

Doc/library/pyexpat.rst

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,13 @@ The :mod:`xml.parsers.expat` module contains two functions:
7272
*encoding* [1]_ is given it will override the implicit or explicit encoding of the
7373
document.
7474

75+
.. _xmlparser-non-root:
76+
77+
Parsers created through :func:`!ParserCreate` are called "root" parsers,
78+
in the sense that they do not have any parent parser attached. Non-root
79+
parsers are created by :meth:`parser.ExternalEntityParserCreate
80+
<xmlparser.ExternalEntityParserCreate>`.
81+
7582
Expat can optionally do XML namespace processing for you, enabled by providing a
7683
value for *namespace_separator*. The value must be a one-character string; a
7784
:exc:`ValueError` will be raised if the string has an illegal length (``None``
@@ -231,6 +238,55 @@ XMLParser Objects
231238
.. versionadded:: 3.13
232239

233240

241+
:class:`!xmlparser` objects have the following methods to mitigate some
242+
common XML vulnerabilities.
243+
244+
.. method:: xmlparser.SetAllocTrackerActivationThreshold(threshold, /)
245+
246+
Sets the number of allocated bytes of dynamic memory needed to activate
247+
protection against disproportionate use of RAM.
248+
249+
By default, parser objects have an allocation activation threshold of 64 MiB,
250+
or equivalently 67,108,864 bytes.
251+
252+
An :exc:`ExpatError` is raised if this method is called on a
253+
|xml-non-root-parser| parser.
254+
The corresponding :attr:`~ExpatError.lineno` and :attr:`~ExpatError.offset`
255+
should not be used as they may have no special meaning.
256+
257+
.. versionadded:: next
258+
259+
.. method:: xmlparser.SetAllocTrackerMaximumAmplification(max_factor, /)
260+
261+
Sets the maximum amplification factor between direct input and bytes
262+
of dynamic memory allocated.
263+
264+
The amplification factor is calculated as ``allocated / direct``
265+
while parsing, where ``direct`` is the number of bytes read from
266+
the primary document in parsing and ``allocated`` is the number
267+
of bytes of dynamic memory allocated in the parser hierarchy.
268+
269+
The *max_factor* value must be a non-NaN :class:`float` value greater than
270+
or equal to 1.0. Amplification factors greater than 100.0 can be observed
271+
near the start of parsing even with benign files in practice. In particular,
272+
the activation threshold should be carefully chosen to avoid false positives.
273+
274+
By default, parser objects have a maximum amplification factor of 100.0.
275+
276+
An :exc:`ExpatError` is raised if this method is called on a
277+
|xml-non-root-parser| parser or if *max_factor* is outside the valid range.
278+
The corresponding :attr:`~ExpatError.lineno` and :attr:`~ExpatError.offset`
279+
should not be used as they may have no special meaning.
280+
281+
.. note::
282+
283+
The maximum amplification factor is only considered if the threshold
284+
that can be adjusted by :meth:`.SetAllocTrackerActivationThreshold`
285+
is exceeded.
286+
287+
.. versionadded:: next
288+
289+
234290
:class:`xmlparser` objects have the following attributes:
235291

236292

@@ -954,3 +1010,4 @@ The ``errors`` module has the following attributes:
9541010
not. See https://www.w3.org/TR/2006/REC-xml11-20060816/#NT-EncodingDecl
9551011
and https://www.iana.org/assignments/character-sets/character-sets.xhtml.
9561012
1013+
.. |xml-non-root-parser| replace:: :ref:`non-root <xmlparser-non-root>`

Doc/library/xml.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ An attacker can abuse XML features to carry out denial of service attacks,
5555
access local files, generate network connections to other machines, or
5656
circumvent firewalls.
5757

58-
Expat versions lower that 2.6.0 may be vulnerable to "billion laughs",
58+
Expat versions lower than 2.6.0 may be vulnerable to "billion laughs",
5959
"quadratic blowup" and "large tokens". Python may be vulnerable if it uses such
6060
older versions of Expat as a system-provided library.
6161
Check :const:`!pyexpat.EXPAT_VERSION`.

Doc/using/configure.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -869,9 +869,9 @@ Libraries options
869869
.. versionchanged:: 3.13
870870
Default to using the installed ``mpdecimal`` library.
871871

872-
.. deprecated-removed:: 3.13 3.15
872+
.. deprecated-removed:: 3.13 3.16
873873
A copy of the ``mpdecimal`` library sources will no longer be distributed
874-
with Python 3.15.
874+
with Python 3.16.
875875

876876
.. seealso:: :option:`LIBMPDEC_CFLAGS` and :option:`LIBMPDEC_LIBS`.
877877

Doc/whatsnew/3.15.rst

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,14 @@ New modules
294294
Improved modules
295295
================
296296

297+
collections
298+
-----------
299+
300+
* Added :meth:`!collections.Counter.__xor__` and
301+
:meth:`!collections.Counter.__ixor__` to compute the symmetric difference
302+
between :class:`~collections.Counter` objects.
303+
(Contributed by Raymond Hettinger in :gh:`138682`.)
304+
297305
collections.abc
298306
---------------
299307

@@ -545,6 +553,16 @@ unittest
545553
(Contributed by Garry Cairns in :gh:`134567`.)
546554

547555

556+
xml.parsers.expat
557+
-----------------
558+
559+
* Add :meth:`~xml.parsers.expat.xmlparser.SetAllocTrackerActivationThreshold`
560+
and :meth:`~xml.parsers.expat.xmlparser.SetAllocTrackerMaximumAmplification`
561+
to :ref:`xmlparser <xmlparser-objects>` objects to prevent use of
562+
disproportional amounts of dynamic memory from within an Expat parser.
563+
(Contributed by Bénédikt Tran in :gh:`90949`.)
564+
565+
548566
zlib
549567
----
550568

0 commit comments

Comments
 (0)