Run the examples in `test`, and display the results using the writer function `out`. The examples are run in the namespace `test.globs`. If `clear_globs` is true (the default), then this namespace will be cleared after the test runs, to help with garb
(self, test, compileflags=None, out=None, clear_globs=True)
| 1459 | return self.save_linecache_getlines(filename, module_globals) |
| 1460 | |
| 1461 | def run(self, test, compileflags=None, out=None, clear_globs=True): |
| 1462 | """ |
| 1463 | Run the examples in `test`, and display the results using the |
| 1464 | writer function `out`. |
| 1465 | |
| 1466 | The examples are run in the namespace `test.globs`. If |
| 1467 | `clear_globs` is true (the default), then this namespace will |
| 1468 | be cleared after the test runs, to help with garbage |
| 1469 | collection. If you would like to examine the namespace after |
| 1470 | the test completes, then use `clear_globs=False`. |
| 1471 | |
| 1472 | `compileflags` gives the set of flags that should be used by |
| 1473 | the Python compiler when running the examples. If not |
| 1474 | specified, then it will default to the set of future-import |
| 1475 | flags that apply to `globs`. |
| 1476 | |
| 1477 | The output of each example is checked using |
| 1478 | `DocTestRunner.check_output`, and the results are formatted by |
| 1479 | the `DocTestRunner.report_*` methods. |
| 1480 | """ |
| 1481 | self.test = test |
| 1482 | |
| 1483 | if compileflags is None: |
| 1484 | compileflags = _extract_future_flags(test.globs) |
| 1485 | |
| 1486 | save_stdout = sys.stdout |
| 1487 | if out is None: |
| 1488 | encoding = save_stdout.encoding |
| 1489 | if encoding is None or encoding.lower() == 'utf-8': |
| 1490 | out = save_stdout.write |
| 1491 | else: |
| 1492 | # Use backslashreplace error handling on write |
| 1493 | def out(s): |
| 1494 | s = str(s.encode(encoding, 'backslashreplace'), encoding) |
| 1495 | save_stdout.write(s) |
| 1496 | sys.stdout = self._fakeout |
| 1497 | |
| 1498 | # Patch pdb.set_trace to restore sys.stdout during interactive |
| 1499 | # debugging (so it's not still redirected to self._fakeout). |
| 1500 | # Note that the interactive output will go to *our* |
| 1501 | # save_stdout, even if that's not the real sys.stdout; this |
| 1502 | # allows us to write test cases for the set_trace behavior. |
| 1503 | save_trace = sys.gettrace() |
| 1504 | save_set_trace = pdb.set_trace |
| 1505 | self.debugger = _OutputRedirectingPdb(save_stdout) |
| 1506 | self.debugger.reset() |
| 1507 | pdb.set_trace = self.debugger.set_trace |
| 1508 | |
| 1509 | # Patch linecache.getlines, so we can see the example's source |
| 1510 | # when we're inside the debugger. |
| 1511 | self.save_linecache_getlines = linecache.getlines |
| 1512 | linecache.getlines = self.__patched_linecache_getlines |
| 1513 | |
| 1514 | # Make sure sys.displayhook just prints the value to stdout |
| 1515 | save_displayhook = sys.displayhook |
| 1516 | sys.displayhook = sys.__displayhook__ |
| 1517 | |
| 1518 | try: |
no test coverage detected