8000 gh-133036: Deprecate codecs.open by methane · Pull Request #133038 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-133036: Deprecate codecs.open #133038

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

Merged
merged 9 commits into from
Apr 30, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
8000
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
codecs_open_nowarn -> codecs_open_no_warn
  • Loading branch information
methane committed Apr 28, 2025
commit 47ebf4f4e203b5e772dfddbfe4efef3f9b384bfb
32 changes: 12 additions & 20 deletions Lib/test/test_codecs.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,12 @@
except ImportError:
_testinternalcapi = None

try:
import ctypes
except ImportError:
ctypes = None
SIZEOF_WCHAR_T = -1
else:
SIZEOF_WCHAR_T = ctypes.sizeof(ctypes.c_wchar)

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

def coding_checker(self, coder):
def check(input, expect):
Expand Down Expand Up @@ -727,19 +719,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_nowarn(os_helper.TESTFN, 'r',
with codecs_open_no_warn(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_nowarn(os_helper.TESTFN, mode, encoding=self.encoding)
codecs_open_no_warn(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_nowarn(os_helper.TESTFN, mode, encoding=self.encoding)
codecs_open_no_warn(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 @@ -1871,7 +1863,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_nowarn(os_helper.TESTFN, 'wt', 'invalid-encoding')
codecs_open_no_warn(os_helper.TESTFN, 'wt', 'invalid-encoding')

file().close.assert_called()

Expand Down Expand Up @@ -2891,7 +2883,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_nowarn(os_helper.TESTFN, 'w+', encoding=encoding) as f:
with codecs_open_no_warn(os_helper.TESTFN, 'w+', encoding=encoding) as f:
f.write(data)
f.write(data)
f.seek(0)
Expand All @@ -2900,7 +2892,7 @@ def test_seek0(self):
self.assertEqual(f.read(), data * 2)

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

# (StreamWriter) Check that the BOM is written after a seek(0)
with codecs_open_nowarn(os_helper.TESTFN, 'w+', encoding=encoding) as f:
with codecs_open_no_warn(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 @@ -2919,7 +2911,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_nowarn(os_helper.TESTFN, 'w+', encoding=encoding) as f:
with codecs_open_no_warn(os_helper.TESTFN, 'w+', encoding=encoding) as f:
f.write(data)
f.seek(f.tell())
f.write(data)
Expand All @@ -2928,7 +2920,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_nowarn(os_helper.TESTFN, 'w+', encoding=encoding) as f:
with codecs_open_no_warn(os_helper.TESTFN, 'w+', encoding=encoding) as f:
f.writer.write(data)
f.writer.seek(f.writer.tell())
f.writer.write(data)
Expand Down
0