Example #1
0
 def test_unicode(self):
     l1 = W_ListObject(self.space, [self.space.wrapbytes("eins"), self.space.wrapbytes("zwei")])
     assert isinstance(l1.strategy, BytesListStrategy)
     l2 = W_ListObject(self.space, [self.space.wrap(u"eins"), self.space.wrap(u"zwei")])
     assert isinstance(l2.strategy, UnicodeListStrategy)
     l3 = W_ListObject(self.space, [self.space.wrapbytes("eins"), self.space.wrap(u"zwei")])
     assert isinstance(l3.strategy, ObjectListStrategy)
Example #2
0
 def test_setslice_int_range(self):
     space = self.space
     w = space.wrap
     l = W_ListObject(space, [w(1), w(2), w(3)])
     assert isinstance(l.strategy, IntegerListStrategy)
     l.setslice(0, 1, 2, make_range_list(space, 5, 1, 4))
     assert isinstance(l.strategy, IntegerListStrategy)
 def test_int_to_any(self):
     l = W_ListObject(self.space, [self.space.wrap(1),self.space.wrap(2),self.space.wrap(3)])
     assert isinstance(l.strategy, IntegerListStrategy)
     l.append(self.space.wrap(4))
     assert isinstance(l.strategy, IntegerListStrategy)
     l.append(self.space.wrap('a'))
     assert isinstance(l.strategy, ObjectListStrategy)
Example #4
0
 def test_setslice_int_range(self):
     space = self.space
     w = space.wrap
     l = W_ListObject(space, [w(1), w(2), w(3)])
     assert isinstance(l.strategy, IntegerListStrategy)
     l.setslice(0, 1, 2, make_range_list(space, 5, 1, 4))
     assert isinstance(l.strategy, IntegerListStrategy)
Example #5
0
 def test_extend_other_with_empty(self):
     space = self.space
     w = space.wrap
     l = W_ListObject(space, [w(1), w(2), w(3)])
     assert isinstance(l.strategy, IntegerListStrategy)
     l.extend(W_ListObject(space, []))
     assert isinstance(l.strategy, IntegerListStrategy)
Example #6
0
 def test_extend_other_with_empty(self):
     space = self.space
     w = space.wrap
     l = W_ListObject(space, [w(1), w(2), w(3)])
     assert isinstance(l.strategy, IntegerListStrategy)
     l.extend(W_ListObject(space, []))
     assert isinstance(l.strategy, IntegerListStrategy)
 def test_string_to_any(self):
     l = W_ListObject(self.space, [self.space.wrap('a'),self.space.wrap('b'),self.space.wrap('c')])
     assert isinstance(l.strategy, StringListStrategy)
     l.append(self.space.wrap('d'))
     assert isinstance(l.strategy, StringListStrategy)
     l.append(self.space.wrap(3))
     assert isinstance(l.strategy, ObjectListStrategy)
Example #8
0
 def test_unicode(self):
     l1 = W_ListObject(self.space, [self.space.newbytes("eins"), self.space.newbytes("zwei")])
     assert isinstance(l1.strategy, BytesListStrategy)
     l2 = W_ListObject(self.space, [self.space.newutf8("eins", 4), self.space.newutf8("zwei", 4)])
     assert isinstance(l2.strategy, AsciiListStrategy)
     l3 = W_ListObject(self.space, [self.space.newbytes("eins"), self.space.newutf8("zwei", 4)])
     assert isinstance(l3.strategy, ObjectListStrategy)
Example #9
0
 def test_float_to_any(self):
     l = W_ListObject(self.space, [self.space.wrap(1.1),self.space.wrap(2.2),self.space.wrap(3.3)])
     assert isinstance(l.strategy, FloatListStrategy)
     l.append(self.space.wrap(4.4))
     assert isinstance(l.strategy, FloatListStrategy)
     l.append(self.space.wrap("a"))
     assert isinstance(l.strategy, ObjectListStrategy)
