Skip to content

Commit ef10d9e

Browse files
Merge branch 'main' into sqlite-changes-alt
2 parents 731dfc5 + 5849af7 commit ef10d9e

Some content is hidden

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

53 files changed

+863
-108
lines changed

Doc/conf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
highlight_language = 'python3'
4646

4747
# Minimum version of sphinx required
48-
needs_sphinx = '1.8'
48+
needs_sphinx = '3.2'
4949

5050
# Ignore any .rst files in the venv/ directory.
5151
exclude_patterns = ['venv/*', 'README.rst']

Doc/howto/logging-cookbook.rst

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -714,6 +714,32 @@ which, when run, produces something like:
714714
2010-09-06 22:38:15,301 d.e.f DEBUG IP: 123.231.231.123 User: fred A message at DEBUG level with 2 parameters
715715
2010-09-06 22:38:15,301 d.e.f INFO IP: 123.231.231.123 User: fred A message at INFO level with 2 parameters
716716
717+
Imparting contextual information in handlers
718+
--------------------------------------------
719+
720+
Each :class:`~Handler` has its own chain of filters.
721+
If you want to add contextual information to a :class:`LogRecord` without leaking
722+
it to other handlers, you can use a filter that returns
723+
a new :class:`~LogRecord` instead of modifying it in-place, as shown in the following script::
724+
725+
import copy
726+
import logging
727+
728+
def filter(record: logging.LogRecord):
729+
record = copy.copy(record)
730+
record.user = 'jim'
731+
return record
732+
733+
if __name__ == '__main__':
734+
logger = logging.getLogger()
735+
logger.setLevel(logging.INFO)
736+
handler = logging.StreamHandler()
737+
formatter = logging.Formatter('%(message)s from %(user)-8s')
738+
handler.setFormatter(formatter)
739+
handler.addFilter(filter)
740+
logger.addHandler(handler)
741+
742+
logger.info('A log message')
717743

718744
.. _multiple-processes:
719745

Doc/library/asyncio-task.rst

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,24 @@ Creating Tasks
226226
.. important::
227227

228228
Save a reference to the result of this function, to avoid
229-
a task disappearing mid execution.
229+
a task disappearing mid execution. The event loop only keeps
230+
weak references to tasks. A task that isn't referenced elsewhere
231+
may get garbage-collected at any time, even before it's done.
232+
For reliable "fire-and-forget" background tasks, gather them in
233+
a collection::
234+
235+
background_tasks = set()
236+
237+
for i in range(10):
238+
task = asyncio.create_task(some_coro(param=i))
239+
240+
# Add task to the set. This creates a strong reference.
241+
background_tasks.add(task)
242+
243+
# To prevent keeping references to finished tasks forever,
244+
# make each task remove its own reference from the set after
245+
# completion:
246+
task.add_done_callback(background_tasks.discard)
230247

231248
.. versionadded:: 3.7
232249

Doc/library/fcntl.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@ descriptor.
5050
constants, which allow to duplicate a file descriptor, the latter setting
5151
``FD_CLOEXEC`` flag in addition.
5252

53+
.. versionchanged:: 3.12
54+
On Linux >= 4.5, the :mod:`fcntl` module exposes the ``FICLONE`` and
55+
``FICLONERANGE`` constants, which allow to share some data of one file with
56+
another file by reflinking on some filesystems (e.g., btrfs, OCFS2, and
57+
XFS). This behavior is commonly referred to as "copy-on-write".
58+
5359
The module defines the following functions:
5460

5561

Doc/library/logging.config.rst

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -661,6 +661,76 @@ it with :func:`staticmethod`. For example::
661661
You don't need to wrap with :func:`staticmethod` if you're setting the import
662662
callable on a configurator *instance*.
663663

