code: def check_doctests_testfile(
      fname, verbose, ns=None, dots=True, doctest_warnings=False
  ):
      """
      Check code in a text file.
  
      Mimic `check_doctests` above, differing mostly in test discovery.
      (which is borrowed from stdlib's doctest.testfile here,
       https://github.com/python-git/python/blob/master/Lib/doctest.py)
  
      Parameters
      ----------
      fname : str
          File name
      verbose : bool
  
      ns : dict
          Name space
  
      dots : bool
  
      doctest_warnings : bool
  
      Returns
      -------
      list
          List of [(item_name, success_flag, output), ...]
  
      Notes
      -----
  
      refguide can be signalled to skip testing code by adding
      ``#doctest: +SKIP`` to the end of the line. If the output varies or is
      random, add ``# may vary`` or ``# random`` to the comment. for example
  
      >>> plt.plot(...)  # doctest: +SKIP
      >>> random.randint(0,10)
      5 # random
  
      We also try to weed out pseudocode:
      * We maintain a list of exceptions which signal pseudocode,
      * We split the text file into "blocks" of code separated by empty lines
        and/or intervening text.
      * If a block contains a marker, the whole block is then assumed to be
        pseudocode. It is then not being doctested.
  
      The rationale is that typically, the text looks like this:
  
      blah
      <BLANKLINE>
      >>> from numpy import some_module   # pseudocode!
      >>> func = some_module.some_function
      >>> func(42)                  # still pseudocode
      146
      <BLANKLINE>
      blah
      <BLANKLINE>
      >>> 2 + 3        # real code, doctest it
      5
  
      """
      if ns is None:
          ns = CHECK_NAMESPACE
      results = []
  
      _, short_name = os.path.split(fname)
      if short_name in DOCTEST_SKIPDICT:
          return results
  
      full_name = fname
      with open(fname, encoding="utf-8") as f:
          text = f.read()
  
      PSEUDOCODE = set(
          [
              "some_function",
              "some_module",
              "import example",
              "ctypes.CDLL",  # likely need compiling, skip it
              "integrate.nquad(func,",  # ctypes integrate tutotial
          ]
      )
  
      # split the text into "blocks" and try to detect and omit pseudocode blocks.
      parser = doctest.DocTestParser()
      good_parts = []
      base_line_no = 0
      for part in text.split("\n\n"):
          try:
              tests = parser.get_doctest(
                  part, ns, fname, fname, base_line_no
              )
          except ValueError as e:
              if e.args[0].startswith("line "):
                  # fix line number since `parser.get_doctest` does not increment
                  # the reported line number by base_line_no in the error message
                  parts = e.args[0].split()
                  parts[1] = str(int(parts[1]) + base_line_no)
                  e.args = (" ".join(parts),) + e.args[1:]
              raise
          if any(
              word in ex.source
              for word in PSEUDOCODE
              for ex in tests.examples
          ):
              # omit it
              pass
          else:
              # `part` looks like a good code, let's doctest it
              good_parts.append((part, base_line_no))
          base_line_no += part.count("\n") + 2
  
      # Reassemble the good bits and doctest them:
      tests = []
      for good_text, line_no in good_parts:
          tests.append(
              parser.get_doctest(good_text, ns, fname, fname, line_no)
          )
      success, output = _run_doctests(
          tests, full_name, verbose, doctest_warnings
      )
  
      if dots:
          output_dot("." if success else "F")
  
      results.append((full_name, success, output))
  
      if HAVE_MATPLOTLIB:
          import matplotlib.pyplot as plt
  
          plt.close("all")
  
      return results
  
imports: import copy
  import doctest
  import inspect
  import io
  import os
  import re
  import shutil
  import sys
  import tempfile
  import warnings
  import docutils.core
  from argparse import ArgumentParser
  from contextlib import contextmanager, redirect_stderr
  from doctest import NORMALIZE_WHITESPACE, ELLIPSIS, IGNORE_EXCEPTION_DETAIL
  from docutils.parsers.rst import directives
  import sphinx
  import numpy
  from numpydoc.docscrape_sphinx import get_doc_object
  from sphinx.directives.other import SeeAlso, Only
label_desc: Use image node_id 12 for calling a global function with example usage: check_doctests_testfile(fname, verbose, ns, dots, doctest_warnings) and returns: results, results