Example #10
0
    def test_create_list_from_set(self):
        from pypy.objspace.std.setobject import W_SetObject
        from pypy.objspace.std.setobject import _initialize_set

        space = self.space
        w = space.wrap

        w_l = W_ListObject(space, [space.wrap(1), space.wrap(2), space.wrap(3)])

        w_set = W_SetObject(self.space)
        _initialize_set(self.space, w_set, w_l)
        w_set.iter = None # make sure fast path is used

        w_l2 = W_ListObject(space, [])
        space.call_method(w_l2, "__init__", w_set)

        w_l2.sort(False)
        assert space.eq_w(w_l, w_l2)

        w_l = W_ListObject(space, [space.wrap("a"), space.wrap("b"), space.wrap("c")])
        _initialize_set(self.space, w_set, w_l)

        space.call_method(w_l2, "__init__", w_set)

        w_l2.sort(False)
        assert space.eq_w(w_l, w_l2)
Example #11
0
 def test_mul_does_not_clone(self):
     # only testing right mul at the moment
     w = self.space.wrap
     arg = w(2)
     w_lis = W_ListObject(self.space, [arg])
     w_lis.clone = None
     # does not crash
     self.space.mul(w_lis, w(5))
Example #12
0
 def test_pop_without_argument_is_fast(self):
     space = self.space
     w_l = W_ListObject(
         space, [space.wrap(1), space.wrap(2),
                 space.wrap(3)])
     w_l.pop = None
     w_res = w_l.descr_pop(space)
     assert space.unwrap(w_res) == 3
 def test_add_does_not_use_getitems(self):
     l1 = W_ListObject(self.space, [self.space.wrap(1), self.space.wrap(2), self.space.wrap(3)])
     l1.getitems = None
     l2 = W_ListObject(self.space, [self.space.wrap(1), self.space.wrap(2), self.space.wrap(3)])
     l2.getitems = None
     l3 = self.space.add(l1, l2)
     l4 = W_ListObject(self.space, [self.space.wrap(1), self.space.wrap(2), self.space.wrap(3), self.space.wrap(1), self.space.wrap(2), self.space.wrap(3)])
     assert self.space.eq_w(l3, l4)
Example #14
0
 def test_is_true(self):
     w = self.space.wrap
     w_list = W_ListObject(self.space, [])
     assert self.space.is_true(w_list) == False
     w_list = W_ListObject(self.space, [w(5)])
     assert self.space.is_true(w_list) == True
     w_list = W_ListObject(self.space, [w(5), w(3)])
     assert self.space.is_true(w_list) == True
Example #15
0
 def test_mul_does_not_clone(self):
     # only testing right mul at the moment
     w = self.space.wrap
     arg = w(2)
     w_lis = W_ListObject(self.space, [arg])
     w_lis.clone = None
     # does not crash
     self.space.mul(w_lis, w(5))
Example #16
0
 def test_len(self):
     w = self.space.wrap
     w_list = W_ListObject(self.space, [])
     assert self.space.eq_w(self.space.len(w_list), w(0))
     w_list = W_ListObject(self.space, [w(5)])
     assert self.space.eq_w(self.space.len(w_list), w(1))
     w_list = W_ListObject(self.space, [w(5), w(3), w(99)] * 111)
     assert self.space.eq_w(self.space.len(w_list), w(333))
Example #17
0
 def test_unicode_to_any(self):
     space = self.space
     l = W_ListObject(space, [space.wrap(u'a'), space.wrap(u'b'), space.wrap(u'c')])
     assert isinstance(l.strategy, UnicodeListStrategy)
     l.append(space.wrap(u'd'))
     assert isinstance(l.strategy, UnicodeListStrategy)
     l.append(space.wrap(3))
     assert isinstance(l.strategy, ObjectListStrategy)
Example #18
0
 def test_int_or_float_setslice_mixed_5(self):
     space = self.space
     w_l1 = W_ListObject(space, [space.wrap(0), space.wrap(1.2)])
     w_l2 = W_ListObject(space, [space.wrap(32), space.wrap(45)])
     assert isinstance(w_l1.strategy, IntOrFloatListStrategy)
     assert isinstance(w_l2.strategy, IntegerListStrategy)
     w_l1.setslice(0, 1, 1, w_l2)
     assert isinstance(w_l1.strategy, IntOrFloatListStrategy)
     assert space.unwrap(w_l1) == [32, 45, 1.2]
