|
| 1 | +def skip_if_no_descriptors(): |
| 2 | + class Descriptor: |
| 3 | + def __get__(self, obj, cls): |
| 4 | + return |
| 5 | + |
| 6 | + class TestClass: |
| 7 | + Forward = Descriptor() |
| 8 | + |
| 9 | + a = TestClass() |
| 10 | + try: |
| 11 | + a.__class__ |
| 12 | + except AttributeError: |
| 13 | + # Target doesn't support __class__. |
| 14 | + print("SKIP") |
| 15 | + raise SystemExit |
| 16 | + |
| 17 | + b = a.Forward |
| 18 | + if "Descriptor" in repr(b.__class__): |
| 19 | + # Target doesn't support descriptors. |
| 20 | + print("SKIP") |
| 21 | + raise SystemExit |
| 22 | + |
| 23 | + |
| 24 | +skip_if_no_descriptors() |
| 25 | + |
| 26 | + |
| 27 | +# Test basic hazard-free mutations of the enclosing class. |
| 28 | +# See also cpydiff/core_class_setname_hazard.py for broken non-hazard-free cases. |
| 29 | + |
| 30 | + |
| 31 | +class GetSibling: |
| 32 | + def __set_name__(self, owner, name): |
| 33 | + print(getattr(owner, name + "_sib")) |
| 34 | + |
| 35 | + |
| 36 | +class GetSiblingTest: |
| 37 | + desc = GetSibling() |
| 38 | + desc_sib = 101 |
| 39 | + |
| 40 | + |
| 41 | +t101 = GetSiblingTest() |
| 42 | + |
| 43 | + |
| 44 | +class SetSibling: |
| 45 | + def __set_name__(self, owner, name): |
| 46 | + setattr(owner, name + "_sib", 102) |
| 47 | + |
| 48 | + |
| 49 | +class SetSiblingTest: |
| 50 | + desc = SetSibling() |
| 51 | + |
| 52 | + |
| 53 | +t102 = SetSiblingTest() |
| 54 | + |
| 55 | +print(t102.desc_sib) |
| 56 | + |
| 57 | + |
| 58 | +class DelSibling: |
| 59 | + def __set_name__(self, owner, name): |
| 60 | + delattr(owner, name + "_sib") |
| 61 | + |
| 62 | + |
| 63 | +class DelSiblingTest: |
| 64 | + desc = DelSibling() |
| 65 | + desc_sib = 103 |
| 66 | + |
| 67 | + |
| 68 | +t103 = DelSiblingTest() |
| 69 | + |
| 70 | +try: |
| 71 | + print(t103.desc_sib) |
| 72 | +except AttributeError: |
| 73 | + print("AttributeError") |
| 74 | + |
| 75 | + |
| 76 | +class GetSelf: |
| 77 | + x = 201 |
| 78 | + |
| 79 | + def __set_name__(self, owner, name): |
| 80 | + print(getattr(owner, name).x) |
| 81 | + |
| 82 | + |
| 83 | +class GetSelfTest: |
| 84 | + desc = GetSelf() |
| 85 | + |
| 86 | + |
| 87 | +t201 = GetSelfTest() |
| 88 | + |
| 89 | + |
| 90 | +class SetSelf: |
| 91 | + def __set_name__(self, owner, name): |
| 92 | + setattr(owner, name, 202) |
| 93 | + |
| 94 | + |
| 95 | +class SetSelfTest: |
| 96 | + desc = SetSelf() |
| 97 | + |
| 98 | + |
| 99 | +t202 = SetSelfTest() |
| 100 | + |
| 101 | +print(t202.desc) |
| 102 | + |
| 103 | + |
| 104 | +class DelSelf: |
| 105 | + def __set_name__(self, owner, name): |
| 106 | + delattr(owner, name) |
| 107 | + |
| 108 | + |
| 109 | +class DelSelfTest: |
| 110 | + desc = DelSelf() |
| 111 | + |
| 112 | + |
| 113 | +t203 = DelSelfTest() |
| 114 | + |
| 115 | +try: |
| 116 | + print(t203.desc) |
| 117 | +except AttributeError: |
| 118 | + print("AttributeError") |
0 commit comments