Skip to content
1 change: 1 addition & 0 deletions Doc/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ venv:
echo "venv already exists."; \
echo "To recreate it, remove it first with \`make clean-venv'."; \
else \
set -e; \
echo "Creating venv in $(VENVDIR)"; \
if $(UV) --version >/dev/null 2>&1; then \
$(UV) venv --python=$(PYTHON) $(VENVDIR); \
Expand Down
20 changes: 17 additions & 3 deletions Doc/library/gzip.rst
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Note that additional file formats which can be decompressed by the
The module defines the following items:


.. function:: open(filename, mode='rb', compresslevel=9, encoding=None, errors=None, newline=None)
.. function:: open(filename, mode='rb', compresslevel=6, encoding=None, errors=None, newline=None)

Open a gzip-compressed file in binary or text mode, returning a :term:`file
object`.
Expand Down Expand Up @@ -59,6 +59,11 @@ The module defines the following items:
.. versionchanged:: 3.6
Accepts a :term:`path-like object`.

.. versionchanged:: next
The default compression level was reduced to 6 (down from 9).
It is the default level used by most compression tools and a better
tradeoff between speed and performance.

.. exception:: BadGzipFile

An exception raised for invalid gzip files. It inherits from :exc:`OSError`.
Expand All @@ -67,7 +72,7 @@ The module defines the following items:

.. versionadded:: 3.8

.. class:: GzipFile(filename=None, mode=None, compresslevel=9, fileobj=None, mtime=None)
.. class:: GzipFile(filename=None, mode=None, compresslevel=6, fileobj=None, mtime=None)

Constructor for the :class:`GzipFile` class, which simulates most of the
methods of a :term:`file object`, with the exception of the :meth:`~io.IOBase.truncate`
Expand Down Expand Up @@ -181,8 +186,13 @@ The module defines the following items:
Remove the ``filename`` attribute, use the :attr:`~GzipFile.name`
attribute instead.

.. versionchanged:: next
The default compression level was reduced to 6 (down from 9).
It is the default level used by most compression tools and a better
tradeoff between speed and performance.


.. function:: compress(data, compresslevel=9, *, mtime=0)
.. function:: compress(data, compresslevel=6, *, mtime=0)

Compress the *data*, returning a :class:`bytes` object containing
the compressed data. *compresslevel* and *mtime* have the same meaning as in
Expand All @@ -206,6 +216,10 @@ The module defines the following items:
The *mtime* parameter now defaults to 0 for reproducible output.
For the previous behaviour of using the current time,
pass ``None`` to *mtime*.
.. versionchanged:: next
The default compression level was reduced to 6 (down from 9).
It is the default level used by most compression tools and a better
tradeoff between speed and performance.

.. function:: decompress(data)

Expand Down
22 changes: 19 additions & 3 deletions Doc/library/http.client.rst
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ The module provides the following classes:


.. class:: HTTPConnection(host, port=None[, timeout], source_address=None, \
blocksize=8192)
blocksize=8192, max_response_headers=None)

An :class:`HTTPConnection` instance represents one transaction with an HTTP
server. It should be instantiated by passing it a host and optional port
Expand All @@ -46,7 +46,9 @@ The module provides the following classes:
The optional *source_address* parameter may be a tuple of a (host, port)
to use as the source address the HTTP connection is made from.
The optional *blocksize* parameter sets the buffer size in bytes for
sending a file-like message body.
sending a file-like message body. The optional *max_response_headers*
parameter sets the maximum number of allowed response headers to help
prevent denial-of-service attacks, otherwise the default value (100) is used.

For example, the following calls all create instances that connect to the server
at the same host and port::
Expand All @@ -66,10 +68,13 @@ The module provides the following classes:
.. versionchanged:: 3.7
*blocksize* parameter was added.

.. versionchanged:: next
*max_response_headers* parameter was added.


.. class:: HTTPSConnection(host, port=None, *[, timeout], \
source_address=None, context=None, \
blocksize=8192)
blocksize=8192, max_response_headers=None)

A subclass of :class:`HTTPConnection` that uses SSL for communication with
secure servers. Default port is ``443``. If *context* is specified, it
Expand Down Expand Up @@ -109,6 +114,9 @@ The module provides the following classes:
The deprecated *key_file*, *cert_file* and *check_hostname* parameters
have been removed.

.. versionchanged:: next
*max_response_headers* parameter was added.


.. class:: HTTPResponse(sock, debuglevel=0, method=None, url=None)

Expand Down Expand Up @@ -416,6 +424,14 @@ HTTPConnection Objects
.. versionadded:: 3.7


.. attribute:: HTTPConnection.max_response_headers

The maximum number of allowed response headers to help prevent denial-of-service
attacks. By default, the maximum number of allowed headers is set to 100.

.. versionadded:: next


As an alternative to using the :meth:`~HTTPConnection.request` method described above, you can
also send your request step by step, by using the four functions below.

Expand Down
46 changes: 33 additions & 13 deletions Doc/library/tarfile.rst
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ Some facts and figures:

For modes ``'w:gz'``, ``'x:gz'``, ``'w|gz'``, ``'w:bz2'``, ``'x:bz2'``,
``'w|bz2'``, :func:`tarfile.open` accepts the keyword argument
*compresslevel* (default ``9``) to specify the compression level of the file.
*compresslevel* (default ``6``) to specify the compression level of the file.

