| 1 |
from pypy.conftest import option |
| 2 |
import pytest |
| 3 |
|
| 4 |
class AppTestObject: |
| 5 |
|
| 6 |
spaceconfig = {'usemodules': ['itertools']} |
| 7 |
|
| 8 |
def setup_class(cls): |
| 9 |
from pypy.interpreter import gateway |
| 10 |
import sys |
| 11 |
|
| 12 |
space = cls.space |
| 13 |
cls.w_appdirect = space.wrap(option.runappdirect) |
| 14 |
|
| 15 |
def w_unwrap_wrap_unicode(space, w_obj): |
| 16 |
return space.newutf8(space.utf8_w(w_obj), w_obj._length) |
| 17 |
cls.w_unwrap_wrap_unicode = space.wrap(gateway.interp2app(w_unwrap_wrap_unicode)) |
| 18 |
def w_unwrap_wrap_bytes(space, w_obj): |
| 19 |
return space.newbytes(space.bytes_w(w_obj)) |
| 20 |
cls.w_unwrap_wrap_bytes = space.wrap(gateway.interp2app(w_unwrap_wrap_bytes)) |
| 21 |
|
| 22 |
def test_hash_method(self): |
| 23 |
o = object() |
| 24 |
assert hash(o) == o.__hash__() |
| 25 |
|
| 26 |
def test_hash_list(self): |
| 27 |
l = list(range(5)) |
| 28 |
raises(TypeError, hash, l) |
| 29 |
|
| 30 |
def test_no_getnewargs(self): |
| 31 |
o = object() |
| 32 |
assert not hasattr(o, '__getnewargs__') |
| 33 |
|
| 34 |
def test_hash_subclass(self): |
| 35 |
import sys |
| 36 |
class X(object): |
| 37 |
pass |
| 38 |
x = X() |
| 39 |
assert hash(x) == object.__hash__(x) |
| 40 |
|
| 41 |
def test_reduce_recursion_bug(self): |
| 42 |
class X(object): |
| 43 |
def __reduce__(self): |
| 44 |
return object.__reduce__(self) + (':-)',) |
| 45 |
s = X().__reduce__() |
| 46 |
assert s[-1] == ':-)' |
| 47 |
|
| 48 |
def test_getnewargs_ex(self): |
| 49 |
class NamedInt(int): |
| 50 |
def __new__(cls, name, **kwargs): |
| 51 |
if len(kwargs) == 0: |
| 52 |
raise TypeError("name and value must be specified") |
| 53 |
self = int.__new__(cls, kwargs['value']) |
| 54 |
self._name = name |
| 55 |
return self |
| 56 |
def __getnewargs_ex__(self): |
| 57 |
return (self._name,), dict(value=int(self)) |
| 58 |
import copyreg |
| 59 |
for protocol in [2, 3, 4]: |
| 60 |
assert NamedInt("Name", value=42).__reduce_ex__(protocol) == ( |
| 61 |
copyreg.__newobj_ex__, |
| 62 |
(NamedInt, ('Name',), dict(value=42)), |
| 63 |
dict(_name='Name'), None, None) |
| 64 |
|
| 65 |
def test_reduce_ex_does_getattr(self): |
| 66 |
seen = [] |
| 67 |
class X: |
| 68 |
def __getattribute__(self, name): |
| 69 |
seen.append(name) |
| 70 |
return object.__getattribute__(self, name) |
| 71 |
X().__reduce_ex__(2) |
| 72 |
|
| 73 |
assert '__reduce__' in seen |
| 74 |
|
| 75 |
|
| 76 |
assert '__getnewargs__' not in seen |
| 77 |
assert '__getnewargs_ex__' not in seen |
| 78 |
|
| 79 |
def test_reduce_ex_errors(self): |
| 80 |
|
| 81 |
args = (-101, "spam") |
| 82 |
kwargs = {'bacon': -201, 'fish': -301} |
| 83 |
|
| 84 |
class C2: |
| 85 |
def __getnewargs__(self): |
| 86 |
return "bad args" |
| 87 |
excinfo = raises(TypeError, C2().__reduce_ex__, 4) |
| 88 |
assert str(excinfo.value) == \ |
| 89 |
"__getnewargs__ should return a tuple, not 'str'" |
| 90 |
|
| 91 |
class C4: |
| 92 |
def __getnewargs_ex__(self): |
| 93 |
return (args, "bad dict") |
| 94 |
excinfo = raises(TypeError, C4().__reduce_ex__, 4) |
| 95 |
assert str(excinfo.value) == ("second item of the tuple " |
| 96 |
"returned by __getnewargs_ex__ must be a dict, not 'str'") |
| 97 |
|
| 98 |
class C5: |
| 99 |
def __getnewargs_ex__(self): |
| 100 |
return ("bad tuple", kwargs) |
| 101 |
excinfo = raises(TypeError, C5().__reduce_ex__, 4) |
| 102 |
assert str(excinfo.value) == ("first item of the tuple " |
| 103 |
"returned by __getnewargs_ex__ must be a tuple, not 'str'") |
| 104 |
|
| 105 |
class C6: |
| 106 |
def __getnewargs_ex__(self): |
| 107 |
return () |
| 108 |
excinfo = raises(ValueError, C6().__reduce_ex__, 4) |
| 109 |
assert str(excinfo.value) == \ |
| 110 |
"__getnewargs_ex__ should return a tuple of length 2, not 0" |
| 111 |
|
| 112 |
class C7: |
| 113 |
def __getnewargs_ex__(self): |
| 114 |
return "bad args" |
| 115 |
excinfo = raises(TypeError, C7().__reduce_ex__, 4) |
| 116 |
assert str(excinfo.value) == \ |
| 117 |
"__getnewargs_ex__ should return a tuple, not 'str'" |
| 118 |
|
| 119 |
|
| 120 |
def test_reduce_state_empty_dict(self): |
| 121 |
class X(object): |
| 122 |
pass |
| 123 |
assert X().__reduce_ex__(2)[2] is None |
| 124 |
|
| 125 |
def test_reduce_arguments(self): |
| 126 |
|
| 127 |
|
| 128 |
with raises(TypeError): |
| 129 |
object().__reduce__(0) |
| 130 |
with raises(TypeError): |
| 131 |
object().__reduce_ex__() |
| 132 |
|
| 133 |
def test_default_format(self): |
| 134 |
class x(object): |
| 135 |
def __str__(self): |
| 136 |
return "Pickle" |
| 137 |
res = format(x()) |
| 138 |
assert res == "Pickle" |
| 139 |
assert isinstance(res, str) |
| 140 |
|
| 141 |
def test_format(self): |
| 142 |
class B: |
| 143 |
pass |
| 144 |
excinfo = raises(TypeError, format, B(), 's') |
| 145 |
assert 'B.__format__' in str(excinfo.value) |
| 146 |
|
| 147 |
|
| 148 |
def test_subclasshook(self): |
| 149 |
class x(object): |
| 150 |
pass |
| 151 |
assert x().__subclasshook__(object()) is NotImplemented |
| 152 |
assert x.__subclasshook__(object()) is NotImplemented |
| 153 |
|
| 154 |
def test_object_init(self): |
| 155 |
import warnings |
| 156 |
|
| 157 |
class A(object): |
| 158 |
pass |
| 159 |
|
| 160 |
raises(TypeError, A().__init__, 3) |
| 161 |
raises(TypeError, A().__init__, a=3) |
| 162 |
|
| 163 |
class B(object): |
| 164 |
def __new__(cls): |
| 165 |
return super(B, cls).__new__(cls) |
| 166 |
|
| 167 |
def __init__(self): |
| 168 |
super(B, self).__init__(a=3) |
| 169 |
|
| 170 |
raises(TypeError, B) |
| 171 |
|
| 172 |
def test_object_init_not_really_overridden(self): |
| 173 |
class A(object): |
| 174 |
def __new__(cls, value): |
| 175 |
return object.__new__(cls) |
| 176 |
__init__ = object.__init__ |
| 177 |
assert isinstance(A(1), A) |
| 178 |
|
| 179 |
def test_object_new_not_really_overridden(self): |
| 180 |
class A(object): |
| 181 |
def __init__(self, value): |
| 182 |
self.value = value |
| 183 |
__new__ = object.__new__ |
| 184 |
assert A(42).value == 42 |
| 185 |
|
| 186 |
def test_object_init_cant_call_parent_with_args(self): |
| 187 |
class A(object): |
| 188 |
def __init__(self, value): |
| 189 |
object.__init__(self, value) |
| 190 |
raises(TypeError, A, 1) |
| 191 |
|
| 192 |
def test_object_new_cant_call_parent_with_args(self): |
| 193 |
class A(object): |
| 194 |
def __new__(cls, value): |
| 195 |
return object.__new__(cls, value) |
| 196 |
raises(TypeError, A, 1) |
| 197 |
|
| 198 |
def test_object_init_and_new_overridden(self): |
| 199 |
class A(object): |
| 200 |
def __new__(cls, value): |
| 201 |
result = object.__new__(cls) |
| 202 |
result.other_value = value + 1 |
| 203 |
return result |
| 204 |
def __init__(self, value): |
| 205 |
self.value = value |
| 206 |
assert A(42).value == 42 |
| 207 |
assert A(42).other_value == 43 |
| 208 |
|
| 209 |
def test_object_str(self): |
| 210 |
|
| 211 |
|
| 212 |
class A(object): |
| 213 |
def __repr__(self): |
| 214 |
return 123456 |
| 215 |
assert A().__str__() == 123456 |
| 216 |
|
| 217 |
def test_object_dir(self): |
| 218 |
class A(object): |
| 219 |
a_var = None |
| 220 |
|
| 221 |
assert hasattr(object, '__dir__') |
| 222 |
obj = A() |
| 223 |
obj_items = dir(obj) |
| 224 |
assert obj_items == sorted(obj_items) |
| 225 |
assert obj_items == sorted(object.__dir__(obj)) |
| 226 |
|
| 227 |
|
| 228 |
@pytest.mark.pypy_only |
| 229 |
def test_is_on_primitives(self): |
| 230 |
assert 1 is 1 |
| 231 |
x = 1000000 |
| 232 |
assert x + 1 is int(str(x + 1)) |
| 233 |
assert 1 is not 1.0 |
| 234 |
assert 1.1 is 1.1 |
| 235 |
assert 0.0 is not -0.0 |
| 236 |
for x in range(10): |
| 237 |
assert x + 0.1 is x + 0.1 |
| 238 |
for x in range(10): |
| 239 |
assert x + 1 is x + 1 |
| 240 |
for x in range(10): |
| 241 |
assert x+1j is x+1j |
| 242 |
assert 1+x*1j is 1+x*1j |
| 243 |
l = [1] |
| 244 |
assert l[0] is l[0] |
| 245 |
|
| 246 |
def test_is_on_strs(self): |
| 247 |
if self.appdirect: |
| 248 |
skip("cannot run this test as apptest") |
| 249 |
l = ["a"] |
| 250 |
assert l[0] is l[0] |
| 251 |
u = "a" |
| 252 |
assert self.unwrap_wrap_unicode(u) is u |
| 253 |
s = b"a" |
| 254 |
assert self.unwrap_wrap_bytes(s) is s |
| 255 |
|
| 256 |
@pytest.mark.pypy_only |
| 257 |
def test_is_by_value(self): |
| 258 |
for typ in [int, float, complex]: |
| 259 |
assert typ(42) is typ(42) |
| 260 |
|
| 261 |
def test_is_on_subclasses(self): |
| 262 |
for typ in [int, float, complex, str]: |
| 263 |
class mytyp(typ): |
| 264 |
pass |
| 265 |
assert mytyp(42) is not mytyp(42) |
| 266 |
assert mytyp(42) is not typ(42) |
| 267 |
assert typ(42) is not mytyp(42) |
| 268 |
x = mytyp(42) |
| 269 |
assert x is x |
| 270 |
assert x is not "43" |
| 271 |
assert x is not None |
| 272 |
assert "43" is not x |
| 273 |
assert None is not x |
| 274 |
x = typ(42) |
| 275 |
assert x is x |
| 276 |
assert x is not "43" |
| 277 |
assert x is not None |
| 278 |
assert "43" is not x |
| 279 |
assert None is not x |
| 280 |
|
| 281 |
@pytest.mark.pypy_only |
| 282 |
def test_id_on_primitives(self): |
| 283 |
assert id(1) == (1 << 4) + 1 |
| 284 |
class myint(int): |
| 285 |
pass |
| 286 |
assert id(myint(1)) != id(1) |
| 287 |
|
| 288 |
assert id(1.0) & 7 == 5 |
| 289 |
assert id(-0.0) != id(0.0) |
| 290 |
assert hex(id(2.0)) == '0x40000000000000005' |
| 291 |
assert id(0.0) == 5 |
| 292 |
|
| 293 |
def test_id_on_strs(self): |
| 294 |
if self.appdirect: |
| 295 |
skip("cannot run this test as apptest") |
| 296 |
for u in [u"", u"a", u"aa"]: |
| 297 |
assert id(self.unwrap_wrap_unicode(u)) == id(u) |
| 298 |
s = u.encode() |
| 299 |
assert id(self.unwrap_wrap_bytes(s)) == id(s) |
| 300 |
|
| 301 |
assert id(b'') == (256 << 4) | 11 |
| 302 |
assert id(u'') == (257 << 4) | 11 |
| 303 |
assert id(b'a') == (ord('a') << 4) | 11 |
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
def test_id_of_tuples(self): |
| 308 |
l = [] |
| 309 |
x = (l,) |
| 310 |
assert id(x) != id((l,)) |
| 311 |
if self.appdirect: |
| 312 |
skip("cannot run this test as apptest") |
| 313 |
assert id(()) == (258 << 4) | 11 |
| 314 |
|
| 315 |
def test_id_of_frozensets(self): |
| 316 |
x = frozenset([4]) |
| 317 |
assert id(x) != id(frozenset([4])) |
| 318 |
if self.appdirect: |
| 319 |
skip("cannot run this test as apptest") |
| 320 |
assert id(frozenset()) == (259 << 4) | 11 |
| 321 |
assert id(frozenset([])) == (259 << 4) | 11 |
| 322 |
|
| 323 |
def test_identity_vs_id_primitives(self): |
| 324 |
import sys |
| 325 |
l = list(range(-10, 10, 2)) |
| 326 |
for i in [0, 1, 3]: |
| 327 |
l.append(float(i)) |
| 328 |
l.append(i + 0.1) |
| 329 |
l.append(i + sys.maxsize) |
| 330 |
l.append(i - sys.maxsize) |
| 331 |
l.append(i + 1j) |
| 332 |
l.append(i - 1j) |
| 333 |
l.append(1 + i * 1j) |
| 334 |
l.append(1 - i * 1j) |
| 335 |
l.append((i,)) |
| 336 |
l.append(frozenset([i])) |
| 337 |
l.append(-0.0) |
| 338 |
l.append(None) |
| 339 |
l.append(True) |
| 340 |
l.append(False) |
| 341 |
l.append(()) |
| 342 |
l.append(tuple([])) |
| 343 |
l.append(frozenset()) |
| 344 |
|
| 345 |
for i, a in enumerate(l): |
| 346 |
for b in l[i:]: |
| 347 |
assert (a is b) == (id(a) == id(b)) |
| 348 |
if a is b: |
| 349 |
assert a == b |
| 350 |
|
| 351 |
def test_identity_vs_id_str(self): |
| 352 |
if self.appdirect: |
| 353 |
skip("cannot run this test as apptest") |
| 354 |
l = [] |
| 355 |
def add(s, u): |
| 356 |
l.append(s) |
| 357 |
l.append(self.unwrap_wrap_bytes(s)) |
| 358 |
l.append(s[:1] + s[1:]) |
| 359 |
l.append(u) |
| 360 |
l.append(self.unwrap_wrap_unicode(u)) |
| 361 |
l.append(u[:1] + u[1:]) |
| 362 |
for i in range(3, 18): |
| 363 |
add(str(i).encode(), str(i)) |
| 364 |
add(b"s", u"s") |
| 365 |
add(b"", u"") |
| 366 |
|
| 367 |
for i, a in enumerate(l): |
| 368 |
for b in l[i:]: |
| 369 |
assert (a is b) == (id(a) == id(b)) |
| 370 |
if a is b: |
| 371 |
assert a == b |
| 372 |
|
| 373 |
def test_identity_bug(self): |
| 374 |
x = 0x4000000000000000 |
| 375 |
y = 2j |
| 376 |
assert id(x) != id(y) |
| 377 |
|
| 378 |
def test_id_must_be_int(self): |
| 379 |
identity = id([]) |
| 380 |
identity2 = int(identity) |
| 381 |
if isinstance(identity2, int): |
| 382 |
assert isinstance(identity, int) |
| 383 |
|
| 384 |
def test_object_hash_immutable(self): |
| 385 |
x = 42 |
| 386 |
y = 40 |
| 387 |
y += 2 |
| 388 |
assert object.__hash__(x) == object.__hash__(y) |
| 389 |
|
| 390 |
def test_richcompare(self): |
| 391 |
o = object() |
| 392 |
o2 = object() |
| 393 |
assert o.__eq__(o) is True |
| 394 |
assert o.__eq__(o2) is NotImplemented |
| 395 |
assert o.__ne__(o) is False |
| 396 |
assert o.__ne__(o2) is NotImplemented |
| 397 |
assert o.__le__(o2) is NotImplemented |
| 398 |
assert o.__lt__(o2) is NotImplemented |
| 399 |
assert o.__ge__(o2) is NotImplemented |
| 400 |
assert o.__gt__(o2) is NotImplemented |
| 401 |
|
| 402 |
def test_init_subclass(self): |
| 403 |
object().__init_subclass__() |
| 404 |
object.__init_subclass__() |
| 405 |
raises(TypeError, object.__init_subclass__, 1) |
| 406 |
|
| 407 |
def test_better_error_init(self): |
| 408 |
class A: pass |
| 409 |
|
| 410 |
with raises(TypeError) as excinfo: |
| 411 |
A(1) |
| 412 |
assert "A() takes no arguments" in str(excinfo.value) |
| 413 |
|
| 414 |
with raises(TypeError) as excinfo: |
| 415 |
A().__init__(1) |
| 416 |
assert "A.__init__() takes exactly one argument (the instance to initialize)" in str(excinfo.value) |
| 417 |
|
| 418 |
class D: |
| 419 |
def __new__(cls, *args, **kwargs): |
| 420 |
super().__new__(cls, *args, **kwargs) |
| 421 |
def __init__(self, *args, **kwargs): |
| 422 |
super().__init__(*args, **kwargs) |
| 423 |
with raises(TypeError) as excinfo: |
| 424 |
D(3) |
| 425 |
assert 'object.__new__() takes exactly one argument (the type to instantiate)' in str(excinfo.value) |
| 426 |
|
| 427 |
|
| 428 |
class E: |
| 429 |
def __init__(self, *args, **kwargs): |
| 430 |
super().__init__(*args, **kwargs) |
| 431 |
|
| 432 |
error_msg = 'object.__init__() takes exactly one argument (the instance to initialize)' |
| 433 |
|
| 434 |
with raises(TypeError) as excinfo: |
| 435 |
E().__init__(42) |
| 436 |
print(excinfo.value) |
| 437 |
assert error_msg in str(excinfo.value) |
| 438 |
|
| 439 |
with raises(TypeError) as excinfo: |
| 440 |
object.__init__(E(), 42) |
| 441 |
print(excinfo.value) |
| 442 |
assert error_msg in str(excinfo.value) |
| 443 |
|
| 444 |
def test_class_getitem(self): |
| 445 |
for cls in [type, tuple]: |
| 446 |
ga = cls[int] |
| 447 |
assert ga.__origin__ is cls |
| 448 |
assert ga.__args__ == (int, ) |
| 449 |
|
| 450 |
def test_isinstance_shortcut(): |
| 451 |
from pypy.objspace.std import objspace |
| 452 |
space = objspace.StdObjSpace() |
| 453 |
w_a = space.newtext("a") |
| 454 |
space.type = None |
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
space.isinstance_w(w_a, space.w_text) |