On Sat, Nov 9, 2019 at 10:57 AM Steve Barnes <gadgetst...@live.co.uk> wrote:
> > > > > *From:* Brett Cannon <br...@python.org> > *Sent:* 08 November 2019 18:10 > *To:* Ricky Teachey <ri...@teachey.org> > *Cc:* Dan Sommers <2qdxy4rzwzuui...@potatochowder.com>; python-ideas < > python-ideas@python.org> > *Subject:* [Python-ideas] Re: Suggest having a mechanism to distinguish > import sources > > > > > > > > On Fri, Nov 8, 2019 at 10:01 AM Ricky Teachey <ri...@teachey.org> wrote: > > throwing this idea out there, no idea if it is practical but it might be > pretty nice/easily understood syntax. > > > > could a context manager be created such that anything imported under it is > guaranteed to be imported from the standard library, and produce an error > otherwise? perhaps by adding a level keyword argument to the __import__ > built in. > > > > __import__ already has a 'level' argument. > > *[Steve Barnes] * > > *To be honest this is the first that I have heard of __import__(level) but > looking at the docs and help for 3.8 doesn’t read like what you describe > below – maybe it is an un/under-documented feature – it certainly wasn’t > easy to find in the documentation (* > https://docs.python.org/3/library/functions.html#__import__*)!* > It has to do with handling relative imports. It doesn't have anything to do with the idea being proposed beyond being a parameter name that already exists. > > > *level* specifies whether to use absolute or relative imports. 0 (the > default) means only perform absolute imports. Positive values for *level* > indicate > the number of parent directories to search relative to the directory of the > module calling __import__() > <https://docs.python.org/3/library/functions.html#__import__> (see *PEP > 328* <https://www.python.org/dev/peps/pep-0328> for the details). > > *PEP 328 doesn’t seem to mention any of the names detailed below.* > I strongly advise reading PEPs as documentation once their work has landed. At that point they are mostly historical documents and will not be kept up to date going forward. IOW do not read any import-related PEPs for help; everything will be in the stdlib docs for importlib or in the language reference for imports. -Brett > > > > > something like: > > > > with __import__(level="std"): > > # imports guaranteed to fail of not in the standard library > > from pathlib import Path > > from sys import argv > > > > with __import__(level="package"): > > # imports guaranteed to fail of not in the current package > > import mod1 > > import mod2 > > > > with __import__(level="local"): > > # imports guaranteed to fail of not in the local directory > > import mod1 > > import mod2 > > > > with __import__(level="site"): > > # imports guaranteed to fail if not in site-packages, or some other > definition that makes sense > > import numpy as np > > > > Not without frame inspection to know what import statements are in the > context manager's block. > > > > This can all be done with code which calls importlib.import_module() and > checks __spec__.origin to see where the module came from. Basically if > you're willing to give up the syntax support of 'import' statements (which > are just calls to __import__ with some assignments afterwards to bind > things to names) you can have this protection today without adding syntax > (which is always a massive ask). >
_______________________________________________ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-le...@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/NO2CKBUAXYCLPVJLFJJ3HN2JUTH4UREF/ Code of Conduct: http://python.org/psf/codeofconduct/