[Python-Dev] cpython: Introduce importlib.util.ModuleManager which is a context manager to (original) (raw)
Eric Snow ericsnowcurrently at gmail.com
Thu May 30 08:10:07 CEST 2013
- Previous message: [Python-Dev] cpython: Introduce importlib.util.ModuleManager which is a context manager to
- Next message: [Python-Dev] cpython: Introduce importlib.util.ModuleManager which is a context manager to
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
On Wed, May 29, 2013 at 2:22 PM, Brett Cannon <brett at python.org> wrote:
So moduletoinit it is unless someone can convince me the bikeshed is a different colour.
Whatever the name is, it should reflect what is happening during the with statement, and more particularly that the thing will end at the end of the with statement. managed_module() seems fine to me though it could still imply the lifetime of the module rather than the management. During the with statement the module is managed, and I expect it's clear that the management is relative to the import system.
However, it could also make sense to split the function into two pieces: getting the module and handling it properly in the face of exceptions in a with statement. So, importlib.util.get_module() and ModuleType.managed():
class Loader: def load_module(self, fullname): module = importlib.util.get_module(fullname) with module.managed(): # Load/initialize the module return module
If ModuleType.managed() returned the module, you could do it on one line:
class Loader: def load_module(self, fullname): with importlib.util.get_module(fullname).managed() as module: # Load/initialize the module return module
On second thought, that "one-liner" is a little too busy. And if it's a problem as a method on ModuleType, make it importlib.util.managed_module():
class Loader: def load_module(self, fullname): module = importlib.util.get_module(fullname) with importlib.util.managed_module(module): # Load/initialize the module return module
It would be nice to have both parts in one function. It would be less boilerplate for the "common" case that way, is easier to read, and eliminates the risk of someone not realizing they need both parts. However, I'm not sure it buys us that much, the separate-part approach helps keep the two concepts distinct (for better or for worse), and each piece could be separately useful. Maybe have a third function that wraps the other two or have managed_module() accept strings (and then call get_module() internally).
-eric
- Previous message: [Python-Dev] cpython: Introduce importlib.util.ModuleManager which is a context manager to
- Next message: [Python-Dev] cpython: Introduce importlib.util.ModuleManager which is a context manager to
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]