Fix circular import between threading and _threading_local - #156357
Draft
aryansk wants to merge 1 commit into
Draft
Fix circular import between threading and _threading_local#156357aryansk wants to merge 1 commit into
aryansk wants to merge 1 commit into
Conversation
Since Python 3.7 thread support is always available, _thread._local always exists. The fallback 'try: from _thread import _local except ImportError: from _threading_local import local' is obsolete and reintroduces a circular import when _thread._local is deleted (e.g. del _thread._local; import threading). - In Lib/threading.py, remove the try/except fallback and directly use 'from _thread import _local as local'. - In Lib/_threading_local.py, remove the top-level 'from threading import current_thread, RLock' and use lazy imports inside get_dict(), create_dict(), and local.__new__(). This breaks the cycle and makes 'import _thread; del _thread._local; import threading' succeed, as reported in the issue. Fixes python#156341 Co-authored-by: Muse Spark <muse-spark@users.noreply.github.com> Co-authored-by: Aryan Singh K <70511529+aryansk@users.noreply.github.com> AI disclosure: Muse Spark assisted in analysis and fix drafting; changes reviewed and tested manually (py_compile ok). Signed-off-by: aryansk <70511529+aryansk@users.noreply.github.com>
|
Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool. If this change has little impact on Python users, wait for a maintainer to apply the |
Member
|
@aryansk please stop opening further PRs until the concerns noted in #156356 (comment) are addressed. Otherwise we'll have to close all of them. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
threadingand_threading_localhave a circular import:This was fine when
threadingnever used thread-local data early, but sincegh-114424addedthreading.current_thread()usage atLib/threading.py:1540, and_threading_local.localnow depends onthreading.current_thread()inget_dict()/create_dict()/__new__, the cycle matters.Repro:
Since Python 3.7 thread support is mandatory,
_thread._localalways exists, so the fallback is obsolete.Fixes #156341
Change
Lib/threading.py: remove thetry/except ImportErrorfallback and directly usefrom _thread import _local as local(with comment explaining why the fallback is obsolete).Lib/_threading_local.py: remove top-levelfrom threading import current_thread, RLockand use lazy imports insideget_dict(),create_dict(), andlocal.__new__().This breaks the import cycle and makes the repro succeed.
Why this approach
The fallback was for platforms without thread support (pre-3.7). Keeping it reintroduces the cycle when someone deletes
_thread._local(as in the repro) or when import order changes. Lazy imports in_threading_localare minimal and match the existing comment that the file already acknowledges circular import risks and delays the import to the bottom. Removing the fallback is the cleanest; lazy imports are the safety net for any remainingfrom _threading_local import localpath.Testing
Documentation and release impact
Review notes
AI disclosure
Muse Spark assisted in analysis and fix drafting; all changes reviewed and tested manually. Co-authored-by trailers included for Pair Extraordinaire.
Co-authored-by: Muse Spark muse-spark@users.noreply.github.com
Co-authored-by: Aryan Singh K 70511529+aryansk@users.noreply.github.com