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 | |
Barry Warsaw | c1e100f | 2001-02-26 18:07:26 +0000 | [diff] [blame] | 19 | if b.__dict__ <> None: |
| 20 | raise TestFailed, 'expected unassigned func.__dict__ to be None' |
| 21 | |
Barry Warsaw | 4a420a0 | 2001-01-15 20:30:15 +0000 | [diff] [blame] | 22 | b.publish = 1 |
| 23 | if b.publish <> 1: |
| 24 | raise TestFailed, 'function attribute not set to expected value' |
| 25 | |
| 26 | docstring = 'its docstring' |
| 27 | b.__doc__ = docstring |
| 28 | if b.__doc__ <> docstring: |
| 29 | raise TestFailed, 'problem with setting __doc__ attribute' |
| 30 | |
| 31 | if 'publish' not in dir(b): |
| 32 | raise TestFailed, 'attribute not in dir()' |
| 33 | |
Barry Warsaw | c1e100f | 2001-02-26 18:07:26 +0000 | [diff] [blame] | 34 | del b.__dict__ |
| 35 | if b.__dict__ <> None: |
| 36 | raise TestFailed, 'del func.__dict__ did not result in __dict__ == None' |
| 37 | |
| 38 | b.publish = 1 |
| 39 | b.__dict__ = None |
| 40 | if b.__dict__ <> None: |
| 41 | raise TestFailed, 'func.__dict__ = None did not result in __dict__ == None' |
| 42 | |
| 43 | |
Barry Warsaw | 4a420a0 | 2001-01-15 20:30:15 +0000 | [diff] [blame] | 44 | f1 = F() |
| 45 | f2 = F() |
| 46 | |
| 47 | try: |
| 48 | F.a.publish |
| 49 | except AttributeError: |
| 50 | pass |
| 51 | else: |
| 52 | raise TestFailed, 'expected AttributeError' |
| 53 | |
| 54 | try: |
| 55 | f1.a.publish |
| 56 | except AttributeError: |
| 57 | pass |
| 58 | else: |
| 59 | raise TestFailed, 'expected AttributeError' |
| 60 | |
Barry Warsaw | c1e100f | 2001-02-26 18:07:26 +0000 | [diff] [blame] | 61 | # In Python 2.1 beta 1, we disallowed setting attributes on unbound methods |
| 62 | # (it was already disallowed on bound methods). See the PEP for details. |
| 63 | try: |
| 64 | F.a.publish = 1 |
| 65 | except TypeError: |
| 66 | pass |
| 67 | else: |
| 68 | raise TestFailed, 'expected TypeError' |
Barry Warsaw | 4a420a0 | 2001-01-15 20:30:15 +0000 | [diff] [blame] | 69 | |
Barry Warsaw | c1e100f | 2001-02-26 18:07:26 +0000 | [diff] [blame] | 70 | # But setting it explicitly on the underlying function object is okay. |
| 71 | F.a.im_func.publish = 1 |
| 72 | |
Barry Warsaw | 4a420a0 | 2001-01-15 20:30:15 +0000 | [diff] [blame] | 73 | if F.a.publish <> 1: |
| 74 | raise TestFailed, 'unbound method attribute not set to expected value' |
| 75 | |
| 76 | if f1.a.publish <> 1: |
| 77 | raise TestFailed, 'bound method attribute access did not work' |
| 78 | |
| 79 | if f2.a.publish <> 1: |
| 80 | raise TestFailed, 'bound method attribute access did not work' |
| 81 | |
| 82 | if 'publish' not in dir(F.a): |
| 83 | raise TestFailed, 'attribute not in dir()' |
| 84 | |
| 85 | try: |
| 86 | f1.a.publish = 0 |
| 87 | except TypeError: |
| 88 | pass |
| 89 | else: |
| 90 | raise TestFailed, 'expected TypeError' |
| 91 | |
Barry Warsaw | c1e100f | 2001-02-26 18:07:26 +0000 | [diff] [blame] | 92 | # See the comment above about the change in semantics for Python 2.1b1 |
| 93 | try: |
| 94 | F.a.myclass = F |
| 95 | except TypeError: |
| 96 | pass |
| 97 | else: |
| 98 | raise TestFailed, 'expected TypeError' |
| 99 | |
| 100 | F.a.im_func.myclass = F |
| 101 | |
Barry Warsaw | 4a420a0 | 2001-01-15 20:30:15 +0000 | [diff] [blame] | 102 | f1.a.myclass |
| 103 | f2.a.myclass |
| 104 | f1.a.myclass |
| 105 | F.a.myclass |
| 106 | |
| 107 | if f1.a.myclass is not f2.a.myclass or \ |
| 108 | f1.a.myclass is not F.a.myclass: |
| 109 | raise TestFailed, 'attributes were not the same' |
| 110 | |
| 111 | # try setting __dict__ |
| 112 | try: |
| 113 | F.a.__dict__ = (1, 2, 3) |
| 114 | except TypeError: |
| 115 | pass |
| 116 | else: |
| 117 | raise TestFailed, 'expected TypeError' |
| 118 | |
Barry Warsaw | c1e100f | 2001-02-26 18:07:26 +0000 | [diff] [blame] | 119 | F.a.im_func.__dict__ = {'one': 11, 'two': 22, 'three': 33} |
| 120 | |
Barry Warsaw | 4a420a0 | 2001-01-15 20:30:15 +0000 | [diff] [blame] | 121 | if f1.a.two <> 22: |
| 122 | raise TestFailed, 'setting __dict__' |
| 123 | |
| 124 | from UserDict import UserDict |
| 125 | d = UserDict({'four': 44, 'five': 55}) |
| 126 | |
| 127 | try: |
| 128 | F.a.__dict__ = d |
| 129 | except TypeError: |
| 130 | pass |
| 131 | else: |
| 132 | raise TestFailed |
| 133 | |
| 134 | if f2.a.one <> f1.a.one <> F.a.one <> 11: |
| 135 | raise TestFailed |
Barry Warsaw | 534c60f | 2001-01-15 21:00:02 +0000 | [diff] [blame] | 136 | |
| 137 | # im_func may not be a Python method! |
| 138 | import new |
| 139 | F.id = new.instancemethod(id, None, F) |
| 140 | |
| 141 | eff = F() |
| 142 | if eff.id() <> id(eff): |
| 143 | raise TestFailed |
| 144 | |
| 145 | try: |
| 146 | F.id.foo |
| 147 | except AttributeError: pass |
| 148 | else: raise TestFailed |
| 149 | |
| 150 | try: |
| 151 | F.id.foo = 12 |
| 152 | except TypeError: pass |
| 153 | else: raise TestFailed |
| 154 | |
| 155 | try: |
| 156 | F.id.foo |
| 157 | except AttributeError: pass |
| 158 | else: raise TestFailed |
| 159 | |
| 160 | try: |
| 161 | eff.id.foo |
| 162 | except AttributeError: pass |
| 163 | else: raise TestFailed |
| 164 | |
| 165 | try: |
| 166 | eff.id.foo = 12 |
| 167 | except TypeError: pass |
| 168 | else: raise TestFailed |
| 169 | |
| 170 | try: |
| 171 | eff.id.foo |
| 172 | except AttributeError: pass |
| 173 | else: raise TestFailed |
Barry Warsaw | 2e9b396 | 2001-01-19 19:55:12 +0000 | [diff] [blame] | 174 | |
| 175 | # Regression test for a crash in pre-2.1a1 |
| 176 | def another(): |
| 177 | pass |
| 178 | del another.__dict__ |
| 179 | del another.func_dict |
| 180 | another.func_dict = None |
| 181 | |
| 182 | try: |
| 183 | del another.bar |
| 184 | except AttributeError: pass |
| 185 | else: raise TestFailed |
| 186 | |
| 187 | # This isn't specifically related to function attributes, but it does test a |
| 188 | # core dump regression in funcobject.c |
| 189 | del another.func_defaults |
Moshe Zadka | 497671e | 2001-01-29 06:21:17 +0000 | [diff] [blame] | 190 | |
| 191 | def foo(): |
Tim Peters | 10fb386 | 2001-02-09 20:17:14 +0000 | [diff] [blame] | 192 | pass |
Moshe Zadka | 497671e | 2001-01-29 06:21:17 +0000 | [diff] [blame] | 193 | |
| 194 | def bar(): |
Tim Peters | 10fb386 | 2001-02-09 20:17:14 +0000 | [diff] [blame] | 195 | pass |
Moshe Zadka | 497671e | 2001-01-29 06:21:17 +0000 | [diff] [blame] | 196 | |
| 197 | def temp(): |
Tim Peters | 10fb386 | 2001-02-09 20:17:14 +0000 | [diff] [blame] | 198 | print 1 |
Moshe Zadka | 497671e | 2001-01-29 06:21:17 +0000 | [diff] [blame] | 199 | |
| 200 | if foo==bar: raise TestFailed |
| 201 | |
| 202 | d={} |
| 203 | d[foo] = 1 |
| 204 | |
| 205 | foo.func_code = temp.func_code |
| 206 | |
| 207 | d[foo] |