Skip to content

Commit 0a33be0

Browse files
authored
Merge branch 'main' into exc-handler-context
2 parents 1417317 + 9f18147 commit 0a33be0

Some content is hidden

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

46 files changed

+28394
-27458
lines changed

Doc/library/itertools.rst

+28-6
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ loops that truncate the stream.
314314

315315
def count(start=0, step=1):
316316
# count(10) --> 10 11 12 13 14 ...
317-
# count(2.5, 0.5) -> 2.5 3.0 3.5 ...
317+
# count(2.5, 0.5) --> 2.5 3.0 3.5 ...
318318
n = start
319319
while True:
320320
yield n
@@ -739,7 +739,7 @@ which incur interpreter overhead.
739739

740740
def prepend(value, iterator):
741741
"Prepend a single value in front of an iterator"
742-
# prepend(1, [2, 3, 4]) -> 1 2 3 4
742+
# prepend(1, [2, 3, 4]) --> 1 2 3 4
743743
return chain([value], iterator)
744744

745745
def tabulate(function, start=0):
@@ -812,6 +812,16 @@ which incur interpreter overhead.
812812
for k in range(len(roots) + 1)
813813
]
814814

815+
def sieve(n):
816+
"Primes less than n"
817+
# sieve(30) --> 2 3 5 7 11 13 17 19 23 29
818+
data = bytearray([1]) * n
819+
data[:2] = 0, 0
820+
limit = math.isqrt(n) + 1
821+
for p in compress(count(), islice(data, limit)):
822+
data[p+p : n : p] = bytearray(len(range(p+p, n, p)))
823+
return compress(count(), data)
824+
815825
def flatten(list_of_lists):
816826
"Flatten one level of nesting"
817827
return chain.from_iterable(list_of_lists)
@@ -842,12 +852,12 @@ which incur interpreter overhead.
842852
843853
def triplewise(iterable):
844854
"Return overlapping triplets from an iterable"
845-
# triplewise('ABCDEFG') -> ABC BCD CDE DEF EFG
855+
# triplewise('ABCDEFG') --> ABC BCD CDE DEF EFG
846856
for (a, _), (b, c) in pairwise(pairwise(iterable)):
847857
yield a, b, c
848858

849859
def sliding_window(iterable, n):
850-
# sliding_window('ABCDEFG', 4) -> ABCD BCDE CDEF DEFG
860+
# sliding_window('ABCDEFG', 4) --> ABCD BCDE CDEF DEFG
851861
it = iter(iterable)
852862
window = collections.deque(islice(it, n), maxlen=n)
853863
if len(window) == n:
@@ -1079,6 +1089,7 @@ which incur interpreter overhead.
10791089
>>> import operator
10801090
>>> import collections
10811091
>>> import math
1092+
>>> import random
10821093

10831094
>>> take(10, count())
10841095
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
@@ -1128,7 +1139,6 @@ which incur interpreter overhead.
11281139
>>> list(repeatfunc(pow, 5, 2, 3))
11291140
[8, 8, 8, 8, 8]
11301141

1131-
>>> import random
11321142
>>> take(5, map(int, repeatfunc(random.random)))
11331143
[0, 0, 0, 0, 0]
11341144

@@ -1156,10 +1166,22 @@ which incur interpreter overhead.
11561166
>>> all(factored(x) == expanded(x) for x in range(-10, 11))
11571167
True
11581168

1169+
>>> list(sieve(30))
1170+
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29]
1171+
>>> len(list(sieve(100)))
1172+
25
1173+
>>> len(list(sieve(1_000)))
1174+
168
1175+
>>> len(list(sieve(10_000)))
1176+
1229
1177+
>>> len(list(sieve(100_000)))
1178+
9592
1179+
>>> len(list(sieve(1_000_000)))
1180+
78498
1181+
11591182
>>> list(flatten([('a', 'b'), (), ('c', 'd', 'e'), ('f',), ('g', 'h', 'i')]))
11601183
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i']
11611184

1162-
>>> import random
11631185
>>> random.seed(85753098575309)
11641186
>>> list(repeatfunc(random.random, 3))
11651187
[0.16370491282496968, 0.45889608687313455, 0.3747076837820118]

Doc/library/sqlite3.rst

