-
-
Notifications
You must be signed in to change notification settings - Fork 32.3k
gh-122255: Synchronize warnings in C and Python implementations of the warnings module #122824
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
serhiy-storchaka
wants to merge
3
commits into
python:main
Choose a base branch
from
serhiy-storchaka:spec-loader-warnings
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -175,21 +175,55 @@ def lazycache(filename, module_globals): | |
return False | ||
if not filename or (filename.startswith('<') and filename.endswith('>')): | ||
return False | ||
# Try for a __loader__, if available | ||
if module_globals and '__name__' in module_globals: | ||
spec = module_globals.get('__spec__') | ||
name = getattr(spec, 'name', None) or module_globals['__name__'] | ||
loader = getattr(spec, 'loader', None) | ||
if loader is None: | ||
loader = module_globals.get('__loader__') | ||
get_source = getattr(loader, 'get_source', None) | ||
|
||
if name and get_source: | ||
def get_lines(name=name, *args, **kwargs): | ||
return get_source(name, *args, **kwargs) | ||
cache[filename] = (get_lines,) | ||
return True | ||
return False | ||
|
||
if module_globals is not None and not isinstance(module_globals, dict): | ||
raise TypeError(f'module_globals must be a dict, not {type(module_globals).__name__}') | ||
if not module_globals or '__name__' not in module_globals: | ||
return False | ||
|
||
spec = module_globals.get('__spec__') | ||
name = getattr(spec, 'name', None) or module_globals['__name__'] | ||
if name is None: | ||
return False | ||
|
||
loader = _bless_my_loader(module_globals) | ||
if loader is None: | ||
return False | ||
|
||
get_source = getattr(loader, 'get_source', None) | ||
if get_source is None: | ||
return False | ||
|
||
def get_lines(name=name, *args, **kwargs): | ||
return get_source(name, *args, **kwargs) | ||
cache[filename] = (get_lines,) | ||
return True | ||
|
||
def _bless_my_loader(module_globals): | ||
# Similar to _bless_my_loader() in importlib._bootstrap_external, | ||
# but always emits warnings instead of errors. | ||
loader = module_globals.get('__loader__') | ||
if loader is None and '__spec__' not in module_globals: | ||
return None | ||
|
||
spec = module_globals.get('__spec__') | ||
spec_loader = getattr(spec, 'loader', None) | ||
if spec_loader is None: | ||
import warnings | ||
warnings.warn( | ||
'Module globals is missing a __spec__.loader', | ||
DeprecationWarning) | ||
return loader | ||
|
||
assert spec_loader is not None | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This assertion looks redundant. |
||
if loader is not None and loader != spec_loader: | ||
import warnings | ||
warnings.warn( | ||
'Module globals; __loader__ != __spec__.loader', | ||
DeprecationWarning) | ||
return loader | ||
|
||
return spec_loader | ||
|
||
|
||
def _register_code(code, string, name): | ||
|
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
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
4 changes: 4 additions & 0 deletions
4
Misc/NEWS.d/next/Library/2024-08-08-12-39-36.gh-issue-122255.J_gU8Y.rst
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
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,4 @@ | ||||||
In the :mod:`linecache` module and in the Python implementation of the | ||||||
:mod:`warnings` module, a ``DeprecationWarning`` is issued when | ||||||
``m.__loader__`` differs from ``m.__spec__.loader`` (like in the C | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
implementation of the :mod:`!warnings` module). |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.