<s> """<STR_LIT>""" <EOL> import itertools <EOL> from jedi . _compatibility import next , hasattr , unicode <EOL> from jedi . parser import representation as pr <EOL> from jedi . parser . tokenize import Token <EOL> from jedi import debug <EOL> from jedi . evaluate import representation as er <EOL> from jedi . evaluate import imports <EOL> from jedi . evaluate import recursion <EOL> from jedi . evaluate import iterable <EOL> from jedi . evaluate . cache import memoize_default <EOL> from jedi . evaluate import stdlib <EOL> from jedi . evaluate import finder <EOL> from jedi . evaluate import compiled <EOL> from jedi . evaluate import precedence <EOL> class Evaluator ( object ) : <EOL> def __init__ ( self ) : <EOL> self . memoize_cache = { } <EOL> self . import_cache = { } <EOL> self . compiled_cache = { } <EOL> self . recursion_detector = recursion . RecursionDetector ( ) <EOL> self . execution_recursion_detector = recursion . ExecutionRecursionDetector ( ) <EOL> def find_types ( self , scope , name_str , position = None , search_global = False , <EOL> is_goto = False , resolve_decorator = True ) : <EOL> """<STR_LIT>""" <EOL> f = finder . NameFinder ( self , scope , name_str , position ) <EOL> scopes = f . scopes ( search_global ) <EOL> if is_goto : <EOL> return f . filter_name ( scopes ) <EOL> return f . find ( scopes , resolve_decorator ) <EOL> @ memoize_default ( default = [ ] , evaluator_is_first_arg = True ) <EOL> @ recursion . recursion_decorator <EOL> @ debug . increase_indent <EOL> def eval_statement ( self , stmt , seek_name = None ) : <EOL> """<STR_LIT>""" <EOL> debug . dbg ( '<STR_LIT>' , stmt , seek_name ) <EOL> expression_list = stmt . expression_list ( ) <EOL> result = self . eval_expression_list ( expression_list ) <EOL> ass_details = stmt . assignment_details <EOL> if ass_details and ass_details [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] != '<STR_LIT:=>' and not isinstance ( stmt , er . InstanceElement ) : <EOL> expr_list , operator = ass_details [ <NUM_LIT:0> ] <EOL> operator = operator [ : - <NUM_LIT:1> ] <EOL> name = str ( expr_list [ <NUM_LIT:0> ] . name ) <EOL> left = self . find_types ( stmt . parent , name , stmt . start_pos ) <EOL> if isinstance ( stmt . parent , pr . ForFlow ) : <EOL> for r in result : <EOL> left = precedence . calculate ( self , left , operator , [ r ] ) <EOL> result = left <EOL> else : <EOL> result = precedence . calculate ( self , left , operator , result ) <EOL> elif len ( stmt . get_defined_names ( ) ) > <NUM_LIT:1> and seek_name and ass_details : <EOL> new_result = [ ] <EOL> for ass_expression_list , op in ass_details : <EOL> new_result += finder . find_assignments ( ass_expression_list [ <NUM_LIT:0> ] , result , seek_name ) <EOL> result = new_result <EOL> return result <EOL> def eval_expression_list ( self , expression_list ) : <EOL> """<STR_LIT>""" <EOL> debug . dbg ( '<STR_LIT>' , expression_list ) <EOL> p = precedence . create_precedence ( expression_list ) <EOL> return self . process_precedence_element ( p ) or [ ] <EOL> def process_precedence_element ( self , el ) : <EOL> if el is None : <EOL> return None <EOL> else : <EOL> if isinstance ( el , precedence . Precedence ) : <EOL> return self . _eval_precedence ( el ) <EOL> else : <EOL> return self . _eval_statement_element ( el ) <EOL> def _eval_precedence ( self , _precedence ) : <EOL> left = self . process_precedence_element ( _precedence . left ) <EOL> right = self . process_precedence_element ( _precedence . right ) <EOL> return precedence . calculate ( self , left , _precedence . operator , right ) <EOL> def _eval_statement_element ( self , element ) : <EOL> if pr . Array . is_type ( element , pr . Array . NOARRAY ) : <EOL> r = list ( itertools . chain . from_iterable ( self . eval_statement ( s ) <EOL> for s in element ) ) <EOL> call_path = element . generate_call_path ( ) <EOL> next ( call_path , None ) <EOL> return self . follow_path ( call_path , r , element . parent ) <EOL> elif isinstance ( element , pr . ListComprehension ) : <EOL> loop = _evaluate_list_comprehension ( element ) <EOL> element . stmt . parent = loop <EOL> return self . eval_statement ( element . stmt ) <EOL> elif isinstance ( element , pr . Lambda ) : <EOL> return [ er . Function ( self , element ) ] <EOL> elif isinstance ( element , pr . Base ) and element . isinstance ( <EOL> er . Function , er . Class , er . Instance , iterable . ArrayInstance ) : <EOL> return [ element ] <EOL> elif isinstance ( element , compiled . CompiledObject ) : <EOL> return [ element ] <EOL> elif not isinstance ( element , Token ) : <EOL> return self . eval_call ( element ) <EOL> else : <EOL> return [ ] <EOL> def eval_call ( self , call ) : <EOL> """<STR_LIT>""" <EOL> path = call . generate_call_path ( ) <EOL> s = call <EOL> while not s . parent . isinstance ( pr . IsScope ) : <EOL> s = s . parent <EOL> return self . eval_call_path ( path , s . parent , s . start_pos ) <EOL> def eval_call_path ( self , path , scope , position ) : <EOL> """<STR_LIT>""" <EOL> current = next ( path ) <EOL> if isinstance ( current , pr . Array ) : <EOL> types = [ iterable . Array ( self , current ) ] <EOL> else : <EOL> if isinstance ( current , pr . NamePart ) : <EOL> types = self . find_types ( scope , current , position = position , <EOL> search_global = True ) <EOL> else : <EOL> types = [ compiled . create ( self , current . value ) ] <EOL> types = imports . strip_imports ( self , types ) <EOL> return self . follow_path ( path , types , scope ) <EOL> def follow_path ( self , path , types , call_scope ) : <EOL> """<STR_LIT>""" <EOL> results_new = [ ] <EOL> iter_paths = itertools . tee ( path , len ( types ) ) <EOL> for i , typ in enumerate ( types ) : <EOL> fp = self . _follow_path ( iter_paths [ i ] , typ , call_scope ) <EOL> if fp is not None : <EOL> results_new += fp <EOL> else : <EOL> return types <EOL> return results_new <EOL> def _follow_path ( self , path , typ , scope ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> current = next ( path ) <EOL> except StopIteration : <EOL> return None <EOL> debug . dbg ( '<STR_LIT>' , current , typ ) <EOL> result = [ ] <EOL> if isinstance ( current , pr . Array ) : <EOL> if current . type == pr . Array . LIST : <EOL> if hasattr ( typ , '<STR_LIT>' ) : <EOL> slc = iterable . create_indexes_or_slices ( self , current ) <EOL> result = typ . get_index_types ( slc ) <EOL> elif current . type not in [ pr . Array . DICT ] : <EOL> result = self . execute ( typ , current ) <EOL> else : <EOL> debug . warning ( '<STR_LIT>' , current , typ ) <EOL> else : <EOL> if typ . isinstance ( er . Function ) : <EOL> typ = typ . get_magic_function_scope ( ) <EOL> else : <EOL> if filter_private_variable ( typ , scope , current ) : <EOL> return [ ] <EOL> types = self . find_types ( typ , current ) <EOL> result = imports . strip_imports ( self , types ) <EOL> return self . follow_path ( path , result , scope ) <EOL> @ debug . increase_indent <EOL> def execute ( self , obj , params = ( ) , evaluate_generator = False ) : <EOL> if obj . isinstance ( er . Function ) : <EOL> obj = obj . get_decorated_func ( ) <EOL> debug . dbg ( '<STR_LIT>' , obj , params ) <EOL> try : <EOL> return stdlib . execute ( self , obj , params ) <EOL> except stdlib . NotInStdLib : <EOL> pass <EOL> if isinstance ( obj , iterable . GeneratorMethod ) : <EOL> return obj . execute ( ) <EOL> elif obj . isinstance ( compiled . CompiledObject ) : <EOL> if obj . is_executable_class ( ) : <EOL> return [ er . Instance ( self , obj , params ) ] <EOL> else : <EOL> return list ( obj . execute_function ( self , params ) ) <EOL> elif obj . isinstance ( er . Class ) : <EOL> return [ er . Instance ( self , obj , params ) ] <EOL> else : <EOL> stmts = [ ] <EOL> if obj . isinstance ( er . Function ) : <EOL> stmts = er . FunctionExecution ( self , obj , params ) . get_return_types ( evaluate_generator ) <EOL> else : <EOL> if hasattr ( obj , '<STR_LIT>' ) : <EOL> try : <EOL> stmts = obj . execute_subscope_by_name ( '<STR_LIT>' , params ) <EOL> except KeyError : <EOL> debug . warning ( "<STR_LIT>" , obj ) <EOL> else : <EOL> debug . warning ( "<STR_LIT>" , obj ) <EOL> debug . dbg ( '<STR_LIT>' , stmts , obj ) <EOL> return imports . strip_imports ( self , stmts ) <EOL> def goto ( self , stmt , call_path ) : <EOL> scope = stmt . get_parent_until ( pr . IsScope ) <EOL> pos = stmt . start_pos <EOL> call_path , search_name_part = call_path [ : - <NUM_LIT:1> ] , call_path [ - <NUM_LIT:1> ] <EOL> if call_path : <EOL> scopes = self . eval_call_path ( iter ( call_path ) , scope , pos ) <EOL> search_global = False <EOL> pos = None <EOL> else : <EOL> scopes = [ scope ] <EOL> search_global = True <EOL> follow_res = [ ] <EOL> for s in scopes : <EOL> follow_res += self . find_types ( s , search_name_part , pos , <EOL> search_global = search_global , is_goto = True ) <EOL> return follow_res , search_name_part <EOL> def filter_private_variable ( scope , call_scope , var_name ) : <EOL> """<STR_LIT>""" <EOL> var_name = str ( var_name ) <EOL> if isinstance ( var_name , ( str , unicode ) ) and isinstance ( scope , er . Instance ) and var_name . startswith ( '<STR_LIT>' ) and not var_name . endswith ( '<STR_LIT>' ) : <EOL> s = call_scope . get_parent_until ( ( pr . Class , er . Instance , compiled . CompiledObject ) ) <EOL> if s != scope : <EOL> if isinstance ( scope . base , compiled . CompiledObject ) : <EOL> if s != scope . base : <EOL> return True <EOL> else : <EOL> if s != scope . base . base : <EOL> return True <EOL> return False <EOL> def _evaluate_list_comprehension ( lc , parent = None ) : <EOL> input = lc . input <EOL> nested_lc = input . expression_list ( ) [ <NUM_LIT:0> ] <EOL> if isinstance ( nested_lc , pr . ListComprehension ) : <EOL> input = nested_lc . stmt <EOL> module = input . get_parent_until ( ) <EOL> loop = pr . ForFlow ( module , [ input ] , lc . stmt . start_pos , lc . middle , True ) <EOL> loop . parent = parent or lc . get_parent_until ( pr . IsScope ) <EOL> if isinstance ( nested_lc , pr . ListComprehension ) : <EOL> loop = _evaluate_list_comprehension ( nested_lc , loop ) <EOL> return loop </s>
