On Mon, Nov 28, 2016 at 5:28 AM, Tomas Orsava <tors...@redhat.com> wrote:
[...]
> Specification
> =============
>
> When, for any reason, a standard library module is not to be included with
> the
> rest, a file with its name and the extension ``.missing.py`` shall be
> created
> and placed in the directory the module itself would have occupied.  This
> file
> can contain any Python code, however, it *should* raise a
> ModuleNotFoundError_
> with a helpful error message.
>
> Currently, when Python tries to import a module ``XYZ``, the ``FileFinder``
> path hook goes through the entries in ``sys.path``, and in each location
> looks
> for a file whose name is ``XYZ`` with one of the valid suffixes (e.g.
> ``.so``,
> ..., ``.py``, ..., ``.pyc``).  The suffixes are tried in order.  If none of
> them are found, Python goes on to try the next directory in ``sys.path``.
>
> The ``.missing.py`` extension will be added to the end of the list, and
> configured to be handled by ``SourceFileLoader``.  Thus, if a module is not
> found in its proper location, the ``XYZ.missing.py`` file is found and
> executed, and further locations are not searched.

I'd suggest that we additional specify that if we find a
foo.missing.py, then the code is executed but -- unlike a regular
module load -- it's not automatically inserted into
sys.modules["foo"]. That seems like it could only create confusion.
And it doesn't restrict functionality, because if someone really wants
to implement some clever shenanigans, they can always modify
sys.modules["foo"] by hand.

This also suggests that the overall error-handling flow for 'import
foo' should look like:

1) run foo.missing.py
2) if it raises an exception: propagate that
3) otherwise, if sys.modules["foo"] is missing: raise some variety of
ImportError.
4) otherwise, use sys.modules["foo"] as the object that should be
bound to 'foo' in the original invoker's namespace

I think this might make everyone who was worried about exception
handling downthread happy -- it allows a .missing.py file to
successfully import if it really wants to, but only if it explicitly
fulfills 'import' requirement that the module should somehow be made
available.

-n

-- 
Nathaniel J. Smith -- https://vorpus.org
_______________________________________________
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to