| 1 |
|
| 2 |
from pytest import raises |
| 3 |
|
| 4 |
class Test_DescrOperation: |
| 5 |
|
| 6 |
def test_nonzero(self): |
| 7 |
space = self.space |
| 8 |
assert space.nonzero(space.w_True) is space.w_True |
| 9 |
assert space.nonzero(space.w_False) is space.w_False |
| 10 |
assert space.nonzero(space.wrap(42)) is space.w_True |
| 11 |
assert space.nonzero(space.wrap(0)) is space.w_False |
| 12 |
l = space.newlist([]) |
| 13 |
assert space.nonzero(l) is space.w_False |
| 14 |
space.call_method(l, 'append', space.w_False) |
| 15 |
assert space.nonzero(l) is space.w_True |
| 16 |
|
| 17 |
def test_isinstance_and_issubtype_ignore_special(self): |
| 18 |
space = self.space |
| 19 |
w_tup = space.appexec((), """(): |
| 20 |
class Meta(type): |
| 21 |
def __subclasscheck__(mcls, cls): |
| 22 |
return False |
| 23 |
class Base(metaclass=Meta): |
| 24 |
pass |
| 25 |
class Sub(Base): |
| 26 |
pass |
| 27 |
return Base, Sub""") |
| 28 |
w_base, w_sub = space.unpackiterable(w_tup) |
| 29 |
assert space.issubtype_w(w_sub, w_base) |
| 30 |
w_inst = space.call_function(w_sub) |
| 31 |
assert space.isinstance_w(w_inst, w_base) |
| 32 |
|
| 33 |
def test_shortcut(self, monkeypatch, space): |
| 34 |
w_l = space.wrap([1, 2, 3, 4]) |
| 35 |
oldlookup = space.lookup |
| 36 |
def lookup(obj, name): |
| 37 |
if name == "iter": |
| 38 |
return None |
| 39 |
return oldlookup(obj, name) |
| 40 |
monkeypatch.setattr(space, "lookup", lookup) |
| 41 |
w_iter = space.iter(w_l) |
| 42 |
w_first = space.next(w_iter) |
| 43 |
assert space.int_w(w_first) == 1 |
| 44 |
|
| 45 |
def test_shortcut_binop(self, monkeypatch, space): |
| 46 |
w_x = space.newutf8('abc', 3) |
| 47 |
w_y = space.newutf8('def', 3) |
| 48 |
monkeypatch.setattr(space, "lookup", None) |
| 49 |
assert space.utf8_w(space.add(w_x, w_y)) == 'abcdef' |
| 50 |
|
| 51 |
def test_shortcut_binop_not_implemented(self, space): |
| 52 |
from pypy.interpreter.error import OperationError |
| 53 |
w_x = space.newutf8('abc', 3) |
| 54 |
w_y = space.newutf8('def', 3) |
| 55 |
with raises(OperationError): |
| 56 |
assert space.utf8_w(space.mul(w_x, w_y)) == 'abcdef' |
| 57 |
|
| 58 |
def test_shortcut_eq(self, monkeypatch, space): |
| 59 |
w_x = space.newutf8('abc', 3) |
| 60 |
w_y = space.newutf8('def', 3) |
| 61 |
monkeypatch.setattr(space, "lookup", None) |
| 62 |
assert not space.eq_w(w_x, w_y) |
| 63 |
|
| 64 |
def test_shortcut_dictiter(self, monkeypatch, space): |
| 65 |
w_x = space.wrap({'a': 1}) |
| 66 |
oldlookup = space.lookup |
| 67 |
def lookup(obj, name): |
| 68 |
if name == "iter": |
| 69 |
return None |
| 70 |
return oldlookup(obj, name) |
| 71 |
monkeypatch.setattr(space, "lookup", lookup) |
| 72 |
w_iter = space.iter(w_x) |
| 73 |
w_first = space.next(w_iter) |
| 74 |
assert space.utf8_w(w_first) == 'a' |
| 75 |
|
| 76 |
def test_shortcut_str_getitem(self, monkeypatch, space): |
| 77 |
w_x = space.newbytes('abc') |
| 78 |
monkeypatch.setattr(space, "lookup", None) |
| 79 |
w_first = space.getitem(w_x, space.newint(0)) |
| 80 |
assert space.int_w(w_first) == ord('a') |
| 81 |
|
| 82 |
def test_shortcut_generatoriterator(self): |
| 83 |
from pypy.interpreter.generator import GeneratorIterator |
| 84 |
assert 'shortcut___next__' in GeneratorIterator.__dict__ |
| 85 |
|
| 86 |
def test_shortcut_len(self, monkeypatch, space): |
| 87 |
monkeypatch.setattr(space, "lookup", None) |
| 88 |
for val in "abc", [1, 2, 3], {'a', 'b', 'c'}, dict(a=1, b=2, d=3): |
| 89 |
assert space.len_w(space.wrap(val)) == 3 |
| 90 |
|