Example #1
0
def peval_expression(node, gen_sym, bindings):

    ctx = immutableadict(bindings=bindings)
    state = immutableadict(gen_sym=gen_sym, temp_bindings=immutableadict())

    state, result = _peval_expression(state, node, ctx)
    if is_known_value(result):
        state, result_node = map_reify(state, result)
        eval_result = EvaluationResult(
            fully_evaluated=True,
            value=result.value,
            node=result_node,
            temp_bindings=state.temp_bindings)
    else:
        eval_result = EvaluationResult(
            fully_evaluated=False,
            node=result,
            temp_bindings=state.temp_bindings)

    return eval_result, state.gen_sym
Example #2
0
    def __call__(self, node, ctx=None, state=None):

        if not self._inspect and state is not None:
            raise ValueError("Pure transformation walker cannot have a state")

        if ctx is not None:
            ctx = immutableadict(ctx)

        if state is not None:
            state = immutableadict(state)

        if isinstance(node, ast.AST):
            new_node, new_state = self._walk_node(node, state, ctx)
        elif isinstance(node, list):
            new_node, new_state = self._walk_list(node, state, ctx)
        else:
            raise TypeError("Cannot walk an object of type " + str(type(node)))

        if self._transform and self._inspect:
            return new_node, new_state
        elif self._transform:
            return new_node
        elif self._inspect:
            return new_state
Example #3
0
    def __init__(self, tree, globals_, closure_vals, compiler_flags):
        self.tree = tree
        self.globals = globals_
        self.closure_vals = closure_vals

        # Extract enabled future features from compiler flags

        self._compiler_flags = compiler_flags
        future_features = {}
        for feature_name, feature in FUTURE_FEATURES.items():
            enabled_by_flag = compiler_flags & feature.compiler_flag != 0

            enabled_from = feature.getMandatoryRelease()
            enabled_by_default = enabled_from is not None and sys.version_info >= enabled_from

            future_features[feature_name] = enabled_by_flag or enabled_by_default

        self.future_features = immutableadict(future_features)
Example #4
0
def test_adict_repr():
    d = immutableadict(a=1)
    nd = eval(repr(d))
    assert type(nd) == type(d)
    assert nd == d
Example #5
0
def test_adict_setattr():
    d = immutableadict(a=1)
    with pytest.raises(AttributeError):
        d.a = 2
Example #6
0
def test_adict_getattr():
    d = immutableadict(a=1)
    assert d.a == 1