On Fri, Nov 8, 2019 at 11:07 AM Ricky Teachey <ri...@teachey.org> wrote:
> > >> __import__ already has a 'level' argument. >> > > doh! maybe "context" is better, then. > > >> Not without frame inspection to know what import statements are in the >> context manager's block. >> >> > I don't doubt you know what you're talking about, so this is a learning > question: why couldn't you do it by simply adding an > "inside_context_manager" flag, and producing an appropriate globals and > locals based on the flag value? > Not thread-safe (we have a custom import lock to avoid this sort of problem) and the instant you start mucking with globals() and locals() you have (a) become a bit magical, and (b) made things extremely slow for PyPy. My opinion on this idea is while I appreciate the motivation behind it, this is proposing a lot of churn and change in Python to fix a single issue that only some people hit and most people learn to avoid the first time to realize what they have done. If it was a problem people ran into constantly throughout their life using Python then maybe I would personally be more receptive, but since it's a one-time mistake for vast majority of people I think I don't feel comfortable making every book on Python be outdated overnight over this. -Brett > > this is really lame, but maybe like this: > > class __import__: > > def __enter__(self): > self.inside_context_manager = True > return None > > def __exit__(self, *args): > self.inside_context_manager = False > > def __call__(self, name, globals=None, locals=None, fromlist=(), > level=0, context=None): > am_i_outside_context_manager = not self.inside_context_manager > if am_i_outside_context_manager: > do_normal_import(name, globals, locals, fromlist, level) > else: > # inside context manager > if context is None: > raise ValueError("invalid import context") > cglobals = contextualized_globals(globals, context) > clocals = contextualized_locals(locals, context) > do_special_import(name, cglobals, clocals, fromlist, level) > > >
_______________________________________________ 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/UM3WJZDPGS6C2PWVUOKJX6WLHLSZXPQI/ Code of Conduct: http://python.org/psf/codeofconduct/