Example #19
0
 def test_int_or_float_extend_mixed_6(self):
     space = self.space
     w_l1 = W_ListObject(space, [space.wrap(-2.5)])
     w_l2 = W_ListObject(space, [space.wrap(3.4), space.wrap(-2)])
     assert isinstance(w_l1.strategy, FloatListStrategy)
     assert isinstance(w_l2.strategy, IntOrFloatListStrategy)
     w_l1.extend(w_l2)
     assert isinstance(w_l1.strategy, IntOrFloatListStrategy)
     assert space.unwrap(w_l1) == [-2.5, 3.4, -2]
Example #20
0
 def test_getitems_does_not_copy_object_list(self):
     l1 = W_ListObject(
         self.space,
         [self.space.wrap(1),
          self.space.wrap("two"),
          self.space.wrap(3)])
     l2 = l1.getitems()
     l2.append(self.space.wrap("four"))
     assert l2 == l1.getitems()
Example #21
0
 def test_int_or_float_extend(self):
     space = self.space
     w_l1 = W_ListObject(space, [space.wrap(0), space.wrap(1.2)])
     w_l2 = W_ListObject(space, [space.wrap(3), space.wrap(4.5)])
     assert isinstance(w_l1.strategy, IntOrFloatListStrategy)
     assert isinstance(w_l2.strategy, IntOrFloatListStrategy)
     w_l1.extend(w_l2)
     assert isinstance(w_l1.strategy, IntOrFloatListStrategy)
     assert space.unwrap(w_l1) == [0, 1.2, 3, 4.5]
Example #22
0
 def test_pop_without_argument_is_fast(self):
     space = self.space
     w_l = W_ListObject(
         space, [space.wrap(1), space.wrap(2),
                 space.wrap(3)])
     w_l.pop = None
     w_res = listobject.list_pop__List_ANY(space, w_l,
                                           space.w_None)  # does not crash
     assert space.unwrap(w_res) == 3
Example #23
0
 def test_copy_list(self):
     l1 = W_ListObject(
         self.space,
         [self.space.wrap(1),
          self.space.wrap(2),
          self.space.wrap(3)])
     l2 = l1.clone()
     l2.append(self.space.wrap(4))
     assert not l2 == l1.getitems()
Example #24
0
    def test_create_set_from_list(self):
        from pypy.interpreter.baseobjspace import W_Root
        from pypy.objspace.std.setobject import BytesSetStrategy, ObjectSetStrategy, UnicodeSetStrategy
        from pypy.objspace.std.floatobject import W_FloatObject

        w = self.space.wrap
        wb = self.space.wrapbytes
        intstr = self.space.fromcache(IntegerSetStrategy)
        tmp_func = intstr.get_storage_from_list
        # test if get_storage_from_list is no longer used
        intstr.get_storage_from_list = None

        w_list = W_ListObject(self.space, [w(1), w(2), w(3)])
        w_set = W_SetObject(self.space)
        _initialize_set(self.space, w_set, w_list)
        assert w_set.strategy is intstr
        assert intstr.unerase(w_set.sstorage) == {1: None, 2: None, 3: None}

        w_list = W_ListObject(self.space, [wb("1"), wb("2"), wb("3")])
        w_set = W_SetObject(self.space)
        _initialize_set(self.space, w_set, w_list)
        assert w_set.strategy is self.space.fromcache(BytesSetStrategy)
        assert w_set.strategy.unerase(w_set.sstorage) == {
            "1": None,
            "2": None,
            "3": None
        }

        w_list = self.space.iter(
            W_ListObject(self.space,
                         [w(u"1"), w(u"2"), w(u"3")]))
        w_set = W_SetObject(self.space)
        _initialize_set(self.space, w_set, w_list)
        assert w_set.strategy is self.space.fromcache(UnicodeSetStrategy)
        assert w_set.strategy.unerase(w_set.sstorage) == {
            u"1": None,
            u"2": None,
            u"3": None
        }

        w_list = W_ListObject(self.space, [w("1"), w(2), w("3")])
        w_set = W_SetObject(self.space)
        _initialize_set(self.space, w_set, w_list)
        assert w_set.strategy is self.space.fromcache(ObjectSetStrategy)
        for item in w_set.strategy.unerase(w_set.sstorage):
            assert isinstance(item, W_Root)

        w_list = W_ListObject(self.space, [w(1.0), w(2.0), w(3.0)])
        w_set = W_SetObject(self.space)
        _initialize_set(self.space, w_set, w_list)
        assert w_set.strategy is self.space.fromcache(ObjectSetStrategy)
        for item in w_set.strategy.unerase(w_set.sstorage):
            assert isinstance(item, W_FloatObject)

        # changed cached object, need to change it back for other tests to pass
        intstr.get_storage_from_list = tmp_func
