Barry Warsaw | 4a420a0 | 2001-01-15 20:30:15 +0000 | [diff] [blame] | 1 | from test_support import verbose, TestFailed |
| 2 | |
| 3 | class F: |
| 4 | def a(self): |
| 5 | pass |
| 6 | |
| 7 | def b(): |
| 8 | 'my docstring' |
| 9 | pass |
| 10 | |
| 11 | # setting attributes on functions |
| 12 | try: |
| 13 | b.publish |
| 14 | except AttributeError: |
| 15 | pass |
| 16 | else: |
| 17 | raise TestFailed, 'expected AttributeError' |
| 18 | |
| 19 | b.publish = 1 |
| 20 | if b.publish <> 1: |
| 21 | raise TestFailed, 'function attribute not set to expected value' |
| 22 | |
| 23 | docstring = 'its docstring' |
| 24 | b.__doc__ = docstring |
| 25 | if b.__doc__ <> docstring: |
| 26 | raise TestFailed, 'problem with setting __doc__ attribute' |
| 27 | |
| 28 | if 'publish' not in dir(b): |
| 29 | raise TestFailed, 'attribute not in dir()' |
| 30 | |
| 31 | f1 = F() |
| 32 | f2 = F() |
| 33 | |
| 34 | try: |
| 35 | F.a.publish |
| 36 | except AttributeError: |
| 37 | pass |
| 38 | else: |
| 39 | raise TestFailed, 'expected AttributeError' |
| 40 | |
| 41 | try: |
| 42 | f1.a.publish |
| 43 | except AttributeError: |
| 44 | pass |
| 45 | else: |
| 46 | raise TestFailed, 'expected AttributeError' |
| 47 | |
| 48 | |
| 49 | F.a.publish = 1 |
| 50 | if F.a.publish <> 1: |
| 51 | raise TestFailed, 'unbound method attribute not set to expected value' |
| 52 | |
| 53 | if f1.a.publish <> 1: |
| 54 | raise TestFailed, 'bound method attribute access did not work' |
| 55 | |
| 56 | if f2.a.publish <> 1: |
| 57 | raise TestFailed, 'bound method attribute access did not work' |
| 58 | |
| 59 | if 'publish' not in dir(F.a): |
| 60 | raise TestFailed, 'attribute not in dir()' |
| 61 | |
| 62 | try: |
| 63 | f1.a.publish = 0 |
| 64 | except TypeError: |
| 65 | pass |
| 66 | else: |
| 67 | raise TestFailed, 'expected TypeError' |
| 68 | |
| 69 | F.a.myclass = F |
| 70 | f1.a.myclass |
| 71 | f2.a.myclass |
| 72 | f1.a.myclass |
| 73 | F.a.myclass |
| 74 | |
| 75 | if f1.a.myclass is not f2.a.myclass or \ |
| 76 | f1.a.myclass is not F.a.myclass: |
| 77 | raise TestFailed, 'attributes were not the same' |
| 78 | |
| 79 | # try setting __dict__ |
| 80 | try: |
| 81 | F.a.__dict__ = (1, 2, 3) |
| 82 | except TypeError: |
| 83 | pass |
| 84 | else: |
| 85 | raise TestFailed, 'expected TypeError' |
| 86 | |
| 87 | F.a.__dict__ = {'one': 11, 'two': 22, 'three': 33} |
| 88 | if f1.a.two <> 22: |
| 89 | raise TestFailed, 'setting __dict__' |
| 90 | |
| 91 | from UserDict import UserDict |
| 92 | d = UserDict({'four': 44, 'five': 55}) |
| 93 | |
| 94 | try: |
| 95 | F.a.__dict__ = d |
| 96 | except TypeError: |
| 97 | pass |
| 98 | else: |
| 99 | raise TestFailed |
| 100 | |
| 101 | if f2.a.one <> f1.a.one <> F.a.one <> 11: |
| 102 | raise TestFailed |