Skip to content

Commit 3d8b6f0

Browse files
authored
[3.13] gh-123930: Better error for "from imports" when script shadows module (GH-123929) (#125937)
gh-123930: Better error for "from imports" when script shadows module (#123929) (cherry picked from commit 500f533)
1 parent 4b55d53 commit 3d8b6f0

File tree

6 files changed

+342
-169
lines changed

6 files changed

+342
-169
lines changed

Doc/whatsnew/3.13.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ Improved error messages
274274
File "/home/me/random.py", line 3, in <module>
275275
print(random.randint(5))
276276
^^^^^^^^^^^^^^
277-
AttributeError: module 'random' has no attribute 'randint' (consider renaming '/home/me/random.py' since it has the same name as the standard library module named 'random' and the import system gives it precedence)
277+
AttributeError: module 'random' has no attribute 'randint' (consider renaming '/home/me/random.py' since it has the same name as the standard library module named 'random' and prevents importing that standard library module)
278278
279279
Similarly, if a script has the same name as a third-party
280280
module that it attempts to import and this results in errors,
@@ -289,7 +289,7 @@ Improved error messages
289289
File "/home/me/numpy.py", line 3, in <module>
290290
np.array([1, 2, 3])
291291
^^^^^^^^
292-
AttributeError: module 'numpy' has no attribute 'array' (consider renaming '/home/me/numpy.py' if it has the same name as a third-party module you intended to import)
292+
AttributeError: module 'numpy' has no attribute 'array' (consider renaming '/home/me/numpy.py' if it has the same name as a library you intended to import)
293293
294294
(Contributed by Shantanu Jain in :gh:`95754`.)
295295

Include/internal/pycore_moduleobject.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ extern "C" {
1111
extern void _PyModule_Clear(PyObject *);
1212
extern void _PyModule_ClearDict(PyObject *);
1313
extern int _PyModuleSpec_IsInitializing(PyObject *);
14+
extern int _PyModuleSpec_GetFileOrigin(PyObject *, PyObject **);
15+
extern int _PyModule_IsPossiblyShadowing(PyObject *);
1416

1517
extern int _PyModule_IsExtension(PyObject *obj);
1618

0 commit comments

Comments
 (0)