Tim Peters | 95c99e5 | 2001-09-03 01:24:30 +0000 | [diff] [blame] | 1 | # This contains most of the executable examples from Guido's descr |
| 2 | # tutorial, once at |
| 3 | # |
| 4 | # http://www.python.org/2.2/descrintro.html |
| 5 | # |
| 6 | # A few examples left implicit in the writeup were fleshed out, a few were |
| 7 | # skipped due to lack of interest (e.g., faking super() by hand isn't |
| 8 | # of much interest anymore), and a few were fiddled to make the output |
| 9 | # deterministic. |
| 10 | |
Barry Warsaw | 04f357c | 2002-07-23 19:04:11 +0000 | [diff] [blame] | 11 | from test.test_support import sortdict |
Tim Peters | 95c99e5 | 2001-09-03 01:24:30 +0000 | [diff] [blame] | 12 | import pprint |
| 13 | |
Tim Peters | a427a2b | 2001-10-29 22:25:45 +0000 | [diff] [blame] | 14 | class defaultdict(dict): |
Tim Peters | 95c99e5 | 2001-09-03 01:24:30 +0000 | [diff] [blame] | 15 | def __init__(self, default=None): |
Tim Peters | a427a2b | 2001-10-29 22:25:45 +0000 | [diff] [blame] | 16 | dict.__init__(self) |
Tim Peters | 95c99e5 | 2001-09-03 01:24:30 +0000 | [diff] [blame] | 17 | self.default = default |
| 18 | |
| 19 | def __getitem__(self, key): |
| 20 | try: |
Tim Peters | a427a2b | 2001-10-29 22:25:45 +0000 | [diff] [blame] | 21 | return dict.__getitem__(self, key) |
Tim Peters | 95c99e5 | 2001-09-03 01:24:30 +0000 | [diff] [blame] | 22 | except KeyError: |
| 23 | return self.default |
| 24 | |
| 25 | def get(self, key, *args): |
| 26 | if not args: |
| 27 | args = (self.default,) |
Tim Peters | a427a2b | 2001-10-29 22:25:45 +0000 | [diff] [blame] | 28 | return dict.get(self, key, *args) |
Tim Peters | 95c99e5 | 2001-09-03 01:24:30 +0000 | [diff] [blame] | 29 | |
| 30 | def merge(self, other): |
| 31 | for key in other: |
| 32 | if key not in self: |
| 33 | self[key] = other[key] |
| 34 | |
| 35 | test_1 = """ |
| 36 | |
| 37 | Here's the new type at work: |
| 38 | |
| 39 | >>> print defaultdict # show our type |
Guido van Rossum | a4cb788 | 2001-09-25 03:56:29 +0000 | [diff] [blame] | 40 | <class 'test.test_descrtut.defaultdict'> |
Tim Peters | 95c99e5 | 2001-09-03 01:24:30 +0000 | [diff] [blame] | 41 | >>> print type(defaultdict) # its metatype |
| 42 | <type 'type'> |
| 43 | >>> a = defaultdict(default=0.0) # create an instance |
| 44 | >>> print a # show the instance |
| 45 | {} |
| 46 | >>> print type(a) # show its type |
Guido van Rossum | a4cb788 | 2001-09-25 03:56:29 +0000 | [diff] [blame] | 47 | <class 'test.test_descrtut.defaultdict'> |
Tim Peters | 95c99e5 | 2001-09-03 01:24:30 +0000 | [diff] [blame] | 48 | >>> print a.__class__ # show its class |
Guido van Rossum | a4cb788 | 2001-09-25 03:56:29 +0000 | [diff] [blame] | 49 | <class 'test.test_descrtut.defaultdict'> |
Tim Peters | 95c99e5 | 2001-09-03 01:24:30 +0000 | [diff] [blame] | 50 | >>> print type(a) is a.__class__ # its type is its class |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 51 | True |
Tim Peters | 95c99e5 | 2001-09-03 01:24:30 +0000 | [diff] [blame] | 52 | >>> a[1] = 3.25 # modify the instance |
| 53 | >>> print a # show the new value |
| 54 | {1: 3.25} |
| 55 | >>> print a[1] # show the new item |
| 56 | 3.25 |
| 57 | >>> print a[0] # a non-existant item |
| 58 | 0.0 |
Tim Peters | a427a2b | 2001-10-29 22:25:45 +0000 | [diff] [blame] | 59 | >>> a.merge({1:100, 2:200}) # use a dict method |
Tim Peters | 95c99e5 | 2001-09-03 01:24:30 +0000 | [diff] [blame] | 60 | >>> print sortdict(a) # show the result |
| 61 | {1: 3.25, 2: 200} |
| 62 | >>> |
| 63 | |
| 64 | We can also use the new type in contexts where classic only allows "real" |
| 65 | dictionaries, such as the locals/globals dictionaries for the exec |
| 66 | statement or the built-in function eval(): |
| 67 | |
| 68 | >>> def sorted(seq): |
| 69 | ... seq.sort() |
| 70 | ... return seq |
| 71 | >>> print sorted(a.keys()) |
| 72 | [1, 2] |
| 73 | >>> exec "x = 3; print x" in a |
| 74 | 3 |
| 75 | >>> print sorted(a.keys()) |
| 76 | [1, 2, '__builtins__', 'x'] |
| 77 | >>> print a['x'] |
| 78 | 3 |
| 79 | >>> |
| 80 | |
| 81 | However, our __getitem__() method is not used for variable access by the |
| 82 | interpreter: |
| 83 | |
| 84 | >>> exec "print foo" in a |
| 85 | Traceback (most recent call last): |
| 86 | File "<stdin>", line 1, in ? |
| 87 | File "<string>", line 1, in ? |
| 88 | NameError: name 'foo' is not defined |
| 89 | >>> |
| 90 | |
| 91 | Now I'll show that defaultdict instances have dynamic instance variables, |
| 92 | just like classic classes: |
| 93 | |
| 94 | >>> a.default = -1 |
| 95 | >>> print a["noway"] |
| 96 | -1 |
| 97 | >>> a.default = -1000 |
| 98 | >>> print a["noway"] |
| 99 | -1000 |
Tim Peters | 5d2b77c | 2001-09-03 05:47:38 +0000 | [diff] [blame] | 100 | >>> 'default' in dir(a) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 101 | True |
Tim Peters | 95c99e5 | 2001-09-03 01:24:30 +0000 | [diff] [blame] | 102 | >>> a.x1 = 100 |
| 103 | >>> a.x2 = 200 |
| 104 | >>> print a.x1 |
| 105 | 100 |
Tim Peters | 5d2b77c | 2001-09-03 05:47:38 +0000 | [diff] [blame] | 106 | >>> d = dir(a) |
| 107 | >>> 'default' in d and 'x1' in d and 'x2' in d |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 108 | True |
Tim Peters | 95c99e5 | 2001-09-03 01:24:30 +0000 | [diff] [blame] | 109 | >>> print a.__dict__ |
| 110 | {'default': -1000, 'x2': 200, 'x1': 100} |
| 111 | >>> |
| 112 | """ |
| 113 | |
Tim Peters | a427a2b | 2001-10-29 22:25:45 +0000 | [diff] [blame] | 114 | class defaultdict2(dict): |
Tim Peters | 95c99e5 | 2001-09-03 01:24:30 +0000 | [diff] [blame] | 115 | __slots__ = ['default'] |
| 116 | |
| 117 | def __init__(self, default=None): |
Tim Peters | a427a2b | 2001-10-29 22:25:45 +0000 | [diff] [blame] | 118 | dict.__init__(self) |
Tim Peters | 95c99e5 | 2001-09-03 01:24:30 +0000 | [diff] [blame] | 119 | self.default = default |
| 120 | |
| 121 | def __getitem__(self, key): |
| 122 | try: |
Tim Peters | a427a2b | 2001-10-29 22:25:45 +0000 | [diff] [blame] | 123 | return dict.__getitem__(self, key) |
Tim Peters | 95c99e5 | 2001-09-03 01:24:30 +0000 | [diff] [blame] | 124 | except KeyError: |
| 125 | return self.default |
| 126 | |
| 127 | def get(self, key, *args): |
| 128 | if not args: |
| 129 | args = (self.default,) |
Tim Peters | a427a2b | 2001-10-29 22:25:45 +0000 | [diff] [blame] | 130 | return dict.get(self, key, *args) |
Tim Peters | 95c99e5 | 2001-09-03 01:24:30 +0000 | [diff] [blame] | 131 | |
| 132 | def merge(self, other): |
| 133 | for key in other: |
| 134 | if key not in self: |
| 135 | self[key] = other[key] |
| 136 | |
| 137 | test_2 = """ |
| 138 | |
| 139 | The __slots__ declaration takes a list of instance variables, and reserves |
| 140 | space for exactly these in the instance. When __slots__ is used, other |
| 141 | instance variables cannot be assigned to: |
| 142 | |
| 143 | >>> a = defaultdict2(default=0.0) |
| 144 | >>> a[1] |
| 145 | 0.0 |
| 146 | >>> a.default = -1 |
| 147 | >>> a[1] |
| 148 | -1 |
| 149 | >>> a.x1 = 1 |
| 150 | Traceback (most recent call last): |
| 151 | File "<stdin>", line 1, in ? |
| 152 | AttributeError: 'defaultdict2' object has no attribute 'x1' |
| 153 | >>> |
| 154 | |
| 155 | """ |
| 156 | |
| 157 | test_3 = """ |
| 158 | |
| 159 | Introspecting instances of built-in types |
| 160 | |
| 161 | For instance of built-in types, x.__class__ is now the same as type(x): |
| 162 | |
| 163 | >>> type([]) |
| 164 | <type 'list'> |
| 165 | >>> [].__class__ |
| 166 | <type 'list'> |
| 167 | >>> list |
| 168 | <type 'list'> |
| 169 | >>> isinstance([], list) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 170 | True |
Tim Peters | a427a2b | 2001-10-29 22:25:45 +0000 | [diff] [blame] | 171 | >>> isinstance([], dict) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 172 | False |
Tim Peters | 95c99e5 | 2001-09-03 01:24:30 +0000 | [diff] [blame] | 173 | >>> isinstance([], object) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 174 | True |
Tim Peters | 95c99e5 | 2001-09-03 01:24:30 +0000 | [diff] [blame] | 175 | >>> |
| 176 | |
| 177 | Under the new proposal, the __methods__ attribute no longer exists: |
| 178 | |
| 179 | >>> [].__methods__ |
| 180 | Traceback (most recent call last): |
| 181 | File "<stdin>", line 1, in ? |
| 182 | AttributeError: 'list' object has no attribute '__methods__' |
| 183 | >>> |
| 184 | |
| 185 | Instead, you can get the same information from the list type: |
| 186 | |
| 187 | >>> pprint.pprint(dir(list)) # like list.__dict__.keys(), but sorted |
| 188 | ['__add__', |
| 189 | '__class__', |
| 190 | '__contains__', |
| 191 | '__delattr__', |
| 192 | '__delitem__', |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 193 | '__delslice__', |
Tim Peters | 8044055 | 2002-02-19 04:25:19 +0000 | [diff] [blame] | 194 | '__doc__', |
Tim Peters | 95c99e5 | 2001-09-03 01:24:30 +0000 | [diff] [blame] | 195 | '__eq__', |
| 196 | '__ge__', |
Guido van Rossum | 867a8d2 | 2001-09-21 19:29:08 +0000 | [diff] [blame] | 197 | '__getattribute__', |
Tim Peters | 95c99e5 | 2001-09-03 01:24:30 +0000 | [diff] [blame] | 198 | '__getitem__', |
| 199 | '__getslice__', |
| 200 | '__gt__', |
| 201 | '__hash__', |
| 202 | '__iadd__', |
| 203 | '__imul__', |
| 204 | '__init__', |
Raymond Hettinger | 14bd6de | 2002-05-31 21:40:38 +0000 | [diff] [blame] | 205 | '__iter__', |
Tim Peters | 95c99e5 | 2001-09-03 01:24:30 +0000 | [diff] [blame] | 206 | '__le__', |
| 207 | '__len__', |
| 208 | '__lt__', |
| 209 | '__mul__', |
| 210 | '__ne__', |
| 211 | '__new__', |
Guido van Rossum | 3926a63 | 2001-09-25 16:25:58 +0000 | [diff] [blame] | 212 | '__reduce__', |
Tim Peters | 95c99e5 | 2001-09-03 01:24:30 +0000 | [diff] [blame] | 213 | '__repr__', |
| 214 | '__rmul__', |
| 215 | '__setattr__', |
| 216 | '__setitem__', |
| 217 | '__setslice__', |
| 218 | '__str__', |
| 219 | 'append', |
| 220 | 'count', |
| 221 | 'extend', |
| 222 | 'index', |
| 223 | 'insert', |
| 224 | 'pop', |
| 225 | 'remove', |
| 226 | 'reverse', |
| 227 | 'sort'] |
| 228 | |
| 229 | The new introspection API gives more information than the old one: in |
| 230 | addition to the regular methods, it also shows the methods that are |
| 231 | normally invoked through special notations, e.g. __iadd__ (+=), __len__ |
| 232 | (len), __ne__ (!=). You can invoke any method from this list directly: |
| 233 | |
| 234 | >>> a = ['tic', 'tac'] |
| 235 | >>> list.__len__(a) # same as len(a) |
| 236 | 2 |
| 237 | >>> a.__len__() # ditto |
| 238 | 2 |
| 239 | >>> list.append(a, 'toe') # same as a.append('toe') |
| 240 | >>> a |
| 241 | ['tic', 'tac', 'toe'] |
| 242 | >>> |
| 243 | |
| 244 | This is just like it is for user-defined classes. |
| 245 | """ |
| 246 | |
| 247 | test_4 = """ |
| 248 | |
| 249 | Static methods and class methods |
| 250 | |
| 251 | The new introspection API makes it possible to add static methods and class |
| 252 | methods. Static methods are easy to describe: they behave pretty much like |
| 253 | static methods in C++ or Java. Here's an example: |
| 254 | |
| 255 | >>> class C: |
| 256 | ... |
| 257 | ... def foo(x, y): |
| 258 | ... print "staticmethod", x, y |
| 259 | ... foo = staticmethod(foo) |
| 260 | |
| 261 | >>> C.foo(1, 2) |
| 262 | staticmethod 1 2 |
| 263 | >>> c = C() |
| 264 | >>> c.foo(1, 2) |
| 265 | staticmethod 1 2 |
| 266 | |
| 267 | Class methods use a similar pattern to declare methods that receive an |
| 268 | implicit first argument that is the *class* for which they are invoked. |
| 269 | |
| 270 | >>> class C: |
| 271 | ... def foo(cls, y): |
| 272 | ... print "classmethod", cls, y |
| 273 | ... foo = classmethod(foo) |
| 274 | |
| 275 | >>> C.foo(1) |
Tim Peters | 90ba8d9 | 2001-09-09 01:21:31 +0000 | [diff] [blame] | 276 | classmethod test.test_descrtut.C 1 |
Tim Peters | 95c99e5 | 2001-09-03 01:24:30 +0000 | [diff] [blame] | 277 | >>> c = C() |
| 278 | >>> c.foo(1) |
Tim Peters | 90ba8d9 | 2001-09-09 01:21:31 +0000 | [diff] [blame] | 279 | classmethod test.test_descrtut.C 1 |
Tim Peters | 95c99e5 | 2001-09-03 01:24:30 +0000 | [diff] [blame] | 280 | |
| 281 | >>> class D(C): |
| 282 | ... pass |
| 283 | |
| 284 | >>> D.foo(1) |
Tim Peters | 90ba8d9 | 2001-09-09 01:21:31 +0000 | [diff] [blame] | 285 | classmethod test.test_descrtut.D 1 |
Tim Peters | 95c99e5 | 2001-09-03 01:24:30 +0000 | [diff] [blame] | 286 | >>> d = D() |
| 287 | >>> d.foo(1) |
Tim Peters | 90ba8d9 | 2001-09-09 01:21:31 +0000 | [diff] [blame] | 288 | classmethod test.test_descrtut.D 1 |
Tim Peters | 95c99e5 | 2001-09-03 01:24:30 +0000 | [diff] [blame] | 289 | |
| 290 | This prints "classmethod __main__.D 1" both times; in other words, the |
| 291 | class passed as the first argument of foo() is the class involved in the |
| 292 | call, not the class involved in the definition of foo(). |
| 293 | |
| 294 | But notice this: |
| 295 | |
| 296 | >>> class E(C): |
| 297 | ... def foo(cls, y): # override C.foo |
| 298 | ... print "E.foo() called" |
| 299 | ... C.foo(y) |
| 300 | ... foo = classmethod(foo) |
| 301 | |
| 302 | >>> E.foo(1) |
| 303 | E.foo() called |
Tim Peters | 90ba8d9 | 2001-09-09 01:21:31 +0000 | [diff] [blame] | 304 | classmethod test.test_descrtut.C 1 |
Tim Peters | 95c99e5 | 2001-09-03 01:24:30 +0000 | [diff] [blame] | 305 | >>> e = E() |
| 306 | >>> e.foo(1) |
| 307 | E.foo() called |
Tim Peters | 90ba8d9 | 2001-09-09 01:21:31 +0000 | [diff] [blame] | 308 | classmethod test.test_descrtut.C 1 |
Tim Peters | 95c99e5 | 2001-09-03 01:24:30 +0000 | [diff] [blame] | 309 | |
| 310 | In this example, the call to C.foo() from E.foo() will see class C as its |
| 311 | first argument, not class E. This is to be expected, since the call |
| 312 | specifies the class C. But it stresses the difference between these class |
| 313 | methods and methods defined in metaclasses (where an upcall to a metamethod |
| 314 | would pass the target class as an explicit first argument). |
| 315 | """ |
| 316 | |
| 317 | test_5 = """ |
| 318 | |
| 319 | Attributes defined by get/set methods |
| 320 | |
| 321 | |
Guido van Rossum | 8bce4ac | 2001-09-06 21:56:42 +0000 | [diff] [blame] | 322 | >>> class property(object): |
Tim Peters | 95c99e5 | 2001-09-03 01:24:30 +0000 | [diff] [blame] | 323 | ... |
| 324 | ... def __init__(self, get, set=None): |
| 325 | ... self.__get = get |
| 326 | ... self.__set = set |
| 327 | ... |
| 328 | ... def __get__(self, inst, type=None): |
| 329 | ... return self.__get(inst) |
| 330 | ... |
| 331 | ... def __set__(self, inst, value): |
| 332 | ... if self.__set is None: |
| 333 | ... raise AttributeError, "this attribute is read-only" |
| 334 | ... return self.__set(inst, value) |
| 335 | |
| 336 | Now let's define a class with an attribute x defined by a pair of methods, |
| 337 | getx() and and setx(): |
| 338 | |
| 339 | >>> class C(object): |
| 340 | ... |
| 341 | ... def __init__(self): |
| 342 | ... self.__x = 0 |
| 343 | ... |
| 344 | ... def getx(self): |
| 345 | ... return self.__x |
| 346 | ... |
| 347 | ... def setx(self, x): |
| 348 | ... if x < 0: x = 0 |
| 349 | ... self.__x = x |
| 350 | ... |
Guido van Rossum | 8bce4ac | 2001-09-06 21:56:42 +0000 | [diff] [blame] | 351 | ... x = property(getx, setx) |
Tim Peters | 95c99e5 | 2001-09-03 01:24:30 +0000 | [diff] [blame] | 352 | |
| 353 | Here's a small demonstration: |
| 354 | |
| 355 | >>> a = C() |
| 356 | >>> a.x = 10 |
| 357 | >>> print a.x |
| 358 | 10 |
| 359 | >>> a.x = -10 |
| 360 | >>> print a.x |
| 361 | 0 |
| 362 | >>> |
| 363 | |
Guido van Rossum | 8bce4ac | 2001-09-06 21:56:42 +0000 | [diff] [blame] | 364 | Hmm -- property is builtin now, so let's try it that way too. |
Tim Peters | 95c99e5 | 2001-09-03 01:24:30 +0000 | [diff] [blame] | 365 | |
Guido van Rossum | 8bce4ac | 2001-09-06 21:56:42 +0000 | [diff] [blame] | 366 | >>> del property # unmask the builtin |
| 367 | >>> property |
| 368 | <type 'property'> |
Tim Peters | 95c99e5 | 2001-09-03 01:24:30 +0000 | [diff] [blame] | 369 | |
| 370 | >>> class C(object): |
| 371 | ... def __init__(self): |
| 372 | ... self.__x = 0 |
| 373 | ... def getx(self): |
| 374 | ... return self.__x |
| 375 | ... def setx(self, x): |
| 376 | ... if x < 0: x = 0 |
| 377 | ... self.__x = x |
Guido van Rossum | 8bce4ac | 2001-09-06 21:56:42 +0000 | [diff] [blame] | 378 | ... x = property(getx, setx) |
Tim Peters | 95c99e5 | 2001-09-03 01:24:30 +0000 | [diff] [blame] | 379 | |
| 380 | |
| 381 | >>> a = C() |
| 382 | >>> a.x = 10 |
| 383 | >>> print a.x |
| 384 | 10 |
| 385 | >>> a.x = -10 |
| 386 | >>> print a.x |
| 387 | 0 |
| 388 | >>> |
| 389 | """ |
| 390 | |
| 391 | test_6 = """ |
| 392 | |
| 393 | Method resolution order |
| 394 | |
| 395 | This example is implicit in the writeup. |
| 396 | |
| 397 | >>> class A: # classic class |
| 398 | ... def save(self): |
| 399 | ... print "called A.save()" |
| 400 | >>> class B(A): |
| 401 | ... pass |
| 402 | >>> class C(A): |
| 403 | ... def save(self): |
| 404 | ... print "called C.save()" |
| 405 | >>> class D(B, C): |
| 406 | ... pass |
| 407 | |
| 408 | >>> D().save() |
| 409 | called A.save() |
| 410 | |
| 411 | >>> class A(object): # new class |
| 412 | ... def save(self): |
| 413 | ... print "called A.save()" |
| 414 | >>> class B(A): |
| 415 | ... pass |
| 416 | >>> class C(A): |
| 417 | ... def save(self): |
| 418 | ... print "called C.save()" |
| 419 | >>> class D(B, C): |
| 420 | ... pass |
| 421 | |
| 422 | >>> D().save() |
| 423 | called C.save() |
| 424 | """ |
| 425 | |
| 426 | class A(object): |
| 427 | def m(self): |
| 428 | return "A" |
| 429 | |
| 430 | class B(A): |
| 431 | def m(self): |
| 432 | return "B" + super(B, self).m() |
| 433 | |
| 434 | class C(A): |
| 435 | def m(self): |
| 436 | return "C" + super(C, self).m() |
| 437 | |
| 438 | class D(C, B): |
| 439 | def m(self): |
| 440 | return "D" + super(D, self).m() |
| 441 | |
| 442 | |
| 443 | test_7 = """ |
| 444 | |
| 445 | Cooperative methods and "super" |
| 446 | |
| 447 | >>> print D().m() # "DCBA" |
| 448 | DCBA |
| 449 | """ |
| 450 | |
| 451 | test_8 = """ |
| 452 | |
| 453 | Backwards incompatibilities |
| 454 | |
| 455 | >>> class A: |
| 456 | ... def foo(self): |
| 457 | ... print "called A.foo()" |
| 458 | |
| 459 | >>> class B(A): |
| 460 | ... pass |
| 461 | |
| 462 | >>> class C(A): |
| 463 | ... def foo(self): |
| 464 | ... B.foo(self) |
| 465 | |
| 466 | >>> C().foo() |
| 467 | Traceback (most recent call last): |
| 468 | ... |
| 469 | TypeError: unbound method foo() must be called with B instance as first argument (got C instance instead) |
| 470 | |
| 471 | >>> class C(A): |
| 472 | ... def foo(self): |
| 473 | ... A.foo(self) |
| 474 | >>> C().foo() |
| 475 | called A.foo() |
| 476 | """ |
| 477 | |
| 478 | __test__ = {"tut1": test_1, |
| 479 | "tut2": test_2, |
| 480 | "tut3": test_3, |
| 481 | "tut4": test_4, |
| 482 | "tut5": test_5, |
| 483 | "tut6": test_6, |
| 484 | "tut7": test_7, |
| 485 | "tut8": test_8} |
| 486 | |
| 487 | # Magic test name that regrtest.py invokes *after* importing this module. |
| 488 | # This worms around a bootstrap problem. |
| 489 | # Note that doctest and regrtest both look in sys.argv for a "-v" argument, |
| 490 | # so this works as expected in both ways of running regrtest. |
Tim Peters | a0a6222 | 2001-09-09 06:12:01 +0000 | [diff] [blame] | 491 | def test_main(verbose=None): |
| 492 | # Obscure: import this module as test.test_descrtut instead of as |
| 493 | # plain test_descrtut because the name of this module works its way |
| 494 | # into the doctest examples, and unless the full test.test_descrtut |
| 495 | # business is used the name can change depending on how the test is |
| 496 | # invoked. |
Barry Warsaw | 04f357c | 2002-07-23 19:04:11 +0000 | [diff] [blame] | 497 | from test import test_support, test_descrtut |
| 498 | test_support.run_doctest(test_descrtut, verbose) |
Tim Peters | 95c99e5 | 2001-09-03 01:24:30 +0000 | [diff] [blame] | 499 | |
| 500 | # This part isn't needed for regrtest, but for running the test directly. |
| 501 | if __name__ == "__main__": |
Tim Peters | a0a6222 | 2001-09-09 06:12:01 +0000 | [diff] [blame] | 502 | test_main(1) |