(module_context)
return added
Detect sys.path modifications within module.
Detect sys.path modifications within module.
Detect sys . path modifications within module .
def check_sys_path_modifications(module_context):
    """
    Detect sys.path modifications within module.
    """
    def get_sys_path_powers(names):
        for name in names:
            power = name.parent.parent
            if power.type in ('power', 'atom_expr'):
                c = power.children
                if c[0].type == 'name' and c[0].value == 'sys' \
                        and c[1].type == 'trailer':
                    n = c[1].children[1]
                    if n.type == 'name' and n.value == 'path':
                        yield name, power

    if module_context.tree_node is None:
        return []

    added = []
    try:
        possible_names = module_context.tree_node.get_used_names()['path']
    except KeyError:
        pass
    else:
        for name, power in get_sys_path_powers(possible_names):
            expr_stmt = power.parent
            if len(power.children) >= 4:
                added.extend(
                    _paths_from_list_modifications(
                        module_context, *power.children[2:4]
                    )
                )
            elif expr_stmt is not None and expr_stmt.type == 'expr_stmt':
                added.extend(_paths_from_assignment(module_context, expr_stmt))
    return added
def check_sys_path_modifications ( module_context ) : def get_sys_path_powers ( names ) : for name in names : power = name . parent . parent if power . type in ( 'power' , 'atom_expr' ) : c = power . children if c [ 0 ] . type == 'name' and c [ 0 ] . value == 'sys' and c [ 1 ] . type == 'trailer' : n = c [ 1 ] . children [ 1 ] if n . type == 'name' and n . value == 'path' : yield name , power if module_context . tree_node is None : return [ ] added = [ ] try : possible_names = module_context . tree_node . get_used_names ( ) [ 'path' ] except KeyError : pass else : for name , power in get_sys_path_powers ( possible_names ) : expr_stmt = power . parent if len ( power . children ) >= 4 : added . extend ( _paths_from_list_modifications ( module_context , * power . children [ 2 : 4 ] ) ) elif expr_stmt is not None and expr_stmt . type == 'expr_stmt' : added . extend ( _paths_from_assignment ( module_context , expr_stmt ) ) return added