On Mon, Jan 25, 2021 at 6:37 PM Chris Jerdonek <chris.jerdonek@gmail.com> wrote:
> On Mon, Jan 25, 2021 at 7:51 AM Ivan Pozdeev via Python-Dev <python-dev@python.org> wrote:
>>
>> Just _names_? There's a recurring error case when a 3rd-party module overrides a standard one if it happens to have the same name. If you
>> filter such a module out, you're shooting yourself in the foot...
>
> Would another use case be to support issuing a warning if a third-party module is imported whose name matches a standard one? A related use case would be to build on this and define a function that accepts an already imported module and return whether it is from the standard library. Unlike, the module_names attribute, this function would reflect the reality of the underlying module, and so not have false positives as with doing a name check alone.
This is a different use case which requires a different solution.
sys.module_names solve some specific use cases (that I listed in my
first email).
In Python 3.9, you can already check if a module __file__ is in the
sysconfig.get_paths()['stdlib'] directory. You don't need to modify
Python for that.
But to issue a warning when a standard module is being overridden like I was suggesting, wouldn’t you also need to know whether the name of the module being imported is a standard name, which is what says.module_names provides?
—Chris
If you also would like to check if an *extension* module comes from
the stdlib, you need to get the "lib-dynload" directory. I failed to
find a programmatic way to get this directory, maybe new API would be
needed for that.
Victor
--
Night gathers, and now my watch begins. It shall not end until my death.