[Python-Dev] cpython: Introduce importlib.util.ModuleManager which is a context manager to (original) (raw)

Brett Cannon brett at python.org
Wed May 29 18:25:45 CEST 2013


On Wed, May 29, 2013 at 11:58 AM, R. David Murray <rdmurray at bitdance.com> wrote:

On Thu, 30 May 2013 00:59:02 +1000, Nick Coghlan <ncoghlan at gmail.com> wrote:

On Thu, May 30, 2013 at 12:47 AM, Brett Cannon <brett at python.org> wrote: > I am willing to compromise to moduletoinitialize, moduletoinit, or > moduletoload. Pick one. =)

I see this as really similar to a database transaction, and those start with "session.begin()". Could you tolerate "with beginmoduleinit(name) as m:"? But for a transaction, it is 'with session', not 'with beginsession'. With 'beginmoduleinit' I would have no idea what 'm' was. With Brett's 'moduletoinit' I have an intuitive idea about what 'm' is. And if 'm' isn't a module, then modulemanager would be better. (Note that I haven't grokked what Brett's context manager is actually doing/returning, I'm speaking here as an ignorant reader of someone else's code :)

In case you want to suggest a name, the context manager returns the module that should be initialized/loaded. So typical usage will be::

class Loader: def load_module(self, fullname): with importlib.util.module_to_init(fullname) as module: # Load/initialize the module return module

Basically the manager either gets the module from sys.modules if it is already there (a reload) or creates a new module and sticks it into sys.modules so other imports will grab the right module object. If there is an exception and the module was new, it deletes it from sys.modules to prevent stale modules from sticking around.



More information about the Python-Dev mailing list