def test_get_module_source(): assert get_module_source('sphinx') == ('file', sphinx.__file__) # failed to obtain source information from builtin modules with pytest.raises(PycodeError): get_module_source('builtins') with pytest.raises(PycodeError): get_module_source('itertools')
def is_importable(self, modname): """Determine whether members of the given module can be documented with sphinx by using the :func:`sphinx.util.get_module_source` function Parameters ---------- modname: str The __name__ attribute of the module to import Returns ------- bool True if sphinx can import the module""" try: get_module_source(modname) return True except Exception: return False
def for_module(cls, modname): if ('module', modname) in cls.cache: entry = cls.cache['module', modname] if isinstance(entry, PycodeError): raise entry return entry try: type, source = get_module_source(modname) if type == 'string': obj = cls.for_string(source, modname) else: obj = cls.for_file(source, modname) except PycodeError, err: cls.cache['module', modname] = err raise