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

Brett Cannon brett at python.org
Wed May 29 16:47:16 CEST 2013


On Wed, May 29, 2013 at 10:34 AM, Nick Coghlan <ncoghlan at gmail.com> wrote:

On Wed, May 29, 2013 at 11:04 PM, Brett Cannon <brett at python.org> wrote:

with initialisemodule(name) as m: # Module initialisation code goes here # Module is rolled back if initialisation fails

But you're not initializing the module; more like getting the module, either new or from sys.modules. But I thought ModuleGetter seemed too Java-like. Could hide the class behind a getmodule function though. The point is to provide a useful mnemonic for why you would use this context manager, and the reason is because the body of the with statement is going to initialize the contents, and you want to unwind things appropriately if that fails.

You should use this context manager to get the correct module to initialize/execute/whatever, e.g. contextlib.closing is about what the context manager is going to do for you, not what you are doing to the object it returned.

initializingmodule is probably a better name than initializedmodule, though (since it isn't initialized yet on entry - instead, that's what should be the case by the end of the statement)

I am willing to compromise to module_to_initialize, module_to_init, or module_to_load. Pick one. =)



More information about the Python-Dev mailing list