def is_defined_by_module(item, module):
from xdoctest import static_analysis as static<EOL>target_modname = module.__name__<EOL>flag = False<EOL>if isinstance(item, types.ModuleType):<EOL><INDENT>if not hasattr(item, '<STR_LIT>'):<EOL><INDENT>try:<EOL><INDENT>name = static.modpath_to_modname(module.__file__)<EOL>flag = name in str(item)<EOL><DEDENT>except:<EOL><INDENT>flag = False<EOL><DEDENT><DEDENT>else:<EOL><INDENT>item_modpath = os.path.realpath(os.path.dirname(item.__file__))<EOL>mod_fpath = module.__file__.replace('<STR_LIT>', '<STR_LIT>')<EOL>if not mod_fpath.endswith('<STR_LIT>'):<EOL><INDENT>flag = False<EOL><DEDENT>else:<EOL><INDENT>modpath = os.path.realpath(os.path.dirname(mod_fpath))<EOL>modpath = modpath.replace('<STR_LIT>', '<STR_LIT>')<EOL>flag = item_modpath.startswith(modpath)<EOL><DEDENT><DEDENT><DEDENT>else:<EOL><INDENT>if isinstance(item, property):<EOL><INDENT>item = item.fget<EOL><DEDENT>if isinstance(item, staticmethod):<EOL><INDENT>item = item.__func__<EOL><DEDENT>if isinstance(item, classmethod):<EOL><INDENT>item = item.__func__<EOL><DEDENT>if getattr(item, '<STR_LIT>', None) == target_modname:<EOL><INDENT>flag = True<EOL><DEDENT>elif hasattr(item, '<STR_LIT>'):<EOL><INDENT>parent = item.__objclass__<EOL>if getattr(parent, '<STR_LIT>', None) == target_modname:<EOL><INDENT>flag = True<EOL><DEDENT><DEDENT>if not flag:<EOL><INDENT>try:<EOL><INDENT>item_modname = _func_globals(item)['<STR_LIT>']<EOL>if item_modname == target_modname:<EOL><INDENT>flag = True<EOL><DEDENT><DEDENT>except  AttributeError:<EOL><INDENT>pass<EOL><DEDENT><DEDENT><DEDENT>return flag<EOL>
Check if item is directly defined by a module.

This check may not always work, especially for decorated functions.

Example:
    >>> from xdoctest import dynamic_analysis
    >>> item = dynamic_analysis.is_defined_by_module
    >>> module = dynamic_analysis
    >>> assert is_defined_by_module(item, module)
    >>> item = dynamic_analysis.six
    >>> assert not is_defined_by_module(item, module)
    >>> item = dynamic_analysis.six.print_
    >>> assert not is_defined_by_module(item, module)
    >>> assert not is_defined_by_module(print, module)
    >>> import _ctypes
    >>> item = _ctypes.Array
    >>> module = _ctypes
    >>> assert is_defined_by_module(item, module)
    >>> item = _ctypes.CFuncPtr.restype
    >>> module = _ctypes
    >>> assert is_defined_by_module(item, module)