Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-51511: Note that codecs.open()'s encoding parameter affects automatic conversion to binary mode #94370

Merged
merged 2 commits into from
Oct 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Doc/library/codecs.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
3 changes: 2 additions & 1 deletion Lib/codecs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion Lib/test/test_codecs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down