Dominic Littlewood <11dlittlew...@gmail.com> added the comment:

Okay, I've started putting together a proper PR, and I've had some thoughts.


There's a useful script at the bottom of the importlib documentation that 
readers should consult. This can be used to correctly find the spec for a 
module, and therefore the loader. (AddPackagePath and ReplacePackage are not 
needed, and should be deprecated.) For modules already on sys.path, the loader 
can be identified from module.__loader__.

If the loader is an InspectLoader, the code can be retrieved and examined to 
see what is imported. (Remember to check whether None is returned.) If not, we 
will assume nothing is imported - which is what modulefinder currently does 
with extension modules.

Since run_script takes a file path rather than a module name, it will have to 
find the appropriate loader for the task. This will be a simple choice between 
SourceFileLoader and SourcelessFileLoader, depending on what kind of file is 
being used.


It is also possible to use functions in importlib._bootstrap instead.
This enhancement would indirectly cause open_code to be used properly, so the 
bug I originally posted is redundant.

----------
title: modulefinder should use io.open_code() instead of open() -> modulefinder 
should use import hooks properly
type: security -> enhancement

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue38721>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to