Skip to content

Commit c835b97

Browse files
gh-51511: Note that codecs.open()'s encoding parameter affects automatic conversion to binary mode (GH-94370)
(cherry picked from commit d9407b1) Co-authored-by: Stanley <[email protected]>
1 parent 68f25e2 commit c835b97

File tree

3 files changed

+6
-3
lines changed

3 files changed

+6
-3
lines changed

Doc/library/codecs.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,8 @@ wider range of codecs when working with binary files:
189189

190190
.. note::
191191

192-
Underlying encoded files are always opened in binary mode.
192+
If *encoding* is not ``None``, then the
193+
underlying encoded files are always opened in binary mode.
193194
No automatic conversion of ``'\n'`` is done on reading and writing.
194195
The *mode* argument may be any binary mode acceptable to the built-in
195196
:func:`open` function; the ``'b'`` is automatically added.

Lib/codecs.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -878,7 +878,8 @@ def open(filename, mode='r', encoding=None, errors='strict', buffering=-1):
878878
codecs. Output is also codec dependent and will usually be
879879
Unicode as well.
880880
881-
Underlying encoded files are always opened in binary mode.
881+
If encoding is not None, then the
882+
underlying encoded files are always opened in binary mode.
882883
The default file mode is 'r', meaning to open the file in read mode.
883884
884885
encoding specifies the encoding which is to be used for the

Lib/test/test_codecs.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -709,7 +709,8 @@ def test_decoder_state(self):
709709
"spamspam", self.spambe)
710710

711711
def test_bug691291(self):
712-
# Files are always opened in binary mode, even if no binary mode was
712+
# If encoding is not None, then
713+
# files are always opened in binary mode, even if no binary mode was
713714
# specified. This means that no automatic conversion of '\n' is done
714715
# on reading and writing.
715716
s1 = 'Hello\r\nworld\r\n'

0 commit comments

Comments
 (0)