]> granicus.if.org Git - python/commitdiff
In DocFileTest:
authorEdward Loper <edloper@gradient.cis.upenn.edu>
Sat, 18 Sep 2004 20:27:04 +0000 (20:27 +0000)
committerEdward Loper <edloper@gradient.cis.upenn.edu>
Sat, 18 Sep 2004 20:27:04 +0000 (20:27 +0000)
  - Fixed bug in handling of absolute paths.
  - If run from an interactive session, make paths relative to the
    directory containing sys.argv[0] (since __main__ doesn't have
    a __file__ attribute).

Lib/doctest.py
Lib/test/test_doctest.py

index c1a87b38811c3dbf56a1150fe02de5945ab34ffd..3414f4abce41783dd1968c14ca9be877e22ad8b3 100644 (file)
@@ -2312,10 +2312,27 @@ class DocFileCase(DocTestCase):
                 )
 
 def DocFileTest(path, package=None, globs=None, **options):
-    package = _normalize_module(package)
     name = path.split('/')[-1]
-    dir = os.path.split(package.__file__)[0]
-    path = os.path.join(dir, *(path.split('/')))
+
+    # Interpret relative paths as relative to the given package's
+    # directory (or the current module, if no package is specified).
+    if not os.path.isabs(path):
+        package = _normalize_module(package)
+        if hasattr(package, '__file__'):
+            # A normal package/module.
+            dir = os.path.split(package.__file__)[0]
+            path = os.path.join(dir, *(path.split('/')))
+        elif package.__name__ == '__main__':
+            # An interactive session.
+            if sys.argv[0] != '':
+                dir = os.path.split(sys.argv[0])[0]
+                path = os.path.join(dir, *(path.split('/')))
+        else:
+            # A module w/o __file__ (this includes builtins)
+            raise ValueError("Can't resolve paths relative to " +
+                             "the module %s (it has" % package +
+                             "no __file__)")
+
     doc = open(path).read()
 
     if globs is None:
index 77b9ef541e477cd79a5746232e14f6dcf25960b5..8c96b2122305f01a3960493c6dd16d3e00e751af 100644 (file)
@@ -1812,14 +1812,37 @@ def test_DocFileSuite():
          >>> suite.run(unittest.TestResult())
          <unittest.TestResult run=2 errors=0 failures=2>
 
-       Note that '/' should be used as a path separator.  It will be
-       converted to a native separator at run time:
-
+       '/' should be used as a path separator.  It will be converted
+       to a native separator at run time:
 
          >>> suite = doctest.DocFileSuite('../test/test_doctest.txt')
          >>> suite.run(unittest.TestResult())
          <unittest.TestResult run=1 errors=0 failures=1>
 
+       If DocFileSuite is used from an interactive session, then files
+       are resolved relative to the directory of sys.argv[0]:
+
+         >>> import new, os.path, test.test_doctest
+         >>> save_argv = sys.argv
+         >>> sys.argv = [test.test_doctest.__file__]
+         >>> suite = doctest.DocFileSuite('test_doctest.txt',
+         ...                              package=new.module('__main__'))
+         >>> sys.argv = save_argv
+
+       Absolute paths may also be used; they should use the native
+       path separator (*not* '/').
+
+         >>> # Get the absolute path of the test package.
+         >>> test_doctest_path = os.path.abspath(test.test_doctest.__file__)
+         >>> test_pkg_path = os.path.split(test_doctest_path)[0]
+
+         >>> # Use it to find the absolute path of test_doctest.txt.
+         >>> test_file = os.path.join(test_pkg_path, 'test_doctest.txt')
+
+         >>> suite = doctest.DocFileSuite(test_file)
+         >>> suite.run(unittest.TestResult())
+         <unittest.TestResult run=1 errors=0 failures=1>
+
        You can specify initial global variables:
 
          >>> suite = doctest.DocFileSuite('test_doctest.txt',