def _lookfor_generate_cache(module, import_modules, regenerate):
global _lookfor_caches<EOL>import inspect<EOL>if sys.version_info[<NUM_LIT:0>] >= <NUM_LIT:3>:<EOL><INDENT>from io import StringIO<EOL><DEDENT>else:<EOL><INDENT>from StringIO import StringIO<EOL><DEDENT>if module is None:<EOL><INDENT>module = "<STR_LIT>"<EOL><DEDENT>if isinstance(module, str):<EOL><INDENT>try:<EOL><INDENT>__import__(module)<EOL><DEDENT>except ImportError:<EOL><INDENT>return {}<EOL><DEDENT>module = sys.modules[module]<EOL><DEDENT>elif isinstance(module, list) or isinstance(module, tuple):<EOL><INDENT>cache = {}<EOL>for mod in module:<EOL><INDENT>cache.update(_lookfor_generate_cache(mod, import_modules,<EOL>regenerate))<EOL><DEDENT>return cache<EOL><DEDENT>if id(module) in _lookfor_caches and not regenerate:<EOL><INDENT>return _lookfor_caches[id(module)]<EOL><DEDENT>cache = {}<EOL>_lookfor_caches[id(module)] = cache<EOL>seen = {}<EOL>index = <NUM_LIT:0><EOL>stack = [(module.__name__, module)]<EOL>while stack:<EOL><INDENT>name, item = stack.pop(<NUM_LIT:0>)<EOL>if id(item) in seen:<EOL><INDENT>continue<EOL><DEDENT>seen[id(item)] = True<EOL>index += <NUM_LIT:1><EOL>kind = "<STR_LIT:object>"<EOL>if inspect.ismodule(item):<EOL><INDENT>kind = "<STR_LIT>"<EOL>try:<EOL><INDENT>_all = item.__all__<EOL><DEDENT>except AttributeError:<EOL><INDENT>_all = None<EOL><DEDENT>if import_modules and hasattr(item, '<STR_LIT>'):<EOL><INDENT>for pth in item.__path__:<EOL><INDENT>for mod_path in os.listdir(pth):<EOL><INDENT>this_py = os.path.join(pth, mod_path)<EOL>init_py = os.path.join(pth, mod_path, '<STR_LIT>')<EOL>if (os.path.isfile(this_py) and<EOL>mod_path.endswith('<STR_LIT>')):<EOL><INDENT>to_import = mod_path[:-<NUM_LIT:3>]<EOL><DEDENT>elif os.path.isfile(init_py):<EOL><INDENT>to_import = mod_path<EOL><DEDENT>else:<EOL><INDENT>continue<EOL><DEDENT>if to_import == '<STR_LIT>':<EOL><INDENT>continue<EOL><DEDENT>try:<EOL><INDENT>base_exc = BaseException<EOL><DEDENT>except NameError:<EOL><INDENT>base_exc = Exception<EOL><DEDENT>try:<EOL><INDENT>old_stdout = sys.stdout<EOL>old_stderr = sys.stderr<EOL>try:<EOL><INDENT>sys.stdout = StringIO()<EOL>sys.stderr = StringIO()<EOL>__import__("<STR_LIT>" % (name, to_import))<EOL><DEDENT>finally:<EOL><INDENT>sys.stdout = old_stdout<EOL>sys.stderr = old_stderr<EOL><DEDENT><DEDENT>except base_exc:<EOL><INDENT>continue<EOL><DEDENT><DEDENT><DEDENT><DEDENT>for n, v in _getmembers(item):<EOL><INDENT>try:<EOL><INDENT>item_name = getattr(v, '<STR_LIT>', "<STR_LIT>" % (name, n))<EOL>mod_name = getattr(v, '<STR_LIT>', None)<EOL><DEDENT>except NameError:<EOL><INDENT>item_name = "<STR_LIT>" % (name, n)<EOL>mod_name = None<EOL><DEDENT>if '<STR_LIT:.>' not in item_name and mod_name:<EOL><INDENT>item_name = "<STR_LIT>" % (mod_name, item_name)<EOL><DEDENT>if not item_name.startswith(name + '<STR_LIT:.>'):<EOL><INDENT>if isinstance(v, ufunc):<EOL><INDENT>pass<EOL><DEDENT>else:<EOL><INDENT>continue<EOL><DEDENT><DEDENT>elif not (inspect.ismodule(v) or _all is None or n in _all):<EOL><INDENT>continue<EOL><DEDENT>stack.append(("<STR_LIT>" % (name, n), v))<EOL><DEDENT><DEDENT>elif inspect.isclass(item):<EOL><INDENT>kind = "<STR_LIT:class>"<EOL>for n, v in _getmembers(item):<EOL><INDENT>stack.append(("<STR_LIT>" % (name, n), v))<EOL><DEDENT><DEDENT>elif hasattr(item, "<STR_LIT>"):<EOL><INDENT>kind = "<STR_LIT>"<EOL><DEDENT>try:<EOL><INDENT>doc = inspect.getdoc(item)<EOL><DEDENT>except NameError:<EOL><INDENT>doc = None<EOL><DEDENT>if doc is not None:<EOL><INDENT>cache[name] = (doc, kind, index)<EOL><DEDENT><DEDENT>return cache<EOL>
Generate docstring cache for given module.

Parameters
----------
module : str, None, module
    Module for which to generate docstring cache
import_modules : bool
    Whether to import sub-modules in packages.
regenerate : bool
    Re-generate the docstring cache

Returns
-------
cache : dict {obj_full_name: (docstring, kind, index), ...}
    Docstring cache for the module, either cached one (regenerate=False)
    or newly generated.