Example #25
0
def descr__new__(space, w_listtype, __args__):
    if space.config.objspace.std.withmultilist:
        from pypy.objspace.std.listmultiobject import W_ListMultiObject
        w_obj = space.allocate_instance(W_ListMultiObject, w_listtype)
        W_ListMultiObject.__init__(w_obj, space)
    else:
        from pypy.objspace.std.listobject import W_ListObject
        w_obj = space.allocate_instance(W_ListObject, w_listtype)
        W_ListObject.__init__(w_obj, [])
    return w_obj
Example #26
0
 def test_int_or_float_sort(self):
     space = self.space
     w_l = W_ListObject(space, [space.wrap(1.2), space.wrap(1),
                                space.wrap(1.0), space.wrap(5)])
     w_l.sort(False)
     assert [(type(x), x) for x in space.unwrap(w_l)] == [
         (int, 1), (float, 1.0), (float, 1.2), (int, 5)]
     w_l.sort(True)
     assert [(type(x), x) for x in space.unwrap(w_l)] == [
         (int, 5), (float, 1.2), (int, 1), (float, 1.0)]
Example #27
0
 def test_int_or_float_from_integer(self):
     space = self.space
     w = space.wrap
     w_l = W_ListObject(space, [space.wrap(int(-2**31))])
     assert isinstance(w_l.strategy, IntegerListStrategy)
     w_l.append(w(-5.1))
     assert isinstance(w_l.strategy, IntOrFloatListStrategy)
     assert space.int_w(w_l.getitem(0)) == -2**31
     assert space.float_w(w_l.getitem(1)) == -5.1
     assert space.len_w(w_l) == 2
Example #28
0
 def test_int_or_float_from_float(self):
     space = self.space
     w = space.wrap
     w_l = W_ListObject(space, [space.wrap(-42.5)])
     assert isinstance(w_l.strategy, FloatListStrategy)
     w_l.append(w(-15))
     assert isinstance(w_l.strategy, IntOrFloatListStrategy)
     assert space.float_w(w_l.getitem(0)) == -42.5
     assert space.int_w(w_l.getitem(1)) == -15
     assert space.len_w(w_l) == 2
Example #29
0
 def test_int_or_float_extend_mixed_1(self):
     space = self.space
     w_l1 = W_ListObject(space, [space.wrap(0), space.wrap(1.2)])
     w_l2 = W_ListObject(space, [space.wrap(3)])
     assert isinstance(w_l1.strategy, IntOrFloatListStrategy)
     assert isinstance(w_l2.strategy, IntegerListStrategy)
     w_l1.extend(w_l2)
     assert isinstance(w_l1.strategy, IntOrFloatListStrategy)
     assert [(type(x), x) for x in space.unwrap(w_l1)] == [
         (int, 0), (float, 1.2), (int, 3)]
Example #30
0
def descr__new__(space, w_listtype, __args__):
    if space.config.objspace.std.withmultilist:
        from pypy.objspace.std.listmultiobject import W_ListMultiObject
        w_obj = space.allocate_instance(W_ListMultiObject, w_listtype)
        W_ListMultiObject.__init__(w_obj, space)
    else:
        from pypy.objspace.std.listobject import W_ListObject
        w_obj = space.allocate_instance(W_ListObject, w_listtype)
        W_ListObject.__init__(w_obj, [])
    return w_obj
