def unpack_sequence(self, w_iterable, expected_length): w_len = op.len(w_iterable).eval(self) w_correct = op.eq(w_len, const(expected_length)).eval(self) if not self.guessbool(op.bool(w_correct).eval(self)): w_exc = self.exc_from_raise(const(ValueError), const(None)) raise Raise(w_exc) return [op.getitem(w_iterable, const(i)).eval(self) for i in range(expected_length)]
def test_generalize_getitem_string(annotator): hlop = op.getitem(Variable(), Variable()) s_int = SomeInteger() s_str = SomeString(can_be_None=True) s_value, s_exc = annotate_op(annotator, hlop, [s_None, s_int]) s_value2, s_exc2 = annotate_op(annotator, hlop, [s_str, s_int]) assert contains_s(s_value2, s_value) assert contains_s(s_exc2, s_exc)
def test_getitem_dict(annotator): bk = annotator.bookkeeper hlop = op.getitem(Variable(), Variable()) with bk.at_position(None): s_dict = bk.newdict() s_dict.dictdef.generalize_key(SomeString()) s_dict.dictdef.generalize_value(SomeInteger()) s_result, _ = annotate_op(annotator, hlop, [s_dict, SomeString()]) assert s_result == SomeInteger()
def test_generalize_getitem_list(annotator): bk = annotator.bookkeeper hlop = op.getitem(Variable(), Variable()) s_int = SomeInteger() with bk.at_position(None): s_empty_list = bk.newlist() s_value, s_exc = annotate_op(annotator, hlop, [s_None, s_int]) s_value2, s_exc2 = annotate_op(annotator, hlop, [s_empty_list, s_int]) assert contains_s(s_value2, s_value) assert contains_s(s_exc2, s_exc)
def redispatch_getfield(self, hop, index): rtyper = self.rtyper s_index = rtyper.annotator.bookkeeper.immutablevalue(index) hop2 = hop.copy() spaceop = op.getitem(hop.args_v[0], Constant(index)) spaceop.result = hop.spaceop.result hop2.spaceop = spaceop hop2.args_v = spaceop.args hop2.args_s = [self.s_tuple, s_index] hop2.args_r = [self.r_tuple, rtyper.getrepr(s_index)] return hop2.dispatch()
def transform_varargs(annotator, v_func, v_shape, *data_v): callspec = CallSpec.fromshape(v_shape.value, list(data_v)) v_vararg = callspec.w_stararg if callspec.w_stararg: s_vararg = annotator.annotation(callspec.w_stararg) if not isinstance(s_vararg, SomeTuple): raise AnnotatorError( "Calls like f(..., *arg) require 'arg' to be a tuple") n_items = len(s_vararg.items) ops = [op.getitem(v_vararg, const(i)) for i in range(n_items)] new_args = callspec.arguments_w + [hlop.result for hlop in ops] if callspec.keywords: newspec = CallSpec(new_args, callspec.keywords) shape, data_v = newspec.flatten() call_op = op.call_args(v_func, const(shape), *data_v) else: call_op = op.simple_call(v_func, *new_args) ops.append(call_op) return ops