+96-97
Original file line numberDiff line numberDiff line change
@@ -570,103 +570,6 @@ Connection objects
570570

571571
An SQLite database connection has the following attributes and methods:
572572

573-
.. attribute:: isolation_level
574-
575-
This attribute controls the :ref:`transaction handling
576-
<sqlite3-controlling-transactions>` performed by :mod:`!sqlite3`.
577-
If set to ``None``, transactions are never implicitly opened.
578-
If set to one of ``"DEFERRED"``, ``"IMMEDIATE"``, or ``"EXCLUSIVE"``,
579-
corresponding to the underlying `SQLite transaction behaviour`_,
580-
implicit :ref:`transaction management
581-
<sqlite3-controlling-transactions>` is performed.
582-
583-
If not overridden by the *isolation_level* parameter of :func:`connect`,
584-
the default is ``""``, which is an alias for ``"DEFERRED"``.
585-
586-
.. attribute:: in_transaction
587-
588-
This read-only attribute corresponds to the low-level SQLite
589-
`autocommit mode`_.
590-
591-
``True`` if a transaction is active (there are uncommitted changes),
592-
``False`` otherwise.
593-
594-
.. versionadded:: 3.2
595-
596-
.. attribute:: row_factory
597-
598-
A callable that accepts two arguments,
599-
a :class:`Cursor` object and the raw row results as a :class:`tuple`,
600-
and returns a custom object representing an SQLite row.
601-
602-
Example:
603-
604-
.. doctest::
605-
606-
>>> def dict_factory(cursor, row):
607-
... col_names = [col[0] for col in cursor.description]
608-
... return {key: value for key, value in zip(col_names, row)}
609-
>>> con = sqlite3.connect(":memory:")
610-
>>> con.row_factory = dict_factory
611-
>>> for row in con.execute("SELECT 1 AS a, 2 AS b"):
612-
... print(row)
613-
{'a': 1, 'b': 2}
614-
615-
If returning a tuple doesn't suffice and you want name-based access to
616-
columns, you should consider setting :attr:`row_factory` to the
617-
highly optimized :class:`sqlite3.Row` type. :class:`Row` provides both
618-
index-based and case-insensitive name-based access to columns with almost no
619-
memory overhead. It will probably be better than your own custom
620-
dictionary-based approach or even a db_row based solution.
621-
622-
.. XXX what's a db_row-based solution?
623-
624-
.. attribute:: text_factory
625-
626-
A callable that accepts a :class:`bytes` parameter and returns a text
627-
representation of it.
628-
The callable is invoked for SQLite values with the ``TEXT`` data type.
629-
By default, this attribute is set to :class:`str`.
630-
If you want to return ``bytes`` instead, set *text_factory* to ``bytes``.
631-
632-
Example:
633-
634-
.. testcode::
635-
636-
con = sqlite3.connect(":memory:")
637-
cur = con.cursor()
638-
639-
AUSTRIA = "Österreich"
640-
641-
# by default, rows are returned as str
642-
cur.execute("SELECT ?", (AUSTRIA,))
643-
row = cur.fetchone()
644-
assert row[0] == AUSTRIA
645-
646-
# but we can make sqlite3 always return bytestrings ...
647-
con.text_factory = bytes
648-
cur.execute("SELECT ?", (AUSTRIA,))
649-
row = cur.fetchone()
650-
assert type(row[0]) is bytes
651-
# the bytestrings will be encoded in UTF-8, unless you stored garbage in the
652-
# database ...
653-
assert row[0] == AUSTRIA.encode("utf-8")
654-
655-
# we can also implement a custom text_factory ...
656-
# here we implement one that appends "foo" to all strings
657-
con.text_factory = lambda x: x.decode("utf-8") + "foo"
658-
cur.execute("SELECT ?", ("bar",))
659-
row = cur.fetchone()
660-
assert row[0] == "barfoo"
661-
662-
con.close()
663-
664-
.. attribute:: total_changes
665-
666-
Return the total number of database rows that have been modified, inserted, or
667-
deleted since the database connection was opened.
668-
669-
670573
.. method:: cursor(factory=Cursor)
671574

672575
Create and return a :class:`Cursor` object.
@@ -1320,6 +1223,102 @@ Connection objects
13201223

