Skip to content

Commit b13cafc

Browse files
authored
Merge branch 'python:main' into gh-93376
2 parents 7723a02 + 83479c2 commit b13cafc

File tree

111 files changed

+1779
-639
lines changed

Some content is hidden

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

111 files changed

+1779
-639
lines changed

Doc/glossary.rst

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -800,6 +800,10 @@ Glossary
800800
thread removes *key* from *mapping* after the test, but before the lookup.
801801
This issue can be solved with locks or by using the EAFP approach.
802802

803+
lexical analyzer
804+
805+
Formal name for the *tokenizer*; see :term:`token`.
806+
803807
list
804808
A built-in Python :term:`sequence`. Despite its name it is more akin
805809
to an array in other languages than to a linked list since access to
@@ -1291,6 +1295,17 @@ Glossary
12911295
See also :term:`binary file` for a file object able to read and write
12921296
:term:`bytes-like objects <bytes-like object>`.
12931297

1298+
token
1299+
1300+
A small unit of source code, generated by the
1301+
:ref:`lexical analyzer <lexical>` (also called the *tokenizer*).
1302+
Names, numbers, strings, operators,
1303+
newlines and similar are represented by tokens.
1304+
1305+
The :mod:`tokenize` module exposes Python's lexical analyzer.
1306+
The :mod:`token` module contains information on the various types
1307+
of tokens.
1308+
12941309
triple-quoted string
12951310
A string which is bound by three instances of either a quotation mark
12961311
(") or an apostrophe ('). While they don't provide any functionality

Doc/library/bdb.rst

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ The :mod:`bdb` module also defines two classes:
118118

119119
Count of the number of times a :class:`Breakpoint` has been hit.
120120

121-
.. class:: Bdb(skip=None)
121+
.. class:: Bdb(skip=None, backend='settrace')
122122

123123
The :class:`Bdb` class acts as a generic Python debugger base class.
124124

@@ -132,9 +132,22 @@ The :mod:`bdb` module also defines two classes:
132132
frame is considered to originate in a certain module is determined
133133
by the ``__name__`` in the frame globals.
134134

135+
The *backend* argument specifies the backend to use for :class:`Bdb`. It
136+
can be either ``'settrace'`` or ``'monitoring'``. ``'settrace'`` uses
137+
:func:`sys.settrace` which has the best backward compatibility. The
138+
``'monitoring'`` backend uses the new :mod:`sys.monitoring` that was
139+
introduced in Python 3.12, which can be much more efficient because it
140+
can disable unused events. We are trying to keep the exact interfaces
141+
for both backends, but there are some differences. The debugger developers
142+
are encouraged to use the ``'monitoring'`` backend to achieve better
143+
performance.
144+
135145
.. versionchanged:: 3.1
136146
Added the *skip* parameter.
137147

148+
.. versionchanged:: 3.14
149+
Added the *backend* parameter.
150+
138151
The following methods of :class:`Bdb` normally don't need to be overridden.
139152

140153
.. method:: canonic(filename)
@@ -146,6 +159,20 @@ The :mod:`bdb` module also defines two classes:
146159
<os.path.abspath>`. A *filename* with angle brackets, such as ``"<stdin>"``
147160
generated in interactive mode, is returned unchanged.
148161

162+
.. method:: start_trace(self)
163+
164+
Start tracing. For ``'settrace'`` backend, this method is equivalent to
165+
``sys.settrace(self.trace_dispatch)``
166+
167+
.. versionadded:: 3.14
168+
169+
.. method:: stop_trace(self)
170+
171+
Stop tracing. For ``'settrace'`` backend, this method is equivalent to
172+
``sys.settrace(None)``
173+
174+
.. versionadded:: 3.14
175+
149176
.. method:: reset()
150177

151178
Set the :attr:`!botframe`, :attr:`!stopframe`, :attr:`!returnframe` and
@@ -364,6 +391,28 @@ The :mod:`bdb` module also defines two classes:
364391
Return all breakpoints that are set.
365392

366393

394+
Derived classes and clients can call the following methods to disable and
395+
restart events to achieve better performance. These methods only work
396+
when using the ``'monitoring'`` backend.
397+
398+
.. method:: disable_current_event()
399+
400+
Disable the current event until the next time :func:`restart_events` is
401+
called. This is helpful when the debugger is not interested in the current
402+
line.
403+
404+
.. versionadded:: 3.14
405+
406+
.. method:: restart_events()
407+
408+
Restart all the disabled events. This function is automatically called in
409+
``dispatch_*`` methods after ``user_*`` methods are called. If the
410+
``dispatch_*`` methods are not overridden, the disabled events will be
411+
restarted after each user interaction.
412+
413+
.. versionadded:: 3.14
414+
415+
367416
Derived classes and clients can call the following methods to get a data
368417
structure representing a stack trace.
369418

Doc/library/dis.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -535,7 +535,7 @@ details of bytecode instructions as :class:`Instruction` instances:
535535
:class:`dis.Positions` object holding the
536536
start and end locations that are covered by this instruction.
537537

538-
.. data::cache_info
538+
.. data:: cache_info
539539

540540
Information about the cache entries of this instruction, as
541541
triplets of the form ``(name, size, data)``, where the ``name``

Doc/library/math.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -350,8 +350,8 @@ Floating point manipulation functions
350350

351351
*abs_tol* is the absolute tolerance; it defaults to ``0.0`` and it must be
352352
nonnegative. When comparing ``x`` to ``0.0``, ``isclose(x, 0)`` is computed
353-
as ``abs(x) <= rel_tol * abs(x)``, which is ``False`` for any ``x`` and
354-
rel_tol less than ``1.0``. So add an appropriate positive abs_tol argument
353+
as ``abs(x) <= rel_tol * abs(x)``, which is ``False`` for any nonzero ``x`` and
354+
*rel_tol* less than ``1.0``. So add an appropriate positive *abs_tol* argument
355355
to the call.
356356

357357
The IEEE 754 special values of ``NaN``, ``inf``, and ``-inf`` will be

Doc/library/pdb.rst

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,13 +203,32 @@ slightly different way:
203203
Enter post-mortem debugging of the exception found in
204204
:data:`sys.last_exc`.
205205

206+
.. function:: set_default_backend(backend)
207+
208+
There are two supported backends for pdb: ``'settrace'`` and ``'monitoring'``.
209+
See :class:`bdb.Bdb` for details. The user can set the default backend to
210+
use if none is specified when instantiating :class:`Pdb`. If no backend is
211+
specified, the default is ``'settrace'``.
212+
213+
.. note::
214+
215+
:func:`breakpoint` and :func:`set_trace` will not be affected by this
216+
function. They always use ``'monitoring'`` backend.
217+
218+
.. versionadded:: 3.14
219+
220+
.. function:: get_default_backend()
221+
222+
Returns the default backend for pdb.
223+
224+
.. versionadded:: 3.14
206225

207226
The ``run*`` functions and :func:`set_trace` are aliases for instantiating the
208227
:class:`Pdb` class and calling the method of the same name. If you want to
209228
access further features, you have to do this yourself:
210229

211230
.. class:: Pdb(completekey='tab', stdin=None, stdout=None, skip=None, \
212-
nosigint=False, readrc=True, mode=None)
231+
nosigint=False, readrc=True, mode=None, backend=None)
213232

214233
:class:`Pdb` is the debugger class.
215234

@@ -235,6 +254,10 @@ access further features, you have to do this yourself:
235254
or ``None`` (for backwards compatible behaviour, as before the *mode*
236255
argument was added).
237256

257+
The *backend* argument specifies the backend to use for the debugger. If ``None``
258+
is passed, the default backend will be used. See :func:`set_default_backend`.
259+
Otherwise the supported backends are ``'settrace'`` and ``'monitoring'``.
260+
238261
Example call to enable tracing with *skip*::
239262

240263
import pdb; pdb.Pdb(skip=['django.*']).set_trace()
@@ -254,6 +277,9 @@ access further features, you have to do this yourself:
254277
.. versionadded:: 3.14
255278
Added the *mode* argument.
256279

280+
.. versionadded:: 3.14
281+
Added the *backend* argument.
282+
257283
.. versionchanged:: 3.14
258284
Inline breakpoints like :func:`breakpoint` or :func:`pdb.set_trace` will
259285
always stop the program at calling frame, ignoring the *skip* pattern (if any).

Doc/library/sqlite3.rst

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -290,9 +290,6 @@ Module functions
290290
:const:`PARSE_DECLTYPES` and :const:`PARSE_COLNAMES`
291291
to enable this.
292292
Column names takes precedence over declared types if both flags are set.
293-
Types cannot be detected for generated fields (for example ``max(data)``),
294-
even when the *detect_types* parameter is set; :class:`str` will be
295-
returned instead.
296293
By default (``0``), type detection is disabled.
297294

298295
:param isolation_level:
@@ -436,21 +433,6 @@ Module constants
436433
old style (pre-Python 3.12) transaction control behaviour.
437434
See :ref:`sqlite3-transaction-control-isolation-level` for more information.
438435

439-
.. data:: PARSE_COLNAMES
440-
441-
Pass this flag value to the *detect_types* parameter of
442-
:func:`connect` to look up a converter function by
443-
using the type name, parsed from the query column name,
444-
as the converter dictionary key.
445-
The type name must be wrapped in square brackets (``[]``).
446-
447-
.. code-block:: sql
448-
449-
SELECT p as "p [point]" FROM test; ! will look up converter "point"
450-
451-
This flag may be combined with :const:`PARSE_DECLTYPES` using the ``|``
452-
(bitwise or) operator.
453-
454436
.. data:: PARSE_DECLTYPES
455437

456438
Pass this flag value to the *detect_types* parameter of
@@ -472,6 +454,27 @@ Module constants
472454
This flag may be combined with :const:`PARSE_COLNAMES` using the ``|``
473455
(bitwise or) operator.
474456

457+
.. note::
458+
459+
Generated fields (for example ``MAX(p)``) are returned as :class:`str`.
460+
Use :const:`!PARSE_COLNAMES` to enforce types for such queries.
461+
462+
.. data:: PARSE_COLNAMES
463+
464+
Pass this flag value to the *detect_types* parameter of
465+
:func:`connect` to look up a converter function by
466+
using the type name, parsed from the query column name,
467+
as the converter dictionary key.
468+
The query column name must be wrapped in double quotes (``"``)
469+
and the type name must be wrapped in square brackets (``[]``).
470+
471+
.. code-block:: sql
472+
473+
SELECT MAX(p) as "p [point]" FROM test; ! will look up converter "point"
474+
475+
This flag may be combined with :const:`PARSE_DECLTYPES` using the ``|``
476+
(bitwise or) operator.
477+
475478
.. data:: SQLITE_OK
476479
SQLITE_DENY
477480
SQLITE_IGNORE

Doc/library/urllib.request.rst

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1249,7 +1249,10 @@ It is also possible to achieve the same result without using the
12491249

12501250
>>> import urllib.request
12511251
>>> f = urllib.request.urlopen('http://www.python.org/')
1252-
>>> print(f.read(100).decode('utf-8'))
1252+
>>> try:
1253+
... print(f.read(100).decode('utf-8'))
1254+
... finally:
1255+
... f.close()
12531256
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
12541257
"http://www.w3.org/TR/xhtml1/DTD/xhtm
12551258

@@ -1294,7 +1297,8 @@ Use of Basic HTTP Authentication::
12941297
opener = urllib.request.build_opener(auth_handler)
12951298
# ...and install it globally so it can be used with urlopen.
12961299
urllib.request.install_opener(opener)
1297-
urllib.request.urlopen('http://www.example.com/login.html')
1300+
with urllib.request.urlopen('http://www.example.com/login.html') as f:
1301+
print(f.read().decode('utf-8'))
12981302

12991303
:func:`build_opener` provides many handlers by default, including a
13001304
:class:`ProxyHandler`. By default, :class:`ProxyHandler` uses the environment
@@ -1312,7 +1316,8 @@ programmatically supplied proxy URLs, and adds proxy authorization support with
13121316

13131317
opener = urllib.request.build_opener(proxy_handler, proxy_auth_handler)
13141318
# This time, rather than install the OpenerDirector, we use it directly:
1315-
opener.open('http://www.example.com/login.html')
1319+
with opener.open('http://www.example.com/login.html') as f:
1320+
print(f.read().decode('utf-8'))
13161321

13171322
Adding HTTP headers:
13181323

@@ -1323,15 +1328,18 @@ Use the *headers* argument to the :class:`Request` constructor, or::
13231328
req.add_header('Referer', 'http://www.python.org/')
13241329
# Customize the default User-Agent header value:
13251330
req.add_header('User-Agent', 'urllib-example/0.1 (Contact: . . .)')
1326-
r = urllib.request.urlopen(req)
1331+
with urllib.request.urlopen(req) as f:
1332+
print(f.read().decode('utf-8'))
1333+
13271334

13281335
:class:`OpenerDirector` automatically adds a :mailheader:`User-Agent` header to
13291336
every :class:`Request`. To change this::
13301337

13311338
import urllib.request
13321339
opener = urllib.request.build_opener()
13331340
opener.addheaders = [('User-agent', 'Mozilla/5.0')]
1334-
opener.open('http://www.example.com/')
1341+
with opener.open('http://www.example.com/') as f:
1342+
print(f.read().decode('utf-8'))
13351343

13361344
Also, remember that a few standard headers (:mailheader:`Content-Length`,
13371345
:mailheader:`Content-Type` and :mailheader:`Host`)

Doc/reference/compound_stmts.rst

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -420,16 +420,16 @@ is executed. If there is a saved exception it is re-raised at the end of the
420420
:keyword:`!finally` clause. If the :keyword:`!finally` clause raises another
421421
exception, the saved exception is set as the context of the new exception.
422422
If the :keyword:`!finally` clause executes a :keyword:`return`, :keyword:`break`
423-
or :keyword:`continue` statement, the saved exception is discarded::
423+
or :keyword:`continue` statement, the saved exception is discarded. For example,
424+
this function returns 42.
424425

425-
>>> def f():
426-
... try:
427-
... 1/0
428-
... finally:
429-
... return 42
430-
...
431-
>>> f()
432-
42
426+
.. code-block::
427+
428+
def f():
429+
try:
430+
1/0
431+
finally:
432+
return 42
433433
434434
The exception information is not available to the program during execution of
435435
the :keyword:`!finally` clause.
@@ -446,21 +446,25 @@ statement, the :keyword:`!finally` clause is also executed 'on the way out.'
446446
The return value of a function is determined by the last :keyword:`return`
447447
statement executed. Since the :keyword:`!finally` clause always executes, a
448448
:keyword:`!return` statement executed in the :keyword:`!finally` clause will
449-
always be the last one executed::
449+
always be the last one executed. The following function returns 'finally'.
450450

451-
>>> def foo():
452-
... try:
453-
... return 'try'
454-
... finally:
455-
... return 'finally'
456-
...
457-
>>> foo()
458-
'finally'
451+
.. code-block::
452+
453+
def foo():
454+
try:
455+
return 'try'
456+
finally:
457+
return 'finally'
459458
460459
.. versionchanged:: 3.8
461460
Prior to Python 3.8, a :keyword:`continue` statement was illegal in the
462461
:keyword:`!finally` clause due to a problem with the implementation.
463462

463+
.. versionchanged:: next
464+
The compiler emits a :exc:`SyntaxWarning` when a :keyword:`return`,
465+
:keyword:`break` or :keyword:`continue` appears in a :keyword:`!finally`
466+
block (see :pep:`765`).
467+
464468

465469
.. _with:
466470
.. _as:

Doc/reference/lexical_analysis.rst

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ Lexical analysis
88
.. index:: lexical analysis, parser, token
99

1010
A Python program is read by a *parser*. Input to the parser is a stream of
11-
*tokens*, generated by the *lexical analyzer*. This chapter describes how the
12-
lexical analyzer breaks a file into tokens.
11+
:term:`tokens <token>`, generated by the *lexical analyzer* (also known as
12+
the *tokenizer*).
13+
This chapter describes how the lexical analyzer breaks a file into tokens.
1314

1415
Python reads program text as Unicode code points; the encoding of a source file
1516
can be given by an encoding declaration and defaults to UTF-8, see :pep:`3120`

0 commit comments

Comments
 (0)