From c4d3e51ab80512c1e54427d3a5397a56236f03f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Lapeyre?= Date: Wed, 13 Mar 2019 01:15:30 +0100 Subject: [PATCH 1/4] Add return value to csv.DictWriter.writeheader --- Doc/library/csv.rst | 7 ++++++- Lib/csv.py | 2 +- Lib/test/test_csv.py | 6 ++++++ .../next/Library/2019-03-13-10-57-41.bpo-27497.JDmIe_.rst | 3 +++ 4 files changed, 16 insertions(+), 2 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2019-03-13-10-57-41.bpo-27497.JDmIe_.rst diff --git a/Doc/library/csv.rst b/Doc/library/csv.rst index 17534fcc4615dc..978af73aa7f4e1 100644 --- a/Doc/library/csv.rst +++ b/Doc/library/csv.rst @@ -467,9 +467,14 @@ DictWriter objects have the following public method: .. method:: DictWriter.writeheader() - Write a row with the field names (as specified in the constructor). + Write a row with the field names (as specified in the constructor) to + the writer's file object, formatted according to the current dialect and + return the return value of the :meth:`csvwriter.writerow` used internally. .. versionadded:: 3.2 + .. versionchanged:: 3.8 + :meth:`writeheader` now also *returns* the value returned by + the :meth:`csvwriter.writerow` method it uses internally. .. _csv-examples: diff --git a/Lib/csv.py b/Lib/csv.py index eeeedabc6bb8a2..dc85077f3ec663 100644 --- a/Lib/csv.py +++ b/Lib/csv.py @@ -140,7 +140,7 @@ def __init__(self, f, fieldnames, restval="", extrasaction="raise", def writeheader(self): header = dict(zip(self.fieldnames, self.fieldnames)) - self.writerow(header) + return self.writerow(header) def _dict_to_list(self, rowdict): if self.extrasaction == "raise": diff --git a/Lib/test/test_csv.py b/Lib/test/test_csv.py index 7a333139b5ea2c..a16d14019f341f 100644 --- a/Lib/test/test_csv.py +++ b/Lib/test/test_csv.py @@ -608,6 +608,12 @@ def test_read_escape_fieldsep(self): class TestDictFields(unittest.TestCase): ### "long" means the row is longer than the number of fieldnames ### "short" means there are fewer elements in the row than fieldnames + def test_writeheader_return_value(self): + with TemporaryFile("w+", newline='') as fileobj: + writer = csv.DictWriter(fileobj, fieldnames = ["f1", "f2", "f3"]) + writeheader_return_value = writer.writeheader() + self.assertEqual(writeheader_return_value, 10) + def test_write_simple_dict(self): with TemporaryFile("w+", newline='') as fileobj: writer = csv.DictWriter(fileobj, fieldnames = ["f1", "f2", "f3"]) diff --git a/Misc/NEWS.d/next/Library/2019-03-13-10-57-41.bpo-27497.JDmIe_.rst b/Misc/NEWS.d/next/Library/2019-03-13-10-57-41.bpo-27497.JDmIe_.rst new file mode 100644 index 00000000000000..77d91a3f365734 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2019-03-13-10-57-41.bpo-27497.JDmIe_.rst @@ -0,0 +1,3 @@ +:meth:`csv.DictWriter.writeheader` now returns the return value of the +underlying :meth:`csv.Writer.writerow` method. Path contributed by Ashish +Nitin Patil. From df897b93f2c96aae8980f9fa2710ddc223a4e681 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Lapeyre?= Date: Thu, 14 Mar 2019 13:52:57 +0100 Subject: [PATCH 2/4] Document the return value of csvwriter.writerow --- Doc/library/csv.rst | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Doc/library/csv.rst b/Doc/library/csv.rst index 978af73aa7f4e1..ac61174304f2f3 100644 --- a/Doc/library/csv.rst +++ b/Doc/library/csv.rst @@ -443,7 +443,8 @@ read CSV files (assuming they support complex numbers at all). .. method:: csvwriter.writerow(row) Write the *row* parameter to the writer's file object, formatted according to - the current dialect. + the current dialect, and return the return value of the call to the *write* + method of the underlying file object. .. versionchanged:: 3.5 Added support of arbitrary iterables. From 8bf1f0eee20a049a57c4e891929a5c0f0170d0bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Lapeyre?= Date: Fri, 15 Mar 2019 14:10:29 +0100 Subject: [PATCH 3/4] Fix typo in blurb --- .../next/Library/2019-03-13-10-57-41.bpo-27497.JDmIe_.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2019-03-13-10-57-41.bpo-27497.JDmIe_.rst b/Misc/NEWS.d/next/Library/2019-03-13-10-57-41.bpo-27497.JDmIe_.rst index 77d91a3f365734..f6da1143681af3 100644 --- a/Misc/NEWS.d/next/Library/2019-03-13-10-57-41.bpo-27497.JDmIe_.rst +++ b/Misc/NEWS.d/next/Library/2019-03-13-10-57-41.bpo-27497.JDmIe_.rst @@ -1,3 +1,3 @@ :meth:`csv.DictWriter.writeheader` now returns the return value of the -underlying :meth:`csv.Writer.writerow` method. Path contributed by Ashish +underlying :meth:`csv.Writer.writerow` method. Patch contributed by Ashish Nitin Patil. From 8f538bc75eb56de3f26a0a27dbb6e0f90c705f9a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Lapeyre?= Date: Fri, 15 Mar 2019 14:16:49 +0100 Subject: [PATCH 4/4] Improve documentation formatting --- Doc/library/csv.rst | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Doc/library/csv.rst b/Doc/library/csv.rst index ac61174304f2f3..49e22fa73ed265 100644 --- a/Doc/library/csv.rst +++ b/Doc/library/csv.rst @@ -443,8 +443,8 @@ read CSV files (assuming they support complex numbers at all). .. method:: csvwriter.writerow(row) Write the *row* parameter to the writer's file object, formatted according to - the current dialect, and return the return value of the call to the *write* - method of the underlying file object. + the current dialect. Return the return value of the call to the *write* method + of the underlying file object. .. versionchanged:: 3.5 Added support of arbitrary iterables. @@ -469,12 +469,12 @@ DictWriter objects have the following public method: .. method:: DictWriter.writeheader() Write a row with the field names (as specified in the constructor) to - the writer's file object, formatted according to the current dialect and - return the return value of the :meth:`csvwriter.writerow` used internally. + the writer's file object, formatted according to the current dialect. Return + the return value of the :meth:`csvwriter.writerow` call used internally. .. versionadded:: 3.2 .. versionchanged:: 3.8 - :meth:`writeheader` now also *returns* the value returned by + :meth:`writeheader` now also returns the value returned by the :meth:`csvwriter.writerow` method it uses internally.