13211224
.. versionadded:: 3.11
13221225

1226+
.. attribute:: in_transaction
1227+
1228+
This read-only attribute corresponds to the low-level SQLite
1229+
`autocommit mode`_.
1230+
1231+
``True`` if a transaction is active (there are uncommitted changes),
1232+
``False`` otherwise.
1233+
1234+
.. versionadded:: 3.2
1235+
1236+
.. attribute:: isolation_level
1237+
1238+
This attribute controls the :ref:`transaction handling
1239+
<sqlite3-controlling-transactions>` performed by :mod:`!sqlite3`.
1240+
If set to ``None``, transactions are never implicitly opened.
1241+
If set to one of ``"DEFERRED"``, ``"IMMEDIATE"``, or ``"EXCLUSIVE"``,
1242+
corresponding to the underlying `SQLite transaction behaviour`_,
1243+
implicit :ref:`transaction management
1244+
<sqlite3-controlling-transactions>` is performed.
1245+
1246+
If not overridden by the *isolation_level* parameter of :func:`connect`,
1247+
the default is ``""``, which is an alias for ``"DEFERRED"``.
1248+
1249+
.. attribute:: row_factory
1250+
1251+
A callable that accepts two arguments,
1252+
a :class:`Cursor` object and the raw row results as a :class:`tuple`,
1253+
and returns a custom object representing an SQLite row.
1254+
1255+
Example:
1256+
1257+
.. doctest::
1258+
1259+
>>> def dict_factory(cursor, row):
1260+
... col_names = [col[0] for col in cursor.description]
1261+
... return {key: value for key, value in zip(col_names, row)}
1262+
>>> con = sqlite3.connect(":memory:")
1263+
>>> con.row_factory = dict_factory
1264+
>>> for row in con.execute("SELECT 1 AS a, 2 AS b"):
1265+
... print(row)
1266+
{'a': 1, 'b': 2}
1267+
1268+
If returning a tuple doesn't suffice and you want name-based access to
1269+
columns, you should consider setting :attr:`row_factory` to the
1270+
highly optimized :class:`sqlite3.Row` type. :class:`Row` provides both
1271+
index-based and case-insensitive name-based access to columns with almost no
1272+
memory overhead. It will probably be better than your own custom
1273+
dictionary-based approach or even a db_row based solution.
1274+
1275+
.. XXX what's a db_row-based solution?
1276+
1277+
.. attribute:: text_factory
1278+
1279+
A callable that accepts a :class:`bytes` parameter and returns a text
1280+
representation of it.
1281+
The callable is invoked for SQLite values with the ``TEXT`` data type.
1282+
By default, this attribute is set to :class:`str`.
1283+
If you want to return ``bytes`` instead, set *text_factory* to ``bytes``.
1284+
1285+
Example:
1286+
1287+
.. testcode::
1288+
1289+
con = sqlite3.connect(":memory:")
1290+
cur = con.cursor()
1291+
1292+
AUSTRIA = "Österreich"
1293+
1294+
# by default, rows are returned as str
1295+
cur.execute("SELECT ?", (AUSTRIA,))
1296+
row = cur.fetchone()
1297+
assert row[0] == AUSTRIA
1298+
1299+
# but we can make sqlite3 always return bytestrings ...
1300+
con.text_factory = bytes
1301+
cur.execute("SELECT ?", (AUSTRIA,))
1302+
row = cur.fetchone()
1303+
assert type(row[0]) is bytes
1304+
# the bytestrings will be encoded in UTF-8, unless you stored garbage in the
1305+
# database ...
1306+
assert row[0] == AUSTRIA.encode("utf-8")
1307+
1308+
# we can also implement a custom text_factory ...
1309+
# here we implement one that appends "foo" to all strings
1310+
con.text_factory = lambda x: x.decode("utf-8") + "foo"
1311+
cur.execute("SELECT ?", ("bar",))
1312+
row = cur.fetchone()
1313+
assert row[0] == "barfoo"
1314+
1315+
con.close()
1316+
1317+
.. attribute:: total_changes
1318+
1319+
Return the total number of database rows that have been modified, inserted, or
1320+
deleted since the database connection was opened.
1321+
13231322

13241323
.. _sqlite3-cursor-objects:
13251324

