Skip to content

Commit af7b60b

Browse files
authored
Merge branch 'main' into pyio_bytesio_threading
2 parents 07d1436 + 0243f97 commit af7b60b

29 files changed

+509
-202
lines changed

Doc/c-api/object.rst

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,13 @@ Object Protocol
197197
in favour of using :c:func:`PyObject_DelAttr`, but there are currently no
198198
plans to remove it.
199199
200+
The function must not be called with ``NULL`` *v* and an an exception set.
201+
This case can arise from forgetting ``NULL`` checks and would delete the
202+
attribute.
203+
204+
.. versionchanged:: next
205+
Must not be called with NULL value if an exception is set.
206+
200207
201208
.. c:function:: int PyObject_SetAttrString(PyObject *o, const char *attr_name, PyObject *v)
202209
@@ -207,6 +214,10 @@ Object Protocol
207214
If *v* is ``NULL``, the attribute is deleted, but this feature is
208215
deprecated in favour of using :c:func:`PyObject_DelAttrString`.
209216
217+
The function must not be called with ``NULL`` *v* and an an exception set.
218+
This case can arise from forgetting ``NULL`` checks and would delete the
219+
attribute.
220+
210221
The number of different attribute names passed to this function
211222
should be kept small, usually by using a statically allocated string
212223
as *attr_name*.
@@ -215,6 +226,10 @@ Object Protocol
215226
For more details, see :c:func:`PyUnicode_InternFromString`, which may be
216227
used internally to create a key object.
217228
229+
.. versionchanged:: next
230+
Must not be called with NULL value if an exception is set.
231+
232+
218233
.. c:function:: int PyObject_GenericSetAttr(PyObject *o, PyObject *name, PyObject *value)
219234
220235
Generic attribute setter and deleter function that is meant

Doc/howto/functional.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1217,7 +1217,7 @@ flow inside a program. The book uses Scheme for its examples, but many of the
12171217
design approaches described in these chapters are applicable to functional-style
12181218
Python code.
12191219

1220-
https://www.defmacro.org/ramblings/fp.html: A general introduction to functional
1220+
https://defmacro.org/2006/06/19/fp.html: A general introduction to functional
12211221
programming that uses Java examples and has a lengthy historical introduction.
12221222

12231223
https://en.wikipedia.org/wiki/Functional_programming: General Wikipedia entry

Doc/installing/index.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ switch::
188188
Once the Development & Deployment part of PPUG is fleshed out, some of
189189
those sections should be linked from new questions here (most notably,
190190
we should have a question about avoiding depending on PyPI that links to
191-
https://packaging.python.org/en/latest/mirrors/)
191+
https://packaging.python.org/en/latest/guides/index-mirrors-and-caches/)
192192
193193
194194
Common installation issues

Doc/library/compression.zstd.rst

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -523,8 +523,14 @@ Advanced parameter control
523523
.. attribute:: compression_level
524524

525525
A high-level means of setting other compression parameters that affect
526-
the speed and ratio of compressing data. Setting the level to zero uses
527-
:attr:`COMPRESSION_LEVEL_DEFAULT`.
526+
the speed and ratio of compressing data.
527+
528+
Regular compression levels are greater than ``0``. Values greater than
529+
``20`` are considered "ultra" compression and require more memory than
530+
other levels. Negative values can be used to trade off faster compression
531+
for worse compression ratios.
532+
533+
Setting the level to zero uses :attr:`COMPRESSION_LEVEL_DEFAULT`.
528534

529535
.. attribute:: window_log
530536

Doc/library/shutil.rst

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -618,7 +618,8 @@ provided. They rely on the :mod:`zipfile` and :mod:`tarfile` modules.
618618
*format* is the archive format: one of
619619
"zip" (if the :mod:`zlib` module is available), "tar", "gztar" (if the
620620
:mod:`zlib` module is available), "bztar" (if the :mod:`bz2` module is
621-
available), or "xztar" (if the :mod:`lzma` module is available).
621+
available), "xztar" (if the :mod:`lzma` module is available), or "zstdtar"
622+
(if the :mod:`compression.zstd` module is available).
622623

