[Python-ideas] PEP: Distributing a Subset of the Standard Library

Wolfgang Maier wolfgang.maier at biologie.uni-freiburg.de
Mon Nov 28 17:42:41 EST 2016


On 28.11.2016 23:19, Nathaniel Smith wrote:
>
> 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.
>

A refined (from my previous post which may have ended up too nested) 
alternative: instead of triggering an immediate search for a .missing.py 
file, why not have the interpreter intercept any ModuleNotFoundError 
that bubbles up to the top without being caught, then uses the name 
attribute of the exception to look for the .missing.py file. Agreed, 
this is more complicated to implement, but it would avoid any 
performance loss in situations where running code knows how to deal with 
the missing module anyway.

Wolfgang



More information about the Python-ideas mailing list