New submission from Armin Ronacher: 3.4 deprecates load_module on the loaders and now proposes to use create_module (optionally) and exec_module. Unfortunately for external callers these interfaces are not useful because you need to reimplement _SpecMethods.create and a whole bunch of other stuff for it.
Right now a caller can get hold of _SpecMethods by importing from _frozen_importlib but that seems very unsupported and is obviously entirely not exposed. Is there a reason those methods are not on the ModuleSpec directly but on a private wrapper? Example usage: from types import ModuleType from importlib.machinery import ModuleSpec, SourceFileLoader from _frozen_importlib import _SpecMethods loader = SourceFileLoader('plugin_base', 'my_file.py') spec = ModuleSpec(name=loader.name, loader=loader) mod = _SpecMethods(spec).create() In the absence of _SpecMethods a caller needs to do this: from importlib.machinery import ModuleSpec, SourceFileLoader loader = SourceFileLoader('plugin_base', 'my_file.py') spec = ModuleSpec(name=loader.name, loader=loader) if not hasattr(loader, 'create_module'): module = types.ModuleType(loader.name) else: module = loader.create_module(spec) module.__loader__ = loader module.__spec__ = spec try: module.__package__ = spec.parent except AttributeError: pass loader.exec_module(module) (And that is not the whole logic yet) ---------- keywords: 3.4regression messages: 216295 nosy: aronacher priority: normal severity: normal status: open title: importlib's spec module create algorithm is not exposed versions: Python 3.4 _______________________________________ Python tracker <rep...@bugs.python.org> <http://bugs.python.org/issue21235> _______________________________________ _______________________________________________ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com