664+
.. _configure-queue:
665+
666+
Configuring QueueHandler and QueueListener
667+
""""""""""""""""""""""""""""""""""""""""""
668+
669+
If you want to configure a :class:`~logging.handlers.QueueHandler`, noting that this
670+
is normally used in conjunction with a :class:`~logging.handlers.QueueListener`, you
671+
can configure both together. After the configuration, the ``QueueListener`` instance
672+
will be available as the :attr:`~logging.handlers.QueueHandler.listener` attribute of
673+
the created handler, and that in turn will be available to you using
674+
:func:`~logging.getHandlerByName` and passing the name you have used for the
675+
``QueueHandler`` in your configuration. The dictionary schema for configuring the pair
676+
is shown in the example YAML snippet below.
677+
678+
.. code-block:: yaml
679+
680+
handlers:
681+
qhand:
682+
class: logging.handlers.QueueHandler
683+
queue: my.module.queue_factory
684+
listener: my.package.CustomListener
685+
handlers:
686+
- hand_name_1
687+
- hand_name_2
688+
...
689+
690+
The ``queue`` and ``listener`` keys are optional.
691+
692+
If the ``queue`` key is present, the corresponding value can be one of the following:
693+
694+
* An actual instance of :class:`queue.Queue` or a subclass thereof. This is of course
695+
only possible if you are constructing or modifying the configuration dictionary in
696+
code.
697+
698+
* A string that resolves to a callable which, when called with no arguments, returns
699+
the :class:`queue.Queue` instance to use. That callable could be a
700+
:class:`queue.Queue` subclass or a function which returns a suitable queue instance,
701+
such as ``my.module.queue_factory()``.
702+
703+
* A dict with a ``'()'`` key which is constructed in the usual way as discussed in
704+
:ref:`logging-config-dict-userdef`. The result of this construction should be a
705+
:class:`queue.Queue` instance.
706+
707+
If the ``queue`` key is absent, a standard unbounded :class:`queue.Queue` instance is
708+
created and used.
709+
710+
If the ``listener`` key is present, the corresponding value can be one of the following:
711+
712+
* A subclass of :class:`logging.handlers.QueueListener`. This is of course only
713+
possible if you are constructing or modifying the configuration dictionary in
714+
code.
715+
716+
* A string which resolves to a class which is a subclass of ``QueueListener``, such as
717+
``'my.package.CustomListener'``.
718+
719+
* A dict with a ``'()'`` key which is constructed in the usual way as discussed in
720+
:ref:`logging-config-dict-userdef`. The result of this construction should be a
721+
callable with the same signature as the ``QueueListener`` initializer.
722+
723+
If the ``listener`` key is absent, :class:`logging.handlers.QueueListener` is used.
724+
725+
The values under the ``handlers`` key are the names of other handlers in the
726+
configuration (not shown in the above snippet) which will be passed to the queue
727+
listener.
728+
729+
Any custom queue handler and listener classes will need to be defined with the same
730+
initialization signatures as :class:`~logging.handlers.QueueHandler` and
731+
:class:`~logging.handlers.QueueListener`.
732+
733+
.. versionadded:: 3.12
664734

665735
.. _logging-config-fileformat:
666736

