> __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?

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/Y4O7TM2ETWWYWDWSAOKNCC2HXIM34FID/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to