77import unittest
88import encodings
99from unittest import mock
10+ import warnings
1011
1112from test import support
1213from test .support import os_helper
2021except ImportError :
2122 _testinternalcapi = None
2223
23- try :
24- import ctypes
25- except ImportError :
26- ctypes = None
27- SIZEOF_WCHAR_T = - 1
28- else :
29- SIZEOF_WCHAR_T = ctypes .sizeof (ctypes .c_wchar )
24+
25+ def codecs_open_no_warn (* args , ** kwargs ):
26+ """Call codecs.open(*args, **kwargs) ignoring DeprecationWarning."""
27+ with warnings .catch_warnings ():
28+ warnings .simplefilter ("ignore" )
29+ return codecs .open (* args , ** kwargs )
3030
3131def coding_checker (self , coder ):
3232 def check (input , expect ):
@@ -35,13 +35,13 @@ def check(input, expect):
3535
3636# On small versions of Windows like Windows IoT or Windows Nano Server not all codepages are present
3737def is_code_page_present (cp ):
38- from ctypes import POINTER , WINFUNCTYPE , WinDLL
38+ from ctypes import POINTER , WINFUNCTYPE , WinDLL , Structure
3939 from ctypes .wintypes import BOOL , BYTE , WCHAR , UINT , DWORD
4040
4141 MAX_LEADBYTES = 12 # 5 ranges, 2 bytes ea., 0 term.
4242 MAX_DEFAULTCHAR = 2 # single or double byte
4343 MAX_PATH = 260
44- class CPINFOEXW (ctypes . Structure ):
44+ class CPINFOEXW (Structure ):
4545 _fields_ = [("MaxCharSize" , UINT ),
4646 ("DefaultChar" , BYTE * MAX_DEFAULTCHAR ),
4747 ("LeadByte" , BYTE * MAX_LEADBYTES ),
@@ -719,19 +719,19 @@ def test_bug691291(self):
719719 self .addCleanup (os_helper .unlink , os_helper .TESTFN )
720720 with open (os_helper .TESTFN , 'wb' ) as fp :
721721 fp .write (s )
722- with codecs . open (os_helper .TESTFN , 'r' ,
722+ with codecs_open_no_warn (os_helper .TESTFN , 'r' ,
723723 encoding = self .encoding ) as reader :
724724 self .assertEqual (reader .read (), s1 )
725725
726726 def test_invalid_modes (self ):
727727 for mode in ('U' , 'rU' , 'r+U' ):
728728 with self .assertRaises (ValueError ) as cm :
729- codecs . open (os_helper .TESTFN , mode , encoding = self .encoding )
729+ codecs_open_no_warn (os_helper .TESTFN , mode , encoding = self .encoding )
730730 self .assertIn ('invalid mode' , str (cm .exception ))
731731
732732 for mode in ('rt' , 'wt' , 'at' , 'r+t' ):
733733 with self .assertRaises (ValueError ) as cm :
734- codecs . open (os_helper .TESTFN , mode , encoding = self .encoding )
734+ codecs_open_no_warn (os_helper .TESTFN , mode , encoding = self .encoding )
735735 self .assertIn ("can't have text and binary mode at once" ,
736736 str (cm .exception ))
737737
@@ -1844,9 +1844,9 @@ def test_all(self):
18441844 def test_open (self ):
18451845 self .addCleanup (os_helper .unlink , os_helper .TESTFN )
18461846 for mode in ('w' , 'r' , 'r+' , 'w+' , 'a' , 'a+' ):
1847- with self .subTest (mode ), \
1848- codecs .open (os_helper .TESTFN , mode , 'ascii' ) as file :
1849- self .assertIsInstance (file , codecs .StreamReaderWriter )
1847+ with self .subTest (mode ), self . assertWarns ( DeprecationWarning ):
1848+ with codecs .open (os_helper .TESTFN , mode , 'ascii' ) as file :
1849+ self .assertIsInstance (file , codecs .StreamReaderWriter )
18501850
18511851 def test_undefined (self ):
18521852 self .assertRaises (UnicodeError , codecs .encode , 'abc' , 'undefined' )
@@ -1863,7 +1863,7 @@ def test_file_closes_if_lookup_error_raised(self):
18631863 mock_open = mock .mock_open ()
18641864 with mock .patch ('builtins.open' , mock_open ) as file :
18651865 with self .assertRaises (LookupError ):
1866- codecs . open (os_helper .TESTFN , 'wt' , 'invalid-encoding' )
1866+ codecs_open_no_warn (os_helper .TESTFN , 'wt' , 'invalid-encoding' )
18671867
18681868 file ().close .assert_called ()
18691869
@@ -2883,7 +2883,7 @@ def test_seek0(self):
28832883 self .addCleanup (os_helper .unlink , os_helper .TESTFN )
28842884 for encoding in tests :
28852885 # Check if the BOM is written only once
2886- with codecs . open (os_helper .TESTFN , 'w+' , encoding = encoding ) as f :
2886+ with codecs_open_no_warn (os_helper .TESTFN , 'w+' , encoding = encoding ) as f :
28872887 f .write (data )
28882888 f .write (data )
28892889 f .seek (0 )
@@ -2892,7 +2892,7 @@ def test_seek0(self):
28922892 self .assertEqual (f .read (), data * 2 )
28932893
28942894 # Check that the BOM is written after a seek(0)
2895- with codecs . open (os_helper .TESTFN , 'w+' , encoding = encoding ) as f :
2895+ with codecs_open_no_warn (os_helper .TESTFN , 'w+' , encoding = encoding ) as f :
28962896 f .write (data [0 ])
28972897 self .assertNotEqual (f .tell (), 0 )
28982898 f .seek (0 )
@@ -2901,7 +2901,7 @@ def test_seek0(self):
29012901 self .assertEqual (f .read (), data )
29022902
29032903 # (StreamWriter) Check that the BOM is written after a seek(0)
2904- with codecs . open (os_helper .TESTFN , 'w+' , encoding = encoding ) as f :
2904+ with codecs_open_no_warn (os_helper .TESTFN , 'w+' , encoding = encoding ) as f :
29052905 f .writer .write (data [0 ])
29062906 self .assertNotEqual (f .writer .tell (), 0 )
29072907 f .writer .seek (0 )
@@ -2911,7 +2911,7 @@ def test_seek0(self):
29112911
29122912 # Check that the BOM is not written after a seek() at a position
29132913 # different than the start
2914- with codecs . open (os_helper .TESTFN , 'w+' , encoding = encoding ) as f :
2914+ with codecs_open_no_warn (os_helper .TESTFN , 'w+' , encoding = encoding ) as f :
29152915 f .write (data )
29162916 f .seek (f .tell ())
29172917 f .write (data )
@@ -2920,7 +2920,7 @@ def test_seek0(self):
29202920
29212921 # (StreamWriter) Check that the BOM is not written after a seek()
29222922 # at a position different than the start
2923- with codecs . open (os_helper .TESTFN , 'w+' , encoding = encoding ) as f :
2923+ with codecs_open_no_warn (os_helper .TESTFN , 'w+' , encoding = encoding ) as f :
29242924 f .writer .write (data )
29252925 f .writer .seek (f .writer .tell ())
29262926 f .writer .write (data )
0 commit comments