Skip to content

Commit

Permalink
pythongh-123930: Better error for "from imports" when script shadows …
Browse files Browse the repository at this point in the history
…module (python#123929)

(cherry picked from commit 500f533)
  • Loading branch information
hauntsaninja committed Oct 24, 2024
1 parent 4b55d53 commit 25d5236
Show file tree
Hide file tree
Showing 6 changed files with 342 additions and 169 deletions.
4 changes: 2 additions & 2 deletions Doc/whatsnew/3.13.rst
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ Improved error messages
File "/home/me/random.py", line 3, in <module>
print(random.randint(5))
^^^^^^^^^^^^^^
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)
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)
Similarly, if a script has the same name as a third-party
module that it attempts to import and this results in errors,
Expand All @@ -289,7 +289,7 @@ Improved error messages
File "/home/me/numpy.py", line 3, in <module>
np.array([1, 2, 3])
^^^^^^^^
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)
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)
(Contributed by Shantanu Jain in :gh:`95754`.)

Expand Down
2 changes: 2 additions & 0 deletions Include/internal/pycore_moduleobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ extern "C" {
extern void _PyModule_Clear(PyObject *);
extern void _PyModule_ClearDict(PyObject *);
extern int _PyModuleSpec_IsInitializing(PyObject *);
extern int _PyModuleSpec_GetFileOrigin(PyObject *, PyObject **);
extern int _PyModule_IsPossiblyShadowing(PyObject *);

extern int _PyModule_IsExtension(PyObject *obj);

Expand Down
Loading

0 comments on commit 25d5236

Please sign in to comment.