Example #31
0
 def test_mul_same_strategy_but_different_object(self):
     l1 = W_ListObject(
         self.space,
         [self.space.wrap(1),
          self.space.wrap(2),
          self.space.wrap(3)])
     l2 = l1.mul(1)
     assert self.space.eq_w(l1, l2)
     l1.setitem(0, self.space.wrap(5))
     assert not self.space.eq_w(l1, l2)
Example #32
0
 def test_clone(self):
     l1 = W_ListObject(
         self.space,
         [self.space.wrap(1),
          self.space.wrap(2),
          self.space.wrap(3)])
     clone = l1.clone()
     assert isinstance(clone.strategy, IntegerListStrategy)
     clone.append(self.space.wrap(7))
     assert not self.space.eq_w(l1, clone)
Example #33
0
    def test_mul(self):
        l1 = W_ListObject(self.space, [self.space.wrap(1), self.space.wrap(2), self.space.wrap(3)])
        l2 = l1.mul(2)
        l3 = W_ListObject(self.space, [self.space.wrap(1), self.space.wrap(2), self.space.wrap(3), self.space.wrap(1), self.space.wrap(2), self.space.wrap(3)])
        assert self.space.eq_w(l2, l3)

        l4 = make_range_list(self.space, 1, 1, 3)
        assert self.space.eq_w(l4, l1)

        l5 = l4.mul(2)
        assert self.space.eq_w(l5, l3)
    def test_mul(self):
        l1 = W_ListObject(self.space, [self.space.wrap(1), self.space.wrap(2), self.space.wrap(3)])
        l2 = l1.mul(2)
        l3 = W_ListObject(self.space, [self.space.wrap(1), self.space.wrap(2), self.space.wrap(3), self.space.wrap(1), self.space.wrap(2), self.space.wrap(3)])
        assert self.space.eq_w(l2, l3)

        l4 = make_range_list(self.space, 1, 1, 3)
        assert self.space.eq_w(l4, l1)

        l5 = l4.mul(2)
        assert self.space.eq_w(l5, l3)
Example #35
0
 def test_random_setitem_delitem(self):
     w = self.space.wrap
     s = range(39)
     w_list = W_ListObject(self.space, map(w, s))
     expected = list(s)
     keys = range(-len(s) - 5, len(s) + 5)
     choices = keys + [None] * 12
     stepchoices = [
         None, None, None, 1, 1, -1, -1, 2, -2,
         len(s) - 1,
         len(s),
         len(s) + 1, -len(s) - 1, -len(s), -len(s) + 1
     ]
     for i in range(50):
         keys.append(
             slice(random.choice(choices), random.choice(choices),
                   random.choice(stepchoices)))
     random.shuffle(keys)
     n = len(s)
     for key in keys:
         if random.random() < 0.15:
             random.shuffle(s)
             w_list = W_ListObject(self.space, map(w, s))
             expected = list(s)
         try:
             value = expected[key]
         except IndexError:
             self.space.raises_w(self.space.w_IndexError,
                                 self.space.setitem, w_list, w(key), w(42))
         else:
             if is_valid_int(value):  # non-slicing
                 if random.random() < 0.25:  # deleting
                     self.space.delitem(w_list, w(key))
                     del expected[key]
                 else:
                     self.space.setitem(w_list, w(key), w(n))
                     expected[key] = n
                     n += 1
             else:  # slice assignment
                 mode = random.choice(['samesize', 'resize', 'delete'])
                 if mode == 'delete':
                     self.space.delitem(w_list, w(key))
                     del expected[key]
                 elif mode == 'samesize':
                     newvalue = range(n, n + len(value))
                     self.space.setitem(w_list, w(key), w(newvalue))
                     expected[key] = newvalue
                     n += len(newvalue)
                 elif mode == 'resize' and key.step is None:
                     newvalue = range(n, n + random.randrange(0, 20))
                     self.space.setitem(w_list, w(key), w(newvalue))
                     expected[key] = newvalue
                     n += len(newvalue)
         assert self.space.unwrap(w_list) == expected
