gh-153062: Fix a crash iterating itertools.tee on the free-threaded build (gh-153063)
itertools.tee branches share a linked list of teedataobject cells. On the free-threaded build, iterating one branch from multiple threads, or iterating sibling branches concurrently, raced on the shared cells and each branch's position, corrupting refcounts and crashing.
Lock each teedataobject while reading, extending, or clearing it, and snapshot each branch's position under the tee object's own lock, revalidating before advancing, so the two locks are never nested. Concurrent iteration of one tee is undefined and may raise RuntimeError as documented, but no longer crashes.
The free-threading snapshot and revalidation add per-element overhead on the default build, where the GIL already serializes access. Guard that path under Py_GIL_DISABLED so the default build keeps the original iteration and the free-threaded path is unchanged.
Co-authored-by: Neil Schemenauer <nas-github@arctrix.com>
Victor Stinner [Thu, 9 Jul 2026 23:29:53 +0000 (01:29 +0200)]
gh-152132: Enhance Py_RunMain() and fix bugs (#153461)
* Check for signals more often. Previously, a pending exception could
be removed by PyErr_Clear().
* Only call _PyInterpreterState_SetNotRunningMain() if
_PyInterpreterState_SetRunningMain() has been called.
* Add pymain_error() and pyrun_error(): write the error message to
sys.stderr instead of the C stream stderr.
* Convert _PyPathConfig_UpdateGlobal() PyStatus error to an
exception.
* Simplify pymain_run_startup(): avoid strerror() which is decoded
from the wrong encoding. Instead display the exception, Py_fopen()
raises an OSError with the path object (and can raise other exceptions).
* Add pymain_set_path0() function.
* Add _PySys_FormatV() function.
Victor Stinner [Thu, 9 Jul 2026 18:28:15 +0000 (20:28 +0200)]
gh-152132: Fix Py_RunMain() to return an exit code (#153446)
* Fix Py_RunMain() to return an exit code, rather than calling
Py_Exit(), when running a script, a command, or the REPL.
* _PyRun_SimpleFile() now logs errors to stderr, for example if setting
__main__.__file__ fails.
* Add tests on Py_RunMain() exitcode.
* Rename functions:
* Change _PyRun_SimpleString(), _PyRun_SimpleFile(), _PyRun_AnyFile() and
_PyRun_InteractiveLoop() return type to PyObject*.
* pymain_repl() now displays the error if PySys_Audit() or
import _pyrepl failed.
* PyRun_SimpleFileExFlags() and PyRun_AnyFileExFlags() now log
PyUnicode_DecodeFSDefault() error. So these functions can no longer
return -1 with an exception set.
gh-152997: Support system locale encodings via an iconv-based codec (GH-153001)
Where the C library provides iconv(), the codecs module now exposes every
encoding iconv() knows but Python has no built-in codec for -- the POSIX
counterpart of the Windows code-page support. Use it by name (e.g.
"cp1133"), or with an "iconv:" prefix to force it over a built-in codec.
The codec is a last-resort search function and never shadows a built-in.
Both directions pivot through native-endian UTF-32, keeping one input unit
per code point so error handlers get the exact string position.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
gh-153422: Return bool from some tkinter query methods (GH-153429)
The winfo_exists(), winfo_ismapped() and winfo_viewable() methods of
tkinter widgets and Text.edit_modified() now return a bool instead of an
integer or, depending on wantobjects, a string.
Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
gh-153406: Raise ValueError, not OverflowError, for out-of-range dates in email.utils.parsedate_to_datetime (#153407)
email.utils.parsedate_to_datetime documented that it raises ValueError for an invalid date, but it leaked OverflowError when the parsed year or timezone offset was too large for the datetime and timedelta constructors, and that OverflowError also escaped the modern header parsing path since DateHeader.parse only caught ValueError. Wrap the datetime and timezone construction so an OverflowError is re-raised as a ValueError with the original chained as the cause, which restores the documented contract and lets the existing header handler record an InvalidDateDefect instead of raising.
gh-153417: Fix BytesWarning in imaplib error messages for bytes arguments (GH-153423)
IMAP4.select() and IMAP4.uid() formatted the mailbox and command argument
with %s in their error messages, which raised BytesWarning under -bb when the
argument was bytes and masked the real error. Use %r instead, which is safe
for bytes and also quotes the value.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
identify(5, 5) could run before the notebook reached its requested size,
so the pixel fell outside the first tab and returned ''. Guard it with a
new opt-in wait_until_mapped(full_size=True).
Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
gh-151757: Skip curses variation-selector test on older macOS (GH-153344)
Older macOS reports a variation selector as a spacing character (wcwidth()
returns 1) instead of a zero-width combining mark, so curses cannot put it in
the same cell as its base.
Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
gh-153380: Adapt ttk tests to Tk 9.1 changes (GH-153381)
- selectmode: new 'single' and 'multiple' modes
- set(): includes the tree column '#0'
- insert()/selection(): item ids keep the passed value's type
- heading_callback: avoid a double click on the second click
- theme_create_image: don't assert the exact (now scaled) width
Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
gh-47005: fix do_open() to let regular headers override unredirected … (#146506)
* gh-47005: fix do_open() to let regular headers override unredirected headers
AbstractHTTPHandler.do_open() was building the request header dict by
starting with unredirected_hdrs and only inserting regular headers that
were not already present, giving unredirected headers priority. This
contradicts get_header() and header_items(), both of which give regular
headers the higher priority.
Fix by unconditionally updating with req.headers so that a header set
via add_header() always overrides one set via add_unredirected_header().
gh-119592: gh-152967: Fix ProcessPoolExecutor stranding submitted work when a max_tasks_per_child worker exits (GH-152978)
gh-119592: Fix ProcessPoolExecutor stranding submitted work when a max_tasks_per_child worker exits
Worker replacement went through the executor object: the manager thread
read executor attributes that shutdown(wait=False) clears concurrently,
and could not replace workers at all once the executor was garbage
collected. A worker exiting at its max_tasks_per_child limit in those
states left the remaining submitted work permanently unexecuted and hung
interpreter exit; the racing case could crash the manager thread.
Replace workers from the executor manager thread using its own state
plus configuration read through the live executor weakref, which
shutdown() never clears:
- After shutdown(wait=False) with the executor still referenced, a
replacement is spawned and the remaining work is executed as
documented.
- Once the executor has been garbage collected (gh-152967), or a
replacement worker cannot be started and no workers remain, the
remaining futures now fail with BrokenProcessPool instead of never
resolving.
- A new _force_shutting_down flag stops both spawn paths from starting
workers that would escape terminate_workers()/kill_workers().
Co-authored-by: Claude Fable 5 <noreply@anthropic.com> Reviewed-multiple-times-by: Gregory P. Smith
gh-46927: Prevent readline from overriding environment variables (GH-153184)
Readline sets COLUMNS and LINES to the terminal size at initialization, and
ncurses prefers those stale variables over an ioctl() query, breaking resize
handling. Set rl_change_environment to 0, when available, so importing
readline no longer modifies the environment.
Implement the modified UTF-7 encoding used for international IMAP4
mailbox names (RFC 3501, section 5.1.3). It differs from UTF-7:
"&" is the shift character ("&-" encodes a literal "&"), "," replaces
"/" in the Base64 alphabet, and all non-printable-ASCII characters are
Base64-encoded.
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
gh-88574: Do not swallow the line after a terminating literal in imaplib (GH-153317)
GH-152751 skipped a spurious blank line after a literal unconditionally,
corrupting a response that ends with a literal (such as a mailbox name
returned by LIST): its empty trailer was mistaken for the blank and the
following line was swallowed.
The blank is now skipped only inside an unclosed parenthesis. After a
literal that ends the response it instead arrives before the next
response and is skipped there.
Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
gh-143990: Preserve the size when creating a Font from a named font (GH-153267)
tkinter.font.Font now copies the options of a named font (via "font
configure") instead of the options resolved by "font actual", which
would resolve a size specified in pixels (a negative size) to points.
A font description is still resolved, as it cannot be parsed otherwise.
Font.copy(), which has always been equivalent to constructing a Font
from the original font, is updated to match and now preserves the size
too.
Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
gh-153056: Fix a data race compiling the string.Template pattern in free-threading builds (#153057)
* gh-153056: Fix a data race compiling the string.Template pattern in free-threading builds
Template compiles its substitution pattern lazily and caches it on the class. On the free-threaded build two concurrent first uses could race: a thread that observed the pattern another thread had just compiled would try to recompile it, and re.compile() rejects flags on an already-compiled pattern, raising a spurious ValueError. Return the already-compiled pattern instead.
As a side effect, a subclass that supplies an already-compiled pattern now works too; previously it raised the same ValueError at class definition.
* Trim test comments and NEWS wording
* Document that the pattern attribute accepts a string or a compiled regex
* Comment the three states of pattern and note the documented-behavior fix in NEWS
gh-143990: Allow tkinter.font.Font to wrap a font description (GH-152025)
With exists=True and no name, Font now wraps the font description as is,
without creating a new named font, so that it is used without loss of
precision by actual(), measure() and metrics(). Its name attribute is then
the description rather than a string. Keyword options now override the
corresponding settings of the given font instead of being ignored.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
gh-153200: Fix math.isqrt() for int subclasses with overridden comparison operators (GH-153203)
The final check-and-correct comparison in the arbitrary precision path
could call a comparison operator overridden in an int subclass.
Compare by value with int's tp_richcompare.
Victor Stinner [Mon, 6 Jul 2026 15:44:21 +0000 (17:44 +0200)]
gh-152785: Upgrade Ubuntu from 24.04 to 26.04 in GitHub Actions (#152717)
* Replace "ubuntu-24.04" with "ubuntu-26.04".
* Replace "ubuntu-latest" with "ubuntu-26.04" for Cross build Linux.
* Replace "ubuntu-latest" with "ubuntu-slim" for small workloads.
* Update ".github/actionlint.yaml" to allow "ubuntu-26.04"
and "ubuntu-26.04-arm" images.
* Install Ubuntu libmpdec-dev package, rather than installing libmpdec
from source (tarball).
* No longer run https://apt.llvm.org/llvm.sh, since ubuntu-26.04 provides
clang-21 by default.
Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com>
Add tests for syntax error messages that had no test coverage (GH-153192)
"illegal target for annotation" and "cannot use dict unpacking here"
were not tested at all, and "f-string: expecting '!', or ':', or '}'"
was only tested for its t-string variant.
Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
gh-153171: Produce specialized syntax errors for 'not' after an operator in more positions (GH-153175)
Move the invalid_factor rule from term to factor and the
invalid_arithmetic rule from shift_expr to sum, so that they are
tried at all positions where the invalid construct can occur.
Previously a generic "invalid syntax" error was reported for
constructs like "1 << 2 + not x" or "1 * + not x".
Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
gh-152026: Track mark-saving contexts with a counter (GH-153160)
Replace the state->repeat checks that guard mark saving and restoring
with an explicit save_marks counter maintained where repeat and
possessive contexts are entered and left. Each push is now paired
with a pop decided by the same condition.
Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
Add a wrapper for the IMAP ID command (RFC 2971). It takes a mapping
of field names to values and returns the server identification
information from the untagged ID response.
Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
gh-143921: Narrow the control character check in imaplib commands (GH-153067)
Only NUL, CR and LF are rejected now. Other control characters are
valid in quoted strings and can occur in mailbox names returned by
the server, so they are now accepted and sent quoted.
Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
gh-43699: Defer the drag cursor in tkinter.dnd until the pointer moves (GH-152371)
The drag cursor is now changed only once the pointer starts moving past
a small threshold rather than on the initial button press, so that a
plain click no longer flashes it.
Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
gh-59396: Use themed widgets in tkinter.filedialog (GH-152036)
The FileDialog, LoadFileDialog and SaveFileDialog dialogs are now built from
the themed tkinter.ttk widgets by default instead of the classic tkinter
widgets, and gained a use_ttk parameter that selects between the classic Tk
widgets and the themed ttk widgets.
They were also brought closer to the native Tk file dialog: the buttons and
field labels gained Alt key accelerators, the default ring follows the keyboard
focus, the Escape key cancels the dialog, the focus traverses the widgets in
their visual order, and the directory and file lists gained a horizontal
scrollbar and type-ahead selection. The dialog is now transient and centered
over its parent, and the SaveFileDialog overwrite confirmation uses a themed
message box.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
gh-79638: Restore "Treat an unreachable robots.txt as disallow all" (GH-152525)
This change was accidentally reverted by f0daba1652c (gh-106693,
GH-149514), which only intended to revert the ob_sval change.
The tests were already restored by GH-149569.
Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
gh-75952: Document negative offsets in tkinter geometry strings (GH-152531)
Tk geometry strings can contain a negative offset (e.g. 200x100+-9+-8)
when a window edge is positioned beyond the corresponding screen edge.
Note this in the geometry() and winfo_geometry() documentation.
Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
A Tk variable wrapper unsets its Tcl variable when garbage collected, so a
reference must be kept while a widget uses it. Otherwise Tk recreates the Tcl
variable but never unsets it again, leaking it.
Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
gh-69134: Wait until mapped in SimpleDialog keyboard tests (GH-152690)
The SimpleDialog keyboard tests generate key events after focus_force(),
which on Windows are dropped until the toplevel is mapped, so they could
fail intermittently (seen on the Windows10 buildbot as
test_return_no_default). Wait until the window is mapped in each of
these tests, as GH-152599 did for the other keyboard tests.
Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>