623624
*root_dir* is a directory that will be the root directory of the
624625
archive, all paths in the archive will be relative to it; for example,
@@ -673,6 +674,8 @@ provided. They rely on the :mod:`zipfile` and :mod:`tarfile` modules.
673674
- *gztar*: gzip'ed tar-file (if the :mod:`zlib` module is available).
674675
- *bztar*: bzip2'ed tar-file (if the :mod:`bz2` module is available).
675676
- *xztar*: xz'ed tar-file (if the :mod:`lzma` module is available).
677+
- *zstdtar*: Zstandard compressed tar-file (if the :mod:`compression.zstd`
678+
module is available).
676679

677680
You can register new formats or provide your own archiver for any existing
678681
formats, by using :func:`register_archive_format`.
@@ -716,8 +719,8 @@ provided. They rely on the :mod:`zipfile` and :mod:`tarfile` modules.
716719
*extract_dir* is the name of the target directory where the archive is
717720
unpacked. If not provided, the current working directory is used.
718721

719-
*format* is the archive format: one of "zip", "tar", "gztar", "bztar", or
720-
"xztar". Or any other format registered with
722+
*format* is the archive format: one of "zip", "tar", "gztar", "bztar",
723+
"xztar", or "zstdtar". Or any other format registered with
721724
:func:`register_unpack_format`. If not provided, :func:`unpack_archive`
722725
will use the archive file name extension and see if an unpacker was
723726
registered for that extension. In case none is found,
@@ -789,6 +792,8 @@ provided. They rely on the :mod:`zipfile` and :mod:`tarfile` modules.
789792
- *gztar*: gzip'ed tar-file (if the :mod:`zlib` module is available).
790793
- *bztar*: bzip2'ed tar-file (if the :mod:`bz2` module is available).
791794
- *xztar*: xz'ed tar-file (if the :mod:`lzma` module is available).
795+
- *zstdtar*: Zstandard compressed tar-file (if the :mod:`compression.zstd`
796+
module is available).
792797

793798
You can register new formats or provide your own unpacker for any existing
794799
formats, by using :func:`register_unpack_format`.

Doc/library/tarfile.rst

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ higher-level functions in :ref:`shutil <archiving-operations>`.
1818

1919
Some facts and figures:
2020

21-
* reads and writes :mod:`gzip`, :mod:`bz2` and :mod:`lzma` compressed archives
22-
if the respective modules are available.
21+
* reads and writes :mod:`gzip`, :mod:`bz2`, :mod:`compression.zstd`, and
22+
:mod:`lzma` compressed archives if the respective modules are available.
2323

2424
* read/write support for the POSIX.1-1988 (ustar) format.
2525

@@ -47,6 +47,10 @@ Some facts and figures:
4747
or paths outside of the destination. Previously, the filter strategy
4848
was equivalent to :func:`fully_trusted <fully_trusted_filter>`.
4949

50+
.. versionchanged:: 3.14
51+
52+
Added support for Zstandard compression using :mod:`compression.zstd`.
53+
5054
.. function:: open(name=None, mode='r', fileobj=None, bufsize=10240, **kwargs)
5155