Example #36
0
 def test_int_or_float_extend_mixed_5_overflow(self):
     if sys.maxint == 2147483647:
         py.test.skip("only on 64-bit")
     space = self.space
     ovf1 = 2 ** 35
     w_l1 = W_ListObject(space, [space.wrap(-2.5)])
     w_l2 = W_ListObject(space, [space.wrap(ovf1)])
     assert isinstance(w_l1.strategy, FloatListStrategy)
     assert isinstance(w_l2.strategy, IntegerListStrategy)
     w_l1.extend(w_l2)
     assert isinstance(w_l1.strategy, ObjectListStrategy)
     assert space.unwrap(w_l1) == [-2.5, ovf1]
Example #37
0
 def test_int_or_float_from_float_special_nan(self):
     from rpython.rlib import longlong2float, rarithmetic
     space = self.space
     w = space.wrap
     ll = rarithmetic.r_longlong(0xfffffffe12345678 - 2**64)
     specialnan = longlong2float.longlong2float(ll)
     w_l = W_ListObject(space, [space.wrap(specialnan)])
     assert isinstance(w_l.strategy, FloatListStrategy)
     w_l.append(w(42))
     assert isinstance(w_l.strategy, ObjectListStrategy)
     assert space.int_w(w_l.getitem(1)) == 42
     assert space.len_w(w_l) == 2
Example #38
0
 def test_int_or_float_setslice_mixed_5_overflow(self):
     if sys.maxint == 2147483647:
         py.test.skip("only on 64-bit")
     space = self.space
     ovf1 = 2 ** 35
     w_l1 = W_ListObject(space, [space.wrap(0), space.wrap(1.2)])
     w_l2 = W_ListObject(space, [space.wrap(32), space.wrap(ovf1)])
     assert isinstance(w_l1.strategy, IntOrFloatListStrategy)
     assert isinstance(w_l2.strategy, IntegerListStrategy)
     w_l1.setslice(0, 1, 1, w_l2)
     assert isinstance(w_l1.strategy, ObjectListStrategy)
     assert space.unwrap(w_l1) == [32, ovf1, 1.2]
Example #39
0
 def test_mul(self):
     # only testing right mul at the moment
     w = self.space.wrap
     arg = w(2)
     n = 3
     w_lis = W_ListObject(self.space, [arg])
     w_lis3 = W_ListObject(self.space, [arg] * n)
     w_res = self.space.mul(w_lis, w(n))
     assert self.space.eq_w(w_lis3, w_res)
     # commute
     w_res = self.space.mul(w(n), w_lis)
     assert self.space.eq_w(w_lis3, w_res)
Example #40
0
 def test_setitem(self):
     w = self.space.wrap
     w_list = W_ListObject(self.space, [w(5), w(3)])
     w_exp1 = W_ListObject(self.space, [w(5), w(7)])
     w_exp2 = W_ListObject(self.space, [w(8), w(7)])
     self.space.setitem(w_list, w(1), w(7))
     assert self.space.eq_w(w_exp1, w_list)
     self.space.setitem(w_list, w(-2), w(8))
     assert self.space.eq_w(w_exp2, w_list)
     self.space.raises_w(self.space.w_IndexError, self.space.setitem,
                         w_list, w(2), w(5))
     self.space.raises_w(self.space.w_IndexError, self.space.setitem,
                         w_list, w(-3), w(5))
Example #41
0
 def test_int_or_float_from_float_int_overflow(self):
     if sys.maxint == 2147483647:
         py.test.skip("only on 64-bit")
     space = self.space
     w = space.wrap
     ovf1 = 2 ** 31
     w_l = W_ListObject(space, [space.wrap(1.2)])
     assert isinstance(w_l.strategy, FloatListStrategy)
     w_l.append(w(ovf1))
     assert isinstance(w_l.strategy, ObjectListStrategy)
     assert space.float_w(w_l.getitem(0)) == 1.2
     assert space.int_w(w_l.getitem(1)) == ovf1
     assert space.len_w(w_l) == 2
