Anonymous committed 14815fc

Introduce an ugly hack allowing doctest to test multi-statement examples.

Comments (0)

Files changed (1)


         self.opt = doctest.DONT_ACCEPT_TRUE_FOR_1 | doctest.ELLIPSIS | \
+        # HACK HACK HACK
+        # doctest compiles its snippets with type 'single'. That is nice
+        # for doctest examples but unusable for multi-statement code such
+        # as setup code -- to be able to use doctest error reporting with
+        # that code nevertheless, we monkey-patch the "compile" it uses.
+        doctest.compile = self.compile
+        self.type = 'single'
         self.total_failures = 0
         self.total_tries = 0
         self.setup_failures = 0
             self.total_failures += res_f
             self.total_tries += res_t
+    def compile(self, code, name, type, flags, dont_inherit):
+        return compile(code, name, self.type, flags, dont_inherit)
     def test_group(self, group, filename):
         ns = {}
         examples = []
                                             filename, 0, None)
             setup_doctest.globs = ns
             old_f = self.setup_runner.failures
+            self.type = 'exec' # the snippet may contain multiple statements
   , out=self._out,
             if self.setup_runner.failures > old_f:
                     new_opt = code[0].options.copy()
                     example.options = new_opt
+                self.type = 'single' # ordinary doctests
                 output = code[1] and code[1].code or ''
                 options = code[1] and code[1].options or None
                 test = doctest.DocTest([example], {},,
                                        filename, code[0].lineno, None)
+                self.type = 'exec' # multiple statements again
             # DocTest.__init__ copies the globs namespace, which we don't want
             test.globs = ns
             # also don't clear the globs namespace after running the doctest
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.