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 |
Barry Warsaw | 534c60f | 2001-01-15 21:00:02 +0000 | [diff] [blame] | 103 | |
| 104 | # im_func may not be a Python method! |
| 105 | import new |
| 106 | F.id = new.instancemethod(id, None, F) |
| 107 | |
| 108 | eff = F() |
| 109 | if eff.id() <> id(eff): |
| 110 | raise TestFailed |
| 111 | |
| 112 | try: |
| 113 | F.id.foo |
| 114 | except AttributeError: pass |
| 115 | else: raise TestFailed |
| 116 | |
| 117 | try: |
| 118 | F.id.foo = 12 |
| 119 | except TypeError: pass |
| 120 | else: raise TestFailed |
| 121 | |
| 122 | try: |
| 123 | F.id.foo |
| 124 | except AttributeError: pass |
| 125 | else: raise TestFailed |
| 126 | |
| 127 | try: |
| 128 | eff.id.foo |
| 129 | except AttributeError: pass |
| 130 | else: raise TestFailed |
| 131 | |
| 132 | try: |
| 133 | eff.id.foo = 12 |
| 134 | except TypeError: pass |
| 135 | else: raise TestFailed |
| 136 | |
| 137 | try: |
| 138 | eff.id.foo |
| 139 | except AttributeError: pass |
| 140 | else: raise TestFailed |
Barry Warsaw | 2e9b396 | 2001-01-19 19:55:12 +0000 | [diff] [blame] | 141 | |
| 142 | # Regression test for a crash in pre-2.1a1 |
| 143 | def another(): |
| 144 | pass |
| 145 | del another.__dict__ |
| 146 | del another.func_dict |
| 147 | another.func_dict = None |
| 148 | |
| 149 | try: |
| 150 | del another.bar |
| 151 | except AttributeError: pass |
| 152 | else: raise TestFailed |
| 153 | |
| 154 | # This isn't specifically related to function attributes, but it does test a |
| 155 | # core dump regression in funcobject.c |
| 156 | del another.func_defaults |
Moshe Zadka | 497671e | 2001-01-29 06:21:17 +0000 | [diff] [blame] | 157 | |
| 158 | def foo(): |
| 159 | pass |
| 160 | |
| 161 | def bar(): |
| 162 | pass |
| 163 | |
| 164 | def temp(): |
| 165 | print 1 |
| 166 | |
| 167 | if foo==bar: raise TestFailed |
| 168 | |
| 169 | d={} |
| 170 | d[foo] = 1 |
| 171 | |
| 172 | foo.func_code = temp.func_code |
| 173 | |
| 174 | d[foo] |
| 175 | |