| 1 |
class AppTestBuiltinApp: |
| 2 |
def test_staticmethod(self): |
| 3 |
class C(object): |
| 4 |
def f(a, b): |
| 5 |
return a+b |
| 6 |
f = staticmethod(f) |
| 7 |
class D(C): |
| 8 |
pass |
| 9 |
|
| 10 |
c = C() |
| 11 |
d = D() |
| 12 |
assert c.f("abc", "def") == "abcdef" |
| 13 |
assert C.f("abc", "def") == "abcdef" |
| 14 |
assert d.f("abc", "def") == "abcdef" |
| 15 |
assert D.f("abc", "def") == "abcdef" |
| 16 |
|
| 17 |
def test_staticmethod_dict(self): |
| 18 |
sm = staticmethod(None) |
| 19 |
assert sm.__dict__ == {"__doc__": None} |
| 20 |
sm.x = 42 |
| 21 |
assert sm.x == 42 |
| 22 |
assert sm.__dict__["x"] == 42 |
| 23 |
del sm.x |
| 24 |
assert not hasattr(sm, "x") |
| 25 |
raises(TypeError, setattr, sm, '__dict__', []) |
| 26 |
raises((AttributeError, TypeError), delattr, sm, '__dict__') |
| 27 |
|
| 28 |
def test_staticmethod_subclass(self): |
| 29 |
class Static(staticmethod): |
| 30 |
pass |
| 31 |
x = Static(1) |
| 32 |
assert isinstance(x, Static) |
| 33 |
|
| 34 |
class C(Static): |
| 35 |
def __init__(self, callable): |
| 36 |
super(C, self).__init__(callable) |
| 37 |
y = C(1) |
| 38 |
assert isinstance(y, C) |
| 39 |
|
| 40 |
def test_classmethod(self): |
| 41 |
class C(object): |
| 42 |
def f(cls, stuff): |
| 43 |
return cls, stuff |
| 44 |
f = classmethod(f) |
| 45 |
class D(C): |
| 46 |
pass |
| 47 |
|
| 48 |
c = C() |
| 49 |
d = D() |
| 50 |
assert c.f("abc") == (C, "abc") |
| 51 |
assert C.f("abc") == (C, "abc") |
| 52 |
assert d.f("abc") == (D, "abc") |
| 53 |
assert D.f("abc") == (D, "abc") |
| 54 |
|
| 55 |
def test_classmethod_subclass(self): |
| 56 |
class Classm(classmethod): |
| 57 |
pass |
| 58 |
x = Classm(1) |
| 59 |
assert isinstance(x, Classm) |
| 60 |
|
| 61 |
class C(Classm): |
| 62 |
def __init__(self, callable): |
| 63 |
super(C, self).__init__(callable) |
| 64 |
y = C(1) |
| 65 |
assert isinstance(y, C) |
| 66 |
|
| 67 |
def test_property_simple(self): |
| 68 |
|
| 69 |
class a(object): |
| 70 |
def _get(self): return 42 |
| 71 |
def _set(self, value): raise AttributeError |
| 72 |
def _del(self): raise KeyError |
| 73 |
name = property(_get, _set, _del) |
| 74 |
a1 = a() |
| 75 |
assert a1.name == 42 |
| 76 |
raises(AttributeError, setattr, a1, 'name', 42) |
| 77 |
raises(KeyError, delattr, a1, 'name') |
| 78 |
|
| 79 |
def test_super(self): |
| 80 |
class A(object): |
| 81 |
def f(self): |
| 82 |
return 'A' |
| 83 |
class B(A): |
| 84 |
def f(self): |
| 85 |
return 'B' + super(B,self).f() |
| 86 |
class C(A): |
| 87 |
def f(self): |
| 88 |
return 'C' + super(C,self).f() |
| 89 |
class D(B, C): |
| 90 |
def f(self): |
| 91 |
return 'D' + super(D,self).f() |
| 92 |
d = D() |
| 93 |
assert d.f() == "DBCA" |
| 94 |
assert D.__mro__ == (D, B, C, A, object) |
| 95 |
|
| 96 |
def test_super_magic(self): |
| 97 |
class A(object): |
| 98 |
def f(self): |
| 99 |
return 'A' |
| 100 |
class B(A): |
| 101 |
def f(self): |
| 102 |
return 'B' + super().f() |
| 103 |
class C(A): |
| 104 |
def f(self): |
| 105 |
return 'C' + super().f() |
| 106 |
class D(B, C): |
| 107 |
def f(self): |
| 108 |
return 'D' + super().f() |
| 109 |
d = D() |
| 110 |
assert d.f() == "DBCA" |
| 111 |
assert D.__mro__ == (D, B, C, A, object) |
| 112 |
|
| 113 |
def test_super_metaclass(self): |
| 114 |
class xtype(type): |
| 115 |
def __init__(self, name, bases, dict): |
| 116 |
super(xtype, self).__init__(name, bases, dict) |
| 117 |
A = xtype('A', (), {}) |
| 118 |
assert isinstance(A, xtype) |
| 119 |
a = A() |
| 120 |
assert isinstance(a, A) |
| 121 |
|
| 122 |
def test_super_classmethod(self): |
| 123 |
class A(object): |
| 124 |
def f(cls): |
| 125 |
return cls |
| 126 |
f = classmethod(f) |
| 127 |
class B(A): |
| 128 |
def f(cls): |
| 129 |
return [cls, super(B, cls).f()] |
| 130 |
f = classmethod(f) |
| 131 |
assert B().f() == [B, B] |
| 132 |
|
| 133 |
def test_super_magic_classmethod(self): |
| 134 |
class A(object): |
| 135 |
def f(cls): |
| 136 |
return cls |
| 137 |
f = classmethod(f) |
| 138 |
class B(A): |
| 139 |
def f(cls): |
| 140 |
return [cls, super().f()] |
| 141 |
f = classmethod(f) |
| 142 |
assert B().f() == [B, B] |
| 143 |
|
| 144 |
def test_super_fail(self): |
| 145 |
try: |
| 146 |
super(list, 2) |
| 147 |
except TypeError as e: |
| 148 |
message = e.args[0] |
| 149 |
assert message.startswith('super(type, obj): obj must be an instance or subtype of type') |
| 150 |
|
| 151 |
def test_super_various(self): |
| 152 |
|
| 153 |
class A(object): |
| 154 |
def meth(self, a): |
| 155 |
return "A(%r)" % a |
| 156 |
|
| 157 |
class B(A): |
| 158 |
def __init__(self): |
| 159 |
self.__super = super(B, self) |
| 160 |
def meth(self, a): |
| 161 |
return "B(%r)" % a + self.__super.meth(a) |
| 162 |
|
| 163 |
assert B().meth(2) == "B(2)A(2)" |
| 164 |
|
| 165 |
class C(A): |
| 166 |
def meth(self, a): |
| 167 |
return "C(%r)" % a + self.__super.meth(a) |
| 168 |
C._C__super = super(C) |
| 169 |
|
| 170 |
assert C().meth(3) == "C(3)A(3)" |
| 171 |
|
| 172 |
class D(C, B): |
| 173 |
def meth(self, a): |
| 174 |
return "D(%r)" % a + super(D, self).meth(a) |
| 175 |
|
| 176 |
assert D().meth(4) == "D(4)C(4)B(4)A(4)" |
| 177 |
|
| 178 |
|
| 179 |
class mysuper(super): |
| 180 |
def __init__(self, *args): |
| 181 |
return super(mysuper, self).__init__(*args) |
| 182 |
|
| 183 |
class E(D): |
| 184 |
def meth(self, a): |
| 185 |
return "E(%r)" % a + mysuper(E, self).meth(a) |
| 186 |
|
| 187 |
assert E().meth(5) == "E(5)D(5)C(5)B(5)A(5)" |
| 188 |
|
| 189 |
class F(E): |
| 190 |
def meth(self, a): |
| 191 |
s = self.__super |
| 192 |
return "F(%r)[%s]" % (a, s.__class__.__name__) + s.meth(a) |
| 193 |
F._F__super = mysuper(F) |
| 194 |
|
| 195 |
assert F().meth(6) == "F(6)[mysuper]E(6)D(6)C(6)B(6)A(6)" |
| 196 |
|
| 197 |
x = mysuper(F, F()) |
| 198 |
x.foobar = 42 |
| 199 |
assert x.foobar == 42 |
| 200 |
|
| 201 |
|
| 202 |
def test_super_lookup(self): |
| 203 |
class DDbase(object): |
| 204 |
def getx(self): |
| 205 |
return 42 |
| 206 |
x = property(getx) |
| 207 |
|
| 208 |
class DDsub(DDbase): |
| 209 |
def getx(self): |
| 210 |
return "hello" |
| 211 |
x = property(getx) |
| 212 |
|
| 213 |
dd = DDsub() |
| 214 |
assert dd.x == "hello" |
| 215 |
assert super(DDsub, dd).x == 42 |
| 216 |
|
| 217 |
def test_super_lookup2(self): |
| 218 |
|
| 219 |
class Base(object): |
| 220 |
aProp = property(lambda self: "foo") |
| 221 |
|
| 222 |
class Sub(Base): |
| 223 |
def test(klass): |
| 224 |
return super(Sub,klass).aProp |
| 225 |
test = classmethod(test) |
| 226 |
|
| 227 |
assert Sub.test() is Base.aProp |
| 228 |
|
| 229 |
def test_proxy_super(self): |
| 230 |
class Proxy(object): |
| 231 |
def __init__(self, obj): |
| 232 |
self.__obj = obj |
| 233 |
def __getattribute__(self, name): |
| 234 |
if name.startswith("_Proxy__"): |
| 235 |
return object.__getattribute__(self, name) |
| 236 |
else: |
| 237 |
return getattr(self.__obj, name) |
| 238 |
|
| 239 |
class B(object): |
| 240 |
def f(self): |
| 241 |
return "B.f" |
| 242 |
|
| 243 |
class C(B): |
| 244 |
def f(self): |
| 245 |
return super(C, self).f() + "->C.f" |
| 246 |
|
| 247 |
obj = C() |
| 248 |
p = Proxy(obj) |
| 249 |
assert C.__dict__["f"](p) == "B.f->C.f" |
| 250 |
|
| 251 |
def test_super_errors(self): |
| 252 |
class C(object): |
| 253 |
pass |
| 254 |
class D(C): |
| 255 |
pass |
| 256 |
raises(TypeError, "super(D, 42)") |
| 257 |
raises(TypeError, "super(D, C())") |
| 258 |
raises(TypeError, "super(D).__get__(12)") |
| 259 |
raises(TypeError, "super(D).__get__(C())") |
| 260 |
|
| 261 |
def test_super_incomplete(self): |
| 262 |
""" |
| 263 |
class M(type): |
| 264 |
def mro(cls): |
| 265 |
if cls.__mro__ is None: |
| 266 |
raises(AttributeError, lambda: super(cls, cls).xxx) |
| 267 |
return type.mro(cls) |
| 268 |
class A(metaclass=M): |
| 269 |
pass |
| 270 |
""" |
| 271 |
|
| 272 |
def test_classmethods_various(self): |
| 273 |
class C(object): |
| 274 |
def foo(*a): return a |
| 275 |
goo = classmethod(foo) |
| 276 |
c = C() |
| 277 |
assert C.goo(1) == (C, 1) |
| 278 |
assert c.goo(1) == (C, 1) |
| 279 |
|
| 280 |
assert c.foo(1) == (c, 1) |
| 281 |
class D(C): |
| 282 |
pass |
| 283 |
d = D() |
| 284 |
assert D.goo(1) == (D, 1) |
| 285 |
assert d.goo(1) == (D, 1) |
| 286 |
assert d.foo(1) == (d, 1) |
| 287 |
assert D.foo(d, 1) == (d, 1) |
| 288 |
def f(cls, arg): return (cls, arg) |
| 289 |
ff = classmethod(f) |
| 290 |
assert ff.__get__(0, int)(42) == (int, 42) |
| 291 |
assert ff.__get__(0)(42) == (int, 42) |
| 292 |
|
| 293 |
assert C.goo.__self__ is C |
| 294 |
assert D.goo.__self__ is D |
| 295 |
assert super(D,D).goo.__self__ is D |
| 296 |
assert super(D,d).goo.__self__ is D |
| 297 |
assert super(D,D).goo() == (D,) |
| 298 |
assert super(D,d).goo() == (D,) |
| 299 |
|
| 300 |
meth = classmethod(1).__get__(1) |
| 301 |
raises(TypeError, meth) |
| 302 |
|
| 303 |
def test_classmethod_dict(self): |
| 304 |
cm = classmethod(None) |
| 305 |
assert cm.__dict__ == {"__doc__": None} |
| 306 |
cm.x = 42 |
| 307 |
assert cm.x == 42 |
| 308 |
assert cm.__dict__["x"] == 42 |
| 309 |
del cm.x |
| 310 |
assert not hasattr(cm, "x") |
| 311 |
cm.x = 42 |
| 312 |
cm.__dict__ = {} |
| 313 |
assert not hasattr(cm, "x") |
| 314 |
raises(TypeError, setattr, cm, '__dict__', []) |
| 315 |
raises((AttributeError, TypeError), delattr, cm, '__dict__') |
| 316 |
|
| 317 |
def test_super_thisclass(self): |
| 318 |
class A(object): |
| 319 |
pass |
| 320 |
|
| 321 |
assert super(A, A()).__thisclass__ is A |
| 322 |
|
| 323 |
class B(A): |
| 324 |
pass |
| 325 |
|
| 326 |
assert super(B, B()).__thisclass__ is B |
| 327 |
assert super(A, B()).__thisclass__ is A |
| 328 |
|
| 329 |
def test_super_self_selfclass(self): |
| 330 |
class A(object): |
| 331 |
pass |
| 332 |
class B(A): |
| 333 |
pass |
| 334 |
b = B() |
| 335 |
assert super(A, b).__self__ is b |
| 336 |
assert super(A).__self__ is None |
| 337 |
assert super(A, b).__self_class__ is B |
| 338 |
assert super(A).__self_class__ is None |
| 339 |
|
| 340 |
def test_super_repr(self): |
| 341 |
class A(object): |
| 342 |
def __repr__(self): |
| 343 |
return super(A, self).__repr__() + '!' |
| 344 |
assert repr(A()).endswith('>!') |
| 345 |
assert repr(super(A, A())) == "<super: <class 'A'>, <A object>>" |
| 346 |
|
| 347 |
def test_super_get_corner_case(self): |
| 348 |
class A(object): |
| 349 |
pass |
| 350 |
s1 = super(A, A()) |
| 351 |
assert s1.__get__(42) is s1 |
| 352 |
assert s1.__get__(42, int) is s1 |
| 353 |
s2 = super(A) |
| 354 |
assert s2.__get__(None, "anything") is s2 |
| 355 |
|
| 356 |
assert s1.__get__(None, "anything") is s1 |
| 357 |
raises(TypeError, s2.__get__, 42) |
| 358 |
raises(TypeError, s2.__get__, 42, int) |
| 359 |
a = A() |
| 360 |
assert s2.__get__(a).__self__ is a |
| 361 |
assert s1.__get__(a) is s1 |
| 362 |
|
| 363 |
def test_property_docstring(self): |
| 364 |
assert property.__doc__.startswith('property') |
| 365 |
|
| 366 |
class A(object): |
| 367 |
pass |
| 368 |
|
| 369 |
A.x = property(lambda x: x, lambda x, y: x, lambda x:x, 'xxx') |
| 370 |
assert A.x.__doc__ == 'xxx' |
| 371 |
|
| 372 |
|
| 373 |
def foo(x): |
| 374 |
"my docstring" |
| 375 |
assert property(foo).__doc__ == "my docstring" |
| 376 |
|
| 377 |
def test_property(self): |
| 378 |
class C(object): |
| 379 |
def getx(self): |
| 380 |
return self.__x |
| 381 |
def setx(self, value): |
| 382 |
self.__x = value |
| 383 |
def delx(self): |
| 384 |
del self.__x |
| 385 |
x = property(getx, setx, delx, doc="I'm the x property.") |
| 386 |
a = C() |
| 387 |
assert not hasattr(a, "x") |
| 388 |
a.x = 42 |
| 389 |
assert a._C__x == 42 |
| 390 |
assert a.x == 42 |
| 391 |
del a.x |
| 392 |
assert not hasattr(a, "x") |
| 393 |
assert not hasattr(a, "_C__x") |
| 394 |
C.x.__set__(a, 100) |
| 395 |
assert C.x.__get__(a) == 100 |
| 396 |
C.x.__delete__(a) |
| 397 |
assert not hasattr(a, "x") |
| 398 |
|
| 399 |
raw = C.__dict__['x'] |
| 400 |
assert isinstance(raw, property) |
| 401 |
|
| 402 |
attrs = dir(raw) |
| 403 |
assert "__doc__" in attrs |
| 404 |
assert "fget" in attrs |
| 405 |
assert "fset" in attrs |
| 406 |
assert "fdel" in attrs |
| 407 |
|
| 408 |
assert raw.__doc__ == "I'm the x property." |
| 409 |
raw.__doc__ = "modified" |
| 410 |
assert raw.__doc__ == "modified" |
| 411 |
assert raw.fget is C.__dict__['getx'] |
| 412 |
assert raw.fset is C.__dict__['setx'] |
| 413 |
assert raw.fdel is C.__dict__['delx'] |
| 414 |
|
| 415 |
for attr in "fget", "fset", "fdel": |
| 416 |
try: |
| 417 |
setattr(raw, attr, 42) |
| 418 |
|
| 419 |
|
| 420 |
except (TypeError, AttributeError) as msg: |
| 421 |
if str(msg).find('readonly') < 0: |
| 422 |
raise Exception("when setting readonly attr %r on a " |
| 423 |
"property, got unexpected TypeError " |
| 424 |
"msg %r" % (attr, str(msg))) |
| 425 |
else: |
| 426 |
raise Exception("expected TypeError from trying to set " |
| 427 |
"readonly %r attr on a property" % attr) |
| 428 |
|
| 429 |
class D(object): |
| 430 |
__getitem__ = property(lambda s: 1/0) |
| 431 |
|
| 432 |
d = D() |
| 433 |
try: |
| 434 |
for i in d: |
| 435 |
str(i) |
| 436 |
except ZeroDivisionError: |
| 437 |
pass |
| 438 |
else: |
| 439 |
raise Exception("expected ZeroDivisionError from bad property") |
| 440 |
|
| 441 |
def test_property_subclass(self): |
| 442 |
class P(property): |
| 443 |
pass |
| 444 |
|
| 445 |
p = P() |
| 446 |
p.name = 0 |
| 447 |
assert p.name == 0 |
| 448 |
|
| 449 |
def test_property_subclass_with_init(self): |
| 450 |
l = [] |
| 451 |
|
| 452 |
def x(self): |
| 453 |
l.append('x') |
| 454 |
|
| 455 |
class P(property): |
| 456 |
def __init__(self): |
| 457 |
property.__init__(self, x) |
| 458 |
|
| 459 |
class X(object): |
| 460 |
x = P() |
| 461 |
|
| 462 |
X().x |
| 463 |
assert l |
| 464 |
|
| 465 |
class P(property): |
| 466 |
def __init__(self, awesome): |
| 467 |
property.__init__(self, x) |
| 468 |
self.awesome = awesome |
| 469 |
|
| 470 |
l[:] = [] |
| 471 |
class X(object): |
| 472 |
x = P(awesome=True) |
| 473 |
|
| 474 |
X().x |
| 475 |
assert l |
| 476 |
assert X.x.awesome |
| 477 |
|
| 478 |
def test_property_decorator(self): |
| 479 |
class X(object): |
| 480 |
@property |
| 481 |
def x(self): |
| 482 |
return 4 |
| 483 |
@x.getter |
| 484 |
def x(self): |
| 485 |
return 2 |
| 486 |
@x.setter |
| 487 |
def x(self, new): |
| 488 |
self.y = 42 |
| 489 |
@x.deleter |
| 490 |
def x(self): |
| 491 |
self.z = 42 |
| 492 |
x = X() |
| 493 |
assert x.x == 2 |
| 494 |
x.x = 32 |
| 495 |
assert x.y == 42 |
| 496 |
del x.x |
| 497 |
assert x.z == 42 |
| 498 |
|
| 499 |
def test_abstract_property(self): |
| 500 |
def foo(self): pass |
| 501 |
foo.__isabstractmethod__ = True |
| 502 |
foo = property(foo) |
| 503 |
assert foo.__isabstractmethod__ is True |
| 504 |
|
| 505 |
def test___class___variable(self): |
| 506 |
class X: |
| 507 |
def f(self): |
| 508 |
return __class__ |
| 509 |
assert X().f() is X |
| 510 |
|
| 511 |
class X: |
| 512 |
@classmethod |
| 513 |
def f(cls): |
| 514 |
return __class__ |
| 515 |
assert X.f() is X |
| 516 |
|
| 517 |
class X: |
| 518 |
@staticmethod |
| 519 |
def f(): |
| 520 |
return __class__ |
| 521 |
assert X.f() is X |
| 522 |
|
| 523 |
def test_obscure_super_errors(self): |
| 524 |
""" |
| 525 |
def f(): |
| 526 |
super() |
| 527 |
raises(RuntimeError, f) |
| 528 |
def f(x): |
| 529 |
del x |
| 530 |
super() |
| 531 |
raises(RuntimeError, f, None) |
| 532 |
class X: |
| 533 |
def f(x): |
| 534 |
nonlocal __class__ |
| 535 |
del __class__ |
| 536 |
super() |
| 537 |
raises(RuntimeError, X().f) |
| 538 |
class X: |
| 539 |
def f(self): |
| 540 |
def g(): |
| 541 |
print(self) # make 'self' a closure inside 'f' |
| 542 |
del self |
| 543 |
super() |
| 544 |
raises(RuntimeError, X().f) |
| 545 |
class X: |
| 546 |
def f(*args): |
| 547 |
super() |
| 548 |
raises(RuntimeError, X().f) |
| 549 |
""" |
| 550 |
|
| 551 |
def test_uninitialized_property(self): |
| 552 |
p = property.__new__(property) |
| 553 |
raises(AttributeError, p.__get__, 42) |
| 554 |
raises(AttributeError, p.__set__, 42, None) |
| 555 |
raises(AttributeError, p.__delete__, 42) |
| 556 |
assert repr(p).startswith("<property object at ") |
| 557 |
assert p.fget is p.fset is p.fdel is p.__doc__ is None |
| 558 |
|
| 559 |
lst = [] |
| 560 |
p.deleter(lst.append).__delete__(42) |
| 561 |
assert lst == [42] |
| 562 |
|
| 563 |
lst = [] |
| 564 |
p.getter(lst.append).__get__(43) |
| 565 |
assert lst == [43] |
| 566 |
|
| 567 |
lst = [] |
| 568 |
p.setter(lambda x, y: lst.append((x, y))).__set__(44, 45) |
| 569 |
assert lst == [(44, 45)] |
| 570 |
|
| 571 |
def test_uninitialized_super(self): |
| 572 |
s = super.__new__(super) |
| 573 |
assert repr(s) == "<super: <class 'NULL'>, NULL>" |
| 574 |
assert s.__thisclass__ is s.__self__ is s.__self_class__ is None |
| 575 |
assert s.__get__(None, "anything") is s |
| 576 |
raises(TypeError, s.__get__, 42) |
| 577 |
raises(TypeError, s.__get__, int) |
| 578 |
raises(TypeError, s.__get__, type(None)) |
| 579 |
raises(AttributeError, "s.abcde") |
| 580 |
raises(AttributeError, "s.abcde = 42") |
| 581 |
raises(AttributeError, "del s.abcde") |
| 582 |
|
| 583 |
def test_cant_pickle_property(self): |
| 584 |
import _pickle as pickle |
| 585 |
p = property(1, 2, 3) |
| 586 |
raises(TypeError, pickle.dumps, p) |