For modes ``'w:xz'``, ``'x:xz'`` and ``'w|xz'``, :func:`tarfile.open` accepts the
keyword argument *preset* to specify the compression level of the file.
Expand Down Expand Up @@ -198,6 +198,10 @@ Some facts and figures:
.. versionchanged:: 3.14
The *preset* keyword argument also works for streams.

.. versionchanged:: next
The default compression level was reduced to 6 (down from 9).
It is the default level used by most compression tools and a better
tradeoff between speed and performance.

.. class:: TarFile
:noindex:
Expand Down Expand Up @@ -1353,6 +1357,9 @@ Command-line options
Examples
--------

Reading examples
~~~~~~~~~~~~~~~~~~~

How to extract an entire tar archive to the current working directory::

import tarfile
Expand All @@ -1375,6 +1382,23 @@ a generator function instead of a list::
tar.extractall(members=py_files(tar))
tar.close()

How to read a gzip compressed tar archive and display some member information::

import tarfile
tar = tarfile.open("sample.tar.gz", "r:gz")
for tarinfo in tar:
print(tarinfo.name, "is", tarinfo.size, "bytes in size and is ", end="")
if tarinfo.isreg():
print("a regular file.")
elif tarinfo.isdir():
print("a directory.")
else:
print("something else.")
tar.close()

Writing examples
~~~~~~~~~~~~~~~~

How to create an uncompressed tar archive from a list of filenames::

import tarfile
Expand All @@ -1390,19 +1414,15 @@ The same example using the :keyword:`with` statement::
for name in ["foo", "bar", "quux"]:
tar.add(name)

How to read a gzip compressed tar archive and display some member information::
How to create and write an archive to stdout using
:data:`sys.stdout.buffer <sys.stdout>` in the *fileobj* parameter
in :meth:`TarFile.add`::

import tarfile
tar = tarfile.open("sample.tar.gz", "r:gz")
for tarinfo in tar:
print(tarinfo.name, "is", tarinfo.size, "bytes in size and is ", end="")
if tarinfo.isreg():
print("a regular file.")
elif tarinfo.isdir():
print("a directory.")
else:
print("something else.")
tar.close()
import sys
import tarfile
with tarfile.open("sample.tar.gz", "w|gz", fileobj=sys.stdout.buffer) as tar:
for name in ["foo", "bar", "quux"]:
tar.add(name)

How to create an archive and reset the user information using the *filter*
parameter in :meth:`TarFile.add`::
Expand Down
10 changes: 10 additions & 0 deletions Doc/whatsnew/3.15.rst
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,16 @@ difflib
(Contributed by Jiahao Li in :gh:`134580`.)


http.client
-----------

* A new *max_response_headers* keyword-only parameter has been added to
:class:`~http.client.HTTPConnection` and :class:`~http.client.HTTPSConnection`
constructors. This parameter overrides the default maximum number of allowed
response headers.
(Contributed by Alexander Enrique Urieles Nieto in :gh:`131724`.)


math
----

Expand Down
6 changes: 3 additions & 3 deletions Lib/gzip.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
_WRITE_BUFFER_SIZE = 4 * io.DEFAULT_BUFFER_SIZE


def open(filename, mode="rb", compresslevel=_COMPRESS_LEVEL_BEST,
def open(filename, mode="rb", compresslevel=_COMPRESS_LEVEL_TRADEOFF,
encoding=None, errors=None, newline=None):
"""Open a gzip-compressed file in binary or text mode.

Expand Down Expand Up @@ -158,7 +158,7 @@ class GzipFile(_streams.BaseStream):
myfileobj = None

def __init__(self, filename=None, mode=None,
compresslevel=_COMPRESS_LEVEL_BEST, fileobj=None, mtime=None):
compresslevel=_COMPRESS_LEVEL_TRADEOFF, fileobj=None, mtime=None):
"""Constructor for the GzipFile class.

At least one of fileobj and filename must be given a
Expand Down Expand Up @@ -621,7 +621,7 @@ def _rewind(self):
self._new_member = True


def compress(data, compresslevel=_COMPRESS_LEVEL_BEST, *, mtime=0):
def compress(data, compresslevel=_COMPRESS_LEVEL_TRADEOFF, *, mtime=0):
"""Compress data in one shot and return the compressed string.

compresslevel sets the compression level in range of 0-9.
Expand Down
20 changes: 15 additions & 5 deletions Lib/hashlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,22 @@ def __get_openssl_constructor(name):
# Prefer our builtin blake2 implementation.
return __get_builtin_constructor(name)
try:
# MD5, SHA1, and SHA2 are in all supported OpenSSL versions
# SHA3/shake are available in OpenSSL 1.1.1+
# Fetch the OpenSSL hash function if it exists,
# independently of the context security policy.
f = getattr(_hashlib, 'openssl_' + name)
# Allow the C module to raise ValueError. The function will be
# defined but the hash not actually available. Don't fall back to
# builtin if the current security policy blocks a digest, bpo#40695.
# Check if the context security policy blocks the digest or not
# by allowing the C module to raise a ValueError. The function
# will be defined but the hash will not be available at runtime.
#
# We use "usedforsecurity=False" to prevent falling back to the
# built-in function in case the security policy does not allow it.
#
# Note that this only affects the explicit named constructors,
# and not the algorithms exposed through hashlib.new() which
# can still be resolved to a built-in function even if the
# current security policy does not allow it.
#
# See https://github.com/python/cpython/issues/84872.
f(usedforsecurity=False)
# Use the C function directly (very fast)
return f
Expand Down
Loading
Loading