]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-43049: Use io.IncrementalNewlineDecoder for doctest newline conversion (GH-24359)
authorPeter Donis <peterdonis@alum.mit.edu>
Tue, 2 Mar 2021 17:06:20 +0000 (12:06 -0500)
committerGitHub <noreply@github.com>
Tue, 2 Mar 2021 17:06:20 +0000 (11:06 -0600)
Followup to bpo-1812 and GH-17385.

Lib/doctest.py

index 5bb35c9715d1e910b20f3438e5bd9654e5e3cdda..e95c333f48aad59cbb3f2b73c88936d9fee1eaef 100644 (file)
@@ -102,7 +102,7 @@ import re
 import sys
 import traceback
 import unittest
-from io import StringIO
+from io import StringIO, IncrementalNewlineDecoder
 from collections import namedtuple
 
 TestResults = namedtuple('TestResults', 'failed attempted')
@@ -212,11 +212,8 @@ def _normalize_module(module, depth=2):
         raise TypeError("Expected a module, string, or None")
 
 def _newline_convert(data):
-    # We have two cases to cover and we need to make sure we do
-    # them in the right order
-    for newline in ('\r\n', '\r'):
-        data = data.replace(newline, '\n')
-    return data
+    # The IO module provides a handy decoder for universal newline conversion
+    return IncrementalNewlineDecoder(None, True).decode(data, True)
 
 def _load_testfile(filename, package, module_relative, encoding):
     if module_relative: