]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
add __file__ to the globals available for tests loaded via DocFileSuite;
authorFred Drake <fdrake@acm.org>
Tue, 21 Dec 2004 23:46:34 +0000 (23:46 +0000)
committerFred Drake <fdrake@acm.org>
Tue, 21 Dec 2004 23:46:34 +0000 (23:46 +0000)
this is useful for locating supporting data files, just as it is in Python
modules

Doc/lib/libdoctest.tex
Lib/doctest.py
Lib/test/test_doctest.py
Lib/test/test_doctest3.txt [new file with mode: 0644]

index b982a73e54cc93f3b656852b93c6a7491c8193ec..0f32f7dc891ecd5303fa84683045a540eb03d53b 100644 (file)
@@ -1107,6 +1107,10 @@ instances from text files and modules with doctests:
   defaults to a normal parser (i.e., \code{\class{DocTestParser}()}).
 
   \versionadded{2.4}
+
+  Starting in Python 2.5, the global \code{__file__} was added to the
+  globals provided to doctests loaded from a text file using
+  \function{DocFileSuite()}.
 \end{funcdesc}
 
 \begin{funcdesc}{DocTestSuite}{\optional{module}\optional{,
index 0a13d77586a9000d45b6d5575235bdc64097c6c7..2708fc7da069644d0446f9d5b5966aad52d27597 100644 (file)
@@ -2328,6 +2328,8 @@ def DocFileTest(path, module_relative=True, package=None,
                 globs=None, parser=DocTestParser(), **options):
     if globs is None:
         globs = {}
+    else:
+        globs = globs.copy()
 
     if package and not module_relative:
         raise ValueError("Package may only be specified for module-"
@@ -2337,6 +2339,8 @@ def DocFileTest(path, module_relative=True, package=None,
     if module_relative:
         package = _normalize_module(package)
         path = _module_relative_path(package, path)
+    if "__file__" not in globs:
+        globs["__file__"] = path
 
     # Find the file and read it.
     name = os.path.basename(path)
index 0ae6b8d180105e30a7c930d82ec52759d3b3be3d..fe4d863b97af98295514c3259c6b38b62b978ac9 100644 (file)
@@ -2010,6 +2010,14 @@ def test_DocFileSuite():
        modified the test globals.  The test globals are
        automatically cleared for us after a test.
 
+       Tests in a file run using `DocFileSuite` can also access the
+       `__file__` global, which is set to the name of the file
+       containing the tests:
+
+         >>> suite = doctest.DocFileSuite('test_doctest3.txt')
+         >>> suite.run(unittest.TestResult())
+         <unittest.TestResult run=1 errors=0 failures=0>
+
        """
 
 def test_trailing_space_in_test():
diff --git a/Lib/test/test_doctest3.txt b/Lib/test/test_doctest3.txt
new file mode 100644 (file)
index 0000000..54a96d5
--- /dev/null
@@ -0,0 +1,5 @@
+
+Here we check that `__file__` is provided:
+
+  >>> type(__file__)
+  <type 'str'>