mirror of
https://github.com/python/cpython.git
synced 2025-08-04 00:48:58 +00:00
gh-133036: Deprecate codecs.open (#133038)
Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com> Co-authored-by: Victor Stinner <vstinner@python.org>
This commit is contained in:
parent
732d1b0241
commit
4e294f6feb
11 changed files with 58 additions and 37 deletions
|
@ -7,6 +7,7 @@ import sys
|
|||
import unittest
|
||||
import encodings
|
||||
from unittest import mock
|
||||
import warnings
|
||||
|
||||
from test import support
|
||||
from test.support import os_helper
|
||||
|
@ -20,13 +21,12 @@ try:
|
|||
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_no_warn(*args, **kwargs):
|
||||
"""Call codecs.open(*args, **kwargs) ignoring DeprecationWarning."""
|
||||
with warnings.catch_warnings():
|
||||
warnings.simplefilter("ignore")
|
||||
return codecs.open(*args, **kwargs)
|
||||
|
||||
def coding_checker(self, coder):
|
||||
def check(input, expect):
|
||||
|
@ -35,13 +35,13 @@ def coding_checker(self, coder):
|
|||
|
||||
# On small versions of Windows like Windows IoT or Windows Nano Server not all codepages are present
|
||||
def is_code_page_present(cp):
|
||||
from ctypes import POINTER, WINFUNCTYPE, WinDLL
|
||||
from ctypes import POINTER, WINFUNCTYPE, WinDLL, Structure
|
||||
from ctypes.wintypes import BOOL, BYTE, WCHAR, UINT, DWORD
|
||||
|
||||
MAX_LEADBYTES = 12 # 5 ranges, 2 bytes ea., 0 term.
|
||||
MAX_DEFAULTCHAR = 2 # single or double byte
|
||||
MAX_PATH = 260
|
||||
class CPINFOEXW(ctypes.Structure):
|
||||
class CPINFOEXW(Structure):
|
||||
_fields_ = [("MaxCharSize", UINT),
|
||||
("DefaultChar", BYTE*MAX_DEFAULTCHAR),
|
||||
("LeadByte", BYTE*MAX_LEADBYTES),
|
||||
|
@ -719,19 +719,19 @@ class UTF16Test(ReadTest, unittest.TestCase):
|
|||
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_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(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(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))
|
||||
|
||||
|
@ -1844,9 +1844,9 @@ class CodecsModuleTest(unittest.TestCase):
|
|||
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')
|
||||
|
@ -1863,7 +1863,7 @@ class CodecsModuleTest(unittest.TestCase):
|
|||
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_no_warn(os_helper.TESTFN, 'wt', 'invalid-encoding')
|
||||
|
||||
file().close.assert_called()
|
||||
|
||||
|
@ -2883,7 +2883,7 @@ class BomTest(unittest.TestCase):
|
|||
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_no_warn(os_helper.TESTFN, 'w+', encoding=encoding) as f:
|
||||
f.write(data)
|
||||
f.write(data)
|
||||
f.seek(0)
|
||||
|
@ -2892,7 +2892,7 @@ class BomTest(unittest.TestCase):
|
|||
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_no_warn(os_helper.TESTFN, 'w+', encoding=encoding) as f:
|
||||
f.write(data[0])
|
||||
self.assertNotEqual(f.tell(), 0)
|
||||
f.seek(0)
|
||||
|
@ -2901,7 +2901,7 @@ class BomTest(unittest.TestCase):
|
|||
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_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)
|
||||
|
@ -2911,7 +2911,7 @@ class BomTest(unittest.TestCase):
|
|||
|
||||
# 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_no_warn(os_helper.TESTFN, 'w+', encoding=encoding) as f:
|
||||
f.write(data)
|
||||
f.seek(f.tell())
|
||||
f.write(data)
|
||||
|
@ -2920,7 +2920,7 @@ class BomTest(unittest.TestCase):
|
|||
|
||||
# (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_no_warn(os_helper.TESTFN, 'w+', encoding=encoding) as f:
|
||||
f.writer.write(data)
|
||||
f.writer.seek(f.writer.tell())
|
||||
f.writer.write(data)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue