Skip to content

Commit 1f1bbc8

Browse files
authored
Merge branch 'main' into gh-130715
2 parents 4734f51 + 92e5f82 commit 1f1bbc8

File tree

75 files changed

+2480
-1910
lines changed

Some content is hidden

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

75 files changed

+2480
-1910
lines changed

Doc/extending/embedding.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,8 +196,8 @@ interesting part with respect to embedding Python starts with ::
196196

197197
After initializing the interpreter, the script is loaded using
198198
:c:func:`PyImport_Import`. This routine needs a Python string as its argument,
199-
which is constructed using the :c:func:`PyUnicode_FromString` data conversion
200-
routine. ::
199+
which is constructed using the :c:func:`PyUnicode_DecodeFSDefault` data
200+
conversion routine. ::
201201

202202
pFunc = PyObject_GetAttrString(pModule, argv[2]);
203203
/* pFunc is a new reference */

Doc/library/functions.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1405,10 +1405,10 @@ are always available. They are listed here in alphabetical order.
14051405
:func:`io.TextIOWrapper.reconfigure`. When no *buffering* argument is
14061406
given, the default buffering policy works as follows:
14071407

1408-
* Binary files are buffered in fixed-size chunks; the size of the buffer is
1409-
chosen using a heuristic trying to determine the underlying device's "block
1410-
size" and falling back on :const:`io.DEFAULT_BUFFER_SIZE`. On many systems,
1411-
the buffer will typically be 4096 or 8192 bytes long.
1408+
* Binary files are buffered in fixed-size chunks; the size of the buffer
1409+
is ``max(min(blocksize, 8 MiB), DEFAULT_BUFFER_SIZE)``
1410+
when the device block size is available.
1411+
On most systems, the buffer will typically be 128 kilobytes long.
14121412

14131413
* "Interactive" text files (files for which :meth:`~io.IOBase.isatty`
14141414
returns ``True``) use line buffering. Other text files use the policy

Doc/library/io.rst

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1147,6 +1147,55 @@ Text I/O
11471147
It inherits from :class:`codecs.IncrementalDecoder`.
11481148

11491149

1150+
Static Typing
1151+
-------------
1152+
1153+
The following protocols can be used for annotating function and method
1154+
arguments for simple stream reading or writing operations. They are decorated
1155+
with :deco:`typing.runtime_checkable`.
1156+
1157+
.. class:: Reader[T]
1158+
1159+
Generic protocol for reading from a file or other input stream. ``T`` will
1160+
usually be :class:`str` or :class:`bytes`, but can be any type that is
1161+
read from the stream.
1162+
1163+
.. versionadded:: next
1164+
1165+
.. method:: read()
1166+
read(size, /)
1167+
1168+
Read data from the input stream and return it. If *size* is
1169+
specified, it should be an integer, and at most *size* items
1170+
(bytes/characters) will be read.
1171+
1172+
For example::
1173+
1174+
def read_it(reader: Reader[str]):
1175+
data = reader.read(11)
1176+
assert isinstance(data, str)
1177+
1178+
.. class:: Writer[T]
1179+
1180+
Generic protocol for writing to a file or other output stream. ``T`` will
1181+
usually be :class:`str` or :class:`bytes`, but can be any type that can be
1182+
written to the stream.
1183+
1184+
.. versionadded:: next
1185+
1186+
.. method:: write(data, /)
1187+
1188+
Write *data* to the output stream and return the number of items
1189+
(bytes/characters) written.
1190+
1191+
For example::
1192+
1193+
def write_binary(writer: Writer[bytes]):
1194+
writer.write(b"Hello world!\n")
1195+
1196+
See :ref:`typing-io` for other I/O related protocols and classes that can be
1197+
used for static type checking.
1198+
11501199
Performance
11511200
-----------
11521201

Doc/library/typing.rst

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2834,17 +2834,35 @@ with :func:`@runtime_checkable <runtime_checkable>`.
28342834
An ABC with one abstract method ``__round__``
28352835
that is covariant in its return type.
28362836

2837-
ABCs for working with IO
2838-
------------------------
2837+
.. _typing-io:
2838+
2839+
ABCs and Protocols for working with I/O
2840+
---------------------------------------
28392841

2840-
.. class:: IO
2841-
TextIO
2842-
BinaryIO
2842+
.. class:: IO[AnyStr]
2843+
TextIO[AnyStr]
2844+
BinaryIO[AnyStr]
28432845

2844-
Generic type ``IO[AnyStr]`` and its subclasses ``TextIO(IO[str])``
2846+
Generic class ``IO[AnyStr]`` and its subclasses ``TextIO(IO[str])``
28452847
and ``BinaryIO(IO[bytes])``
28462848
represent the types of I/O streams such as returned by
2847-
:func:`open`.
2849+
:func:`open`. Please note that these classes are not protocols, and
2850+
their interface is fairly broad.
2851+
2852+
The protocols :class:`io.Reader` and :class:`io.Writer` offer a simpler
2853+
alternative for argument types, when only the ``read()`` or ``write()``
2854+
methods are accessed, respectively::
2855+
2856+
def read_and_write(reader: Reader[str], writer: Writer[bytes]):
2857+
data = reader.read()
2858+
writer.write(data.encode())
2859+
2860+
Also consider using :class:`collections.abc.Iterable` for iterating over
2861+
the lines of an input stream::
2862+
2863+
def read_config(stream: Iterable[str]):
2864+
for line in stream:
2865+
...
28482866

28492867
Functions and decorators
28502868
------------------------

Doc/library/webbrowser.rst

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,23 @@ a new tab, with the browser being brought to the foreground. The use of the
4040
:mod:`webbrowser` module on iOS requires the :mod:`ctypes` module. If
4141
:mod:`ctypes` isn't available, calls to :func:`.open` will fail.
4242

43+
.. program:: webbrowser
44+
4345
The script :program:`webbrowser` can be used as a command-line interface for the
4446
module. It accepts a URL as the argument. It accepts the following optional
4547
parameters:
4648

47-
* ``-n``/``--new-window`` opens the URL in a new browser window, if possible.
48-
* ``-t``/``--new-tab`` opens the URL in a new browser page ("tab").
49+
.. option:: -n, --new-window
50+
51+
Opens the URL in a new browser window, if possible.
52+
53+
.. option:: -t, --new-tab
54+
55+
Opens the URL in a new browser tab.
56+
57+
The options are, naturally, mutually exclusive. Usage example:
4958

50-
The options are, naturally, mutually exclusive. Usage example::
59+
.. code-block:: bash
5160
5261
python -m webbrowser -t "https://www.python.org"
5362

Doc/library/zipfile.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -554,6 +554,14 @@ Path Objects
554554
e.g. 'dir/file.txt', 'dir/', or ''. Defaults to the empty string,
555555
indicating the root.
556556

557+
.. note::
558+
The :class:`Path` class does not sanitize filenames within the ZIP archive. Unlike
559+
the :meth:`ZipFile.extract` and :meth:`ZipFile.extractall` methods, it is the
560+
caller's responsibility to validate or sanitize filenames to prevent path traversal
561+
vulnerabilities (e.g., filenames containing ".." or absolute paths). When handling
562+
untrusted archives, consider resolving filenames using :func:`os.path.abspath`
563+
and checking against the target directory with :func:`os.path.commonpath`.
564+
557565
Path objects expose the following features of :mod:`pathlib.Path`
558566
objects:
559567

Doc/whatsnew/3.14.rst

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -301,11 +301,11 @@ For further information on how to build Python, see
301301
cautiously revised down to 3-5%. While we expect performance results to be better
302302
than what we report, our estimates are more conservative due to a
303303
`compiler bug <https://github.com/llvm/llvm-project/issues/106846>`_ found in
304-
Clang/LLVM 19. We were unaware of this bug, and it artifically boosted
305-
our numbers, resulting in inaccurate results. We sincerely apologize for
304+
Clang/LLVM 19, which causes the normal interpreter to be slower. We were unaware of this bug,
305+
resulting in inaccurate results. We sincerely apologize for
306306
communicating results that were only accurate for certain versions of LLVM 19
307307
and 20. At the time of writing, this bug has not yet been fixed in LLVM 19-21. Thus
308-
any benchmarks with those versions of LLVM may produce artifically inflated numbers.
308+
any benchmarks with those versions of LLVM may produce inaccurate numbers.
309309
(Thanks to Nelson Elhage for bringing this to light.)
310310

311311
(Contributed by Ken Jin in :gh:`128563`, with ideas on how to implement this
@@ -619,6 +619,11 @@ io
619619
:exc:`BlockingIOError` if the operation cannot immediately return bytes.
620620
(Contributed by Giovanni Siragusa in :gh:`109523`.)
621621

622+
* Add protocols :class:`io.Reader` and :class:`io.Writer` as a simpler
623+
alternatives to the pseudo-protocols :class:`typing.IO`,
624+
:class:`typing.TextIO`, and :class:`typing.BinaryIO`.
625+
(Contributed by Sebastian Rittau in :gh:`127648`.)
626+
622627

623628
json
624629
----

Include/cpython/pystate.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,16 +83,14 @@ struct _ts {
8383
unsigned int bound_gilstate:1;
8484
/* Currently in use (maybe holds the GIL). */
8585
unsigned int active:1;
86-
/* Currently holds the GIL. */
87-
unsigned int holds_gil:1;
8886

8987
/* various stages of finalization */
9088
unsigned int finalizing:1;
9189
unsigned int cleared:1;
9290
unsigned int finalized:1;
9391

9492
/* padding to align to 4 bytes */
95-
unsigned int :23;
93+
unsigned int :24;
9694
} _status;
9795
#ifdef Py_BUILD_CORE
9896
# define _PyThreadState_WHENCE_NOTSET -1
@@ -103,6 +101,10 @@ struct _ts {
103101
# define _PyThreadState_WHENCE_GILSTATE 4
104102
# define _PyThreadState_WHENCE_EXEC 5
105103
#endif
104+
105+
/* Currently holds the GIL. Must be its own field to avoid data races */
106+
int holds_gil;
107+
106108
int _whence;
107109

108110
/* Thread state (_Py_THREAD_ATTACHED, _Py_THREAD_DETACHED, _Py_THREAD_SUSPENDED).

Include/internal/pycore_magic_number.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,8 @@ Known values:
270270
Python 3.14a5 3615 (CALL_FUNCTION_EX always take a kwargs argument)
271271
Python 3.14a5 3616 (Remove BINARY_SUBSCR and family. Make them BINARY_OPs)
272272
Python 3.14a6 3617 (Branch monitoring for async for loops)
273+
Python 3.14a6 3618 (Add oparg to END_ASYNC_FOR)
274+
Python 3.14a6 3619 (Renumber RESUME opcode from 149 to 128)
273275
274276
Python 3.15 will start with 3650
275277
@@ -282,7 +284,7 @@ PC/launcher.c must also be updated.
282284
283285
*/
284286

285-
#define PYC_MAGIC_NUMBER 3617
287+
#define PYC_MAGIC_NUMBER 3619
286288
/* This is equivalent to converting PYC_MAGIC_NUMBER to 2 bytes
287289
(little-endian) and then appending b'\r\n'. */
288290
#define PYC_MAGIC_NUMBER_TOKEN \

Include/internal/pycore_opcode_metadata.h

Lines changed: 23 additions & 23 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)