Example #42
0
 def test_add_to_rangelist(self):
     l1 = make_range_list(self.space, 1, 1, 3)
     l2 = W_ListObject(self.space, [self.space.wrap(4), self.space.wrap(5)])
     l3 = l1.descr_add(self.space, l2)
     assert self.space.eq_w(
         l3,
         W_ListObject(self.space, [
             self.space.wrap(1),
             self.space.wrap(2),
             self.space.wrap(3),
             self.space.wrap(4),
             self.space.wrap(5)
         ]))
Example #43
0
 def test_add_to_rangelist(self):
     l1 = make_range_list(self.space, 1, 1, 3)
     l2 = W_ListObject(self.space, [self.space.wrap(4), self.space.wrap(5)])
     from pypy.objspace.std.listobject import add__List_List
     l3 = add__List_List(self.space, l1, l2)
     assert self.space.eq_w(
         l3,
         W_ListObject(self.space, [
             self.space.wrap(1),
             self.space.wrap(2),
             self.space.wrap(3),
             self.space.wrap(4),
             self.space.wrap(5)
         ]))
Example #44
0
    def test_create_list_from_set(self):
        from pypy.objspace.std.setobject import W_SetObject
        from pypy.objspace.std.setobject import _initialize_set

        space = self.space
        w = space.wrap

        w_l = W_ListObject(
            space, [space.wrap(1), space.wrap(2),
                    space.wrap(3)])

        w_set = W_SetObject(self.space)
        _initialize_set(self.space, w_set, w_l)
        w_set.iter = None  # make sure fast path is used

        w_l2 = W_ListObject(space, [])
        space.call_method(w_l2, "__init__", w_set)

        w_l2.sort(False)
        assert space.eq_w(w_l, w_l2)

        w_l = W_ListObject(space,
                           [space.wrap("a"),
                            space.wrap("b"),
                            space.wrap("c")])
        _initialize_set(self.space, w_set, w_l)

        space.call_method(w_l2, "__init__", w_set)

        w_l2.sort(False)
        assert space.eq_w(w_l, w_l2)
Example #45
0
 def test_add(self):
     w = self.space.wrap
     w_list0 = W_ListObject([])
     w_list1 = W_ListObject([w(5), w(3), w(99)])
     w_list2 = W_ListObject([w(-7)] * 111)
     assert self.space.eq_w(
         self.space.add(w_list1, w_list1),
         W_ListObject([w(5), w(3), w(99),
                       w(5), w(3), w(99)]))
     assert self.space.eq_w(
         self.space.add(w_list1, w_list2),
         W_ListObject([w(5), w(3), w(99)] + [w(-7)] * 111))
     assert self.space.eq_w(self.space.add(w_list1, w_list0), w_list1)
     assert self.space.eq_w(self.space.add(w_list0, w_list2), w_list2)
    def test_extend(self):
        l = W_ListObject(self.space, [])
        assert isinstance(l.strategy, EmptyListStrategy)
        l.extend(W_ListObject(self.space, [self.space.wrap(1), self.space.wrap(2), self.space.wrap(3)]))
        assert isinstance(l.strategy, IntegerListStrategy)

        l = W_ListObject(self.space, [self.space.wrap(1), self.space.wrap(2), self.space.wrap(3)])
        assert isinstance(l.strategy, IntegerListStrategy)
        l.extend(W_ListObject(self.space, [self.space.wrap('a'), self.space.wrap('b'), self.space.wrap('c')]))
        assert isinstance(l.strategy, ObjectListStrategy)

        l = W_ListObject(self.space, [self.space.wrap(1), self.space.wrap(2), self.space.wrap(3)])
        assert isinstance(l.strategy, IntegerListStrategy)
        l.extend(W_ListObject(self.space, [self.space.wrap(4), self.space.wrap(5), self.space.wrap(6)]))
        assert isinstance(l.strategy, IntegerListStrategy)
    def notest_list_empty_after_delete(self):
        l = W_ListObject(self.space, [self.space.wrap(3)])
        assert isinstance(l.strategy, IntegerListStrategy)
        l.deleteitem(0)
        assert isinstance(l.strategy, EmptyListStrategy)

        l = W_ListObject(self.space, [self.space.wrap(1), self.space.wrap(2)])
        assert isinstance(l.strategy, IntegerListStrategy)
        l.deleteslice(0, 1, 2)
        assert isinstance(l.strategy, EmptyListStrategy)

        l = W_ListObject(self.space, [self.space.wrap(1)])
        assert isinstance(l.strategy, IntegerListStrategy)
        l.pop(-1)
        assert isinstance(l.strategy, EmptyListStrategy)
    def test_empty_to_any(self):
        l = W_ListObject(self.space, [])
        assert isinstance(l.strategy, EmptyListStrategy)
        l.append(self.space.wrap(1.))
        assert isinstance(l.strategy, ObjectListStrategy)

        l = W_ListObject(self.space, [])
        assert isinstance(l.strategy, EmptyListStrategy)
        l.append(self.space.wrap(1))
        assert isinstance(l.strategy, IntegerListStrategy)

        l = W_ListObject(self.space, [])
        assert isinstance(l.strategy, EmptyListStrategy)
        l.append(self.space.wrap('a'))
        assert isinstance(l.strategy, StringListStrategy)