5256
Return a :class:`TarFile` object for the pathname *name*. For detailed
@@ -71,6 +75,8 @@ Some facts and figures:
7175
+------------------+---------------------------------------------+
7276
| ``'r:xz'`` | Open for reading with lzma compression. |
7377
+------------------+---------------------------------------------+
78+
| ``'r:zst'`` | Open for reading with Zstandard compression.|
79+
+------------------+---------------------------------------------+
7480
| ``'x'`` or | Create a tarfile exclusively without |
7581
| ``'x:'`` | compression. |
7682
| | Raise a :exc:`FileExistsError` exception |
@@ -88,6 +94,10 @@ Some facts and figures:
8894
| | Raise a :exc:`FileExistsError` exception |
8995
| | if it already exists. |
9096
+------------------+---------------------------------------------+
97+
| ``'x:zst'`` | Create a tarfile with Zstandard compression.|
98+
| | Raise a :exc:`FileExistsError` exception |
99+
| | if it already exists. |
100+
+------------------+---------------------------------------------+
91101
| ``'a' or 'a:'`` | Open for appending with no compression. The |
92102
| | file is created if it does not exist. |
93103
+------------------+---------------------------------------------+
@@ -99,6 +109,8 @@ Some facts and figures:
99109
+------------------+---------------------------------------------+
100110
| ``'w:xz'`` | Open for lzma compressed writing. |
101111
+------------------+---------------------------------------------+
112+
| ``'w:zst'`` | Open for Zstandard compressed writing. |
113+
+------------------+---------------------------------------------+
102114

103115
Note that ``'a:gz'``, ``'a:bz2'`` or ``'a:xz'`` is not possible. If *mode*
104116
is not suitable to open a certain (compressed) file for reading,
@@ -115,6 +127,15 @@ Some facts and figures:
115127
For modes ``'w:xz'``, ``'x:xz'`` and ``'w|xz'``, :func:`tarfile.open` accepts the
116128
keyword argument *preset* to specify the compression level of the file.
117129

130+
For modes ``'w:zst'``, ``'x:zst'`` and ``'w|zst'``, :func:`tarfile.open`
131+
accepts the keyword argument *level* to specify the compression level of
132+
the file. The keyword argument *options* may also be passed, providing
133+
advanced Zstandard compression parameters described by
134+
:class:`~compression.zstd.CompressionParameter`. The keyword argument
135+
*zstd_dict* can be passed to provide a :class:`~compression.zstd.ZstdDict`,
136+
a Zstandard dictionary used to improve compression of smaller amounts of
137+
data.
138+
118139
For special purposes, there is a second format for *mode*:
119140
``'filemode|[compression]'``. :func:`tarfile.open` will return a :class:`TarFile`
120141
object that processes its data as a stream of blocks. No random seeking will
@@ -146,6 +167,9 @@ Some facts and figures:
146167
| ``'r|xz'`` | Open an lzma compressed *stream* for |
147168
| | reading. |
148169
+-------------+--------------------------------------------+
170+
| ``'r|zst'`` | Open a Zstandard compressed *stream* for |
171+
| | reading. |
172+
+-------------+--------------------------------------------+
149173
| ``'w|'`` | Open an uncompressed *stream* for writing. |
150174
+-------------+--------------------------------------------+
151175
| ``'w|gz'`` | Open a gzip compressed *stream* for |
@@ -157,6 +181,9 @@ Some facts and figures:
157181
| ``'w|xz'`` | Open an lzma compressed *stream* for |
158182
| | writing. |
159183
+-------------+--------------------------------------------+
184+
| ``'w|zst'`` | Open a Zstandard compressed *stream* for |
185+
| | writing. |
186+
+-------------+--------------------------------------------+
160187

161188
.. versionchanged:: 3.5
162189
The ``'x'`` (exclusive creation) mode was added.

Doc/library/zipfile.rst

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -129,14 +129,28 @@ The module defines the following items:
129129

130130
.. versionadded:: 3.3
131131

132+
.. data:: ZIP_ZSTANDARD
133+
134+
The numeric constant for Zstandard compression. This requires the
135+
:mod:`compression.zstd` module.
136+
132137
.. note::
133138

