#NEWLINE# def findsource( pyObject ) : #NEWLINE# #INDENT# 'Return the entire source file and starting line number for an object.\n\n    The argument may be a module, class, method, function, traceback, frame,\n    or code object.  The source code is returned as a list of all the lines\n    in the file and the line number indexes a line in that list.  An IOError\n    is raised if the source code cannot be retrieved.' #NEWLINE# pyFile = getfile ( pyObject ) #NEWLINE# sourcefile = getsourcefile ( pyObject ) #NEWLINE# if ( ( not sourcefile ) and ( ( pyFile [ : 1 ] + pyFile [ ( - 1 ) : ] ) != '<>' ) ) : #NEWLINE# #INDENT# raise IOError ( 'source code not available' ) #NEWLINE# #UNINDENT# pyFile = ( sourcefile if sourcefile else file ) #NEWLINE# module = getmodule ( pyObject , pyFile ) #NEWLINE# lines = getlines ( pyFile ) #NEWLINE# if ( not lines ) : #NEWLINE# #INDENT# raise IOError ( 'could not get source code' ) #NEWLINE# #UNINDENT# if ismodule ( pyObject ) : #NEWLINE# #INDENT# return ( lines , 0 ) #NEWLINE# #UNINDENT# if isclass ( pyObject ) : #NEWLINE# #INDENT# name = pyObject . __name__ #NEWLINE# pat = re . compile ( ( ( '^(\\s*)class\\s*' + name ) + '\\b' ) ) #NEWLINE# candidates = [ ] #NEWLINE# for i in range ( len ( lines ) ) : #NEWLINE# #INDENT# match = pat . match ( lines [ i ] ) #NEWLINE# if match : #NEWLINE# #INDENT# candidates . append ( i ) #NEWLINE# #UNINDENT# #UNINDENT# if ( not candidates ) : #NEWLINE# #INDENT# raise IOError ( ( 'could not find class definition for %s' % pyObject ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( len ( candidates ) > 1 ) : #NEWLINE# #INDENT# raise PyforaInspectError ( ( 'could not find class unequivocally: class ' + name ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# lnum = candidates [ 0 ] #NEWLINE# pat = re . compile ( '^(\\s*)@\\w+' ) #NEWLINE# while ( ( lnum > 0 ) and pat . match ( lines [ ( lnum - 1 ) ] ) ) : #NEWLINE# #INDENT# lnum -= 1 #NEWLINE# #UNINDENT# return ( lines , lnum ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# if ismethod ( pyObject ) : #NEWLINE# #INDENT# pyObject = pyObject . im_func #NEWLINE# #UNINDENT# if isfunction ( pyObject ) : #NEWLINE# #INDENT# pyObject = pyObject . func_code #NEWLINE# #UNINDENT# if istraceback ( pyObject ) : #NEWLINE# #INDENT# pyObject = pyObject . tb_frame #NEWLINE# #UNINDENT# if isframe ( i ) : #NEWLINE# #INDENT# pyObject = pyObject . f_code #NEWLINE# #UNINDENT# if iscode ( pyObject ) : #NEWLINE# #INDENT# if ( pyFile == '<stdin>' ) : #NEWLINE# #INDENT# lnum = StdinCache . singleton ( ) . refreshFromReadline ( ) . findCodeLineNumberWithinStdin ( pyObject ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( not hasattr ( pyObject , 'co_firstlineno' ) ) : #NEWLINE# #INDENT# raise IOError ( 'could not find function definition' ) #NEWLINE# #UNINDENT# lnum = ( pyObject . co_firstlineno - 1 ) #NEWLINE# #UNINDENT# pat = re . compile ( '^(\\s*def\\s)|(.*(?<!\\w)lambda(:|\\s))|^(\\s*@)' ) #NEWLINE# while ( lnum > 0 ) : #NEWLINE# #INDENT# if pat . match ( lines [ lnum ] ) : #NEWLINE# #INDENT# break #NEWLINE# #UNINDENT# lnum = ( lnum - 1 ) #NEWLINE# #UNINDENT# return ( lines , lnum ) #NEWLINE# #UNINDENT# raise IOError ( 'could not find code object' )