Example #49
0
    def test_list_empty_after_delete(self):
        py.test.skip("return to emptyliststrategy is not supported anymore")
        l = W_ListObject(self.space, [self.space.wrap(3)])
        assert isinstance(l.strategy, IntegerListStrategy)
        l.deleteitem(0)
        assert isinstance(l.strategy, EmptyListStrategy)

        l = W_ListObject(self.space, [self.space.wrap(1), self.space.wrap(2)])
        assert isinstance(l.strategy, IntegerListStrategy)
        l.deleteslice(0, 1, 2)
        assert isinstance(l.strategy, EmptyListStrategy)

        l = W_ListObject(self.space, [self.space.wrap(1)])
        assert isinstance(l.strategy, IntegerListStrategy)
        l.pop(-1)
        assert isinstance(l.strategy, EmptyListStrategy)
Example #50
0
 def test_find_fast_on_intlist(self, monkeypatch):
     monkeypatch.setattr(self.space, "eq_w", None)
     w = self.space.wrap
     intlist = W_ListObject(self.space, [w(1),w(2),w(3),w(4),w(5),w(6),w(7)])
     res = intlist.find(w(4), 0, 7)
     assert res == 3
     res = intlist.find(w(4), 0, 100)
     assert res == 3
     with py.test.raises(ValueError):
         intlist.find(w(4), 4, 7)
     with py.test.raises(ValueError):
         intlist.find(w(4), 0, 2)
    def test_get_items_copy(self):
        l1 = W_ListObject(self.space, [self.space.wrap(1), self.space.wrap(2), self.space.wrap(3)])
        l2 = l1.getitems()
        l2.append(self.space.wrap(4))
        assert not l2 == l1.getitems()

        l1 = W_ListObject(self.space, [self.space.wrap(1), self.space.wrap("two"), self.space.wrap(3)])
        l2 = l1.getitems()
        l2.append(self.space.wrap("four"))
        assert l2 == l1.getitems()
Example #52
0
def PySequence_Fast(space, w_obj, m):
    """Returns the sequence o as a tuple, unless it is already a tuple or list, in
    which case o is returned.  Use PySequence_Fast_GET_ITEM() to access the
    members of the result.  Returns NULL on failure.  If the object cannot be
    converted to a sequence, and raises a TypeError, raise a new TypeError with
    m as the message text. If the conversion otherwise, fails, reraise the
    original exception"""
    if isinstance(w_obj, W_ListObject):
        # make sure we can return a borrowed obj from PySequence_Fast_GET_ITEM    
        w_obj.convert_to_cpy_strategy(space)
        return w_obj
    try:
        return W_ListObject.newlist_cpyext(space, space.listview(w_obj))
    except OperationError as e:
        if e.match(space, space.w_TypeError):
            raise OperationError(space.w_TypeError, space.wrap(rffi.charp2str(m)))
        raise e
Example #53
0
 def newlist_unicode(self, list_u):
     return W_ListObject.newlist_unicode(self, list_u)