134-
The ZIP file format specification has included support for bzip2 compression
135-
since 2001, and for LZMA compression since 2006. However, some tools
136-
(including older Python releases) do not support these compression
137-
methods, and may either refuse to process the ZIP file altogether,
138-
or fail to extract individual files.
139+
In APPNOTE 6.3.7, the method ID ``20`` was assigned to Zstandard
140+
compression. This was changed in APPNOTE 6.3.8 to method ID ``93`` to
141+
avoid conflicts, with method ID ``20`` being deprecated. For
142+
compatibility, the :mod:`!zipfile` module reads both method IDs but will
143+
only write data with method ID ``93``.
144+
145+
.. versionadded:: 3.14
146+
147+
.. note::
139148

149+
The ZIP file format specification has included support for bzip2 compression
150+
since 2001, for LZMA compression since 2006, and Zstandard compression since
151+
2020. However, some tools (including older Python releases) do not support
152+
these compression methods, and may either refuse to process the ZIP file
153+
altogether, or fail to extract individual files.
140154

141155
.. seealso::
142156

@@ -176,10 +190,11 @@ ZipFile Objects
176190

177191
*compression* is the ZIP compression method to use when writing the archive,
178192
and should be :const:`ZIP_STORED`, :const:`ZIP_DEFLATED`,
179-
:const:`ZIP_BZIP2` or :const:`ZIP_LZMA`; unrecognized
180-
values will cause :exc:`NotImplementedError` to be raised. If
181-
:const:`ZIP_DEFLATED`, :const:`ZIP_BZIP2` or :const:`ZIP_LZMA` is specified
182-
but the corresponding module (:mod:`zlib`, :mod:`bz2` or :mod:`lzma`) is not
193+
:const:`ZIP_BZIP2`, :const:`ZIP_LZMA`, or :const:`ZIP_ZSTANDARD`;
194+
unrecognized values will cause :exc:`NotImplementedError` to be raised. If
195+
:const:`ZIP_DEFLATED`, :const:`ZIP_BZIP2`, :const:`ZIP_LZMA`, or
196+
:const:`ZIP_ZSTANDARD` is specified but the corresponding module
197+
(:mod:`zlib`, :mod:`bz2`, :mod:`lzma`, or :mod:`compression.zstd`) is not
183198
available, :exc:`RuntimeError` is raised. The default is :const:`ZIP_STORED`.
184199

185200
If *allowZip64* is ``True`` (the default) zipfile will create ZIP files that
@@ -194,6 +209,10 @@ ZipFile Objects
194209
(see :class:`zlib <zlib.compressobj>` for more information).
195210
When using :const:`ZIP_BZIP2` integers ``1`` through ``9`` are accepted
196211
(see :class:`bz2 <bz2.BZ2File>` for more information).
212+
When using :const:`ZIP_ZSTANDARD` integers ``-131072`` through ``22`` are
213+
commonly accepted (see
214+
:attr:`CompressionParameter.compression_level <compression.zstd.CompressionParameter.compression_level>`
215+
for more on retrieving valid values and their meaning).
197216

198217
The *strict_timestamps* argument, when set to ``False``, allows to
199218
zip files older than 1980-01-01 at the cost of setting the
@@ -415,9 +434,10 @@ ZipFile Objects
415434
read or append. *pwd* is the password used for encrypted files as a :class:`bytes`
416435
object and, if specified, overrides the default password set with :meth:`setpassword`.
417436
Calling :meth:`read` on a ZipFile that uses a compression method other than
418-
:const:`ZIP_STORED`, :const:`ZIP_DEFLATED`, :const:`ZIP_BZIP2` or
419-
:const:`ZIP_LZMA` will raise a :exc:`NotImplementedError`. An error will also
420-
be raised if the corresponding compression module is not available.
437+
:const:`ZIP_STORED`, :const:`ZIP_DEFLATED`, :const:`ZIP_BZIP2`,
438+
:const:`ZIP_LZMA`, or :const:`ZIP_ZSTANDARD` will raise a
439+
:exc:`NotImplementedError`. An error will also be raised if the
440+
corresponding compression module is not available.
421441

