[Zope3-checkins] SVN: zope.testing/branches/doctest-encoding/src/zope/testing/doctest.py Merged encoding api patch from Python svn.

Jim Fulton jim at zope.com
Tue Nov 15 11:07:00 EST 2005


Log message for revision 40131:
  Merged encoding api patch from Python svn.
  

Changed:
  U   zope.testing/branches/doctest-encoding/src/zope/testing/doctest.py

-=-
Modified: zope.testing/branches/doctest-encoding/src/zope/testing/doctest.py
===================================================================
--- zope.testing/branches/doctest-encoding/src/zope/testing/doctest.py	2005-11-15 15:54:10 UTC (rev 40130)
+++ zope.testing/branches/doctest-encoding/src/zope/testing/doctest.py	2005-11-15 16:06:59 UTC (rev 40131)
@@ -1866,7 +1866,8 @@
 
 def 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()):
+             extraglobs=None, raise_on_error=False, parser=DocTestParser(),
+             encoding=None):
     """
     Test examples in the given file.  Return (#failures, #tests).
 
@@ -1931,6 +1932,9 @@
     Optional keyword arg "parser" specifies a DocTestParser (or
     subclass) that should be used to extract tests from the files.
 
+    Optional keyword arg "encoding" specifies an encoding that should
+    be used to convert the file to unicode.
+
     Advanced tomfoolery:  testmod runs methods of a local instance of
     class doctest.Tester, then merges the results into (or creates)
     global Tester instance doctest.master.  Methods of doctest.master
@@ -1969,6 +1973,8 @@
 
     # Read the file, convert it to a test, and run it.
     s = open(filename).read()
+    if encoding:
+        s = s.decode(encoding)
     test = parser.get_doctest(s, globs, name, filename, 0)
     runner.run(test)
 
@@ -2369,7 +2375,8 @@
                 )
 
 def DocFileTest(path, module_relative=True, package=None,
-                globs=None, parser=DocTestParser(), **options):
+                globs=None, parser=DocTestParser(),
+                encoding=None, **options):
     if globs is None:
         globs = {}
     else:
@@ -2390,6 +2397,10 @@
     name = os.path.basename(path)
     doc = open(path).read()
 
+    # If an encoding is specified, use it to convert the file to unicode
+    if encoding:
+        doc = doc.decode(encoding)
+
     # Convert it to a test, and wrap it in a DocFileCase.
     test = parser.get_doctest(doc, globs, name, path, 0)
     return DocFileCase(test, **options)
@@ -2446,6 +2457,9 @@
     parser
       A DocTestParser (or subclass) that should be used to extract
       tests from the files.
+
+    encoding
+      An encoding that will be used to convert the files to unicode.
     """
     suite = unittest.TestSuite()
 



More information about the Zope3-Checkins mailing list