| 1 |
from pypy.objspace.std.test import test_typeobject |
| 2 |
from pypy.objspace.std.longobject import W_LongObject |
| 3 |
from rpython.rlib.rbigint import rbigint |
| 4 |
|
| 5 |
class TestVersionedType(test_typeobject.TestTypeObject): |
| 6 |
|
| 7 |
def get_three_classes(self): |
| 8 |
space = self.space |
| 9 |
w_types = space.appexec([], """(): |
| 10 |
class A(object): |
| 11 |
def f(self): pass |
| 12 |
class B(A): |
| 13 |
pass |
| 14 |
class X: |
| 15 |
pass |
| 16 |
class Y(object): |
| 17 |
pass |
| 18 |
class C(Y, X): |
| 19 |
pass |
| 20 |
return A, B, C |
| 21 |
""") |
| 22 |
return space.unpackiterable(w_types) |
| 23 |
|
| 24 |
def test_tag_changes(self): |
| 25 |
space = self.space |
| 26 |
w_A, w_B, w_C = self.get_three_classes() |
| 27 |
atag = w_A.version_tag() |
| 28 |
btag = w_B.version_tag() |
| 29 |
assert atag is not None |
| 30 |
assert btag is not None |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
assert atag is not btag |
| 35 |
w_types = space.appexec([w_A, w_B], """(A, B): |
| 36 |
B.g = lambda self: None |
| 37 |
""") |
| 38 |
assert w_B.version_tag() is not btag |
| 39 |
assert w_A.version_tag() is atag |
| 40 |
btag = w_B.version_tag() |
| 41 |
w_types = space.appexec([w_A, w_B], """(A, B): |
| 42 |
A.f = lambda self: None |
| 43 |
""") |
| 44 |
assert w_B.version_tag() is not btag |
| 45 |
assert w_A.version_tag() is not atag |
| 46 |
atag = w_A.version_tag() |
| 47 |
btag = w_B.version_tag() |
| 48 |
assert atag is not btag |
| 49 |
w_types = space.appexec([w_A, w_B], """(A, B): |
| 50 |
del A.f |
| 51 |
""") |
| 52 |
assert w_B.version_tag() is not btag |
| 53 |
assert w_A.version_tag() is not atag |
| 54 |
atag = w_A.version_tag() |
| 55 |
btag = w_B.version_tag() |
| 56 |
assert atag is not btag |
| 57 |
|
| 58 |
def test_tag_changes_when_bases_change(self): |
| 59 |
space = self.space |
| 60 |
w_A, w_B, w_C = self.get_three_classes() |
| 61 |
atag = w_A.version_tag() |
| 62 |
btag = w_B.version_tag() |
| 63 |
w_types = space.appexec([w_A, w_B, w_C], """(A, B, C): |
| 64 |
class D(object): |
| 65 |
pass |
| 66 |
B.__bases__ = (D, ) |
| 67 |
""") |
| 68 |
assert w_B.version_tag() is not btag |
| 69 |
|
| 70 |
def test_tag_changes_only_when_dict_changes(self): |
| 71 |
space = self.space |
| 72 |
w_A, w_B, w_C = self.get_three_classes() |
| 73 |
atag = w_A.version_tag() |
| 74 |
|
| 75 |
w_types = space.appexec([w_A, w_B, w_C], """(A, B, C): |
| 76 |
A.__name__ = "hello" |
| 77 |
""") |
| 78 |
|
| 79 |
assert w_A.version_tag() is atag |
| 80 |
|
| 81 |
w_types = space.appexec([w_A, w_B, w_C], """(A, B, C): |
| 82 |
try: |
| 83 |
del A.__name__ |
| 84 |
except AttributeError: |
| 85 |
pass |
| 86 |
""") |
| 87 |
assert w_A.version_tag() is atag |
| 88 |
|
| 89 |
|
| 90 |
w_types = space.appexec([w_A, w_B, w_C], """(A, B, C): |
| 91 |
try: |
| 92 |
del A.abc |
| 93 |
except AttributeError: |
| 94 |
pass |
| 95 |
""") |
| 96 |
assert w_A.version_tag() is atag |
| 97 |
|
| 98 |
def test_tag_changes_When_module_changes(self): |
| 99 |
space = self.space |
| 100 |
w_A, w_B, w_C = self.get_three_classes() |
| 101 |
atag = w_A.version_tag() |
| 102 |
|
| 103 |
w_types = space.appexec([w_A, w_B, w_C], """(A, B, C): |
| 104 |
A.__module__ = "hello" |
| 105 |
""") |
| 106 |
|
| 107 |
assert w_A.version_tag() is not atag |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
def test_version_tag_of_builtin_types(self): |
| 112 |
space = self.space |
| 113 |
assert space.w_list.version_tag() is not None |
| 114 |
assert space.w_dict.version_tag() is not None |
| 115 |
assert space.type(space.sys).version_tag() is not None |
| 116 |
assert space.w_type.version_tag() is not None |
| 117 |
w_function = space.appexec([], """(): |
| 118 |
def f(): |
| 119 |
pass |
| 120 |
return type(f) |
| 121 |
""") |
| 122 |
assert w_function.version_tag() is not None |
| 123 |
assert space.w_Exception.version_tag() is not None |
| 124 |
|
| 125 |
def test_version_tag_of_subclasses_of_builtin_types(self): |
| 126 |
space = self.space |
| 127 |
w_types = space.appexec([], """(): |
| 128 |
import sys |
| 129 |
class LIST(list): |
| 130 |
def f(self): pass |
| 131 |
class DICT(dict): |
| 132 |
pass |
| 133 |
class TYPE(type): |
| 134 |
pass |
| 135 |
class MODULE(type(sys)): |
| 136 |
pass |
| 137 |
class OBJECT(object): |
| 138 |
pass |
| 139 |
return [LIST, DICT, TYPE, MODULE, OBJECT] |
| 140 |
""") |
| 141 |
(w_LIST, w_DICT, w_TYPE, w_MODULE, |
| 142 |
w_OBJECT) = space.unpackiterable(w_types) |
| 143 |
assert w_LIST.version_tag() is not None |
| 144 |
assert w_DICT.version_tag() is not None |
| 145 |
assert w_TYPE.version_tag() is not None |
| 146 |
assert w_MODULE.version_tag() is not None |
| 147 |
assert w_OBJECT.version_tag() is not None |
| 148 |
|
| 149 |
def test_version_tag_of_modules(self): |
| 150 |
space = self.space |
| 151 |
w_mod = space.appexec([], """(): |
| 152 |
import sys |
| 153 |
return type(sys) |
| 154 |
""") |
| 155 |
atag = w_mod.version_tag() |
| 156 |
btag = w_mod.version_tag() |
| 157 |
assert btag is atag |
| 158 |
assert btag is not None |
| 159 |
|
| 160 |
def test_version_tag_when_changing_a_lot(self): |
| 161 |
space = self.space |
| 162 |
w_x = space.wrap("x") |
| 163 |
w_A, w_B, w_C = self.get_three_classes() |
| 164 |
atag = w_A.version_tag() |
| 165 |
space.setattr(w_A, w_x, space.newint(1)) |
| 166 |
assert w_A.version_tag() is not atag |
| 167 |
assert space.int_w(space.getattr(w_A, w_x)) == 1 |
| 168 |
|
| 169 |
atag = w_A.version_tag() |
| 170 |
space.setattr(w_A, w_x, space.newint(2)) |
| 171 |
assert w_A.version_tag() is not atag |
| 172 |
assert space.int_w(space.getattr(w_A, w_x)) == 2 |
| 173 |
|
| 174 |
atag = w_A.version_tag() |
| 175 |
space.setattr(w_A, w_x, space.newint(3)) |
| 176 |
assert w_A.version_tag() is atag |
| 177 |
assert space.int_w(space.getattr(w_A, w_x)) == 3 |
| 178 |
|
| 179 |
space.setattr(w_A, w_x, space.newint(4)) |
| 180 |
assert w_A.version_tag() is atag |
| 181 |
assert space.int_w(space.getattr(w_A, w_x)) == 4 |
| 182 |
|
| 183 |
def test_no_cell_when_writing_same_value(self): |
| 184 |
space = self.space |
| 185 |
w_x = space.wrap("x") |
| 186 |
w_A, w_B, w_C = self.get_three_classes() |
| 187 |
atag = w_A.version_tag() |
| 188 |
w_val = space.newint(1) |
| 189 |
space.setattr(w_A, w_x, w_val) |
| 190 |
space.setattr(w_A, w_x, w_val) |
| 191 |
w_val1 = w_A._getdictvalue_no_unwrapping(space, "x") |
| 192 |
assert w_val1 is w_val |
| 193 |
|
| 194 |
def test_int_cells(self): |
| 195 |
space = self.space |
| 196 |
w_x = space.wrap("x") |
| 197 |
w_A, w_B, w_C = self.get_three_classes() |
| 198 |
atag = w_A.version_tag() |
| 199 |
space.setattr(w_A, w_x, space.newint(1)) |
| 200 |
assert w_A.version_tag() is not atag |
| 201 |
assert space.int_w(space.getattr(w_A, w_x)) == 1 |
| 202 |
|
| 203 |
atag = w_A.version_tag() |
| 204 |
space.setattr(w_A, w_x, space.newint(2)) |
| 205 |
assert w_A.version_tag() is not atag |
| 206 |
assert space.int_w(space.getattr(w_A, w_x)) == 2 |
| 207 |
cell = w_A._getdictvalue_no_unwrapping(space, "x") |
| 208 |
assert cell.intvalue == 2 |
| 209 |
|
| 210 |
atag = w_A.version_tag() |
| 211 |
space.setattr(w_A, w_x, space.newint(3)) |
| 212 |
assert w_A.version_tag() is atag |
| 213 |
assert space.int_w(space.getattr(w_A, w_x)) == 3 |
| 214 |
assert cell.intvalue == 3 |
| 215 |
|
| 216 |
space.setattr(w_A, w_x, space.newint(4)) |
| 217 |
assert w_A.version_tag() is atag |
| 218 |
assert space.int_w(space.getattr(w_A, w_x)) == 4 |
| 219 |
assert cell.intvalue == 4 |
| 220 |
|
| 221 |
def test_int_cell_turns_into_cell(self): |
| 222 |
space = self.space |
| 223 |
w_x = space.wrap("x") |
| 224 |
w_A, w_B, w_C = self.get_three_classes() |
| 225 |
atag = w_A.version_tag() |
| 226 |
space.setattr(w_A, w_x, space.newint(1)) |
| 227 |
space.setattr(w_A, w_x, space.newint(2)) |
| 228 |
space.setattr(w_A, w_x, space.newfloat(2.2)) |
| 229 |
cell = w_A._getdictvalue_no_unwrapping(space, "x") |
| 230 |
assert space.float_w(cell.w_value) == 2.2 |
| 231 |
|
| 232 |
def test_integer_strategy_with_w_long(self): |
| 233 |
space = self.space |
| 234 |
w_x = space.wrap("x") |
| 235 |
w_A, w_B, w_C = self.get_three_classes() |
| 236 |
atag = w_A.version_tag() |
| 237 |
w = W_LongObject(rbigint.fromlong(42)) |
| 238 |
space.setattr(w_A, w_x, w) |
| 239 |
assert w_A.version_tag() is not atag |
| 240 |
assert space.int_w(space.getattr(w_A, w_x)) == 42 |
| 241 |
|
| 242 |
atag = w_A.version_tag() |
| 243 |
w = W_LongObject(rbigint.fromlong(43)) |
| 244 |
space.setattr(w_A, w_x, w) |
| 245 |
assert w_A.version_tag() is not atag |
| 246 |
assert space.int_w(space.getattr(w_A, w_x)) == 43 |
| 247 |
cell = w_A._getdictvalue_no_unwrapping(space, "x") |
| 248 |
assert cell.intvalue == 43 |
| 249 |
|
| 250 |
atag = w_A.version_tag() |
| 251 |
w = W_LongObject(rbigint.fromlong(44)) |
| 252 |
space.setattr(w_A, w_x, w) |
| 253 |
assert w_A.version_tag() is atag |
| 254 |
assert space.int_w(space.getattr(w_A, w_x)) == 44 |
| 255 |
assert cell.intvalue == 44 |