On 28Nov2016 1433, Steve Dower wrote:
On 28Nov2016 1419, 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.
In before Brett says "you can do this with an import hook", because,
well, we can do this with an import hook :)
And since I suggested it, here's a rough proof-of-concept:
import importlib.abc
import os
import sys
class MissingPathFinder(importlib.abc.MetaPathFinder):
def find_spec(self, fullname, path, target=None):
for p in (path or sys.path):
file = os.path.join(p, fullname + ".missing")
if os.path.isfile(file):
with open(file, 'r', encoding='utf-8') as f:
raise ModuleNotFoundError(f.read())
sys.meta_path.append(MissingPathFinder())
import foo
Add a "foo.missing" file to your working directory and you'll get the
message from that instead of the usual one.
Cheers,
Steve
_______________________________________________
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/