Antoine Pitrou | 1d80a56 | 2018-04-07 18:14:03 +0200 | [diff] [blame] | 1 | """Test module for the custom examples |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 2 | |
Antoine Pitrou | 1d80a56 | 2018-04-07 18:14:03 +0200 | [diff] [blame] | 3 | Custom 1: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 4 | |
Antoine Pitrou | 1d80a56 | 2018-04-07 18:14:03 +0200 | [diff] [blame] | 5 | >>> import custom |
| 6 | >>> c1 = custom.Custom() |
| 7 | >>> c2 = custom.Custom() |
| 8 | >>> del c1 |
| 9 | >>> del c2 |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 10 | |
| 11 | |
Antoine Pitrou | 1d80a56 | 2018-04-07 18:14:03 +0200 | [diff] [blame] | 12 | Custom 2 |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 13 | |
Antoine Pitrou | 1d80a56 | 2018-04-07 18:14:03 +0200 | [diff] [blame] | 14 | >>> import custom2 |
| 15 | >>> c1 = custom2.Custom('jim', 'fulton', 42) |
| 16 | >>> c1.first |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 17 | 'jim' |
Antoine Pitrou | 1d80a56 | 2018-04-07 18:14:03 +0200 | [diff] [blame] | 18 | >>> c1.last |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 19 | 'fulton' |
Antoine Pitrou | 1d80a56 | 2018-04-07 18:14:03 +0200 | [diff] [blame] | 20 | >>> c1.number |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 21 | 42 |
Antoine Pitrou | 1d80a56 | 2018-04-07 18:14:03 +0200 | [diff] [blame] | 22 | >>> c1.name() |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 23 | 'jim fulton' |
Antoine Pitrou | 1d80a56 | 2018-04-07 18:14:03 +0200 | [diff] [blame] | 24 | >>> c1.first = 'will' |
| 25 | >>> c1.name() |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 26 | 'will fulton' |
Antoine Pitrou | 1d80a56 | 2018-04-07 18:14:03 +0200 | [diff] [blame] | 27 | >>> c1.last = 'tell' |
| 28 | >>> c1.name() |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 29 | 'will tell' |
Antoine Pitrou | 1d80a56 | 2018-04-07 18:14:03 +0200 | [diff] [blame] | 30 | >>> del c1.first |
| 31 | >>> c1.name() |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 32 | Traceback (most recent call last): |
| 33 | ... |
| 34 | AttributeError: first |
Antoine Pitrou | 1d80a56 | 2018-04-07 18:14:03 +0200 | [diff] [blame] | 35 | >>> c1.first |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 36 | Traceback (most recent call last): |
| 37 | ... |
| 38 | AttributeError: first |
Antoine Pitrou | 1d80a56 | 2018-04-07 18:14:03 +0200 | [diff] [blame] | 39 | >>> c1.first = 'drew' |
| 40 | >>> c1.first |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 41 | 'drew' |
Antoine Pitrou | 1d80a56 | 2018-04-07 18:14:03 +0200 | [diff] [blame] | 42 | >>> del c1.number |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 43 | Traceback (most recent call last): |
| 44 | ... |
| 45 | TypeError: can't delete numeric/char attribute |
Antoine Pitrou | 1d80a56 | 2018-04-07 18:14:03 +0200 | [diff] [blame] | 46 | >>> c1.number=2 |
| 47 | >>> c1.number |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 48 | 2 |
Antoine Pitrou | 1d80a56 | 2018-04-07 18:14:03 +0200 | [diff] [blame] | 49 | >>> c1.first = 42 |
| 50 | >>> c1.name() |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 51 | '42 tell' |
Antoine Pitrou | 1d80a56 | 2018-04-07 18:14:03 +0200 | [diff] [blame] | 52 | >>> c2 = custom2.Custom() |
| 53 | >>> c2.name() |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 54 | ' ' |
Antoine Pitrou | 1d80a56 | 2018-04-07 18:14:03 +0200 | [diff] [blame] | 55 | >>> c2.first |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 56 | '' |
Antoine Pitrou | 1d80a56 | 2018-04-07 18:14:03 +0200 | [diff] [blame] | 57 | >>> c2.last |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 58 | '' |
Antoine Pitrou | 1d80a56 | 2018-04-07 18:14:03 +0200 | [diff] [blame] | 59 | >>> del c2.first |
| 60 | >>> c2.first |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 61 | Traceback (most recent call last): |
| 62 | ... |
| 63 | AttributeError: first |
Antoine Pitrou | 1d80a56 | 2018-04-07 18:14:03 +0200 | [diff] [blame] | 64 | >>> c2.first |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 65 | Traceback (most recent call last): |
| 66 | ... |
| 67 | AttributeError: first |
Antoine Pitrou | 1d80a56 | 2018-04-07 18:14:03 +0200 | [diff] [blame] | 68 | >>> c2.name() |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 69 | Traceback (most recent call last): |
| 70 | File "<stdin>", line 1, in ? |
| 71 | AttributeError: first |
Antoine Pitrou | 1d80a56 | 2018-04-07 18:14:03 +0200 | [diff] [blame] | 72 | >>> c2.number |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 73 | 0 |
Antoine Pitrou | 1d80a56 | 2018-04-07 18:14:03 +0200 | [diff] [blame] | 74 | >>> n3 = custom2.Custom('jim', 'fulton', 'waaa') |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 75 | Traceback (most recent call last): |
| 76 | File "<stdin>", line 1, in ? |
Antoine Pitrou | 1d80a56 | 2018-04-07 18:14:03 +0200 | [diff] [blame] | 77 | TypeError: an integer is required (got type str) |
| 78 | >>> del c1 |
| 79 | >>> del c2 |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 80 | |
| 81 | |
Antoine Pitrou | 1d80a56 | 2018-04-07 18:14:03 +0200 | [diff] [blame] | 82 | Custom 3 |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 83 | |
Antoine Pitrou | 1d80a56 | 2018-04-07 18:14:03 +0200 | [diff] [blame] | 84 | >>> import custom3 |
| 85 | >>> c1 = custom3.Custom('jim', 'fulton', 42) |
| 86 | >>> c1 = custom3.Custom('jim', 'fulton', 42) |
| 87 | >>> c1.name() |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 88 | 'jim fulton' |
Antoine Pitrou | 1d80a56 | 2018-04-07 18:14:03 +0200 | [diff] [blame] | 89 | >>> del c1.first |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 90 | Traceback (most recent call last): |
| 91 | File "<stdin>", line 1, in ? |
| 92 | TypeError: Cannot delete the first attribute |
Antoine Pitrou | 1d80a56 | 2018-04-07 18:14:03 +0200 | [diff] [blame] | 93 | >>> c1.first = 42 |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 94 | Traceback (most recent call last): |
| 95 | File "<stdin>", line 1, in ? |
| 96 | TypeError: The first attribute value must be a string |
Antoine Pitrou | 1d80a56 | 2018-04-07 18:14:03 +0200 | [diff] [blame] | 97 | >>> c1.first = 'will' |
| 98 | >>> c1.name() |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 99 | 'will fulton' |
Antoine Pitrou | 1d80a56 | 2018-04-07 18:14:03 +0200 | [diff] [blame] | 100 | >>> c2 = custom3.Custom() |
| 101 | >>> c2 = custom3.Custom() |
| 102 | >>> c2 = custom3.Custom() |
| 103 | >>> n3 = custom3.Custom('jim', 'fulton', 'waaa') |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 104 | Traceback (most recent call last): |
| 105 | File "<stdin>", line 1, in ? |
Antoine Pitrou | 1d80a56 | 2018-04-07 18:14:03 +0200 | [diff] [blame] | 106 | TypeError: an integer is required (got type str) |
| 107 | >>> del c1 |
| 108 | >>> del c2 |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 109 | |
Antoine Pitrou | 1d80a56 | 2018-04-07 18:14:03 +0200 | [diff] [blame] | 110 | Custom 4 |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 111 | |
Antoine Pitrou | 1d80a56 | 2018-04-07 18:14:03 +0200 | [diff] [blame] | 112 | >>> import custom4 |
| 113 | >>> c1 = custom4.Custom('jim', 'fulton', 42) |
| 114 | >>> c1.first |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 115 | 'jim' |
Antoine Pitrou | 1d80a56 | 2018-04-07 18:14:03 +0200 | [diff] [blame] | 116 | >>> c1.last |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 117 | 'fulton' |
Antoine Pitrou | 1d80a56 | 2018-04-07 18:14:03 +0200 | [diff] [blame] | 118 | >>> c1.number |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 119 | 42 |
Antoine Pitrou | 1d80a56 | 2018-04-07 18:14:03 +0200 | [diff] [blame] | 120 | >>> c1.name() |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 121 | 'jim fulton' |
Antoine Pitrou | 1d80a56 | 2018-04-07 18:14:03 +0200 | [diff] [blame] | 122 | >>> c1.first = 'will' |
| 123 | >>> c1.name() |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 124 | 'will fulton' |
Antoine Pitrou | 1d80a56 | 2018-04-07 18:14:03 +0200 | [diff] [blame] | 125 | >>> c1.last = 'tell' |
| 126 | >>> c1.name() |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 127 | 'will tell' |
Antoine Pitrou | 1d80a56 | 2018-04-07 18:14:03 +0200 | [diff] [blame] | 128 | >>> del c1.first |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 129 | Traceback (most recent call last): |
| 130 | ... |
Antoine Pitrou | 1d80a56 | 2018-04-07 18:14:03 +0200 | [diff] [blame] | 131 | TypeError: Cannot delete the first attribute |
| 132 | >>> c1.name() |
| 133 | 'will tell' |
| 134 | >>> c1.first = 'drew' |
| 135 | >>> c1.first |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 136 | 'drew' |
Antoine Pitrou | 1d80a56 | 2018-04-07 18:14:03 +0200 | [diff] [blame] | 137 | >>> del c1.number |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 138 | Traceback (most recent call last): |
| 139 | ... |
| 140 | TypeError: can't delete numeric/char attribute |
Antoine Pitrou | 1d80a56 | 2018-04-07 18:14:03 +0200 | [diff] [blame] | 141 | >>> c1.number=2 |
| 142 | >>> c1.number |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 143 | 2 |
Antoine Pitrou | 1d80a56 | 2018-04-07 18:14:03 +0200 | [diff] [blame] | 144 | >>> c1.first = 42 |
| 145 | Traceback (most recent call last): |
| 146 | ... |
| 147 | TypeError: The first attribute value must be a string |
| 148 | >>> c1.name() |
| 149 | 'drew tell' |
| 150 | >>> c2 = custom4.Custom() |
| 151 | >>> c2 = custom4.Custom() |
| 152 | >>> c2 = custom4.Custom() |
| 153 | >>> c2 = custom4.Custom() |
| 154 | >>> c2.name() |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 155 | ' ' |
Antoine Pitrou | 1d80a56 | 2018-04-07 18:14:03 +0200 | [diff] [blame] | 156 | >>> c2.first |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 157 | '' |
Antoine Pitrou | 1d80a56 | 2018-04-07 18:14:03 +0200 | [diff] [blame] | 158 | >>> c2.last |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 159 | '' |
Antoine Pitrou | 1d80a56 | 2018-04-07 18:14:03 +0200 | [diff] [blame] | 160 | >>> c2.number |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 161 | 0 |
Antoine Pitrou | 1d80a56 | 2018-04-07 18:14:03 +0200 | [diff] [blame] | 162 | >>> n3 = custom4.Custom('jim', 'fulton', 'waaa') |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 163 | Traceback (most recent call last): |
Antoine Pitrou | 1d80a56 | 2018-04-07 18:14:03 +0200 | [diff] [blame] | 164 | ... |
| 165 | TypeError: an integer is required (got type str) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 166 | |
| 167 | |
| 168 | Test cyclic gc(?) |
| 169 | |
| 170 | >>> import gc |
| 171 | >>> gc.disable() |
| 172 | |
Antoine Pitrou | 1d80a56 | 2018-04-07 18:14:03 +0200 | [diff] [blame] | 173 | >>> class Subclass(custom4.Custom): pass |
| 174 | ... |
| 175 | >>> s = Subclass() |
| 176 | >>> s.cycle = [s] |
| 177 | >>> s.cycle.append(s.cycle) |
| 178 | >>> x = object() |
| 179 | >>> s.x = x |
| 180 | >>> del s |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 181 | >>> sys.getrefcount(x) |
| 182 | 3 |
| 183 | >>> ignore = gc.collect() |
| 184 | >>> sys.getrefcount(x) |
| 185 | 2 |
| 186 | |
| 187 | >>> gc.enable() |
| 188 | """ |
| 189 | |
| 190 | import os |
| 191 | import sys |
| 192 | from distutils.util import get_platform |
Serhiy Storchaka | 885bdc4 | 2016-02-11 13:10:36 +0200 | [diff] [blame] | 193 | PLAT_SPEC = "%s-%d.%d" % (get_platform(), *sys.version_info[:2]) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 194 | src = os.path.join("build", "lib.%s" % PLAT_SPEC) |
| 195 | sys.path.append(src) |
| 196 | |
| 197 | if __name__ == "__main__": |
| 198 | import doctest, __main__ |
| 199 | doctest.testmod(__main__) |