Skip to content

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

Merged
serhiy-storchaka merged 2 commits into
python:mainfrom
serhiy-storchaka:gh-123011-warn-explicit-main
Aug 18, 2026
Merged

gh-123011: Fix warn_explicit() with the globals of the __main__ module#155318
serhiy-storchaka merged 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).
@serhiy-storchaka serhiy-storchaka added needs backport to 3.13 bugs and security fixes needs backport to 3.14 bugs and security fixes needs backport to 3.15 pre-release feature fixes, bugs and security fixes labels Aug 18, 2026
@serhiy-storchaka
serhiy-storchaka merged commit f2eaf17 into python:main Aug 18, 2026
59 checks passed
@serhiy-storchaka
serhiy-storchaka deleted the gh-123011-warn-explicit-main branch August 18, 2026 07:29
@miss-islington-app

Copy link
Copy Markdown

Thanks @serhiy-storchaka for the PR 🌮🎉.. I'm working now to backport this PR to: 3.13, 3.14, 3.15.
🐍🍒⛏🤖

@bedevere-app

bedevere-app Bot commented Aug 18, 2026

Copy link
Copy Markdown

GH-155989 is a backport of this pull request to the 3.15 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.15 pre-release feature fixes, bugs and security fixes label Aug 18, 2026
@bedevere-app

bedevere-app Bot commented Aug 18, 2026

Copy link
Copy Markdown

GH-155990 is a backport of this pull request to the 3.14 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.14 bugs and security fixes label Aug 18, 2026
@bedevere-app

bedevere-app Bot commented Aug 18, 2026

Copy link
Copy Markdown

GH-155991 is a backport of this pull request to the 3.13 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.13 bugs and security fixes label Aug 18, 2026
serhiy-storchaka added a commit that referenced this pull request Aug 18, 2026
…_ module (GH-155318) (GH-155991)

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).
(cherry picked from commit f2eaf17)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
serhiy-storchaka added a commit that referenced this pull request Aug 18, 2026
…_ module (GH-155318) (GH-155990)

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).
(cherry picked from commit f2eaf17)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
@bedevere-bot

Copy link
Copy Markdown

⚠️⚠️⚠️ Buildbot failure ⚠️⚠️⚠️

Hi! The buildbot s390x Fedora Stable Clang Installed 3.x (tier-3) has failed when building commit f2eaf17.

