Skip to content

Commit aeb530f

Browse files
committed
gh-156955: Keep the line terminator test additions in their own tests
Restore test_write_lineterminator to its original body and move the reviewer's suggestion into test_write_lineterminator_in_field, so the existing test keeps its charter -- the terminator is emitted between records -- and the new coverage stands on its own. Also strip a trailing space from the NEWS entry, which was failing the trim-trailing-whitespace pre-commit hook and turning the lint job red.
1 parent 40e6756 commit aeb530f

2 files changed

Lines changed: 14 additions & 8 deletions

File tree

Lib/test/test_csv.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,19 @@ def test_write_escape(self):
260260
escapechar='\\', quoting=csv.QUOTE_MINIMAL)
261261

262262
def test_write_lineterminator(self):
263+
for lineterminator in '\r\n', '\n', '\r', '!@#', '\0':
264+
with self.subTest(lineterminator=lineterminator):
265+
with StringIO() as sio:
266+
writer = csv.writer(sio, lineterminator=lineterminator)
267+
writer.writerow(['a', 'b'])
268+
writer.writerow([1, 2])
269+
writer.writerow(['\r', '\n'])
270+
self.assertEqual(sio.getvalue(),
271+
f'a,b{lineterminator}'
272+
f'1,2{lineterminator}'
273+
f'"\r","\n"{lineterminator}')
274+
275+
def test_write_lineterminator_in_field(self):
263276
for lineterminator in ('\r\n', '\n', '\r', '!@#', '\0', '\x85',
264277
'\u2028', '\U0001f600'):
265278
with self.subTest(lineterminator=lineterminator):
@@ -276,10 +289,6 @@ def test_write_lineterminator(self):
276289
f'"a{lineterminator[-1]}b",c{lineterminator}')
277290

278291
def test_write_lineterminator_quoting(self):
279-
# Every character of the line terminator forces quoting, not just the
280-
# last one, and no other character does. Each terminator is paired
281-
# with characters that bracket it in code point order, to pin that
282-
# boundary.
283292
for lineterminator, plain in ('!@#', ' ?A'), ('\u2028', '\u2027\u2029'):
284293
with self.subTest(lineterminator=lineterminator):
285294
for c in lineterminator:
@@ -289,9 +298,6 @@ def test_write_lineterminator_quoting(self):
289298
lineterminator=lineterminator)
290299

291300
def test_write_empty_lineterminator(self):
292-
# An empty line terminator separates nothing and, having no characters
293-
# of its own, forces no quoting -- not even of '\0', the lowest code
294-
# point. '\r' and '\n' are quoted whatever the terminator is.
295301
with StringIO() as sio:
296302
writer = csv.writer(sio, lineterminator='')
297303
writer.writerow(['a', 'b'])
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
Speed up :func:`csv.writer` by up to 2.9x when fields contain no special
1+
Speed up :func:`csv.writer` by up to 2.9x when fields contain no special
22
characters.

0 commit comments

Comments
 (0)