[Zope3-checkins] SVN: zope.testing/branches/regebro-py3-3rdtimelucky/src/zope/testing/doctest/__init__.py Support for Python 2.4.

Lennart Regebro regebro at gmail.com
Sun Apr 25 13:48:05 EDT 2010


Log message for revision 111409:
  Support for Python 2.4.
  

Changed:
  U   zope.testing/branches/regebro-py3-3rdtimelucky/src/zope/testing/doctest/__init__.py

-=-
Modified: zope.testing/branches/regebro-py3-3rdtimelucky/src/zope/testing/doctest/__init__.py
===================================================================
--- zope.testing/branches/regebro-py3-3rdtimelucky/src/zope/testing/doctest/__init__.py	2010-04-25 17:28:55 UTC (rev 111408)
+++ zope.testing/branches/regebro-py3-3rdtimelucky/src/zope/testing/doctest/__init__.py	2010-04-25 17:48:04 UTC (rev 111409)
@@ -5,7 +5,6 @@
     'DONT_ACCEPT_BLANKLINE',
     'NORMALIZE_WHITESPACE',
     'ELLIPSIS',
-    'SKIP',
     'IGNORE_EXCEPTION_DETAIL',
     'COMPARISON_FLAGS',
     'REPORT_UDIFF',
@@ -44,6 +43,9 @@
     'debug',
 ]
 
+import sys
+if sys.version > '2.5':
+    __all__.append('SKIP')
 # Tell people to use the builtin module instead.
 import warnings
 warnings.warn('zope.testing.doctest is deprecated in favour of '
@@ -53,6 +55,7 @@
 
 # Patch to fix an error that makes subsequent tests fail after you have
 # returned unicode in a test.
+
 import doctest
 
 _org_SpoofOut = doctest._SpoofOut
@@ -68,21 +71,95 @@
 # Patch to fix tests that has mixed line endings:
 import os
 
-def _patched_load_testfile(filename, package, module_relative):
-    if module_relative:
-        package = doctest._normalize_module(package, 3)
-        filename = doctest._module_relative_path(package, filename)
-        if hasattr(package, '__loader__'):
-            if hasattr(package.__loader__, 'get_data'):
-                file_contents = package.__loader__.get_data(filename)
-                # get_data() opens files as 'rb', so one must do the equivalent
-                # conversion as universal newlines would do.
-                return file_contents.replace(os.linesep, '\n'), filename
-    return open(filename, 'U').read(), filename
+if sys.version < '2.5':
+    from doctest import DocTestParser, master
+    def _patched_testfile(filename, module_relative=True, name=None, package=None,
+                 globs=None, verbose=None, report=True, optionflags=0,
+                 extraglobs=None, raise_on_error=False, parser=DocTestParser()):
+        global master
+    
+        if package and not module_relative:
+            raise ValueError("Package may only be specified for module-"
+                             "relative paths.")
+    
+        # Relativize the path
+        if module_relative:
+            package = _normalize_module(package)
+            filename = _module_relative_path(package, filename)
+    
+        # If no name was given, then use the file's name.
+        if name is None:
+            name = os.path.basename(filename)
+    
+        # Assemble the globals.
+        if globs is None:
+            globs = {}
+        else:
+            globs = globs.copy()
+        if extraglobs is not None:
+            globs.update(extraglobs)
+    
+        if raise_on_error:
+            runner = DebugRunner(verbose=verbose, optionflags=optionflags)
+        else:
+            runner = DocTestRunner(verbose=verbose, optionflags=optionflags)
+    
+        # Read the file, convert it to a test, and run it.
+        s = open(filename, 'U').read()
+        test = parser.get_doctest(s, globs, name, filename, 0)
+        runner.run(test)
+    
+        if report:
+            runner.summarize()
+    
+        if master is None:
+            master = runner
+        else:
+            master.merge(runner)
+    
+        return runner.failures, runner.tries
+    doctest.testfile = _patched_testfile
+    
+    from doctest import _normalize_module, _module_relative_path, DocFileCase
+    def _patched_DocFileTest(path, module_relative=True, package=None,
+                    globs=None, parser=DocTestParser(), **options):
+        if globs is None:
+            globs = {}
+    
+        if package and not module_relative:
+            raise ValueError("Package may only be specified for module-"
+                             "relative paths.")
+    
+        # Relativize the path.
+        if module_relative:
+            package = _normalize_module(package)
+            path = _module_relative_path(package, path)
+    
+        # Find the file and read it.
+        name = os.path.basename(path)
+        doc = open(path, 'U').read()
+    
+        # Convert it to a test, and wrap it in a DocFileCase.
+        test = parser.get_doctest(doc, globs, name, path, 0)
+        return DocFileCase(test, **options)    
+    doctest.DocFileTest = _patched_DocFileTest
+else:
+    
+    def _patched_load_testfile(filename, package, module_relative):
+        if module_relative:
+            package = doctest._normalize_module(package, 3)
+            filename = doctest._module_relative_path(package, filename)
+            if hasattr(package, '__loader__'):
+                if hasattr(package.__loader__, 'get_data'):
+                    file_contents = package.__loader__.get_data(filename)
+                    # get_data() opens files as 'rb', so one must do the equivalent
+                    # conversion as universal newlines would do.
+                    return file_contents.replace(os.linesep, '\n'), filename
+        return open(filename, 'U').read(), filename
+    
+    doctest._load_testfile = _patched_load_testfile
 
-doctest._load_testfile = _patched_load_testfile
 
-
 # Use a special exception for the test runner:
 from zope.testing.exceptions import DocTestFailureException
 doctest.DocTestCase.failureException = DocTestFailureException
@@ -148,5 +225,4 @@
         raise self.failureException(self.format_failure(new.getvalue()))
     
 doctest.DocTestCase.runTest = _patched_runTest
-    
 from doctest import *



More information about the Zope3-Checkins mailing list