Skip to content

[3.13] gh-130932: Fix incorrect exception handling in _PyModule_IsPossiblyShadowing (GH-130934) #130939

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

Merged
merged 1 commit into from
Mar 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions Lib/test/test_import/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1203,6 +1203,28 @@ class substr(str):
for line in lines:
self.assertRegex(line, rb"cannot import name 'Fraction' from 'fractions' \(.*\)")

@unittest.skipIf(sys.platform == 'win32', 'Cannot delete cwd on Windows')
def test_script_shadowing_stdlib_cwd_failure(self):
with os_helper.temp_dir() as tmp:
subtmp = os.path.join(tmp, "subtmp")
os.mkdir(subtmp)
with open(os.path.join(subtmp, "main.py"), "w", encoding='utf-8') as f:
f.write(f"""
import sys
assert sys.path[0] == ''

import os
import shutil
shutil.rmtree(os.getcwd())

os.does_not_exist
""")
# Use -c to ensure sys.path[0] is ""
popen = script_helper.spawn_python("-c", "import main", cwd=subtmp)
stdout, stderr = popen.communicate()
expected_error = rb"AttributeError: module 'os' has no attribute 'does_not_exist'"
self.assertRegex(stdout, expected_error)

def test_script_shadowing_stdlib_sys_path_modification(self):
script_errors = [
(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix incorrect exception handling in ``_PyModule_IsPossiblyShadowing``
4 changes: 3 additions & 1 deletion Objects/moduleobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -908,7 +908,9 @@ _PyModule_IsPossiblyShadowing(PyObject *origin)
if (sys_path_0[0] == L'\0') {
// if sys.path[0] == "", treat it as if it were the current directory
if (!_Py_wgetcwd(sys_path_0_buf, MAXPATHLEN)) {
return -1;
// If we failed to getcwd, don't raise an exception and instead
// let the caller proceed assuming no shadowing
return 0;
}
sys_path_0 = sys_path_0_buf;
}
Expand Down
Loading