Skip to content

Commit 453d886

Browse files
guociblurb-it[bot]brettcannon
authored
pythonGH-90344: replace single-call io.IncrementalNewlineDecoder usage with non-incremental newline decoders (pythonGH-30276)
Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com> Co-authored-by: Brett Cannon <[email protected]>
1 parent f0a8bc7 commit 453d886

File tree

4 files changed

+5
-10
lines changed

4 files changed

+5
-10
lines changed

Lib/doctest.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ def _test():
104104
import traceback
105105
import types
106106
import unittest
107-
from io import StringIO, IncrementalNewlineDecoder
107+
from io import StringIO, TextIOWrapper, BytesIO
108108
from collections import namedtuple
109109
import _colorize # Used in doctests
110110
from _colorize import ANSIColors, can_colorize
@@ -237,10 +237,6 @@ def _normalize_module(module, depth=2):
237237
else:
238238
raise TypeError("Expected a module, string, or None")
239239

240-
def _newline_convert(data):
241-
# The IO module provides a handy decoder for universal newline conversion
242-
return IncrementalNewlineDecoder(None, True).decode(data, True)
243-
244240
def _load_testfile(filename, package, module_relative, encoding):
245241
if module_relative:
246242
package = _normalize_module(package, 3)
@@ -252,10 +248,9 @@ def _load_testfile(filename, package, module_relative, encoding):
252248
pass
253249
if hasattr(loader, 'get_data'):
254250
file_contents = loader.get_data(filename)
255-
file_contents = file_contents.decode(encoding)
256251
# get_data() opens files as 'rb', so one must do the equivalent
257252
# conversion as universal newlines would do.
258-
return _newline_convert(file_contents), filename
253+
return TextIOWrapper(BytesIO(file_contents), encoding=encoding, newline=None).read(), filename
259254
with open(filename, encoding=encoding) as f:
260255
return f.read(), filename
261256

Lib/importlib/_bootstrap_external.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -552,8 +552,7 @@ def decode_source(source_bytes):
552552
import tokenize # To avoid bootstrap issues.
553553
source_bytes_readline = _io.BytesIO(source_bytes).readline
554554
encoding = tokenize.detect_encoding(source_bytes_readline)
555-
newline_decoder = _io.IncrementalNewlineDecoder(None, True)
556-
return newline_decoder.decode(source_bytes.decode(encoding[0]))
555+
return _io.TextIOWrapper(_io.BytesIO(source_bytes), encoding=encoding[0], newline=None).read()
557556

558557

559558
# Module specifications #######################################################

Lib/test/test_importlib/test_abc.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -904,7 +904,7 @@ def test_universal_newlines(self):
904904
mock = self.SourceOnlyLoaderMock('mod.file')
905905
source = "x = 42\r\ny = -13\r\n"
906906
mock.source = source.encode('utf-8')
907-
expect = io.IncrementalNewlineDecoder(None, True).decode(source)
907+
expect = io.StringIO(source, newline=None).getvalue()
908908
self.assertEqual(mock.get_source(name), expect)
909909

910910

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Replace :class:`io.IncrementalNewlineDecoder` with non incremental newline decoders in codebase where :meth:`!io.IncrementalNewlineDecoder.decode` was being called once.

0 commit comments

Comments
 (0)