def test_module_utils_basic_imports(self): realimport = builtins.__import__ def _mock_import(name, *args, **kwargs): if name == 'json': raise ImportError() realimport(name, *args, **kwargs) with patch.object(builtins, '__import__', _mock_import, create=True) as m: m('ansible.module_utils.basic') builtins.__import__('ansible.module_utils.basic')
def cython_import(filename, **kwds): """ Compile a file containing Cython code, then import and return the module. Raises an ``ImportError`` if anything goes wrong. INPUT: - ``filename`` - a string; name of a file that contains Cython code See the function :func:`sage.misc.cython.cython` for documentation for the other inputs. OUTPUT: - the module that contains the compiled Cython code. """ name, build_dir = cython(filename, **kwds) oldpath = sys.path try: sys.path.append(build_dir) return builtins.__import__(name) finally: sys.path = oldpath
def cython_import(filename, verbose=False, compile_message=False, use_cache=False, create_local_c_file=True, **kwds): """ Compile a file containing Cython code, then import and return the module. Raises an ``ImportError`` if anything goes wrong. INPUT: - ``filename`` - a string; name of a file that contains Cython code See the function :func:`sage.misc.cython.cython` for documentation for the other inputs. OUTPUT: - the module that contains the compiled Cython code. """ name, build_dir = cython(filename, verbose=verbose, compile_message=compile_message, use_cache=use_cache, create_local_c_file=create_local_c_file, **kwds) sys.path.append(build_dir) return builtins.__import__(name)