8
8
from unittest import mock
9
9
10
10
from test import support
11
- from test .support import os_helper
11
+ from test .support import os_helper , warnings_helper
12
12
13
13
try :
14
14
import _testcapi
@@ -2711,6 +2711,11 @@ def test_seek0(self):
2711
2711
"rot_13" : ["rot13" ],
2712
2712
}
2713
2713
2714
+ deprecated_transforms = {
2715
+ "uu_codec" ,
2716
+ }
2717
+
2718
+
2714
2719
try :
2715
2720
import zlib
2716
2721
except ImportError :
@@ -2727,33 +2732,45 @@ def test_seek0(self):
2727
2732
transform_aliases ["bz2_codec" ] = ["bz2" ]
2728
2733
2729
2734
2735
+ def _check_transform_warning (encoding ):
2736
+ """Helper to check that deprecated transforms warn and silence them."""
2737
+ if encoding not in deprecated_transforms :
2738
+ return contextlib .nullcontext ()
2739
+ return warnings_helper .check_warnings (
2740
+ (f".*({ encoding } ).*" , DeprecationWarning ))
2741
+
2742
+
2730
2743
class TransformCodecTest (unittest .TestCase ):
2731
2744
2732
2745
def test_basics (self ):
2733
2746
binput = bytes (range (256 ))
2734
2747
for encoding in bytes_transform_encodings :
2735
2748
with self .subTest (encoding = encoding ):
2736
2749
# generic codecs interface
2737
- (o , size ) = codecs .getencoder (encoding )(binput )
2750
+ with _check_transform_warning (encoding ):
2751
+ (o , size ) = codecs .getencoder (encoding )(binput )
2738
2752
self .assertEqual (size , len (binput ))
2739
- (i , size ) = codecs .getdecoder (encoding )(o )
2753
+ with _check_transform_warning (encoding ):
2754
+ (i , size ) = codecs .getdecoder (encoding )(o )
2740
2755
self .assertEqual (size , len (o ))
2741
2756
self .assertEqual (i , binput )
2742
2757
2743
2758
def test_read (self ):
2744
2759
for encoding in bytes_transform_encodings :
2745
2760
with self .subTest (encoding = encoding ):
2746
- sin = codecs .encode (b"\x80 " , encoding )
2747
- reader = codecs .getreader (encoding )(io .BytesIO (sin ))
2748
- sout = reader .read ()
2761
+ with _check_transform_warning (encoding ):
2762
+ sin = codecs .encode (b"\x80 " , encoding )
2763
+ reader = codecs .getreader (encoding )(io .BytesIO (sin ))
2764
+ sout = reader .read ()
2749
2765
self .assertEqual (sout , b"\x80 " )
2750
2766
2751
2767
def test_readline (self ):
2752
2768
for encoding in bytes_transform_encodings :
2753
2769
with self .subTest (encoding = encoding ):
2754
- sin = codecs .encode (b"\x80 " , encoding )
2755
- reader = codecs .getreader (encoding )(io .BytesIO (sin ))
2756
- sout = reader .readline ()
2770
+ with _check_transform_warning (encoding ):
2771
+ sin = codecs .encode (b"\x80 " , encoding )
2772
+ reader = codecs .getreader (encoding )(io .BytesIO (sin ))
2773
+ sout = reader .readline ()
2757
2774
self .assertEqual (sout , b"\x80 " )
2758
2775
2759
2776
def test_buffer_api_usage (self ):
@@ -2765,13 +2782,16 @@ def test_buffer_api_usage(self):
2765
2782
with self .subTest (encoding = encoding ):
2766
2783
data = original
2767
2784
view = memoryview (data )
2768
- data = codecs .encode (data , encoding )
2769
- view_encoded = codecs .encode (view , encoding )
2785
+ with _check_transform_warning (encoding ):
2786
+ data = codecs .encode (data , encoding )
2787
+ view_encoded = codecs .encode (view , encoding )
2770
2788
self .assertEqual (view_encoded , data )
2771
2789
view = memoryview (data )
2772
- data = codecs .decode (data , encoding )
2790
+ with _check_transform_warning (encoding ):
2791
+ data = codecs .decode (data , encoding )
2773
2792
self .assertEqual (data , original )
2774
- view_decoded = codecs .decode (view , encoding )
2793
+ with _check_transform_warning (encoding ):
2794
+ view_decoded = codecs .decode (view , encoding )
2775
2795
self .assertEqual (view_decoded , data )
2776
2796
2777
2797
def test_text_to_binary_denylists_binary_transforms (self ):
@@ -2799,7 +2819,8 @@ def test_binary_to_text_denylists_binary_transforms(self):
2799
2819
data = b"encode first to ensure we meet any format restrictions"
2800
2820
for encoding in bytes_transform_encodings :
2801
2821
with self .subTest (encoding = encoding ):
2802
- encoded_data = codecs .encode (data , encoding )
2822
+ with _check_transform_warning (encoding ):
2823
+ encoded_data = codecs .encode (data , encoding )
2803
2824
fmt = (r"{!r} is not a text encoding; "
2804
2825
r"use codecs.decode\(\) to handle arbitrary codecs" )
2805
2826
msg = fmt .format (encoding )
@@ -2857,7 +2878,8 @@ def test_quopri_stateless(self):
2857
2878
2858
2879
def test_uu_invalid (self ):
2859
2880
# Missing "begin" line
2860
- self .assertRaises (ValueError , codecs .decode , b"" , "uu-codec" )
2881
+ with _check_transform_warning ("uu_codec" ):
2882
+ self .assertRaises (ValueError , codecs .decode , b"" , "uu-codec" )
2861
2883
2862
2884
2863
2885
# The codec system tries to wrap exceptions in order to ensure the error
0 commit comments