Doc/library/stdtypes.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ Notes:
353353
The numeric literals accepted include the digits ``0`` to ``9`` or any
354354
Unicode equivalent (code points with the ``Nd`` property).
355355

356-
See https://www.unicode.org/Public/14.0.0/ucd/extracted/DerivedNumericType.txt
356+
See https://www.unicode.org/Public/15.0.0/ucd/extracted/DerivedNumericType.txt
357357
for a complete list of code points with the ``Nd`` property.
358358

359359

Doc/library/sys.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -1331,7 +1331,7 @@ always available.
13311331

13321332
.. availability:: Unix.
13331333

1334-
.. function:: set_int_max_str_digits(n)
1334+
.. function:: set_int_max_str_digits(maxdigits)
13351335

13361336
Set the :ref:`integer string conversion length limitation
13371337
<int_max_str_digits>` used by this interpreter. See also

Doc/library/unicodedata.rst

+4-4
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717

1818
This module provides access to the Unicode Character Database (UCD) which
1919
defines character properties for all Unicode characters. The data contained in
20-
this database is compiled from the `UCD version 14.0.0
21-
<https://www.unicode.org/Public/14.0.0/ucd>`_.
20+
this database is compiled from the `UCD version 15.0.0
21+
<https://www.unicode.org/Public/15.0.0/ucd>`_.
2222

2323
The module uses the same names and symbols as defined by Unicode
2424
Standard Annex #44, `"Unicode Character Database"
@@ -175,6 +175,6 @@ Examples:
175175

176176
.. rubric:: Footnotes
177177

178-
.. [#] https://www.unicode.org/Public/14.0.0/ucd/NameAliases.txt
178+
.. [#] https://www.unicode.org/Public/15.0.0/ucd/NameAliases.txt
179179
180-
.. [#] https://www.unicode.org/Public/14.0.0/ucd/NamedSequences.txt
180+
.. [#] https://www.unicode.org/Public/15.0.0/ucd/NamedSequences.txt

Doc/reference/lexical_analysis.rst

+4-4
Original file line numberDiff line numberDiff line change
@@ -315,16 +315,16 @@ The Unicode category codes mentioned above stand for:
315315
* *Nd* - decimal numbers
316316
* *Pc* - connector punctuations
317317
* *Other_ID_Start* - explicit list of characters in `PropList.txt
318-
<https://www.unicode.org/Public/14.0.0/ucd/PropList.txt>`_ to support backwards
318+
<https://www.unicode.org/Public/15.0.0/ucd/PropList.txt>`_ to support backwards
319319
compatibility
320320
* *Other_ID_Continue* - likewise
321321

322322
All identifiers are converted into the normal form NFKC while parsing; comparison
323323
of identifiers is based on NFKC.
324324

325325
A non-normative HTML file listing all valid identifier characters for Unicode
326-
14.0.0 can be found at
327-
https://www.unicode.org/Public/14.0.0/ucd/DerivedCoreProperties.txt
326+
15.0.0 can be found at
327+
https://www.unicode.org/Public/15.0.0/ucd/DerivedCoreProperties.txt
328328

329329

330330
.. _keywords:
@@ -1013,4 +1013,4 @@ occurrence outside string literals and comments is an unconditional error:
10131013
10141014
.. rubric:: Footnotes
10151015

1016-
.. [#] https://www.unicode.org/Public/11.0.0/ucd/NameAliases.txt
1016+
.. [#] https://www.unicode.org/Public/15.0.0/ucd/NameAliases.txt

Doc/whatsnew/3.11.rst

+8
Original file line numberDiff line numberDiff line change
@@ -523,6 +523,14 @@ Added non parallel-safe :func:`~contextlib.chdir` context manager to change
523523
the current working directory and then restore it on exit. Simple wrapper
524524
around :func:`~os.chdir`. (Contributed by Filipe Laíns in :issue:`25625`)
525525

526+
dataclasses
527+
-----------
528+
529+
* Change field default mutability check, allowing only defaults which are
530+
:term:`hashable` instead of any object which is not an instance of
531+
:class:`dict`, :class:`list` or :class:`set`. (Contributed by Eric V. Smith in
532+
:issue:`44674`.)
533+
526534
datetime
527535
--------
528536

0 commit comments

Comments
 (0)