And how would Python compute the "full basename of the file to be
imported"?
How could it guess among all directories on sys.path the one containing
the "file", and check if it is a file or a package without going through
the existing mechanism?
Maybe this proposal is good - but possibly, just cutingt the "full
basename" and pass
to the "simple hook" whatever string is on the import statement would be
better.
On Tue, 8 Dec 2020 at 06:30, Paul Sokolovsky
Hello,
It would be nice to have a simple and easy way to override module importing, instead of that babylon which CPython has in that regard. (Even if initially "limited" to filesystem importing, most people won't ever find that limiting at all.)
The desired semantics would be:
For "import foo", before "/path1/foo.py", "/path2/foo.py" (where /path1:/path2 come from sys.path) is tried to be imported, a user-defined hook function can be called. It should take just one param - full basename of the module to be imported, e.g. "/path1/foo", "/path2/foo" for the cases above. The hook should either return a module object, the result of the import, or None to signal that standard processing should take place. The hook should be aware that there might have been previous hooks, and it's current hook's responsibility to call them.
Example of a hook:
def import_hook(path): if os.path.exists(path + ".my_ext"): mod = imp.new_module("") # populate mod here return mod
A crucial semantic detail is that all possible module types from one sys.path entry should be tried first, before going to the next entry. (CPython import extensibility instead seems to be built around idea that it's fine to do multiple passes over sys.path, loading only a particular extension module type on each pass, which is not the desired semantics).
Questions:
1. Does anybody know such thing to already exist? 2. Would you tweak anything in the API/boilerplate semantics of the above?
-- Best regards, Paul mailto:pmiscml@gmail.com _______________________________________________ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-leave@python.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/NIQQVA... Code of Conduct: http://python.org/psf/codeofconduct/