Title: warn_explicit() issues a warning or an error when used directly in a script · Issue #123011 · python/cpython · GitHub
Open Graph Title: warn_explicit() issues a warning or an error when used directly in a script · Issue #123011 · python/cpython
X Title: warn_explicit() issues a warning or an error when used directly in a script · Issue #123011 · python/cpython
Description: For example, running the following script: import warnings warnings.warn_explicit('eggs', UserWarning, 'bar', 1, module_globals=globals()) emits a warning: $ ./python t.py
Open Graph Description: For example, running the following script: import warnings warnings.warn_explicit('eggs', UserWarning, 'bar', 1, module_globals=globals()) emits a warning: $ ./python t.py X Description: For example, running the following script: import warnings warnings.warn_explicit('eggs', UserWarning, 'bar', 1, module_globals=globals()) emits a warning: $ ./python t.py <froze...
Opengraph URL: https://github.com/python/cpython/issues/123011
X: @github
Domain: github.com
Links:
Viewport: width=device-width
Hey, it has json ld scripts:
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"warn_explicit() issues a warning or an error when used directly in a script","articleBody":"For example, running the following script:\r\n```py\r\nimport warnings\r\nwarnings.warn_explicit('eggs', UserWarning, 'bar', 1, module_globals=globals())\r\n```\r\nemits a warning:\r\n```\r\n$ ./python t.py\r\n\u003cfrozen importlib._bootstrap_external\u003e:932: DeprecationWarning: Module globals is missing a __spec__.loader\r\n/home/serhiy/py/cpython3.13/t.py:1: UserWarning: eggs\r\n import warnings\r\n```\r\n\r\nRunning it as a module produces an error:\r\n```\r\n$ ./python -m t\r\nTraceback (most recent call last):\r\n File \"/home/serhiy/py/cpython3.13/Lib/runpy.py\", line 198, in _run_module_as_main\r\n return _run_code(code, main_globals, None,\r\n \"__main__\", mod_spec)\r\n File \"/home/serhiy/py/cpython3.13/Lib/runpy.py\", line 88, in _run_code\r\n exec(code, run_globals)\r\n ~~~~^^^^^^^^^^^^^^^^^^^\r\n File \"/home/serhiy/py/cpython3.13/t.py\", line 2, in \u003cmodule\u003e\r\n warnings.warn_explicit('eggs', UserWarning, __file__, 1, module_globals=globals())\r\n ~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\r\n File \"\u003cfrozen importlib._bootstrap_external\u003e\", line 1073, in get_source\r\n File \"\u003cfrozen importlib._bootstrap_external\u003e\", line 674, in _check_name_wrapper\r\nImportError: loader for t cannot handle __main__\r\n```\r\n\r\nRunning it in the REPL produces similar error:\r\n```pycon\r\n\u003e\u003e\u003e import warnings\r\n... warnings.warn_explicit('eggs', UserWarning, 'bar', 1, module_globals=globals())\r\n... \r\nTraceback (most recent call last):\r\n File \"\u003cpython-input-0\u003e\", line 2, in \u003cmodule\u003e\r\n warnings.warn_explicit('eggs', UserWarning, 'bar', 1, module_globals=globals())\r\n ~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\r\n File \"\u003cfrozen importlib._bootstrap_external\u003e\", line 1073, in get_source\r\n File \"\u003cfrozen importlib._bootstrap_external\u003e\", line 674, in _check_name_wrapper\r\nImportError: loader for _pyrepl.__main__ cannot handle __main__\r\n```\r\n\r\nBut importing it as a module is OK:\r\n```pycon\r\n\u003e\u003e\u003e import t\r\n/home/serhiy/py/cpython3.13/t.py:1: UserWarning: eggs\r\n import warnings\r\n```\r\n\r\nThe warning was introduced in #86298, but I think that it was for modules created in non-standard way. Running Python code as a script is pretty standard, this is not a user error. We should handle this case specially.\r\n\r\ncc @brettcannon, @warsaw","author":{"url":"https://github.com/serhiy-storchaka","@type":"Person","name":"serhiy-storchaka"},"datePublished":"2024-08-14T13:50:59.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":0},"url":"https://github.com/123011/cpython/issues/123011"}
route-pattern /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) route-controller voltron_issues_fragments route-action issue_layout fetch-nonce v2:c20e6ad0-1e88-34ac-5606-f3b43fceee18 current-catalog-service-hash 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 request-id EB76:392B41:AFA4D2:F17DB2:696A86C1 html-safe-nonce 2ae9141c05fc2847169b538350eb31c3a4c572aad6715b56e8d19585b4c81171 visitor-payload eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFQjc2OjM5MkI0MTpBRkE0RDI6RjE3REIyOjY5NkE4NkMxIiwidmlzaXRvcl9pZCI6Ijc5MTAzNDMzNzg4MTEzODk2MzMiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== visitor-hmac c612ba1e7481d8d1858a2df7445fed53a9f3da1f94281db28a491d1be4619176 hovercard-subject-tag issue:2465902755 github-keyboard-shortcuts repository,issues,copilot google-site-verification Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I octolytics-url https://collector.github.com/github/collect analytics-location / fb:app_id 1401488693436528 apple-itunes-app app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/cpython/123011/issue_layout twitter:image https://opengraph.githubassets.com/1cce9a48c9ba54b5dd7076591eea5156748e4c94e697f05f40778f766274e611/python/cpython/issues/123011 twitter:card summary_large_image og:image https://opengraph.githubassets.com/1cce9a48c9ba54b5dd7076591eea5156748e4c94e697f05f40778f766274e611/python/cpython/issues/123011 og:image:alt For example, running the following script: import warnings warnings.warn_explicit('eggs', UserWarning, 'bar', 1, module_globals=globals()) emits a warning: $ ./python t.py og:image:width 1200 og:image:height 600 og:site_name GitHub og:type object og:author:username serhiy-storchaka hostname github.com expected-hostname github.com None d2aa6dedd7d97fa651b17c40401f2605f309df5ca5fb659bf0554cf34f1de825 turbo-cache-control no-preview go-import github.com/python/cpython git https://github.com/python/cpython.git octolytics-dimension-user_id 1525981 octolytics-dimension-user_login python octolytics-dimension-repository_id 81598961 octolytics-dimension-repository_nwo python/cpython octolytics-dimension-repository_public true octolytics-dimension-repository_is_fork false octolytics-dimension-repository_network_root_id 81598961 octolytics-dimension-repository_network_root_nwo python/cpython turbo-body-classes logged-out env-production page-responsive disable-turbo false browser-stats-url https://api.github.com/_private/browser/stats browser-errors-url https://api.github.com/_private/browser/errors release c1dbca8981c5e3d6a69bce2f1d289e732b86241d ui-target full theme-color #1e2327 color-scheme light dark
URLs of crawlers that visited me.