interpreter: use typed_pos_args for func_import

and make the helper method private
pull/8950/head
Dylan Baker 4 years ago
parent 6337e40115
commit 351a1e9ec9
  1. 34
      mesonbuild/interpreter/interpreter.py

@ -601,35 +601,33 @@ class Interpreter(InterpreterBase, HoldableObject):
dep = df.lookup(kwargs, force_fallback=True) dep = df.lookup(kwargs, force_fallback=True)
self.build.stdlibs[for_machine][l] = dep self.build.stdlibs[for_machine][l] = dep
def import_module(self, modname): @typed_pos_args('import', str)
if modname in self.modules:
return
try:
module = importlib.import_module('mesonbuild.modules.' + modname)
except ImportError:
raise InvalidArguments(f'Module "{modname}" does not exist')
ext_module = module.initialize(self)
assert isinstance(ext_module, ModuleObject)
self.modules[modname] = ext_module
@stringArgs
@noKwargs @noKwargs
def func_import(self, node, args, kwargs): def func_import(self, node: mparser.BaseNode, args: T.Tuple[str], kwargs) -> ModuleObject:
if len(args) != 1:
raise InvalidCode('Import takes one argument.')
modname = args[0] modname = args[0]
if modname.startswith('unstable-'): if modname.startswith('unstable-'):
plainname = modname.split('-', 1)[1] plainname = modname.split('-', 1)[1]
try: try:
# check if stable module exists # check if stable module exists
self.import_module(plainname) self._import_module(plainname)
mlog.warning(f'Module {modname} is now stable, please use the {plainname} module instead.') mlog.warning(f'Module {modname} is now stable, please use the {plainname} module instead.')
modname = plainname modname = plainname
except InvalidArguments: except InvalidArguments:
mlog.warning('Module %s has no backwards or forwards compatibility and might not exist in future releases.' % modname, location=node) mlog.warning('Module %s has no backwards or forwards compatibility and might not exist in future releases.' % modname, location=node)
modname = 'unstable_' + plainname modname = 'unstable_' + plainname
self.import_module(modname)
return self.modules[modname] if modname in self.modules:
return self.modules[modname]
try:
module = importlib.import_module('mesonbuild.modules.' + modname)
except ImportError:
raise InvalidArguments(f'Module "{modname}" does not exist')
ext_module = module.initialize(self)
assert isinstance(ext_module, ModuleObject)
self.modules[modname] = ext_module
return ext_module
@stringArgs @stringArgs
@noKwargs @noKwargs

Loading…
Cancel
Save