Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d8b509b

Browse files
committed
#13012: use splitlines(keepends=True/False) instead of splitlines(0/1).
1 parent a6e50f5 commit d8b509b

16 files changed

Lines changed: 39 additions & 39 deletions

Lib/argparse.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -641,7 +641,7 @@ class RawDescriptionHelpFormatter(HelpFormatter):
641641
"""
642642

643643
def _fill_text(self, text, width, indent):
644-
return ''.join([indent + line for line in text.splitlines(True)])
644+
return ''.join(indent + line for line in text.splitlines(keepends=True))
645645

646646

647647
class RawTextHelpFormatter(RawDescriptionHelpFormatter):

Lib/codecs.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -484,7 +484,7 @@ def read(self, size=-1, chars=-1, firstline=False):
484484
if firstline:
485485
newchars, decodedbytes = \
486486
self.decode(data[:exc.start], self.errors)
487-
lines = newchars.splitlines(True)
487+
lines = newchars.splitlines(keepends=True)
488488
if len(lines)<=1:
489489
raise
490490
else:
@@ -526,7 +526,7 @@ def readline(self, size=None, keepends=True):
526526
self.charbuffer = self.linebuffer[0]
527527
self.linebuffer = None
528528
if not keepends:
529-
line = line.splitlines(False)[0]
529+
line = line.splitlines(keepends=False)[0]
530530
return line
531531

532532
readsize = size or 72
@@ -543,7 +543,7 @@ def readline(self, size=None, keepends=True):
543543
data += self.read(size=1, chars=1)
544544

545545
line += data
546-
lines = line.splitlines(True)
546+
lines = line.splitlines(keepends=True)
547547
if lines:
548548
if len(lines) > 1:
549549
# More than one line result; the first line is a full line
@@ -559,10 +559,10 @@ def readline(self, size=None, keepends=True):
559559
# only one remaining line, put it back into charbuffer
560560
self.charbuffer = lines[0] + self.charbuffer
561561
if not keepends:
562-
line = line.splitlines(False)[0]
562+
line = line.splitlines(keepends=False)[0]
563563
break
564564
line0withend = lines[0]
565-
line0withoutend = lines[0].splitlines(False)[0]
565+
line0withoutend = lines[0].splitlines(keepends=False)[0]
566566
if line0withend != line0withoutend: # We really have a line end
567567
# Put the rest back together and keep it until the next call
568568
self.charbuffer = self._empty_charbuffer.join(lines[1:]) + \
@@ -575,7 +575,7 @@ def readline(self, size=None, keepends=True):
575575
# we didn't get anything or this was our only try
576576
if not data or size is not None:
577577
if line and not keepends:
578-
line = line.splitlines(False)[0]
578+
line = line.splitlines(keepends=False)[0]
579579
break
580580
if readsize < 8000:
581581
readsize *= 2
@@ -803,7 +803,7 @@ def readlines(self, sizehint=None):
803803

804804
data = self.reader.read()
805805
data, bytesencoded = self.encode(data, self.errors)
806-
return data.splitlines(1)
806+
return data.splitlines(keepends=True)
807807

808808
def __next__(self):
809809

Lib/collections/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1045,7 +1045,7 @@ def split(self, sep=None, maxsplit=-1):
10451045
return self.data.split(sep, maxsplit)
10461046
def rsplit(self, sep=None, maxsplit=-1):
10471047
return self.data.rsplit(sep, maxsplit)
1048-
def splitlines(self, keepends=0): return self.data.splitlines(keepends)
1048+
def splitlines(self, keepends=False): return self.data.splitlines(keepends)
10491049
def startswith(self, prefix, start=0, end=_sys.maxsize):
10501050
return self.data.startswith(prefix, start, end)
10511051
def strip(self, chars=None): return self.__class__(self.data.strip(chars))

Lib/difflib.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -800,7 +800,7 @@ class Differ:
800800
... 2. Explicit is better than implicit.
801801
... 3. Simple is better than complex.
802802
... 4. Complex is better than complicated.
803-
... '''.splitlines(1)
803+
... '''.splitlines(keepends=True)
804804
>>> len(text1)
805805
4
806806
>>> text1[0][-1]
@@ -809,7 +809,7 @@ class Differ:
809809
... 3. Simple is better than complex.
810810
... 4. Complicated is better than complex.
811811
... 5. Flat is better than nested.
812-
... '''.splitlines(1)
812+
... '''.splitlines(keepends=True)
813813
814814
Next we instantiate a Differ object:
815815
@@ -896,8 +896,8 @@ def compare(self, a, b):
896896
897897
Example:
898898
899-
>>> print(''.join(Differ().compare('one\ntwo\nthree\n'.splitlines(1),
900-
... 'ore\ntree\nemu\n'.splitlines(1))),
899+
>>> print(''.join(Differ().compare('one\ntwo\nthree\n'.splitlines(True),
900+
... 'ore\ntree\nemu\n'.splitlines(True))),
901901
... end="")
902902
- one
903903
? ^
@@ -1269,8 +1269,8 @@ def context_diff(a, b, fromfile='', tofile='',
12691269
12701270
Example:
12711271
1272-
>>> print(''.join(context_diff('one\ntwo\nthree\nfour\n'.splitlines(1),
1273-
... 'zero\none\ntree\nfour\n'.splitlines(1), 'Original', 'Current')),
1272+
>>> print(''.join(context_diff('one\ntwo\nthree\nfour\n'.splitlines(True),
1273+
... 'zero\none\ntree\nfour\n'.splitlines(True), 'Original', 'Current')),
12741274
... end="")
12751275
*** Original
12761276
--- Current
@@ -1339,8 +1339,8 @@ def ndiff(a, b, linejunk=None, charjunk=IS_CHARACTER_JUNK):
13391339
13401340
Example:
13411341
1342-
>>> diff = ndiff('one\ntwo\nthree\n'.splitlines(1),
1343-
... 'ore\ntree\nemu\n'.splitlines(1))
1342+
>>> diff = ndiff('one\ntwo\nthree\n'.splitlines(keepends=True),
1343+
... 'ore\ntree\nemu\n'.splitlines(keepends=True))
13441344
>>> print(''.join(diff), end="")
13451345
- one
13461346
? ^
@@ -2034,8 +2034,8 @@ def restore(delta, which):
20342034
20352035
Examples:
20362036
2037-
>>> diff = ndiff('one\ntwo\nthree\n'.splitlines(1),
2038-
... 'ore\ntree\nemu\n'.splitlines(1))
2037+
>>> diff = ndiff('one\ntwo\nthree\n'.splitlines(keepends=True),
2038+
... 'ore\ntree\nemu\n'.splitlines(keepends=True))
20392039
>>> diff = list(diff)
20402040
>>> print(''.join(restore(diff, 1)), end="")
20412041
one

Lib/doctest.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1332,7 +1332,7 @@ def __patched_linecache_getlines(self, filename, module_globals=None):
13321332
m = self.__LINECACHE_FILENAME_RE.match(filename)
13331333
if m and m.group('name') == self.test.name:
13341334
example = self.test.examples[int(m.group('examplenum'))]
1335-
return example.source.splitlines(True)
1335+
return example.source.splitlines(keepends=True)
13361336
else:
13371337
return self.save_linecache_getlines(filename, module_globals)
13381338

@@ -1595,8 +1595,8 @@ def output_difference(self, example, got, optionflags):
15951595
# Check if we should use diff.
15961596
if self._do_a_fancy_diff(want, got, optionflags):
15971597
# Split want & got into lines.
1598-
want_lines = want.splitlines(True) # True == keep line ends
1599-
got_lines = got.splitlines(True)
1598+
want_lines = want.splitlines(keepends=True)
1599+
got_lines = got.splitlines(keepends=True)
16001600
# Use difflib to find their differences.
16011601
if optionflags & REPORT_UDIFF:
16021602
diff = difflib.unified_diff(want_lines, got_lines, n=2)

Lib/lib2to3/refactor.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -560,7 +560,7 @@ def refactor_docstring(self, input, filename):
560560
block_lineno = None
561561
indent = None
562562
lineno = 0
563-
for line in input.splitlines(True):
563+
for line in input.splitlines(keepends=True):
564564
lineno += 1
565565
if line.lstrip().startswith(self.PS1):
566566
if block is not None:
@@ -604,7 +604,7 @@ def refactor_doctest(self, block, lineno, indent, filename):
604604
filename, lineno, err.__class__.__name__, err)
605605
return block
606606
if self.refactor_tree(tree, filename):
607-
new = str(tree).splitlines(True)
607+
new = str(tree).splitlines(keepends=True)
608608
# Undo the adjustment of the line numbers in wrap_toks() below.
609609
clipped, new = new[:lineno-1], new[lineno-1:]
610610
assert clipped == ["\n"] * (lineno-1), clipped

Lib/string.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ def __init__(self, template):
8484

8585
def _invalid(self, mo):
8686
i = mo.start('invalid')
87-
lines = self.template[:i].splitlines(True)
87+
lines = self.template[:i].splitlines(keepends=True)
8888
if not lines:
8989
colno = 1
9090
lineno = 1

Lib/test/test_calendar.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ def neitherspacenordigit(c):
177177
return not c.isspace() and not c.isdigit()
178178

179179
lines = []
180-
for line in s.splitlines(False):
180+
for line in s.splitlines(keepends=False):
181181
# Drop texts, as they are locale dependent
182182
if line and not filter(neitherspacenordigit, line):
183183
lines.append(line)

Lib/test/test_email/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def _msgobj(self, filename):
3838
return email.message_from_file(fp)
3939

4040
def _bytes_repr(self, b):
41-
return [repr(x) for x in b.splitlines(True)]
41+
return [repr(x) for x in b.splitlines(keepends=True)]
4242

4343
def assertBytesEqual(self, first, second, msg):
4444
"""Our byte strings are really encoded strings; improve diff output"""

Lib/test/test_gzip.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ def test_buffered_reader(self):
139139
with io.BufferedReader(f) as r:
140140
lines = [line for line in r]
141141

142-
self.assertEqual(lines, 50 * data1.splitlines(True))
142+
self.assertEqual(lines, 50 * data1.splitlines(keepends=True))
143143

144144
def test_readline(self):
145145
self.test_write()
@@ -340,7 +340,7 @@ def sizes():
340340

341341
def test_textio_readlines(self):
342342
# Issue #10791: TextIOWrapper.readlines() fails when wrapping GzipFile.
343-
lines = (data1 * 50).decode("ascii").splitlines(True)
343+
lines = (data1 * 50).decode("ascii").splitlines(keepends=True)
344344
self.test_write()
345345
with gzip.GzipFile(self.filename, 'r') as f:
346346
with io.TextIOWrapper(f, encoding="ascii") as t:

0 commit comments

Comments
 (0)