| 1 |
def test_non_data_descr(): |
| 2 |
class X(object): |
| 3 |
def f(self): |
| 4 |
return 42 |
| 5 |
x = X() |
| 6 |
assert x.f() == 42 |
| 7 |
x.f = 43 |
| 8 |
assert x.f == 43 |
| 9 |
del x.f |
| 10 |
assert x.f() == 42 |
| 11 |
|
| 12 |
def test_set_without_get(): |
| 13 |
class Descr(object): |
| 14 |
|
| 15 |
def __init__(self, name): |
| 16 |
self.name = name |
| 17 |
|
| 18 |
def __set__(self, obj, value): |
| 19 |
obj.__dict__[self.name] = value |
| 20 |
descr = Descr("a") |
| 21 |
|
| 22 |
class X(object): |
| 23 |
a = descr |
| 24 |
|
| 25 |
x = X() |
| 26 |
assert x.a is descr |
| 27 |
x.a = 42 |
| 28 |
assert x.a == 42 |
| 29 |
|
| 30 |
def test_failing_get(): |
| 31 |
|
| 32 |
|
| 33 |
class X(object): |
| 34 |
def get_v(self): |
| 35 |
raise AttributeError |
| 36 |
v = property(get_v) |
| 37 |
|
| 38 |
def __getattr__(self, name): |
| 39 |
if name == 'v': |
| 40 |
return 42 |
| 41 |
x = X() |
| 42 |
assert x.v == 42 |
| 43 |
|
| 44 |
|
| 45 |
class Y(object): |
| 46 |
def get_w(self): |
| 47 |
raise AttributeError |
| 48 |
def set_w(self, value): |
| 49 |
raise AttributeError |
| 50 |
w = property(get_w, set_w) |
| 51 |
y = Y() |
| 52 |
y.__dict__['w'] = 42 |
| 53 |
raises(AttributeError, getattr, y, 'w') |
| 54 |
|
| 55 |
def test_member(): |
| 56 |
class X(object): |
| 57 |
def __init__(self): |
| 58 |
self._v = 0 |
| 59 |
def get_v(self): |
| 60 |
return self._v |
| 61 |
def set_v(self, v): |
| 62 |
self._v = v |
| 63 |
v = property(get_v, set_v) |
| 64 |
x = X() |
| 65 |
assert x.v == 0 |
| 66 |
assert X.v.__get__(x) == 0 |
| 67 |
x.v = 1 |
| 68 |
assert x.v == 1 |
| 69 |
X.v.__set__(x, 0) |
| 70 |
assert x.v == 0 |
| 71 |
raises(AttributeError, delattr, x, 'v') |
| 72 |
raises(AttributeError, X.v.__delete__, x) |
| 73 |
|
| 74 |
def test_invalid_unicode_identifier(): |
| 75 |
skip("utf-8 encoding before translation accepts lone surrogates, " |
| 76 |
"because it is Python 2.7, but after translation it does not. " |
| 77 |
"Moreover, CPython 3.x accepts such unicode attributes anyway. " |
| 78 |
"This makes this test half-wrong for now.") |
| 79 |
class X(object): |
| 80 |
pass |
| 81 |
x = X() |
| 82 |
raises(AttributeError, setattr, x, '\ud800', 1) |
| 83 |
raises(AttributeError, getattr, x, '\ud800') |
| 84 |
raises(AttributeError, delattr, x, '\ud800') |
| 85 |
raises(AttributeError, getattr, x, '\uDAD1\uD51E') |
| 86 |
|
| 87 |
def test_special_methods_returning_strings(): |
| 88 |
class A(object): |
| 89 |
seen = [] |
| 90 |
def __str__(self): |
| 91 |
self.seen.append(1) |
| 92 |
def __repr__(self): |
| 93 |
self.seen.append(2) |
| 94 |
def __oct__(self): |
| 95 |
self.seen.append(3) |
| 96 |
def __hex__(self): |
| 97 |
self.seen.append(4) |
| 98 |
|
| 99 |
inst = A() |
| 100 |
raises(TypeError, str, inst) |
| 101 |
raises(TypeError, repr, inst) |
| 102 |
raises(TypeError, oct, inst) |
| 103 |
raises(TypeError, hex, inst) |
| 104 |
assert A.seen == [1,2] |
| 105 |
|
| 106 |
def test_hash(): |
| 107 |
class A(object): |
| 108 |
pass |
| 109 |
hash(A()) |
| 110 |
|
| 111 |
class B(object): |
| 112 |
def __eq__(self, other): pass |
| 113 |
assert B.__hash__ is None |
| 114 |
with raises(TypeError): |
| 115 |
hash(B()) |
| 116 |
|
| 117 |
class E(object): |
| 118 |
def __hash__(self): |
| 119 |
return "something" |
| 120 |
raises(TypeError, hash, E()) |
| 121 |
|
| 122 |
class G: |
| 123 |
def __hash__(self): |
| 124 |
return 1 |
| 125 |
assert isinstance(hash(G()), int) |
| 126 |
|
| 127 |
|
| 128 |
class myint(int): |
| 129 |
pass |
| 130 |
class I(object): |
| 131 |
def __hash__(self): |
| 132 |
return myint(15) |
| 133 |
assert hash(I()) == 15 |
| 134 |
assert type(hash(I())) is int |
| 135 |
|
| 136 |
|
| 137 |
class myint(int): |
| 138 |
pass |
| 139 |
class myfloat(float): |
| 140 |
pass |
| 141 |
class myHashClass(object): |
| 142 |
def __hash__(self): |
| 143 |
return -1 |
| 144 |
class myHashClass3(object): |
| 145 |
def __hash__(self): |
| 146 |
return -10**100 |
| 147 |
|
| 148 |
assert hash(-1) == -2 |
| 149 |
assert hash(-1.0) == -2 |
| 150 |
assert hash(-1 + 0j) == -2 |
| 151 |
assert hash(myint(-1)) == -2 |
| 152 |
assert hash(myfloat(-1.0)) == -2 |
| 153 |
assert hash(myHashClass()) == -2 |
| 154 |
assert hash(myHashClass3()) == hash(-10**100) |
| 155 |
|
| 156 |
def test_descr_funny_new(): |
| 157 |
class C(object): |
| 158 |
@classmethod |
| 159 |
def __new__(*args): |
| 160 |
return args |
| 161 |
|
| 162 |
assert C.__new__(1,2) == (C, 1, 2) |
| 163 |
assert C(1,2) == (C, C, 1, 2) |
| 164 |
|
| 165 |
def test_issue3255(): |
| 166 |
class MagicCaller(object): |
| 167 |
def __get__(self, *args): |
| 168 |
raise Exception("should not be called") |
| 169 |
def __call__(self, *args): |
| 170 |
return lambda attr: attr |
| 171 |
class Descriptor(object): |
| 172 |
__get__ = MagicCaller() |
| 173 |
class X(object): |
| 174 |
__getattribute__ = Descriptor() |
| 175 |
assert X().foo == "foo" |