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
gh-95754: Better error for shadowing stdlib module #113769
base: main
Are you sure you want to change the base?
Conversation
9279982
to
6f14b0d
Compare
f69e0b5
to
d4111e5
Compare
|
🤖 New build scheduled with the buildbot fleet by @hauntsaninja for commit d4111e5 🤖 If you want to schedule another build, you need to add the 🔨 test-with-refleak-buildbots label again. |
d4111e5
to
0933591
Compare
|
Oh actually looks like I can use stdlib_module_names.h do get sys.stdlib_module_names in C. And I can of course do the path munging in C as well. |
4c09d8e
to
9cb7291
Compare
9cb7291
to
5e22968
Compare
|
Should I disable this if Oh hmm, |
I think sacrificing a bit of accuracy to avoid bad recursion errors and nasty conditions is very much worth it so my advice is to target maintainability without making the errors plainly worng. In other words: false negatives are a good tradeoff for maintenance. |
| #include "pycore_interp.h" // PyInterpreterState.importlib | ||
| #include "pycore_modsupport.h" // _PyModule_CreateInitialized() | ||
| #include "pycore_moduleobject.h" // _PyModule_GetDef() | ||
| #include "pycore_object.h" // _PyType_AllocNoTrack | ||
| #include "pycore_pyerrors.h" // _PyErr_FormatFromCause() | ||
| #include "pycore_pystate.h" // _PyInterpreterState_GET() | ||
|
|
||
| #include "osdefs.h" // MAXPATHLEN | ||
| #include "../Python/stdlib_module_names.h" // _Py_stdlib_module_names |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this need to be a relative include? (I don't like relative includes.)
This is a very common mistake for beginners. This PR tries to detect the most common case: where a file in the current directory ends up on sys.path and shadows a standard library module. (My previous PR #112577 was only marginally helpful and wouldn't help with many such errors)
The first commit in this PR is just refactoring.
I'm a little nervous about running this much Python and the potential for recursion in import machinery. It's also easy to do evil things that will make bad things happen. Please let me know if you have suggestions (should I do the path munging in C?) for additional test cases or sanity checks!📚 Documentation preview 📚: https://cpython-previews--113769.org.readthedocs.build/