]> granicus.if.org Git - python/commitdiff
#8471: reset _SpoofOut.buf to an empty string when truncating; if Unicode had been...
authorGeorg Brandl <georg@python.org>
Sun, 1 Aug 2010 08:22:05 +0000 (08:22 +0000)
committerGeorg Brandl <georg@python.org>
Sun, 1 Aug 2010 08:22:05 +0000 (08:22 +0000)
Lib/doctest.py
Lib/test/test_doctest.py
Misc/NEWS

index 70d335954586b11acd1e412a36b401bf632419e8..917684cfc70a9e1653328879a634c0375c6a7eba 100644 (file)
@@ -263,6 +263,9 @@ class _SpoofOut(StringIO):
         StringIO.truncate(self, size)
         if hasattr(self, "softspace"):
             del self.softspace
+        if not self.buf:
+            # Reset it to an empty string, to make sure it's not unicode.
+            self.buf = ''
 
 # Worst-case linear-time ellipsis matching.
 def _ellipsis_match(want, got):
index 105106809c830fcdf20aaf19781fd1587cc1f60f..e51bacca926b45f3d8bfb56d3efdb7b2f6c93236 100644 (file)
@@ -1581,7 +1581,33 @@ source:
     >>> test = doctest.DocTestParser().get_doctest(s, {}, 's', 's.py', 0)
     Traceback (most recent call last):
     ValueError: line 0 of the doctest for s has an option directive on a line with no example: '# doctest: +ELLIPSIS'
-"""
+
+    """
+
+    def test_unicode_output(self): r"""
+
+Check that unicode output works:
+
+    >>> u'\xe9'
+    u'\xe9'
+
+If we return unicode, SpoofOut's buf variable becomes automagically
+converted to unicode. This means all subsequent output becomes converted
+to unicode, and if the output contains non-ascii characters that failed.
+It used to be that this state change carried on between tests, meaning
+tests would fail if unicode has been output previously in the testrun.
+This test tests that this is no longer so:
+
+    >>> print u'abc'
+    abc
+
+And then return a string with non-ascii characters:
+
+    >>> print u'\xe9'.encode('utf-8')
+    é
+
+    """
+
 
 def test_testsource(): r"""
 Unit tests for `testsource()`.
index a7928c0843569ef0cd3eaab222d1b6df9d8f0bfb..14f3fb042d5da5d53af6a33a5cfd4b7092a47dd9 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -18,6 +18,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #8471: In doctest, properly reset the output stream to an empty
+  string when Unicode was previously output.
+
 - Issue #8620: when a Cmd is fed input that reaches EOF without a final
   newline, it no longer truncates the last character of the last command line.