From 471ba851ebd7d01f947b94f0051255db76156313 Mon Sep 17 00:00:00 2001 From: slateny <46876382+slateny@users.noreply.github.com> Date: Tue, 28 Jun 2022 00:44:38 -0700 Subject: [PATCH 1/2] Note that codecs.open()'s encoding parameter affects automatic conversion to binary mode --- Doc/library/codecs.rst | 3 ++- Lib/codecs.py | 3 ++- Lib/test/test_codecs.py | 3 ++- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/Doc/library/codecs.rst b/Doc/library/codecs.rst index d131408175fd16..269aa759fde8ee 100644 --- a/Doc/library/codecs.rst +++ b/Doc/library/codecs.rst @@ -189,7 +189,8 @@ wider range of codecs when working with binary files: .. note:: - Underlying encoded files are always opened in binary mode. + If ``encoding`` is not None, then the + underlying encoded files are always opened in binary mode. No automatic conversion of ``'\n'`` is done on reading and writing. The *mode* argument may be any binary mode acceptable to the built-in :func:`open` function; the ``'b'`` is automatically added. diff --git a/Lib/codecs.py b/Lib/codecs.py index e6ad6e3a052364..3b173b612101e7 100644 --- a/Lib/codecs.py +++ b/Lib/codecs.py @@ -878,7 +878,8 @@ def open(filename, mode='r', encoding=None, errors='strict', buffering=-1): codecs. Output is also codec dependent and will usually be Unicode as well. - Underlying encoded files are always opened in binary mode. + If encoding is not None, then the + underlying encoded files are always opened in binary mode. The default file mode is 'r', meaning to open the file in read mode. encoding specifies the encoding which is to be used for the diff --git a/Lib/test/test_codecs.py b/Lib/test/test_codecs.py index 57f3648eb7017c..32a704f4e97e41 100644 --- a/Lib/test/test_codecs.py +++ b/Lib/test/test_codecs.py @@ -708,7 +708,8 @@ def test_decoder_state(self): "spamspam", self.spambe) def test_bug691291(self): - # Files are always opened in binary mode, even if no binary mode was + # If encoding is not None, then + # files are always opened in binary mode, even if no binary mode was # specified. This means that no automatic conversion of '\n' is done # on reading and writing. s1 = 'Hello\r\nworld\r\n' From 07bababf99789d208015aa565780cafc8f8511a8 Mon Sep 17 00:00:00 2001 From: slateny <46876382+slateny@users.noreply.github.com> Date: Thu, 20 Oct 2022 20:45:48 -0700 Subject: [PATCH 2/2] Fix formatting --- Doc/library/codecs.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/library/codecs.rst b/Doc/library/codecs.rst index 269aa759fde8ee..11c9f4c276d225 100644 --- a/Doc/library/codecs.rst +++ b/Doc/library/codecs.rst @@ -189,7 +189,7 @@ wider range of codecs when working with binary files: .. note:: - If ``encoding`` is not None, then the + If *encoding* is not ``None``, then the underlying encoded files are always opened in binary mode. No automatic conversion of ``'\n'`` is done on reading and writing. The *mode* argument may be any binary mode acceptable to the built-in