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