Doc/library/logging.handlers.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1051,7 +1051,13 @@ possible, while any potentially slow operations (such as sending an email via
10511051
want to override this if you want to use blocking behaviour, or a
10521052
timeout, or a customized queue implementation.
10531053

1054+
.. attribute:: listener
10541055

1056+
When created via configuration using :func:`~logging.config.dictConfig`, this
1057+
attribute will contain a :class:`QueueListener` instance for use with this
1058+
handler. Otherwise, it will be ``None``.
1059+
1060+
.. versionadded:: 3.12
10551061

10561062
.. _queue-listener:
10571063

Doc/library/logging.rst

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -666,9 +666,10 @@ empty string, all events are passed.
666666

667667
.. method:: filter(record)
668668

669-
Is the specified record to be logged? Returns zero for no, nonzero for
670-
yes. If deemed appropriate, the record may be modified in-place by this
671-
method.
669+
Is the specified record to be logged? Returns falsy for no, truthy for
670+
yes. Filters can either modify log records in-place or return a completely
671+
different record instance which will replace the original
672+
log record in any future processing of the event.
672673

673674
Note that filters attached to handlers are consulted before an event is
674675
emitted by the handler, whereas filters attached to loggers are consulted
@@ -690,6 +691,12 @@ which has a ``filter`` method with the same semantics.
690691
parameter. The returned value should conform to that returned by
691692
:meth:`~Filter.filter`.
692693

694+
.. versionchanged:: 3.12
695+
You can now return a :class:`LogRecord` instance from filters to replace
696+
the log record rather than modifying it in place. This allows filters attached to
697+
a :class:`Handler` to modify the log record before it is emitted, without
698+
having side effects on other handlers.
699+
693700
Although filters are used primarily to filter records based on more
694701
sophisticated criteria than levels, they get to see every record which is
695702
processed by the handler or logger they're attached to: this can be useful if
@@ -1164,6 +1171,19 @@ functions.
11641171
This undocumented behaviour was considered a mistake, and was removed in
11651172
Python 3.4, but reinstated in 3.4.2 due to retain backward compatibility.
11661173

1174+
.. function:: getHandlerByName(name)
1175+
1176+
Returns a handler with the specified *name*, or ``None`` if there is no handler
1177+
with that name.
1178+
1179+
.. versionadded:: 3.12
1180+
1181+
.. function:: getHandlerNames()
1182+
1183+
Returns an immutable set of all known handler names.
1184+
1185+
.. versionadded:: 3.12
1186+
11671187
.. function:: makeLogRecord(attrdict)
11681188

11691189
Creates and returns a new :class:`LogRecord` instance whose attributes are

Doc/library/pathlib.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1265,7 +1265,7 @@ Below is a table mapping various :mod:`os` functions to their corresponding
12651265
:func:`os.link` :meth:`Path.hardlink_to`
12661266
:func:`os.symlink` :meth:`Path.symlink_to`
12671267
:func:`os.readlink` :meth:`Path.readlink`
1268-
:func:`os.path.relpath` :meth:`Path.relative_to` [#]_
1268+
:func:`os.path.relpath` :meth:`PurePath.relative_to` [#]_
12691269
:func:`os.stat` :meth:`Path.stat`,
12701270
:meth:`Path.owner`,
12711271
:meth:`Path.group`
@@ -1280,4 +1280,4 @@ Below is a table mapping various :mod:`os` functions to their corresponding
12801280
.. rubric:: Footnotes
12811281

12821282
.. [#] :func:`os.path.abspath` normalizes the resulting path, which may change its meaning in the presence of symlinks, while :meth:`Path.absolute` does not.
1283-
.. [#] :meth:`Path.relative_to` requires ``self`` to be the subpath of the argument, but :func:`os.path.relpath` does not.
1283+
.. [#] :meth:`PurePath.relative_to` requires ``self`` to be the subpath of the argument, but :func:`os.path.relpath` does not.

Doc/library/sqlite3.rst

Lines changed: 61 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -142,12 +142,22 @@ Module functions and constants
142142
The version number of this module, as a string. This is not the version of
143143
the SQLite library.
144144

145+
.. deprecated-removed:: 3.12 3.14
146+
This constant used to reflect the version number of the ``pysqlite``
147+
package, a third-party library which used to upstream changes to
148+
``sqlite3``. Today, it carries no meaning or practical value.
149+
145150

146151
.. data:: version_info
147152

148153
The version number of this module, as a tuple of integers. This is not the
149154
version of the SQLite library.
150155

156+
.. deprecated-removed:: 3.12 3.14
157+
This constant used to reflect the version number of the ``pysqlite``
158+
package, a third-party library which used to upstream changes to
159+
``sqlite3``. Today, it carries no meaning or practical value.
160+
151161

152162
.. data:: sqlite_version
153163

@@ -581,7 +591,7 @@ Connection Objects
581591
method with :const:`None` for *handler*.
582592

583593
Returning a non-zero value from the handler function will terminate the
584-
currently executing query and cause it to raise an :exc:`OperationalError`
594+
currently executing query and cause it to raise a :exc:`DatabaseError`
585595
exception.
586596

587597

@@ -813,7 +823,7 @@ Connection Objects
813823
*name*, and reopen *name* as an in-memory database based on the
814824
serialization contained in *data*. Deserialization will raise
815825
:exc:`OperationalError` if the database connection is currently involved
816-
in a read transaction or a backup operation. :exc:`DataError` will be
826+
in a read transaction or a backup operation. :exc:`OverflowError` will be
817827
raised if ``len(data)`` is larger than ``2**63 - 1``, and
818828
:exc:`DatabaseError` will be raised if *data* does not contain a valid
819829
SQLite database.
@@ -844,7 +854,7 @@ Cursor Objects
844854
:ref:`placeholders <sqlite3-placeholders>`.
845855

846856
:meth:`execute` will only execute a single SQL statement. If you try to execute
847-
more than one statement with it, it will raise a :exc:`.Warning`. Use
857+
more than one statement with it, it will raise a :exc:`ProgrammingError`. Use
848858
:meth:`executescript` if you want to execute multiple SQL statements with one
849859
call.
850860

@@ -1098,14 +1108,20 @@ Blob Objects
10981108
Exceptions
10991109
----------
11001110

1111+
The exception hierarchy is defined by the DB-API 2.0 (:pep:`249`).
1112+
11011113
.. exception:: Warning
11021114

1103-
A subclass of :exc:`Exception`.
1115+
This exception is not currently raised by the ``sqlite3`` module,
1116+
but may be raised by applications using ``sqlite3``,
1117+
for example if a user-defined function truncates data while inserting.
1118+
``Warning`` is a subclass of :exc:`Exception`.
11041119

11051120
.. exception:: Error
11061121

1107-
The base class of the other exceptions in this module. It is a subclass
1108-
of :exc:`Exception`.
1122+
The base class of the other exceptions in this module.
1123+
Use this to catch all errors with one single :keyword:`except` statement.
1124+
``Error`` is a subclass of :exc:`Exception`.
11091125

11101126
.. attribute:: sqlite_errorcode
11111127

@@ -1121,34 +1137,60 @@ Exceptions
11211137

11221138
.. versionadded:: 3.11
11231139

1140+
.. exception:: InterfaceError
1141+
1142+
Exception raised for misuse of the low-level SQLite C API.
1143+
In other words, if this exception is raised, it probably indicates a bug in the
1144+
``sqlite3`` module.
1145+
``InterfaceError`` is a subclass of :exc:`Error`.
1146+
11241147
.. exception:: DatabaseError
11251148

11261149
Exception raised for errors that are related to the database.
1150+
This serves as the base exception for several types of database errors.
1151+
It is only raised implicitly through the specialised subclasses.
1152+
``DatabaseError`` is a subclass of :exc:`Error`.
1153+
1154+
.. exception:: DataError
1155+
1156+
Exception raised for errors caused by problems with the processed data,
1157+
like numeric values out of range, and strings which are too long.
1158+
``DataError`` is a subclass of :exc:`DatabaseError`.
1159+
1160+
.. exception:: OperationalError
1161+
1162+
Exception raised for errors that are related to the database's operation,
1163+
and not necessarily under the control of the programmer.
1164+
For example, the database path is not found,
1165+
or a transaction could not be processed.
1166+
``OperationalError`` is a subclass of :exc:`DatabaseError`.
11271167

11281168
.. exception:: IntegrityError
11291169

11301170
Exception raised when the relational integrity of the database is affected,
11311171
e.g. a foreign key check fails. It is a subclass of :exc:`DatabaseError`.
11321172

1133-
.. exception:: ProgrammingError
1173+
.. exception:: InternalError
11341174

1135-
Exception raised for programming errors, e.g. table not found or already
1136-
exists, syntax error in the SQL statement, wrong number of parameters
1137-
specified, etc. It is a subclass of :exc:`DatabaseError`.
1175+
Exception raised when SQLite encounters an internal error.
1176+
If this is raised, it may indicate that there is a problem with the runtime
1177+
SQLite library.
1178+
``InternalError`` is a subclass of :exc:`DatabaseError`.
11381179

1139-
.. exception:: OperationalError
1180+
.. exception:: ProgrammingError
11401181

1141-
Exception raised for errors that are related to the database's operation
1142-
and not necessarily under the control of the programmer, e.g. an unexpected
1143-
disconnect occurs, the data source name is not found, a transaction could
1144-
not be processed, etc. It is a subclass of :exc:`DatabaseError`.
1182+
Exception raised for ``sqlite3`` API programming errors,
1183+
for example supplying the wrong number of bindings to a query,
1184+
or trying to operate on a closed :class:`Connection`.
1185+
``ProgrammingError`` is a subclass of :exc:`DatabaseError`.
11451186

11461187
.. exception:: NotSupportedError
11471188

1148-
Exception raised in case a method or database API was used which is not
1149-
supported by the database, e.g. calling the :meth:`~Connection.rollback`
1150-
method on a connection that does not support transaction or has
1151-
transactions turned off. It is a subclass of :exc:`DatabaseError`.
1189+
Exception raised in case a method or database API is not supported by the
1190+
underlying SQLite library. For example, setting *deterministic* to
1191+
:const:`True` in :meth:`~Connection.create_function`, if the underlying SQLite library
1192+
does not support deterministic functions.
1193+
``NotSupportedError`` is a subclass of :exc:`DatabaseError`.
11521194

11531195

11541196
.. _sqlite3-blob-objects:

Doc/whatsnew/3.11.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -736,7 +736,7 @@ For major changes, see :ref:`new-feat-related-type-hints-311`.
736736
(Contributed by Jelle Zijlstra in :gh:`90638`.)
737737

738738
* :data:`typing.TypedDict` types can now be generic. (Contributed by
739-
Samodya Abey in :gh:`89026`.)
739+
Samodya Abeysiriwardane in :gh:`89026`.)
740740

741741
* :class:`~typing.NamedTuple` types can now be generic.
742742
(Contributed by Serhiy Storchaka in :issue:`43923`.)

Include/internal/pycore_code.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ typedef struct {
6565
typedef struct {
6666
_Py_CODEUNIT counter;
6767
_Py_CODEUNIT type_version[2];
68-
_Py_CODEUNIT dict_offset;
6968
_Py_CODEUNIT keys_version[2];
7069
_Py_CODEUNIT descr[4];
7170
} _PyLoadMethodCache;

0 commit comments

Comments
 (0)