(object)
Return the entire source file and starting line number for an object.

    The argument may be a module, class, method, function, traceback, frame,
    or code object.  The source code is returned as a list of all the lines
    in the file and the line number indexes a line in that list.  An OSError
    is raised if the source code cannot be retrieved.
Return the entire source file and starting line number for an object.
Return the entire source file and starting line number for an object .
def findsource(object):
    """Return the entire source file and starting line number for an object.

    The argument may be a module, class, method, function, traceback, frame,
    or code object.  The source code is returned as a list of all the lines
    in the file and the line number indexes a line in that list.  An OSError
    is raised if the source code cannot be retrieved."""

    file = getsourcefile(object)
    if file:
        # Invalidate cache if needed.
        linecache.checkcache(file)
    else:
        file = getfile(object)
        # Allow filenames in form of "<something>" to pass through.
        # `doctest` monkeypatches `linecache` module to enable
        # inspection, so let `linecache.getlines` to be called.
        if not (file.startswith('<') and file.endswith('>')):
            raise OSError('source code not available')

    module = getmodule(object, file)
    if module:
        lines = linecache.getlines(file, module.__dict__)
    else:
        lines = linecache.getlines(file)
    if not lines:
        raise OSError('could not get source code')

    if ismodule(object):
        return lines, 0

    if isclass(object):
        qualname = object.__qualname__
        source = ''.join(lines)
        tree = ast.parse(source)
        class_finder = _ClassFinder(qualname)
        try:
            class_finder.visit(tree)
        except ClassFoundException as e:
            line_number = e.args[0]
            return lines, line_number
        else:
            raise OSError('could not find class definition')

    if ismethod(object):
        object = object.__func__
    if isfunction(object):
        object = object.__code__
    if istraceback(object):
        object = object.tb_frame
    if isframe(object):
        object = object.f_code
    if iscode(object):
        if not hasattr(object, 'co_firstlineno'):
            raise OSError('could not find function definition')
        lnum = object.co_firstlineno - 1
        pat = re.compile(r'^(\s*def\s)|(\s*async\s+def\s)|(.*(?<!\w)lambda(:|\s))|^(\s*@)')
        while lnum > 0:
            try:
                line = lines[lnum]
            except IndexError:
                raise OSError('lineno is out of bounds')
            if pat.match(line):
                break
            lnum = lnum - 1
        return lines, lnum
    raise OSError('could not find code object')
def findsource ( object ) : file = getsourcefile ( object ) if file : # Invalidate cache if needed. linecache . checkcache ( file ) else : file = getfile ( object ) # Allow filenames in form of "<something>" to pass through. # `doctest` monkeypatches `linecache` module to enable # inspection, so let `linecache.getlines` to be called. if not ( file . startswith ( '<' ) and file . endswith ( '>' ) ) : raise OSError ( 'source code not available' ) module = getmodule ( object , file ) if module : lines = linecache . getlines ( file , module . __dict__ ) else : lines = linecache . getlines ( file ) if not lines : raise OSError ( 'could not get source code' ) if ismodule ( object ) : return lines , 0 if isclass ( object ) : qualname = object . __qualname__ source = '' . join ( lines ) tree = ast . parse ( source ) class_finder = _ClassFinder ( qualname ) try : class_finder . visit ( tree ) except ClassFoundException as e : line_number = e . args [ 0 ] return lines , line_number else : raise OSError ( 'could not find class definition' ) if ismethod ( object ) : object = object . __func__ if isfunction ( object ) : object = object . __code__ if istraceback ( object ) : object = object . tb_frame if isframe ( object ) : object = object . f_code if iscode ( object ) : if not hasattr ( object , 'co_firstlineno' ) : raise OSError ( 'could not find function definition' ) lnum = object . co_firstlineno - 1 pat = re . compile ( r'^(\s*def\s)|(\s*async\s+def\s)|(.*(?<!\w)lambda(:|\s))|^(\s*@)' ) while lnum > 0 : try : line = lines [ lnum ] except IndexError : raise OSError ( 'lineno is out of bounds' ) if pat . match ( line ) : break lnum = lnum - 1 return lines , lnum raise OSError ( 'could not find code object' )