422442
.. versionchanged:: 3.6
423443
Calling :meth:`read` on a closed ZipFile will raise a :exc:`ValueError`.

Doc/using/configure.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ Features and minimum versions required to build CPython:
2929

3030
* Tcl/Tk 8.5.12 for the :mod:`tkinter` module.
3131

32+
* `libmpdec <https://www.bytereef.org/mpdecimal/doc/libmpdec/>`_ 2.5.0
33+
for the :mod:`decimal` module.
34+
3235
* Autoconf 2.72 and aclocal 1.16.5 are required to regenerate the
3336
:file:`configure` script.
3437

Lib/asyncio/base_events.py

Lines changed: 35 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1016,38 +1016,43 @@ async def _connect_sock(self, exceptions, addr_info, local_addr_infos=None):
10161016
family, type_, proto, _, address = addr_info
10171017
sock = None
10181018
try:
1019-
sock = socket.socket(family=family, type=type_, proto=proto)
1020-
sock.setblocking(False)
1021-
if local_addr_infos is not None:
1022-
for lfamily, _, _, _, laddr in local_addr_infos:
1023-
# skip local addresses of different family
1024-
if lfamily != family:
1025-
continue
1026-
try:
1027-
sock.bind(laddr)
1028-
break
1029-
except OSError as exc:
1030-
msg = (
1031-
f'error while attempting to bind on '
1032-
f'address {laddr!r}: {str(exc).lower()}'
1033-
)
1034-
exc = OSError(exc.errno, msg)
1035-
my_exceptions.append(exc)
1036-
else: # all bind attempts failed
1037-
if my_exceptions:
1038-
raise my_exceptions.pop()
1039-
else:
1040-
raise OSError(f"no matching local address with {family=} found")
1041-
await self.sock_connect(sock, address)
1042-
return sock
1043-
except OSError as exc:
1044-
my_exceptions.append(exc)
1045-
if sock is not None:
1046-
sock.close()
1047-
raise
1019+
try:
1020+
sock = socket.socket(family=family, type=type_, proto=proto)
1021+
sock.setblocking(False)
1022+
if local_addr_infos is not None:
1023+
for lfamily, _, _, _, laddr in local_addr_infos:
1024+
# skip local addresses of different family
1025+
if lfamily != family:
1026+
continue
1027+
try:
1028+
sock.bind(laddr)
1029+
break
1030+
except OSError as exc:
1031+
msg = (
1032+
f'error while attempting to bind on '
1033+
f'address {laddr!r}: {str(exc).lower()}'
1034+
)
1035+
exc = OSError(exc.errno, msg)
1036+
my_exceptions.append(exc)
1037+
else: # all bind attempts failed
1038+
if my_exceptions:
1039+
raise my_exceptions.pop()
1040+
else:
1041+
raise OSError(f"no matching local address with {family=} found")
1042+
await self.sock_connect(sock, address)
1043+
return sock
1044+
except OSError as exc:
1045+
my_exceptions.append(exc)
1046+
raise
10481047
except:
10491048
if sock is not None:
1050-
sock.close()
1049+
try:
1050+
sock.close()
1051+
except OSError:
1052+
# An error when closing a newly created socket is
1053+
# not important, but it can overwrite more important
1054+
# non-OSError error. So ignore it.
1055+
pass
10511056
raise
10521057
finally:
10531058
exceptions = my_exceptions = None

Lib/encodings/idna.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ def _buffer_encode(self, input, errors, final):
316316
class IncrementalDecoder(codecs.BufferedIncrementalDecoder):
317317
def _buffer_decode(self, input, errors, final):
318318
if errors != 'strict':
319-
raise UnicodeError("Unsupported error handling: {errors}")
319+
raise UnicodeError(f"Unsupported error handling: {errors}")
320320

321321
if not input:
322322
return ("", 0)

0 commit comments

Comments
 (0)