[Python-checkins] r78493 - in python/branches/py3k: Lib/doctest.py Lib/test/test_doctest.py Misc/NEWS

florent.xicluna python-checkins at python.org
Sat Feb 27 15:21:58 CET 2010


Author: florent.xicluna
Date: Sat Feb 27 15:21:57 2010
New Revision: 78493

Log:
For 3.x, the "backsplashreplace" error handling is plugged on the "write" method.

Recorded merge of revisions 78488 via svnmerge from 
svn+ssh://pythondev@svn.python.org/python/trunk

........
  r78488 | florent.xicluna | 2010-02-27 14:31:23 +0100 (sam, 27 fév 2010) | 2 lines
  
  Issue #1729305: Fix doctest to handle encode error with "backslashreplace".  It fixes #7667 too.
........


Modified:
   python/branches/py3k/   (props changed)
   python/branches/py3k/Lib/doctest.py
   python/branches/py3k/Lib/test/test_doctest.py
   python/branches/py3k/Misc/NEWS

Modified: python/branches/py3k/Lib/doctest.py
==============================================================================
--- python/branches/py3k/Lib/doctest.py	(original)
+++ python/branches/py3k/Lib/doctest.py	Sat Feb 27 15:21:57 2010
@@ -218,8 +218,8 @@
 
 def _indent(s, indent=4):
     """
-    Add the given number of space characters to the beginning every
-    non-blank line in `s`, and return the result.
+    Add the given number of space characters to the beginning of
+    every non-blank line in `s`, and return the result.
     """
     # This regexp matches the start of non-blank lines:
     return re.sub('(?m)^(?!$)', indent*' ', s)
@@ -1354,7 +1354,14 @@
 
         save_stdout = sys.stdout
         if out is None:
-            out = save_stdout.write
+            encoding = save_stdout.encoding
+            if encoding is None or encoding.lower() == 'utf-8':
+                out = save_stdout.write
+            else:
+                # Use backslashreplace error handling on write
+                def out(s):
+                    s = str(s.encode(encoding, 'backslashreplace'), encoding)
+                    save_stdout.write(s)
         sys.stdout = self._fakeout
 
         # Patch pdb.set_trace to restore sys.stdout during interactive

Modified: python/branches/py3k/Lib/test/test_doctest.py
==============================================================================
--- python/branches/py3k/Lib/test/test_doctest.py	(original)
+++ python/branches/py3k/Lib/test/test_doctest.py	Sat Feb 27 15:21:57 2010
@@ -2149,6 +2149,13 @@
 called with the name of a file, which is taken to be relative to the
 calling module.  The return value is (#failures, #tests).
 
+We don't want `-v` in sys.argv for these tests.
+
+    >>> save_argv = sys.argv
+    >>> if '-v' in sys.argv:
+    ...     sys.argv = [arg for arg in save_argv if arg != '-v']
+
+
     >>> doctest.testfile('test_doctest.txt') # doctest: +ELLIPSIS
     **********************************************************************
     File "...", line 6, in test_doctest.txt
@@ -2288,6 +2295,28 @@
     >>> doctest.testfile('test_doctest4.txt', encoding='utf-8')
     TestResults(failed=0, attempted=2)
     >>> doctest.master = None  # Reset master.
+
+Test the verbose output:
+
+    >>> doctest.testfile('test_doctest4.txt', encoding='utf-8', verbose=True)
+    Trying:
+        'föö'
+    Expecting:
+        'f\xf6\xf6'
+    ok
+    Trying:
+        'bąr'
+    Expecting:
+        'b\u0105r'
+    ok
+    1 items passed all tests:
+       2 tests in test_doctest4.txt
+    2 tests in 1 items.
+    2 passed and 0 failed.
+    Test passed.
+    TestResults(failed=0, attempted=2)
+    >>> doctest.master = None  # Reset master.
+    >>> sys.argv = save_argv
 """
 
 def test_testmod(): r"""
@@ -2297,7 +2326,7 @@
 out of the binary module.
 
     >>> import unicodedata
-    >>> doctest.testmod(unicodedata)
+    >>> doctest.testmod(unicodedata, verbose=False)
     TestResults(failed=0, attempted=0)
 """
 

Modified: python/branches/py3k/Misc/NEWS
==============================================================================
--- python/branches/py3k/Misc/NEWS	(original)
+++ python/branches/py3k/Misc/NEWS	Sat Feb 27 15:21:57 2010
@@ -260,6 +260,8 @@
 Library
 -------
 
+- Issue #1729305: Fix doctest to handle encode error with "backslashreplace".
+
 - Issue #691291: codecs.open() should not convert end of lines on reading and
   writing.
 


More information about the Python-checkins mailing list