[Python-checkins] commit of r41452 - in python/branches/jim-doctest/Lib: . test

jim.fulton@python.org jim.fulton at python.org
Tue Nov 15 18:05:01 CET 2005


Author: jim.fulton
Date: Tue Nov 15 18:05:00 2005
New Revision: 41452

Added:
   python/branches/jim-doctest/Lib/test/test_doctest5.txt   (contents, props changed)
   python/branches/jim-doctest/Lib/test/test_doctest6.txt   (contents, props changed)
   python/branches/jim-doctest/Lib/test/test_doctest7.txt   (contents, props changed)
Modified:
   python/branches/jim-doctest/Lib/doctest.py
   python/branches/jim-doctest/Lib/test/test_doctest.py
Log:
Added support for pep 263 encoding specifications in doctest text test
files.


Modified: python/branches/jim-doctest/Lib/doctest.py
==============================================================================
--- python/branches/jim-doctest/Lib/doctest.py	(original)
+++ python/branches/jim-doctest/Lib/doctest.py	Tue Nov 15 18:05:00 2005
@@ -1960,7 +1960,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)
@@ -1975,6 +1977,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):
     """
@@ -2354,7 +2367,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.

Modified: python/branches/jim-doctest/Lib/test/test_doctest.py
==============================================================================
--- python/branches/jim-doctest/Lib/test/test_doctest.py	(original)
+++ python/branches/jim-doctest/Lib/test/test_doctest.py	Tue Nov 15 18:05:00 2005
@@ -2037,6 +2037,26 @@
          >>> suite.run(unittest.TestResult())
          <unittest.TestResult run=3 errors=0 failures=2>
 
+       Encoding can also be specified in the test file itself,
+       following the same rules as defined by PEP 263,
+       http://www.python.org/peps/pep-0263.html.
+
+         >>> suite = doctest.DocFileSuite('test_doctest5.txt')
+         >>> suite.run(unittest.TestResult())
+         <unittest.TestResult run=1 errors=0 failures=0>
+
+         >>> suite = doctest.DocFileSuite('test_doctest6.txt')
+         >>> suite.run(unittest.TestResult())
+         <unittest.TestResult run=1 errors=0 failures=0>
+
+       The 2 examples above illustrate some valid PEP 263 encoding
+       examples. The example below failes because the coding is in the
+       last line:
+
+         >>> suite = doctest.DocFileSuite('test_doctest7.txt')
+         >>> suite.run(unittest.TestResult())
+         <unittest.TestResult run=1 errors=0 failures=1>
+
        """
 
 def test_trailing_space_in_test():
@@ -2269,6 +2289,31 @@
     >>> doctest.testfile('test_doctest4.txt', encoding='utf-8')
     (0, 4)
     >>> doctest.master = None  # Reset master.
+
+Encoding can also be specified in the test file itself, following the
+same rules as defined by PEP 263, http://www.python.org/peps/pep-0263.html.
+
+    >>> doctest.testfile('test_doctest5.txt')
+    (0, 4)
+
+    >>> doctest.testfile('test_doctest6.txt')
+    (0, 4)
+
+The 2 examples above illustrate some valid PEP 263 encoding
+examples. The example below failes because the coding is in the last
+line:
+
+    >>> doctest.testfile('test_doctest7.txt') # doctest: +ELLIPSIS
+    **********************************************************************
+    File "...test_doctest7.txt", line 11, in test_doctest7.txt
+    Failed example:
+    ...
+    **********************************************************************
+    1 items had failures:
+       2 of   4 in test_doctest7.txt
+    ***Test Failed*** 2 failures.
+    (2, 4)
+
 """
 
 # old_test1, ... used to live in doctest.py, but cluttered it.  Note

Added: python/branches/jim-doctest/Lib/test/test_doctest5.txt
==============================================================================
--- (empty file)
+++ python/branches/jim-doctest/Lib/test/test_doctest5.txt	Tue Nov 15 18:05:00 2005
@@ -0,0 +1,19 @@
+.. -*- coding: utf-8 -*-
+
+
+This is a sample doctest in a text file that contains non-ASCII characters.
+
+In order to get this test to pass, we have to manually specify the
+encoding.  We did so in the "coding" line above.
+
+  >>> u'föö'
+  u'f\xf6\xf6'
+
+  >>> u'bąr'
+  u'b\u0105r'
+
+  >>> 'föö'
+  'f\xc3\xb6\xc3\xb6'
+
+  >>> 'bąr'
+  'b\xc4\x85r'

Added: python/branches/jim-doctest/Lib/test/test_doctest6.txt
==============================================================================
--- (empty file)
+++ python/branches/jim-doctest/Lib/test/test_doctest6.txt	Tue Nov 15 18:05:00 2005
@@ -0,0 +1,20 @@
+
+.. -*- coding=utf-8 -*-
+
+
+This is a sample doctest in a text file that contains non-ASCII characters.
+
+In order to get this test to pass, we have to manually specify the
+encoding.  We did so in the "coding" line above.
+
+  >>> u'föö'
+  u'f\xf6\xf6'
+
+  >>> u'bąr'
+  u'b\u0105r'
+
+  >>> 'föö'
+  'f\xc3\xb6\xc3\xb6'
+
+  >>> 'bąr'
+  'b\xc4\x85r'

Added: python/branches/jim-doctest/Lib/test/test_doctest7.txt
==============================================================================
--- (empty file)
+++ python/branches/jim-doctest/Lib/test/test_doctest7.txt	Tue Nov 15 18:05:00 2005
@@ -0,0 +1,21 @@
+
+
+.. -*- coding: utf-8 -*-
+
+
+This is a sample doctest in a text file that contains non-ASCII characters.
+
+In order to get this test to pass, we have to manually specify the
+encoding.  We did so in the "coding" line above.
+
+  >>> u'föö'
+  u'f\xf6\xf6'
+
+  >>> u'bąr'
+  u'b\u0105r'
+
+  >>> 'föö'
+  'f\xc3\xb6\xc3\xb6'
+
+  >>> 'bąr'
+  'b\xc4\x85r'


More information about the Python-checkins mailing list