aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/Doc/library
diff options
context:
space:
mode:
Diffstat (limited to 'Doc/library')
-rw-r--r--Doc/library/codecs.rst8
-rw-r--r--Doc/library/compression.zstd.rst10
-rw-r--r--Doc/library/concurrent.interpreters.rst223
-rw-r--r--Doc/library/csv.rst42
-rw-r--r--Doc/library/exceptions.rst9
-rw-r--r--Doc/library/fractions.rst2
-rw-r--r--Doc/library/math.rst2
-rw-r--r--Doc/library/shutil.rst11
-rw-r--r--Doc/library/sys.monitoring.rst61
-rw-r--r--Doc/library/tarfile.rst31
-rw-r--r--Doc/library/time.rst7
-rw-r--r--Doc/library/unittest.rst9
-rw-r--r--Doc/library/venv.rst70
-rw-r--r--Doc/library/zipfile.rst44
14 files changed, 422 insertions, 107 deletions
diff --git a/Doc/library/codecs.rst b/Doc/library/codecs.rst
index 86511602fa5..b231fa568cf 100644
--- a/Doc/library/codecs.rst
+++ b/Doc/library/codecs.rst
@@ -53,6 +53,14 @@ any codec:
:exc:`UnicodeDecodeError`). Refer to :ref:`codec-base-classes` for more
information on codec error handling.
+.. function:: charmap_build(string)
+
+ Return a mapping suitable for encoding with a custom single-byte encoding.
+ Given a :class:`str` *string* of up to 256 characters representing a
+ decoding table, returns either a compact internal mapping object
+ ``EncodingMap`` or a :class:`dictionary <dict>` mapping character ordinals
+ to byte values. Raises a :exc:`TypeError` on invalid input.
+
The full details for each codec can also be looked up directly:
.. function:: lookup(encoding, /)
diff --git a/Doc/library/compression.zstd.rst b/Doc/library/compression.zstd.rst
index 57ad8e3377f..a901403621b 100644
--- a/Doc/library/compression.zstd.rst
+++ b/Doc/library/compression.zstd.rst
@@ -523,8 +523,14 @@ Advanced parameter control
.. attribute:: compression_level
A high-level means of setting other compression parameters that affect
- the speed and ratio of compressing data. Setting the level to zero uses
- :attr:`COMPRESSION_LEVEL_DEFAULT`.
+ the speed and ratio of compressing data.
+
+ Regular compression levels are greater than ``0``. Values greater than
+ ``20`` are considered "ultra" compression and require more memory than
+ other levels. Negative values can be used to trade off faster compression
+ for worse compression ratios.
+
+ Setting the level to zero uses :attr:`COMPRESSION_LEVEL_DEFAULT`.
.. attribute:: window_log
diff --git a/Doc/library/concurrent.interpreters.rst b/Doc/library/concurrent.interpreters.rst
index 8860418e87a..524d505bcf1 100644
--- a/Doc/library/concurrent.interpreters.rst
+++ b/Doc/library/concurrent.interpreters.rst
@@ -13,17 +13,26 @@
--------------
-
-Introduction
-------------
-
The :mod:`!concurrent.interpreters` module constructs higher-level
interfaces on top of the lower level :mod:`!_interpreters` module.
-.. XXX Add references to the upcoming HOWTO docs in the seealso block.
+The module is primarily meant to provide a basic API for managing
+interpreters (AKA "subinterpreters") and running things in them.
+Running mostly involves switching to an interpreter (in the current
+thread) and calling a function in that execution context.
+
+For concurrency, interpreters themselves (and this module) don't
+provide much more than isolation, which on its own isn't useful.
+Actual concurrency is available separately through
+:mod:`threads <threading>` See `below <interp-concurrency_>`_
.. seealso::
+ :class:`~concurrent.futures.InterpreterPoolExecutor`
+ combines threads with interpreters in a familiar interface.
+
+ .. XXX Add references to the upcoming HOWTO docs in the seealso block.
+
:ref:`isolating-extensions-howto`
how to update an extension module to support multiple interpreters
@@ -41,18 +50,155 @@ interfaces on top of the lower level :mod:`!_interpreters` module.
Key details
-----------
-Before we dive into examples, there are a small number of details
+Before we dive in further, there are a small number of details
to keep in mind about using multiple interpreters:
-* isolated, by default
+* `isolated <interp-isolation_>`_, by default
* no implicit threads
* not all PyPI packages support use in multiple interpreters yet
.. XXX Are there other relevant details to list?
-In the context of multiple interpreters, "isolated" means that
-different interpreters do not share any state. In practice, there is some
-process-global data they all share, but that is managed by the runtime.
+
+.. _interpreters-intro:
+
+Introduction
+------------
+
+An "interpreter" is effectively the execution context of the Python
+runtime. It contains all of the state the runtime needs to execute
+a program. This includes things like the import state and builtins.
+(Each thread, even if there's only the main thread, has some extra
+runtime state, in addition to the current interpreter, related to
+the current exception and the bytecode eval loop.)
+
+The concept and functionality of the interpreter have been a part of
+Python since version 2.2, but the feature was only available through
+the C-API and not well known, and the `isolation <interp-isolation_>`_
+was relatively incomplete until version 3.12.
+
+.. _interp-isolation:
+
+Multiple Interpreters and Isolation
+^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+
+A Python implementation may support using multiple interpreters in the
+same process. CPython has this support. Each interpreter is
+effectively isolated from the others (with a limited number of
+carefully managed process-global exceptions to the rule).
+
+That isolation is primarily useful as a strong separation between
+distinct logical components of a program, where you want to have
+careful control of how those components interact.
+
+.. note::
+
+ Interpreters in the same process can technically never be strictly
+ isolated from one another since there are few restrictions on memory
+ access within the same process. The Python runtime makes a best
+ effort at isolation but extension modules may easily violate that.
+ Therefore, do not use multiple interpreters in security-sensitive
+ situations, where they shouldn't have access to each other's data.
+
+Running in an Interpreter
+^^^^^^^^^^^^^^^^^^^^^^^^^
+
+Running in a different interpreter involves switching to it in the
+current thread and then calling some function. The runtime will
+execute the function using the current interpreter's state. The
+:mod:`!concurrent.interpreters` module provides a basic API for
+creating and managing interpreters, as well as the switch-and-call
+operation.
+
+No other threads are automatically started for the operation.
+There is `a helper <interp-call-in-thread_>`_ for that though.
+There is another dedicated helper for calling the builtin
+:func:`exec` in an interpreter.
+
+When :func:`exec` (or :func:`eval`) are called in an interpreter,
+they run using the interpreter's :mod:`!__main__` module as the
+"globals" namespace. The same is true for functions that aren't
+associated with any module. This is the same as how scripts invoked
+from the command-line run in the :mod:`!__main__` module.
+
+
+.. _interp-concurrency:
+
+Concurrency and Parallelism
+^^^^^^^^^^^^^^^^^^^^^^^^^^^
+
+As noted earlier, interpreters do not provide any concurrency
+on their own. They strictly represent the isolated execution
+context the runtime will use *in the current thread*. That isolation
+makes them similar to processes, but they still enjoy in-process
+efficiency, like threads.
+
+All that said, interpreters do naturally support certain flavors of
+concurrency, as a powerful side effect of that isolation.
+There's a powerful side effect of that isolation. It enables a
+different approach to concurrency than you can take with async or
+threads. It's a similar concurrency model to CSP or the actor model,
+a model which is relatively easy to reason about.
+
+You can take advantage of that concurrency model in a single thread,
+switching back and forth between interpreters, Stackless-style.
+However, this model is more useful when you combine interpreters
+with multiple threads. This mostly involves starting a new thread,
+where you switch to another interpreter and run what you want there.
+
+Each actual thread in Python, even if you're only running in the main
+thread, has its own *current* execution context. Multiple threads can
+use the same interpreter or different ones.
+
+At a high level, you can think of the combination of threads and
+interpreters as threads with opt-in sharing.
+
+As a significant bonus, interpreters are sufficiently isolated that
+they do not share the :term:`GIL`, which means combining threads with
+multiple interpreters enables full multi-core parallelism.
+(This has been the case since Python 3.12.)
+
+Communication Between Interpreters
+^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+
+In practice, multiple interpreters are useful only if we have a way
+to communicate between them. This usually involves some form of
+message passing, but can even mean sharing data in some carefully
+managed way.
+
+With this in mind, the :mod:`!concurrent.interpreters` module provides
+a :class:`queue.Queue` implementation, available through
+:func:`create_queue`.
+
+.. _interp-object-sharing:
+
+"Sharing" Objects
+^^^^^^^^^^^^^^^^^
+
+Any data actually shared between interpreters loses the thread-safety
+provided by the :term:`GIL`. There are various options for dealing with
+this in extension modules. However, from Python code the lack of
+thread-safety means objects can't actually be shared, with a few
+exceptions. Instead, a copy must be created, which means mutable
+objects won't stay in sync.
+
+By default, most objects are copied with :mod:`pickle` when they are
+passed to another interpreter. Nearly all of the immutable builtin
+objects are either directly shared or copied efficiently. For example:
+
+* :const:`None`
+* :class:`bool` (:const:`True` and :const:`False`)
+* :class:`bytes`
+* :class:`str`
+* :class:`int`
+* :class:`float`
+* :class:`tuple` (of similarly supported objects)
+
+There is a small number of Python types that actually share mutable
+data between interpreters:
+
+* :class:`memoryview`
+* :class:`Queue`
Reference
@@ -73,12 +219,19 @@ This module defines the following functions:
.. function:: get_main()
Return an :class:`Interpreter` object for the main interpreter.
+ This is the interpreter the runtime created to run the :term:`REPL`
+ or the script given at the command-line. It is usually the only one.
.. function:: create()
Initialize a new (idle) Python interpreter
and return a :class:`Interpreter` object for it.
+.. function:: create_queue()
+
+ Initialize a new cross-interpreter queue and return a :class:`Queue`
+ object for it.
+
Interpreter objects
^^^^^^^^^^^^^^^^^^^
@@ -94,7 +247,7 @@ Interpreter objects
(read-only)
- The interpreter's ID.
+ The underlying interpreter's ID.
.. attribute:: whence
@@ -113,8 +266,10 @@ Interpreter objects
.. method:: prepare_main(ns=None, **kwargs)
- Bind "shareable" objects in the interpreter's
- :mod:`!__main__` module.
+ Bind objects in the interpreter's :mod:`!__main__` module.
+
+ Some objects are actually shared and some are copied efficiently,
+ but most are copied via :mod:`pickle`. See :ref:`interp-object-sharing`.
.. method:: exec(code, /, dedent=True)
@@ -125,6 +280,8 @@ Interpreter objects
Return the result of calling running the given function in the
interpreter (in the current thread).
+ .. _interp-call-in-thread:
+
.. method:: call_in_thread(callable, /, *args, **kwargs)
Run the given function in the interpreter (in a new thread).
@@ -159,7 +316,36 @@ Exceptions
an object cannot be sent to another interpreter.
-.. XXX Add functions for communicating between interpreters.
+Communicating Between Interpreters
+^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+
+.. class:: Queue(id)
+
+ A wrapper around a low-level, cross-interpreter queue, which
+ implements the :class:`queue.Queue` interface. The underlying queue
+ can only be created through :func:`create_queue`.
+
+ Some objects are actually shared and some are copied efficiently,
+ but most are copied via :mod:`pickle`. See :ref:`interp-object-sharing`.
+
+ .. attribute:: id
+
+ (read-only)
+
+ The queue's ID.
+
+
+.. exception:: QueueEmptyError
+
+ This exception, a subclass of :exc:`queue.Empty`, is raised from
+ :meth:`!Queue.get` and :meth:`!Queue.get_nowait` when the queue
+ is empty.
+
+.. exception:: QueueFullError
+
+ This exception, a subclass of :exc:`queue.Full`, is raised from
+ :meth:`!Queue.put` and :meth:`!Queue.put_nowait` when the queue
+ is full.
Basic usage
@@ -184,6 +370,12 @@ Creating an interpreter and running code in it::
print('spam!')
"""))
+ def run(arg):
+ return arg
+
+ res = interp.call(run, 'spam!')
+ print(res)
+
def run():
print('spam!')
@@ -193,6 +385,3 @@ Creating an interpreter and running code in it::
t = interp.call_in_thread(run)
t.join()
-
-
-.. XXX Explain about object "sharing".
diff --git a/Doc/library/csv.rst b/Doc/library/csv.rst
index 2e513bff651..d39c4ca4a58 100644
--- a/Doc/library/csv.rst
+++ b/Doc/library/csv.rst
@@ -53,7 +53,7 @@ The :mod:`csv` module defines the following functions:
.. index::
single: universal newlines; csv.reader function
-.. function:: reader(csvfile, dialect='excel', **fmtparams)
+.. function:: reader(csvfile, /, dialect='excel', **fmtparams)
Return a :ref:`reader object <reader-objects>` that will process
lines from the given *csvfile*. A csvfile must be an iterable of
@@ -84,7 +84,7 @@ The :mod:`csv` module defines the following functions:
Spam, Lovely Spam, Wonderful Spam
-.. function:: writer(csvfile, dialect='excel', **fmtparams)
+.. function:: writer(csvfile, /, dialect='excel', **fmtparams)
Return a writer object responsible for converting the user's data into delimited
strings on the given file-like object. *csvfile* can be any object with a
@@ -323,8 +323,8 @@ The :mod:`csv` module defines the following constants:
.. data:: QUOTE_MINIMAL
Instructs :class:`writer` objects to only quote those fields which contain
- special characters such as *delimiter*, *quotechar* or any of the characters in
- *lineterminator*.
+ special characters such as *delimiter*, *quotechar*, ``'\r'``, ``'\n'``
+ or any of the characters in *lineterminator*.
.. data:: QUOTE_NONNUMERIC
@@ -342,10 +342,13 @@ The :mod:`csv` module defines the following constants:
.. data:: QUOTE_NONE
- Instructs :class:`writer` objects to never quote fields. When the current
- *delimiter* occurs in output data it is preceded by the current *escapechar*
- character. If *escapechar* is not set, the writer will raise :exc:`Error` if
+ Instructs :class:`writer` objects to never quote fields.
+ When the current *delimiter*, *quotechar*, *escapechar*, ``'\r'``, ``'\n'``
+ or any of the characters in *lineterminator* occurs in output data
+ it is preceded by the current *escapechar* character.
+ If *escapechar* is not set, the writer will raise :exc:`Error` if
any characters that require escaping are encountered.
+ Set *quotechar* to ``None`` to prevent its escaping.
Instructs :class:`reader` objects to perform no special processing of quote characters.
@@ -414,9 +417,16 @@ Dialects support the following attributes:
.. attribute:: Dialect.escapechar
- A one-character string used by the writer to escape the *delimiter* if *quoting*
- is set to :const:`QUOTE_NONE` and the *quotechar* if *doublequote* is
- :const:`False`. On reading, the *escapechar* removes any special meaning from
+ A one-character string used by the writer to escape characters that
+ require escaping:
+
+ * the *delimiter*, the *quotechar*, ``'\r'``, ``'\n'`` and any of the
+ characters in *lineterminator* are escaped if *quoting* is set to
+ :const:`QUOTE_NONE`;
+ * the *quotechar* is escaped if *doublequote* is :const:`False`;
+ * the *escapechar* itself.
+
+ On reading, the *escapechar* removes any special meaning from
the following character. It defaults to :const:`None`, which disables escaping.
.. versionchanged:: 3.11
@@ -436,9 +446,12 @@ Dialects support the following attributes:
.. attribute:: Dialect.quotechar
- A one-character string used to quote fields containing special characters, such
- as the *delimiter* or *quotechar*, or which contain new-line characters. It
- defaults to ``'"'``.
+ A one-character string used to quote fields containing special characters,
+ such as the *delimiter* or the *quotechar*, or which contain new-line
+ characters (``'\r'``, ``'\n'`` or any of the characters in *lineterminator*).
+ It defaults to ``'"'``.
+ Can be set to ``None`` to prevent escaping ``'"'`` if *quoting* is set
+ to :const:`QUOTE_NONE`.
.. versionchanged:: 3.11
An empty *quotechar* is not allowed.
@@ -447,7 +460,8 @@ Dialects support the following attributes:
Controls when quotes should be generated by the writer and recognised by the
reader. It can take on any of the :ref:`QUOTE_\* constants <csv-constants>`
- and defaults to :const:`QUOTE_MINIMAL`.
+ and defaults to :const:`QUOTE_MINIMAL` if *quotechar* is not ``None``,
+ and :const:`QUOTE_NONE` otherwise.
.. attribute:: Dialect.skipinitialspace
diff --git a/Doc/library/exceptions.rst b/Doc/library/exceptions.rst
index 9806ae80905..c09e1615a5b 100644
--- a/Doc/library/exceptions.rst
+++ b/Doc/library/exceptions.rst
@@ -429,7 +429,9 @@ The following exceptions are the exceptions that are usually raised.
* Creating a new Python thread.
* :meth:`Joining <threading.Thread.join>` a running daemon thread.
- * :func:`os.fork`.
+ * :func:`os.fork`,
+ * acquiring a lock such as :class:`threading.Lock`, when it is known that
+ the operation would otherwise deadlock.
See also the :func:`sys.is_finalizing` function.
@@ -440,6 +442,11 @@ The following exceptions are the exceptions that are usually raised.
:meth:`threading.Thread.join` can now raise this exception.
+ .. versionchanged:: next
+
+ This exception may be raised when acquiring :meth:`threading.Lock`
+ or :meth:`threading.RLock`.
+
.. exception:: RecursionError
This exception is derived from :exc:`RuntimeError`. It is raised when the
diff --git a/Doc/library/fractions.rst b/Doc/library/fractions.rst
index fc7f9a6301a..392b6d40e86 100644
--- a/Doc/library/fractions.rst
+++ b/Doc/library/fractions.rst
@@ -142,7 +142,7 @@ another rational number, or from a string.
.. versionadded:: 3.12
- .. classmethod:: from_float(flt)
+ .. classmethod:: from_float(f)
Alternative constructor which only accepts instances of
:class:`float` or :class:`numbers.Integral`. Beware that
diff --git a/Doc/library/math.rst b/Doc/library/math.rst
index 03da0e4713c..bf7a00549fc 100644
--- a/Doc/library/math.rst
+++ b/Doc/library/math.rst
@@ -804,7 +804,7 @@ Constants
The mathematical constant *τ* = 6.283185..., to available precision.
Tau is a circle constant equal to 2\ *π*, the ratio of a circle's circumference to
its radius. To learn more about Tau, check out Vi Hart's video `Pi is (still)
- Wrong <https://www.youtube.com/watch?v=jG7vhMMXagQ>`_, and start celebrating
+ Wrong <https://vimeo.com/147792667>`_, and start celebrating
`Tau day <https://tauday.com/>`_ by eating twice as much pie!
.. versionadded:: 3.6
diff --git a/Doc/library/shutil.rst b/Doc/library/shutil.rst
index 2dde40c9d92..dde38498206 100644
--- a/Doc/library/shutil.rst
+++ b/Doc/library/shutil.rst
@@ -618,7 +618,8 @@ provided. They rely on the :mod:`zipfile` and :mod:`tarfile` modules.
*format* is the archive format: one of
"zip" (if the :mod:`zlib` module is available), "tar", "gztar" (if the
:mod:`zlib` module is available), "bztar" (if the :mod:`bz2` module is
- available), or "xztar" (if the :mod:`lzma` module is available).
+ available), "xztar" (if the :mod:`lzma` module is available), or "zstdtar"
+ (if the :mod:`compression.zstd` module is available).
*root_dir* is a directory that will be the root directory of the
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.
- *gztar*: gzip'ed tar-file (if the :mod:`zlib` module is available).
- *bztar*: bzip2'ed tar-file (if the :mod:`bz2` module is available).
- *xztar*: xz'ed tar-file (if the :mod:`lzma` module is available).
+ - *zstdtar*: Zstandard compressed tar-file (if the :mod:`compression.zstd`
+ module is available).
You can register new formats or provide your own archiver for any existing
formats, by using :func:`register_archive_format`.
@@ -716,8 +719,8 @@ provided. They rely on the :mod:`zipfile` and :mod:`tarfile` modules.
*extract_dir* is the name of the target directory where the archive is
unpacked. If not provided, the current working directory is used.
- *format* is the archive format: one of "zip", "tar", "gztar", "bztar", or
- "xztar". Or any other format registered with
+ *format* is the archive format: one of "zip", "tar", "gztar", "bztar",
+ "xztar", or "zstdtar". Or any other format registered with
:func:`register_unpack_format`. If not provided, :func:`unpack_archive`
will use the archive file name extension and see if an unpacker was
registered for that extension. In case none is found,
@@ -789,6 +792,8 @@ provided. They rely on the :mod:`zipfile` and :mod:`tarfile` modules.
- *gztar*: gzip'ed tar-file (if the :mod:`zlib` module is available).
- *bztar*: bzip2'ed tar-file (if the :mod:`bz2` module is available).
- *xztar*: xz'ed tar-file (if the :mod:`lzma` module is available).
+ - *zstdtar*: Zstandard compressed tar-file (if the :mod:`compression.zstd`
+ module is available).
You can register new formats or provide your own unpacker for any existing
formats, by using :func:`register_unpack_format`.
diff --git a/Doc/library/sys.monitoring.rst b/Doc/library/sys.monitoring.rst
index 0674074b8c0..f62a4011e41 100644
--- a/Doc/library/sys.monitoring.rst
+++ b/Doc/library/sys.monitoring.rst
@@ -137,7 +137,8 @@ The following events are supported:
.. monitoring-event:: PY_UNWIND
- Exit from a Python function during exception unwinding.
+ Exit from a Python function during exception unwinding. This includes exceptions raised directly within the
+ function and that are allowed to continue to propagate.
.. monitoring-event:: PY_YIELD
@@ -171,7 +172,7 @@ events, use the expression ``PY_RETURN | PY_START``.
if get_events(DEBUGGER_ID) == NO_EVENTS:
...
-Events are divided into three groups:
+ Setting this event deactivates all events.
.. _monitoring-event-local:
@@ -243,20 +244,23 @@ raise an exception unless it would be visible to other code.
To allow tools to monitor for real exceptions without slowing down generators
and coroutines, the :monitoring-event:`STOP_ITERATION` event is provided.
-:monitoring-event:`STOP_ITERATION` can be locally disabled, unlike :monitoring-event:`RAISE`.
+:monitoring-event:`STOP_ITERATION` can be locally disabled, unlike
+:monitoring-event:`RAISE`.
-Note that the :monitoring-event:`STOP_ITERATION` event and the :monitoring-event:`RAISE`
-event for a :exc:`StopIteration` exception are equivalent, and are treated as interchangeable
-when generating events. Implementations will favor :monitoring-event:`STOP_ITERATION` for
-performance reasons, but may generate a :monitoring-event:`RAISE` event with a :exc:`StopIteration`.
+Note that the :monitoring-event:`STOP_ITERATION` event and the
+:monitoring-event:`RAISE` event for a :exc:`StopIteration` exception are
+equivalent, and are treated as interchangeable when generating events.
+Implementations will favor :monitoring-event:`STOP_ITERATION` for performance
+reasons, but may generate a :monitoring-event:`RAISE` event with a
+:exc:`StopIteration`.
Turning events on and off
-------------------------
In order to monitor an event, it must be turned on and a corresponding callback
-must be registered.
-Events can be turned on or off by setting the events either globally or
-for a particular code object.
+must be registered. Events can be turned on or off by setting the events either
+globally and/or for a particular code object. An event will trigger only once,
+even if it is turned on both globally and locally.
Setting events globally
@@ -292,10 +296,6 @@ in Python (see :ref:`c-api-monitoring`).
Activates all the local events for *code* which are set in *event_set*.
Raises a :exc:`ValueError` if *tool_id* is not in use.
-Local events add to global events, but do not mask them.
-In other words, all global events will trigger for a code object,
-regardless of the local events.
-
Disabling events
''''''''''''''''
@@ -325,8 +325,6 @@ except for a few breakpoints.
Registering callback functions
------------------------------
-To register a callable for events call
-
.. function:: register_callback(tool_id: int, event: int, func: Callable | None, /) -> Callable | None
Registers the callable *func* for the *event* with the given *tool_id*
@@ -335,12 +333,16 @@ To register a callable for events call
it is unregistered and returned.
Otherwise :func:`register_callback` returns ``None``.
-
Functions can be unregistered by calling
``sys.monitoring.register_callback(tool_id, event, None)``.
Callback functions can be registered and unregistered at any time.
+Callbacks are called only once regardless if the event is turned on both
+globally and locally. As such, if an event could be turned on for both global
+and local events by your code then the callback needs to be written to handle
+either trigger.
+
Registering or unregistering a callback function will generate a :func:`sys.audit` event.
@@ -353,37 +355,46 @@ Callback function arguments
that there are no arguments to the call.
When an active event occurs, the registered callback function is called.
+Callback functions returning an object other than :data:`DISABLE` will have no effect.
Different events will provide the callback function with different arguments, as follows:
* :monitoring-event:`PY_START` and :monitoring-event:`PY_RESUME`::
- func(code: CodeType, instruction_offset: int) -> DISABLE | Any
+ func(code: CodeType, instruction_offset: int) -> object
* :monitoring-event:`PY_RETURN` and :monitoring-event:`PY_YIELD`::
- func(code: CodeType, instruction_offset: int, retval: object) -> DISABLE | Any
+ func(code: CodeType, instruction_offset: int, retval: object) -> object
-* :monitoring-event:`CALL`, :monitoring-event:`C_RAISE` and :monitoring-event:`C_RETURN`::
+* :monitoring-event:`CALL`, :monitoring-event:`C_RAISE` and :monitoring-event:`C_RETURN`
+ (*arg0* can be :data:`MISSING` specifically)::
- func(code: CodeType, instruction_offset: int, callable: object, arg0: object | MISSING) -> DISABLE | Any
+ func(code: CodeType, instruction_offset: int, callable: object, arg0: object) -> object
+ *code* represents the code object where the call is being made, while
+ *callable* is the object that is about to be called (and thus
+ triggered the event).
If there are no arguments, *arg0* is set to :data:`sys.monitoring.MISSING`.
+ For instance methods, *callable* will be the function object as found on the
+ class with *arg0* set to the instance (i.e. the ``self`` argument to the
+ method).
+
* :monitoring-event:`RAISE`, :monitoring-event:`RERAISE`, :monitoring-event:`EXCEPTION_HANDLED`,
:monitoring-event:`PY_UNWIND`, :monitoring-event:`PY_THROW` and :monitoring-event:`STOP_ITERATION`::
- func(code: CodeType, instruction_offset: int, exception: BaseException) -> DISABLE | Any
+ func(code: CodeType, instruction_offset: int, exception: BaseException) -> object
* :monitoring-event:`LINE`::
- func(code: CodeType, line_number: int) -> DISABLE | Any
+ func(code: CodeType, line_number: int) -> object
* :monitoring-event:`BRANCH_LEFT`, :monitoring-event:`BRANCH_RIGHT` and :monitoring-event:`JUMP`::
- func(code: CodeType, instruction_offset: int, destination_offset: int) -> DISABLE | Any
+ func(code: CodeType, instruction_offset: int, destination_offset: int) -> object
Note that the *destination_offset* is where the code will next execute.
* :monitoring-event:`INSTRUCTION`::
- func(code: CodeType, instruction_offset: int) -> DISABLE | Any
+ func(code: CodeType, instruction_offset: int) -> object
diff --git a/Doc/library/tarfile.rst b/Doc/library/tarfile.rst
index 7cec108a5bd..70466fbbc4d 100644
--- a/Doc/library/tarfile.rst
+++ b/Doc/library/tarfile.rst
@@ -18,8 +18,8 @@ higher-level functions in :ref:`shutil <archiving-operations>`.
Some facts and figures:
-* reads and writes :mod:`gzip`, :mod:`bz2` and :mod:`lzma` compressed archives
- if the respective modules are available.
+* reads and writes :mod:`gzip`, :mod:`bz2`, :mod:`compression.zstd`, and
+ :mod:`lzma` compressed archives if the respective modules are available.
* read/write support for the POSIX.1-1988 (ustar) format.
@@ -47,6 +47,10 @@ Some facts and figures:
or paths outside of the destination. Previously, the filter strategy
was equivalent to :func:`fully_trusted <fully_trusted_filter>`.
+.. versionchanged:: 3.14
+
+ Added support for Zstandard compression using :mod:`compression.zstd`.
+
.. function:: open(name=None, mode='r', fileobj=None, bufsize=10240, **kwargs)
Return a :class:`TarFile` object for the pathname *name*. For detailed
@@ -71,6 +75,8 @@ Some facts and figures:
+------------------+---------------------------------------------+
| ``'r:xz'`` | Open for reading with lzma compression. |
+------------------+---------------------------------------------+
+ | ``'r:zst'`` | Open for reading with Zstandard compression.|
+ +------------------+---------------------------------------------+
| ``'x'`` or | Create a tarfile exclusively without |
| ``'x:'`` | compression. |
| | Raise a :exc:`FileExistsError` exception |
@@ -88,6 +94,10 @@ Some facts and figures:
| | Raise a :exc:`FileExistsError` exception |
| | if it already exists. |
+------------------+---------------------------------------------+
+ | ``'x:zst'`` | Create a tarfile with Zstandard compression.|
+ | | Raise a :exc:`FileExistsError` exception |
+ | | if it already exists. |
+ +------------------+---------------------------------------------+
| ``'a' or 'a:'`` | Open for appending with no compression. The |
| | file is created if it does not exist. |
+------------------+---------------------------------------------+
@@ -99,6 +109,8 @@ Some facts and figures:
+------------------+---------------------------------------------+
| ``'w:xz'`` | Open for lzma compressed writing. |
+------------------+---------------------------------------------+
+ | ``'w:zst'`` | Open for Zstandard compressed writing. |
+ +------------------+---------------------------------------------+
Note that ``'a:gz'``, ``'a:bz2'`` or ``'a:xz'`` is not possible. If *mode*
is not suitable to open a certain (compressed) file for reading,
@@ -115,6 +127,15 @@ Some facts and figures:
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.
+ For modes ``'w:zst'``, ``'x:zst'`` and ``'w|zst'``, :func:`tarfile.open`
+ accepts the keyword argument *level* to specify the compression level of
+ the file. The keyword argument *options* may also be passed, providing
+ advanced Zstandard compression parameters described by
+ :class:`~compression.zstd.CompressionParameter`. The keyword argument
+ *zstd_dict* can be passed to provide a :class:`~compression.zstd.ZstdDict`,
+ a Zstandard dictionary used to improve compression of smaller amounts of
+ data.
+
For special purposes, there is a second format for *mode*:
``'filemode|[compression]'``. :func:`tarfile.open` will return a :class:`TarFile`
object that processes its data as a stream of blocks. No random seeking will
@@ -146,6 +167,9 @@ Some facts and figures:
| ``'r|xz'`` | Open an lzma compressed *stream* for |
| | reading. |
+-------------+--------------------------------------------+
+ | ``'r|zst'`` | Open a Zstandard compressed *stream* for |
+ | | reading. |
+ +-------------+--------------------------------------------+
| ``'w|'`` | Open an uncompressed *stream* for writing. |
+-------------+--------------------------------------------+
| ``'w|gz'`` | Open a gzip compressed *stream* for |
@@ -157,6 +181,9 @@ Some facts and figures:
| ``'w|xz'`` | Open an lzma compressed *stream* for |
| | writing. |
+-------------+--------------------------------------------+
+ | ``'w|zst'`` | Open a Zstandard compressed *stream* for |
+ | | writing. |
+ +-------------+--------------------------------------------+
.. versionchanged:: 3.5
The ``'x'`` (exclusive creation) mode was added.
diff --git a/Doc/library/time.rst b/Doc/library/time.rst
index 542493a82af..29b695a9b19 100644
--- a/Doc/library/time.rst
+++ b/Doc/library/time.rst
@@ -712,13 +712,18 @@ Functions
Clock:
- * On Windows, call ``GetSystemTimeAsFileTime()``.
+ * On Windows, call ``GetSystemTimePreciseAsFileTime()``.
* Call ``clock_gettime(CLOCK_REALTIME)`` if available.
* Otherwise, call ``gettimeofday()``.
Use :func:`time_ns` to avoid the precision loss caused by the :class:`float`
type.
+.. versionchanged:: 3.13
+
+ On Windows, calls ``GetSystemTimePreciseAsFileTime()`` instead of
+ ``GetSystemTimeAsFileTime()``.
+
.. function:: time_ns() -> int
diff --git a/Doc/library/unittest.rst b/Doc/library/unittest.rst
index dcdda1719bf..d526e835caa 100644
--- a/Doc/library/unittest.rst
+++ b/Doc/library/unittest.rst
@@ -1131,7 +1131,7 @@ Test cases
.. versionchanged:: 3.3
Added the *msg* keyword argument when used as a context manager.
- .. method:: assertLogs(logger=None, level=None)
+ .. method:: assertLogs(logger=None, level=None, formatter=None)
A context manager to test that at least one message is logged on
the *logger* or one of its children, with at least the given
@@ -1146,6 +1146,10 @@ Test cases
its string equivalent (for example either ``"ERROR"`` or
:const:`logging.ERROR`). The default is :const:`logging.INFO`.
+ If given, *formatter* should be a :class:`logging.Formatter` object.
+ The default is a formatter with format string
+ ``"%(levelname)s:%(name)s:%(message)s"``
+
The test passes if at least one message emitted inside the ``with``
block matches the *logger* and *level* conditions, otherwise it fails.
@@ -1173,6 +1177,9 @@ Test cases
.. versionadded:: 3.4
+ .. versionchanged:: next
+ Now accepts a *formatter* to control how messages are formatted.
+
.. method:: assertNoLogs(logger=None, level=None)
A context manager to test that no messages are logged on
diff --git a/Doc/library/venv.rst b/Doc/library/venv.rst
index bed799aedfd..f16e24eac08 100644
--- a/Doc/library/venv.rst
+++ b/Doc/library/venv.rst
@@ -105,36 +105,52 @@ The command, if run with ``-h``, will show the available options::
Creates virtual Python environments in one or more target directories.
- positional arguments:
- ENV_DIR A directory to create the environment in.
-
- options:
- -h, --help show this help message and exit
- --system-site-packages
- Give the virtual environment access to the system
- site-packages dir.
- --symlinks Try to use symlinks rather than copies, when
- symlinks are not the default for the platform.
- --copies Try to use copies rather than symlinks, even when
- symlinks are the default for the platform.
- --clear Delete the contents of the environment directory
- if it already exists, before environment creation.
- --upgrade Upgrade the environment directory to use this
- version of Python, assuming Python has been
- upgraded in-place.
- --without-pip Skips installing or upgrading pip in the virtual
- environment (pip is bootstrapped by default)
- --prompt PROMPT Provides an alternative prompt prefix for this
- environment.
- --upgrade-deps Upgrade core dependencies (pip) to the latest
- version in PyPI
- --without-scm-ignore-files
- Skips adding SCM ignore files to the environment
- directory (Git is supported by default).
-
Once an environment has been created, you may wish to activate it, e.g. by
sourcing an activate script in its bin directory.
+.. _venv-cli:
+.. program:: venv
+
+.. option:: ENV_DIR
+
+ A required argument specifying the directory to create the environment in.
+
+.. option:: --system-site-packages
+
+ Give the virtual environment access to the system site-packages directory.
+
+.. option:: --symlinks
+
+ Try to use symlinks rather than copies, when symlinks are not the default for the platform.
+
+.. option:: --copies
+
+ Try to use copies rather than symlinks, even when symlinks are the default for the platform.
+
+.. option:: --clear
+
+ Delete the contents of the environment directory if it already exists, before environment creation.
+
+.. option:: --upgrade
+
+ Upgrade the environment directory to use this version of Python, assuming Python has been upgraded in-place.
+
+.. option:: --without-pip
+
+ Skips installing or upgrading pip in the virtual environment (pip is bootstrapped by default).
+
+.. option:: --prompt <PROMPT>
+
+ Provides an alternative prompt prefix for this environment.
+
+.. option:: --upgrade-deps
+
+ Upgrade core dependencies (pip) to the latest version in PyPI.
+
+.. option:: --without-scm-ignore-files
+
+ Skips adding SCM ignore files to the environment directory (Git is supported by default).
+
.. versionchanged:: 3.4
Installs pip by default, added the ``--without-pip`` and ``--copies``
diff --git a/Doc/library/zipfile.rst b/Doc/library/zipfile.rst
index 6a4fa67332e..bf9136a2139 100644
--- a/Doc/library/zipfile.rst
+++ b/Doc/library/zipfile.rst
@@ -129,14 +129,28 @@ The module defines the following items:
.. versionadded:: 3.3
+.. data:: ZIP_ZSTANDARD
+
+ The numeric constant for Zstandard compression. This requires the
+ :mod:`compression.zstd` module.
+
.. note::
- The ZIP file format specification has included support for bzip2 compression
- since 2001, and for LZMA compression since 2006. However, some tools
- (including older Python releases) do not support these compression
- methods, and may either refuse to process the ZIP file altogether,
- or fail to extract individual files.
+ In APPNOTE 6.3.7, the method ID ``20`` was assigned to Zstandard
+ compression. This was changed in APPNOTE 6.3.8 to method ID ``93`` to
+ avoid conflicts, with method ID ``20`` being deprecated. For
+ compatibility, the :mod:`!zipfile` module reads both method IDs but will
+ only write data with method ID ``93``.
+
+ .. versionadded:: 3.14
+
+.. note::
+ The ZIP file format specification has included support for bzip2 compression
+ since 2001, for LZMA compression since 2006, and Zstandard compression since
+ 2020. However, some tools (including older Python releases) do not support
+ these compression methods, and may either refuse to process the ZIP file
+ altogether, or fail to extract individual files.
.. seealso::
@@ -176,10 +190,11 @@ ZipFile Objects
*compression* is the ZIP compression method to use when writing the archive,
and should be :const:`ZIP_STORED`, :const:`ZIP_DEFLATED`,
- :const:`ZIP_BZIP2` or :const:`ZIP_LZMA`; unrecognized
- values will cause :exc:`NotImplementedError` to be raised. If
- :const:`ZIP_DEFLATED`, :const:`ZIP_BZIP2` or :const:`ZIP_LZMA` is specified
- but the corresponding module (:mod:`zlib`, :mod:`bz2` or :mod:`lzma`) is not
+ :const:`ZIP_BZIP2`, :const:`ZIP_LZMA`, or :const:`ZIP_ZSTANDARD`;
+ unrecognized values will cause :exc:`NotImplementedError` to be raised. If
+ :const:`ZIP_DEFLATED`, :const:`ZIP_BZIP2`, :const:`ZIP_LZMA`, or
+ :const:`ZIP_ZSTANDARD` is specified but the corresponding module
+ (:mod:`zlib`, :mod:`bz2`, :mod:`lzma`, or :mod:`compression.zstd`) is not
available, :exc:`RuntimeError` is raised. The default is :const:`ZIP_STORED`.
If *allowZip64* is ``True`` (the default) zipfile will create ZIP files that
@@ -194,6 +209,10 @@ ZipFile Objects
(see :class:`zlib <zlib.compressobj>` for more information).
When using :const:`ZIP_BZIP2` integers ``1`` through ``9`` are accepted
(see :class:`bz2 <bz2.BZ2File>` for more information).
+ When using :const:`ZIP_ZSTANDARD` integers ``-131072`` through ``22`` are
+ commonly accepted (see
+ :attr:`CompressionParameter.compression_level <compression.zstd.CompressionParameter.compression_level>`
+ for more on retrieving valid values and their meaning).
The *strict_timestamps* argument, when set to ``False``, allows to
zip files older than 1980-01-01 at the cost of setting the
@@ -415,9 +434,10 @@ ZipFile Objects
read or append. *pwd* is the password used for encrypted files as a :class:`bytes`
object and, if specified, overrides the default password set with :meth:`setpassword`.
Calling :meth:`read` on a ZipFile that uses a compression method other than
- :const:`ZIP_STORED`, :const:`ZIP_DEFLATED`, :const:`ZIP_BZIP2` or
- :const:`ZIP_LZMA` will raise a :exc:`NotImplementedError`. An error will also
- be raised if the corresponding compression module is not available.
+ :const:`ZIP_STORED`, :const:`ZIP_DEFLATED`, :const:`ZIP_BZIP2`,
+ :const:`ZIP_LZMA`, or :const:`ZIP_ZSTANDARD` will raise a
+ :exc:`NotImplementedError`. An error will also be raised if the
+ corresponding compression module is not available.
.. versionchanged:: 3.6
Calling :meth:`read` on a closed ZipFile will raise a :exc:`ValueError`.