Skip to content

Commit d9407b1

Browse files
authored
gh-51511: Note that codecs.open()'s encoding parameter affects automatic conversion to binary mode (#94370)
1 parent 4c1145b commit d9407b1

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
@@ -708,7 +708,8 @@ def test_decoder_state(self):
708708
"spamspam", self.spambe)
709709

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

0 commit comments

Comments
 (0)