What do you need to do:

  1. Don't panic.
  2. Check the buildbot page in the devguide if you don't know what the buildbots are or how they work.
  3. Go to the page of the buildbot that failed (https://buildbot.python.org/#/builders/1645/builds/3237) and take a look at the build logs.
  4. Check if the failure is related to this commit (f2eaf17) or if it is a false positive.
  5. If the failure is related to this commit, please, reflect that on the issue and make a new Pull Request with a fix.

You can take a look at the buildbot page here:

https://buildbot.python.org/#/builders/1645/builds/3237

Summary of the results of the build (if available):

Click to see traceback logs
Note: switching to 'f2eaf174b2505be1b3dfbe817db243adf7fd5d6b'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by switching back to a branch.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -c with the switch command. Example:

  git switch -c <new-branch-name>

Or undo this operation with:

  git switch -

Turn off this advice by setting config variable advice.detachedHead to false

HEAD is now at f2eaf174b2505b gh-123011: Fix warn_explicit() with the globals of the __main__ module (GH-155318)
Switched to and reset branch 'main'

configure: WARNING: s390x-ibm-linux-gnu/clang is not supported
configure: WARNING:

Platform "s390x-ibm-linux-gnu" with compiler "clang" is not supported by the
CPython core team, see https://peps.python.org/pep-0011/ for more information.


Objects/unicodeobject.c:4906:1: warning: unused function 'ctz' [-Wunused-function]
 4906 | ctz(size_t v)
      | ^~~
1 warning generated.
In file included from ./Modules/expat/xmltok.c:312:
./Modules/expat/xmltok_impl.c:285:5: warning: fallthrough annotation in unreachable code [-Wunreachable-code-fallthrough]
  285 |     CHECK_NMSTRT_CASES(enc, ptr, end, nextTokPtr)
      |     ^
./Modules/expat/xmltok_impl.c:118:5: note: expanded from macro 'CHECK_NMSTRT_CASES'
  118 |     EXPAT_FALLTHROUGH;                                                         \
      |     ^
./Modules/expat/fallthrough.h:47:33: note: expanded from macro 'EXPAT_FALLTHROUGH'
   47 | #      define EXPAT_FALLTHROUGH __attribute__((fallthrough))
      |                                 ^
In file included from ./Modules/expat/xmltok.c:312:
./Modules/expat/xmltok_impl.c:292:7: warning: fallthrough annotation in unreachable code [-Wunreachable-code-fallthrough]
  292 |       CHECK_NAME_CASES(enc, ptr, end, nextTokPtr)
      |       ^
./Modules/expat/xmltok_impl.c:89:5: note: expanded from macro 'CHECK_NAME_CASES'
   89 |     EXPAT_FALLTHROUGH;                                                         \
      |     ^
./Modules/expat/fallthrough.h:47:33: note: expanded from macro 'EXPAT_FALLTHROUGH'
   47 | #      define EXPAT_FALLTHROUGH __attribute__((fallthrough))
      |                                 ^
In file included from ./Modules/expat/xmltok.c:312:
./Modules/expat/xmltok_impl.c:438:5: warning: fallthrough annotation in unreachable code [-Wunreachable-code-fallthrough]
  438 |     CHECK_NMSTRT_CASES(enc, ptr, end, nextTokPtr)
      |     ^
./Modules/expat/xmltok_impl.c:118:5: note: expanded from macro 'CHECK_NMSTRT_CASES'
  118 |     EXPAT_FALLTHROUGH;                                                         \
      |     ^
./Modules/expat/fallthrough.h:47:33: note: expanded from macro 'EXPAT_FALLTHROUGH'
   47 | #      define EXPAT_FALLTHROUGH __attribute__((fallthrough))
      |                                 ^
In file included from ./Modules/expat/xmltok.c:312:
./Modules/expat/xmltok_impl.c:445:7: warning: fallthrough annotation in unreachable code [-Wunreachable-code-fallthrough]
  445 |       CHECK_NAME_CASES(enc, ptr, end, nextTokPtr)
      |       ^
./Modules/expat/xmltok_impl.c:89:5: note: expanded from macro 'CHECK_NAME_CASES'
   89 |     EXPAT_FALLTHROUGH;                                                         \
      |     ^
./Modules/expat/fallthrough.h:47:33: note: expanded from macro 'EXPAT_FALLTHROUGH'
   47 | #      define EXPAT_FALLTHROUGH __attribute__((fallthrough))
      |                                 ^
In file included from ./Modules/expat/xmltok.c:312:
./Modules/expat/xmltok_impl.c:551:5: warning: fallthrough annotation in unreachable code [-Wunreachable-code-fallthrough]
  551 |     CHECK_NMSTRT_CASES(enc, ptr, end, nextTokPtr)
      |     ^
./Modules/expat/xmltok_impl.c:118:5: note: expanded from macro 'CHECK_NMSTRT_CASES'
  118 |     EXPAT_FALLTHROUGH;                                                         \
      |     ^
./Modules/expat/fallthrough.h:47:33: note: expanded from macro 'EXPAT_FALLTHROUGH'
   47 | #      define EXPAT_FALLTHROUGH __attribute__((fallthrough))
      |                                 ^
In file included from ./Modules/expat/xmltok.c:312:
./Modules/expat/xmltok_impl.c:560:7: warning: fallthrough annotation in unreachable code [-Wunreachable-code-fallthrough]
  560 |       CHECK_NAME_CASES(enc, ptr, end, nextTokPtr)
      |       ^
./Modules/expat/xmltok_impl.c:89:5: note: expanded from macro 'CHECK_NAME_CASES'
   89 |     EXPAT_FALLTHROUGH;                                                         \
      |     ^
./Modules/expat/fallthrough.h:47:33: note: expanded from macro 'EXPAT_FALLTHROUGH'
   47 | #      define EXPAT_FALLTHROUGH __attribute__((fallthrough))
      |                                 ^
In file included from ./Modules/expat/xmltok.c:312:
./Modules/expat/xmltok_impl.c:582:7: warning: fallthrough annotation in unreachable code [-Wunreachable-code-fallthrough]
  582 |       CHECK_NAME_CASES(enc, ptr, end, nextTokPtr)
      |       ^
./Modules/expat/xmltok_impl.c:89:5: note: expanded from macro 'CHECK_NAME_CASES'
   89 |     EXPAT_FALLTHROUGH;                                                         \
      |     ^
./Modules/expat/fallthrough.h:47:33: note: expanded from macro 'EXPAT_FALLTHROUGH'
   47 | #      define EXPAT_FALLTHROUGH __attribute__((fallthrough))
      |                                 ^
In file included from ./Modules/expat/xmltok.c:312:
./Modules/expat/xmltok_impl.c:593:9: warning: fallthrough annotation in unreachable code [-Wunreachable-code-fallthrough]
  593 |         CHECK_NMSTRT_CASES(enc, ptr, end, nextTokPtr)
      |         ^
./Modules/expat/xmltok_impl.c:118:5: note: expanded from macro 'CHECK_NMSTRT_CASES'
  118 |     EXPAT_FALLTHROUGH;                                                         \
      |     ^
./Modules/expat/fallthrough.h:47:33: note: expanded from macro 'EXPAT_FALLTHROUGH'
   47 | #      define EXPAT_FALLTHROUGH __attribute__((fallthrough))
      |                                 ^
In file included from ./Modules/expat/xmltok.c:312:
./Modules/expat/xmltok_impl.c:690:11: warning: fallthrough annotation in unreachable code [-Wunreachable-code-fallthrough]
  690 |           CHECK_NMSTRT_CASES(enc, ptr, end, nextTokPtr)
      |           ^
./Modules/expat/xmltok_impl.c:118:5: note: expanded from macro 'CHECK_NMSTRT_CASES'
  118 |     EXPAT_FALLTHROUGH;                                                         \
      |     ^
./Modules/expat/fallthrough.h:47:33: note: expanded from macro 'EXPAT_FALLTHROUGH'
   47 | #      define EXPAT_FALLTHROUGH __attribute__((fallthrough))
      |                                 ^
In file included from ./Modules/expat/xmltok.c:312:
./Modules/expat/xmltok_impl.c:735:5: warning: fallthrough annotation in unreachable code [-Wunreachable-code-fallthrough]
  735 |     CHECK_NMSTRT_CASES(enc, ptr, end, nextTokPtr)
      |     ^
./Modules/expat/xmltok_impl.c:118:5: note: expanded from macro 'CHECK_NMSTRT_CASES'
  118 |     EXPAT_FALLTHROUGH;                                                         \
      |     ^
./Modules/expat/fallthrough.h:47:33: note: expanded from macro 'EXPAT_FALLTHROUGH'
   47 | #      define EXPAT_FALLTHROUGH __attribute__((fallthrough))
      |                                 ^
In file included from ./Modules/expat/xmltok.c:312:
./Modules/expat/xmltok_impl.c:761:7: warning: fallthrough annotation in unreachable code [-Wunreachable-code-fallthrough]
  761 |       CHECK_NAME_CASES(enc, ptr, end, nextTokPtr)
      |       ^
./Modules/expat/xmltok_impl.c:89:5: note: expanded from macro 'CHECK_NAME_CASES'
   89 |     EXPAT_FALLTHROUGH;                                                         \
      |     ^
./Modules/expat/fallthrough.h:47:33: note: expanded from macro 'EXPAT_FALLTHROUGH'
   47 | #      define EXPAT_FALLTHROUGH __attribute__((fallthrough))
      |                                 ^
In file included from ./Modules/expat/xmltok.c:312:
./Modules/expat/xmltok_impl.c:772:9: warning: fallthrough annotation in unreachable code [-Wunreachable-code-fallthrough]
  772 |         CHECK_NMSTRT_CASES(enc, ptr, end, nextTokPtr)
      |         ^
./Modules/expat/xmltok_impl.c:118:5: note: expanded from macro 'CHECK_NMSTRT_CASES'
  118 |     EXPAT_FALLTHROUGH;                                                         \
      |     ^
./Modules/expat/fallthrough.h:47:33: note: expanded from macro 'EXPAT_FALLTHROUGH'
   47 | #      define EXPAT_FALLTHROUGH __attribute__((fallthrough))
      |                                 ^
In file included from ./Modules/expat/xmltok.c:312:
./Modules/expat/xmltok_impl.c:785:11: warning: fallthrough annotation in unreachable code [-Wunreachable-code-fallthrough]
  785 |           CHECK_NMSTRT_CASES(enc, ptr, end, nextTokPtr)
      |           ^
./Modules/expat/xmltok_impl.c:118:5: note: expanded from macro 'CHECK_NMSTRT_CASES'
  118 |     EXPAT_FALLTHROUGH;                                                         \
      |     ^
./Modules/expat/fallthrough.h:47:33: note: expanded from macro 'EXPAT_FALLTHROUGH'
   47 | #      define EXPAT_FALLTHROUGH __attribute__((fallthrough))
      |                                 ^
In file included from ./Modules/expat/xmltok.c:312:
./Modules/expat/xmltok_impl.c:930:5: warning: fallthrough annotation in unreachable code [-Wunreachable-code-fallthrough]
  930 |     CHECK_NMSTRT_CASES(enc, ptr, end, nextTokPtr)
      |     ^
./Modules/expat/xmltok_impl.c:118:5: note: expanded from macro 'CHECK_NMSTRT_CASES'
  118 |     EXPAT_FALLTHROUGH;                                                         \
      |     ^
./Modules/expat/fallthrough.h:47:33: note: expanded from macro 'EXPAT_FALLTHROUGH'
   47 | #      define EXPAT_FALLTHROUGH __attribute__((fallthrough))
      |                                 ^
In file included from ./Modules/expat/xmltok.c:312:
./Modules/expat/xmltok_impl.c:943:7: warning: fallthrough annotation in unreachable code [-Wunreachable-code-fallthrough]
  943 |       CHECK_NAME_CASES(enc, ptr, end, nextTokPtr)
      |       ^
./Modules/expat/xmltok_impl.c:89:5: note: expanded from macro 'CHECK_NAME_CASES'
   89 |     EXPAT_FALLTHROUGH;                                                         \
      |     ^
./Modules/expat/fallthrough.h:47:33: note: expanded from macro 'EXPAT_FALLTHROUGH'
   47 | #      define EXPAT_FALLTHROUGH __attribute__((fallthrough))
      |                                 ^
In file included from ./Modules/expat/xmltok.c:312:
./Modules/expat/xmltok_impl.c:960:5: warning: fallthrough annotation in unreachable code [-Wunreachable-code-fallthrough]
  960 |     CHECK_NMSTRT_CASES(enc, ptr, end, nextTokPtr)
      |     ^
./Modules/expat/xmltok_impl.c:118:5: note: expanded from macro 'CHECK_NMSTRT_CASES'
  118 |     EXPAT_FALLTHROUGH;                                                         \
      |     ^
./Modules/expat/fallthrough.h:47:33: note: expanded from macro 'EXPAT_FALLTHROUGH'
   47 | #      define EXPAT_FALLTHROUGH __attribute__((fallthrough))
      |                                 ^
In file included from ./Modules/expat/xmltok.c:312:
./Modules/expat/xmltok_impl.c:967:7: warning: fallthrough annotation in unreachable code [-Wunreachable-code-fallthrough]
  967 |       CHECK_NAME_CASES(enc, ptr, end, nextTokPtr)
      |       ^
./Modules/expat/xmltok_impl.c:89:5: note: expanded from macro 'CHECK_NAME_CASES'
   89 |     EXPAT_FALLTHROUGH;                                                         \
      |     ^
./Modules/expat/fallthrough.h:47:33: note: expanded from macro 'EXPAT_FALLTHROUGH'
   47 | #      define EXPAT_FALLTHROUGH __attribute__((fallthrough))
      |                                 ^
In file included from ./Modules/expat/xmltok.c:312:
./Modules/expat/xmltok_impl.c:1202:7: warning: fallthrough annotation in unreachable code [-Wunreachable-code-fallthrough]
 1202 |       CHECK_NAME_CASES(enc, ptr, end, nextTokPtr)
      |       ^
./Modules/expat/xmltok_impl.c:89:5: note: expanded from macro 'CHECK_NAME_CASES'
   89 |     EXPAT_FALLTHROUGH;                                                         \
      |     ^
./Modules/expat/fallthrough.h:47:33: note: expanded from macro 'EXPAT_FALLTHROUGH'
   47 | #      define EXPAT_FALLTHROUGH __attribute__((fallthrough))
      |                                 ^
In file included from ./Modules/expat/xmltok.c:312:
./Modules/expat/xmltok_impl.c:1222:11: warning: fallthrough annotation in unreachable code [-Wunreachable-code-fallthrough]
 1222 |           CHECK_NAME_CASES(enc, ptr, end, nextTokPtr)
      |           ^
./Modules/expat/xmltok_impl.c:89:5: note: expanded from macro 'CHECK_NAME_CASES'
   89 |     EXPAT_FALLTHROUGH;                                                         \
      |     ^
./Modules/expat/fallthrough.h:47:33: note: expanded from macro 'EXPAT_FALLTHROUGH'
   47 | #      define EXPAT_FALLTHROUGH __attribute__((fallthrough))
      |                                 ^
19 warnings generated.
ar: unable to copy file 'libpython3.16.a'; reason: No space left on device
make: *** [Makefile:1164: libpython3.16.a] Error 1

chmod: cannot access 'target/': No such file or directory

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants