Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit45e6dd6

Browse files
authored
gh-128030: Avoid error from PyModule_GetFilenameObject for non-module (#128047)
I missed the extra `PyModule_Check` in#127660 because I was looking at3.12 as the base implementation for import from. This meant that Imissed the `PyModuleCheck` introduced in#112661.
1 parentdaa260e commit45e6dd6

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

‎Lib/test/test_import/__init__.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -851,6 +851,29 @@ def test_frozen_module_from_import_error(self):
851851
stdout,stderr=popen.communicate()
852852
self.assertIn(expected_error,stdout)
853853

854+
deftest_non_module_from_import_error(self):
855+
prefix="""
856+
import sys
857+
class NotAModule: ...
858+
nm = NotAModule()
859+
nm.symbol = 123
860+
sys.modules["not_a_module"] = nm
861+
from not_a_module import symbol
862+
"""
863+
scripts= [
864+
prefix+"from not_a_module import missing_symbol",
865+
prefix+"nm.__spec__ = []\nfrom not_a_module import missing_symbol",
866+
]
867+
forscriptinscripts:
868+
withself.subTest(script=script):
869+
expected_error= (
870+
b"ImportError: cannot import name 'missing_symbol' from "
871+
b"'<unknown module name>' (unknown location)"
872+
)
873+
popen=script_helper.spawn_python("-c",script)
874+
stdout,stderr=popen.communicate()
875+
self.assertIn(expected_error,stdout)
876+
854877
deftest_script_shadowing_stdlib(self):
855878
script_errors= [
856879
(
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Avoid error from calling ``PyModule_GetFilenameObject`` on a non-module object when importing a non-existent symbol from a non-module object.

‎Python/ceval.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2860,7 +2860,7 @@ _PyEval_ImportFrom(PyThreadState *tstate, PyObject *v, PyObject *name)
28602860
}
28612861
}
28622862

2863-
if (origin==NULL) {
2863+
if (origin==NULL&&PyModule_Check(v)) {
28642864
// Fall back to __file__ for diagnostics if we don't have
28652865
// an origin that is a location
28662866
origin=PyModule_GetFilenameObject(v);

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp