Skip to content

gh-123011: Fix warn_explicit() with the globals of the __main__ module - #155318

Open
serhiy-storchaka wants to merge 2 commits into
python:mainfrom
serhiy-storchaka:gh-123011-warn-explicit-main
Open

gh-123011: Fix warn_explicit() with the globals of the __main__ module#155318
serhiy-storchaka wants to merge 2 commits into
python:mainfrom
serhiy-storchaka:gh-123011-warn-explicit-main

Conversation

@serhiy-storchaka

Copy link
Copy Markdown
Member

warnings.warn_explicit(module_globals=globals()) failed in the __main__ module:

  • executed as a script it emitted a spurious DeprecationWarning: Module globals is missing a __spec__.loader, because __main__ has __spec__ set to None;
  • executed with -m it raised ImportError: loader for spam cannot handle __main__, because __name__ was used instead of __spec__.name;
  • executed as a command or in the REPL it raised ImportError: '__main__' is not a built-in module, because the built-in importer has no source.

The source line is now optional: an ImportError from the loader is no longer propagated.

The pure Python implementation, which uses linecache, was not affected.

… module

The __main__ module executed as a script or a command has __spec__ set to
None, so warn_explicit(module_globals=globals()) emitted a spurious
DeprecationWarning.  It also raised ImportError when the loader was unable
to provide the source of the module: when the module was executed with -m
(the loader can only handle its own module name) or as a command (the
built-in importer has no source).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant