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

Jim Fulton jim at zope.com
Tue Nov 15 12:06:54 EST 2005


Log message for revision 40133:
  Merged revision 41452 from Python svn:
  
  Added support for pep 263 encoding specifications in doctest text
    test files.
  

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 16:32:38 UTC (rev 40132)
+++ zope.testing/branches/doctest-encoding/src/zope/testing/doctest.py	2005-11-15 17:06:53 UTC (rev 40133)
@@ -1973,7 +1973,9 @@
 
     # Read the file, convert it to a test, and run it.
     s = open(filename).read()
-    if encoding:
+    if encoding is None:
+        encoding = pep263_encoding(s)
+    if encoding is not None:
         s = s.decode(encoding)
     test = parser.get_doctest(s, globs, name, filename, 0)
     runner.run(test)
@@ -1988,6 +1990,17 @@
 
     return runner.failures, runner.tries
 
+pep263_re_search = re.compile("coding[:=]\s*([-\w.]+)").search
+def pep263_encoding(s):
+    """Try to find the encoding of a string by looking for a pep263 coding.
+    """
+    for line in s.split('\n')[:2]:
+        r = pep263_re_search(line)
+        if r:
+            return r.group(1)
+
+    
+
 def run_docstring_examples(f, globs, verbose=False, name="NoName",
                            compileflags=None, optionflags=0):
     """
@@ -2398,7 +2411,9 @@
     doc = open(path).read()
 
     # If an encoding is specified, use it to convert the file to unicode
-    if encoding:
+    if encoding is None:
+        encoding = pep263_encoding(doc)
+    if encoding is not None:
         doc = doc.decode(encoding)
 
     # Convert it to a test, and wrap it in a DocFileCase.



More information about the Zope3-Checkins mailing list