Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 4 additions & 0 deletions Doc/library/codecs.rst
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,10 @@ wider range of codecs when working with binary files:
.. versionchanged:: 3.11
The ``'U'`` mode has been removed.

.. deprecated:: next

:func:`codecs.open` has been superseded by :func:`open`.


.. function:: EncodedFile(file, data_encoding, file_encoding=None, errors='strict')

Expand Down
4 changes: 4 additions & 0 deletions Doc/whatsnew/3.14.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1575,6 +1575,10 @@ Deprecated
as a single positional argument.
(Contributed by Serhiy Storchaka in :gh:`109218`.)

* :mod:`codecs`:
:func:`codecs.open` is now deprecated. Use :func:`open` instead.
(Contributed by Inada Naoki in :gh:`133036`.)

* :mod:`functools`:
Calling the Python implementation of :func:`functools.reduce` with *function*
or *sequence* as keyword arguments is now deprecated.
Expand Down
6 changes: 4 additions & 2 deletions Lib/codecs.py
Original file line number Diff line number Diff line change
Expand Up @@ -884,7 +884,6 @@ def __reduce_ex__(self, proto):
### Shortcuts

def open(filename, mode='r', encoding=None, errors='strict', buffering=-1):

""" Open an encoded file using the given mode and return
a wrapped version providing transparent encoding/decoding.

Expand Down Expand Up @@ -912,8 +911,11 @@ def open(filename, mode='r', encoding=None, errors='strict', buffering=-1):
.encoding which allows querying the used encoding. This
attribute is only available if an encoding was specified as
parameter.

"""
import warnings
warnings.warn("codecs.open() is deprecated. Use open() instead.",
DeprecationWarning, stacklevel=2)

if encoding is not None and \
'b' not in mode:
# Force opening of the file in binary mode
Expand Down
32 changes: 20 additions & 12 deletions Lib/test/test_codecs.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import unittest
import encodings
from unittest import mock
import warnings

from test import support
from test.support import os_helper
Expand All @@ -28,6 +29,13 @@
else:
SIZEOF_WCHAR_T = ctypes.sizeof(ctypes.c_wchar)

def codecs_open_nowarn(filename, mode='r', encoding=None, errors='strict', buffering=-1):
with warnings.catch_warnings():
warnings.simplefilter("ignore")
return codecs.open(
filename, mode, encoding=encoding, errors=errors,
buffering=buffering)

def coding_checker(self, coder):
def check(input, expect):
self.assertEqual(coder(input), (expect, len(input)))
Expand Down Expand Up @@ -719,19 +727,19 @@ def test_bug691291(self):
self.addCleanup(os_helper.unlink, os_helper.TESTFN)
with open(os_helper.TESTFN, 'wb') as fp:
fp.write(s)
with codecs.open(os_helper.TESTFN, 'r',
with codecs_open_nowarn(os_helper.TESTFN, 'r',
encoding=self.encoding) as reader:
self.assertEqual(reader.read(), s1)

def test_invalid_modes(self):
for mode in ('U', 'rU', 'r+U'):
with self.assertRaises(ValueError) as cm:
codecs.open(os_helper.TESTFN, mode, encoding=self.encoding)
codecs_open_nowarn(os_helper.TESTFN, mode, encoding=self.encoding)
self.assertIn('invalid mode', str(cm.exception))

for mode in ('rt', 'wt', 'at', 'r+t'):
with self.assertRaises(ValueError) as cm:
codecs.open(os_helper.TESTFN, mode, encoding=self.encoding)
codecs_open_nowarn(os_helper.TESTFN, mode, encoding=self.encoding)
self.assertIn("can't have text and binary mode at once",
str(cm.exception))

Expand Down Expand Up @@ -1844,9 +1852,9 @@ def test_all(self):
def test_open(self):
self.addCleanup(os_helper.unlink, os_helper.TESTFN)
for mode in ('w', 'r', 'r+', 'w+', 'a', 'a+'):
with self.subTest(mode), \
codecs.open(os_helper.TESTFN, mode, 'ascii') as file:
self.assertIsInstance(file, codecs.StreamReaderWriter)
with self.subTest(mode), self.assertWarns(DeprecationWarning):
with codecs.open(os_helper.TESTFN, mode, 'ascii') as file:
self.assertIsInstance(file, codecs.StreamReaderWriter)

def test_undefined(self):
self.assertRaises(UnicodeError, codecs.encode, 'abc', 'undefined')
Expand All @@ -1863,7 +1871,7 @@ def test_file_closes_if_lookup_error_raised(self):
mock_open = mock.mock_open()
with mock.patch('builtins.open', mock_open) as file:
with self.assertRaises(LookupError):
codecs.open(os_helper.TESTFN, 'wt', 'invalid-encoding')
codecs_open_nowarn(os_helper.TESTFN, 'wt', 'invalid-encoding')

file().close.assert_called()

Expand Down Expand Up @@ -2883,7 +2891,7 @@ def test_seek0(self):
self.addCleanup(os_helper.unlink, os_helper.TESTFN)
for encoding in tests:
# Check if the BOM is written only once
with codecs.open(os_helper.TESTFN, 'w+', encoding=encoding) as f:
with codecs_open_nowarn(os_helper.TESTFN, 'w+', encoding=encoding) as f:
f.write(data)
f.write(data)
f.seek(0)
Expand All @@ -2892,7 +2900,7 @@ def test_seek0(self):
self.assertEqual(f.read(), data * 2)

# Check that the BOM is written after a seek(0)
with codecs.open(os_helper.TESTFN, 'w+', encoding=encoding) as f:
with codecs_open_nowarn(os_helper.TESTFN, 'w+', encoding=encoding) as f:
f.write(data[0])
self.assertNotEqual(f.tell(), 0)
f.seek(0)
Expand All @@ -2901,7 +2909,7 @@ def test_seek0(self):
self.assertEqual(f.read(), data)

# (StreamWriter) Check that the BOM is written after a seek(0)
with codecs.open(os_helper.TESTFN, 'w+', encoding=encoding) as f:
with codecs_open_nowarn(os_helper.TESTFN, 'w+', encoding=encoding) as f:
f.writer.write(data[0])
self.assertNotEqual(f.writer.tell(), 0)
f.writer.seek(0)
Expand All @@ -2911,7 +2919,7 @@ def test_seek0(self):

# Check that the BOM is not written after a seek() at a position
# different than the start
with codecs.open(os_helper.TESTFN, 'w+', encoding=encoding) as f:
with codecs_open_nowarn(os_helper.TESTFN, 'w+', encoding=encoding) as f:
f.write(data)
f.seek(f.tell())
f.write(data)
Expand All @@ -2920,7 +2928,7 @@ def test_seek0(self):

# (StreamWriter) Check that the BOM is not written after a seek()
# at a position different than the start
with codecs.open(os_helper.TESTFN, 'w+', encoding=encoding) as f:
with codecs_open_nowarn(os_helper.TESTFN, 'w+', encoding=encoding) as f:
f.writer.write(data)
f.writer.seek(f.writer.tell())
f.writer.write(data)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
:func:`codecs.open` is now deprecated. Use :func:`open` instead. Contributed
by Inada Naoki.
Loading