MCPcopy Index your code
hub / github.com/python/cpython / test_set_class

Method test_set_class

Lib/test/test_descr.py:3417–3504  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

3415 self.assertEqual(NewClass().__doc__, 'object=NewClass instance; type=NewClass')
3416
3417 def test_set_class(self):
3418 # Testing __class__ assignment...
3419 class C(object): pass
3420 class D(object): pass
3421 class E(object): pass
3422 class F(D, E): pass
3423 for cls in C, D, E, F:
3424 for cls2 in C, D, E, F:
3425 x = cls()
3426 x.__class__ = cls2
3427 self.assertIs(x.__class__, cls2)
3428 x.__class__ = cls
3429 self.assertIs(x.__class__, cls)
3430 def cant(x, C):
3431 try:
3432 x.__class__ = C
3433 except TypeError:
3434 pass
3435 else:
3436 self.fail("shouldn't allow %r.__class__ = %r" % (x, C))
3437 try:
3438 delattr(x, "__class__")
3439 except (TypeError, AttributeError):
3440 pass
3441 else:
3442 self.fail("shouldn't allow del %r.__class__" % x)
3443 cant(C(), list)
3444 cant(list(), C)
3445 cant(C(), 1)
3446 cant(C(), object)
3447 cant(object(), list)
3448 cant(list(), object)
3449 class Int(int): __slots__ = []
3450 cant(True, int)
3451 cant(2, bool)
3452 o = object()
3453 cant(o, int)
3454 cant(o, type(None))
3455 del o
3456 class G(object):
3457 __slots__ = ["a", "b"]
3458 class H(object):
3459 __slots__ = ["b", "a"]
3460 class I(object):
3461 __slots__ = ["a", "b"]
3462 class J(object):
3463 __slots__ = ["c", "b"]
3464 class K(object):
3465 __slots__ = ["a", "b", "d"]
3466 class L(H):
3467 __slots__ = ["e"]
3468 class M(I):
3469 __slots__ = ["e"]
3470 class N(J):
3471 __slots__ = ["__weakref__"]
3472 class P(J):
3473 __slots__ = ["__dict__"]
3474 class Q(J):

Callers

nothing calls this directly

Calls 6

listClass · 0.85
CClass · 0.70
AClass · 0.70
clsClass · 0.50
assertIsMethod · 0.45
assertEqualMethod · 0.45

Tested by

no test coverage detected