Skip to content

Commit cb8197a

Browse files
Raise valueerrors
1 parent 609d5ad commit cb8197a

File tree

8 files changed

+52
-16
lines changed

8 files changed

+52
-16
lines changed

Lib/encodings/base64_codec.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,13 @@
1111
### Codec APIs
1212

1313
def base64_encode(input, errors='strict'):
14-
assert errors == 'strict'
14+
if errors != 'strict':
15+
raise ValueError(f'Unsupported error handling mode: "{errors}" - must be "strict"')
1516
return (base64.encodebytes(input), len(input))
1617

1718
def base64_decode(input, errors='strict'):
18-
assert errors == 'strict'
19+
if errors != 'strict':
20+
raise ValueError(f'Unsupported error handling mode: "{errors}" - must be "strict"')
1921
return (base64.decodebytes(input), len(input))
2022

2123
class Codec(codecs.Codec):

Lib/encodings/bz2_codec.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,20 @@
1010
import codecs
1111
import bz2 # this codec needs the optional bz2 module !
1212

13+
### Codec Helpers
14+
15+
def _assert_strict(errors):
16+
if errors != 'strict':
17+
raise ValueError(f'Unsupported error handling mode: "{errors}" - must be "strict"')
18+
1319
### Codec APIs
1420

1521
def bz2_encode(input, errors='strict'):
16-
assert errors == 'strict'
22+
_assert_strict(errors)
1723
return (bz2.compress(input), len(input))
1824

1925
def bz2_decode(input, errors='strict'):
20-
assert errors == 'strict'
26+
_assert_strict(errors)
2127
return (bz2.decompress(input), len(input))
2228

2329
class Codec(codecs.Codec):
@@ -28,7 +34,7 @@ def decode(self, input, errors='strict'):
2834

2935
class IncrementalEncoder(codecs.IncrementalEncoder):
3036
def __init__(self, errors='strict'):
31-
assert errors == 'strict'
37+
_assert_strict(errors)
3238
self.errors = errors
3339
self.compressobj = bz2.BZ2Compressor()
3440

@@ -44,7 +50,7 @@ def reset(self):
4450

4551
class IncrementalDecoder(codecs.IncrementalDecoder):
4652
def __init__(self, errors='strict'):
47-
assert errors == 'strict'
53+
_assert_strict(errors)
4854
self.errors = errors
4955
self.decompressobj = bz2.BZ2Decompressor()
5056

Lib/encodings/hex_codec.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,13 @@
1111
### Codec APIs
1212

1313
def hex_encode(input, errors='strict'):
14-
assert errors == 'strict'
14+
if errors != 'strict':
15+
raise ValueError(f'Unsupported error handling mode: "{errors}" - must be "strict"')
1516
return (binascii.b2a_hex(input), len(input))
1617

1718
def hex_decode(input, errors='strict'):
18-
assert errors == 'strict'
19+
if errors != 'strict':
20+
raise ValueError(f'Unsupported error handling mode: "{errors}" - must be "strict"')
1921
return (binascii.a2b_hex(input), len(input))
2022

2123
class Codec(codecs.Codec):

Lib/encodings/quopri_codec.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,16 @@
88
from io import BytesIO
99

1010
def quopri_encode(input, errors='strict'):
11-
assert errors == 'strict'
11+
if errors != 'strict':
12+
raise ValueError(f'Unsupported error handling mode: "{errors}" - must be "strict"')
1213
f = BytesIO(input)
1314
g = BytesIO()
1415
quopri.encode(f, g, quotetabs=True)
1516
return (g.getvalue(), len(input))
1617

1718
def quopri_decode(input, errors='strict'):
18-
assert errors == 'strict'
19+
if errors != 'strict':
20+
raise ValueError(f'Unsupported error handling mode: "{errors}" - must be "strict"')
1921
f = BytesIO(input)
2022
g = BytesIO()
2123
quopri.decode(f, g)

Lib/encodings/uu_codec.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
### Codec APIs
1515

1616
def uu_encode(input, errors='strict', filename='<data>', mode=0o666):
17-
assert errors == 'strict'
17+
if errors != 'strict':
18+
raise ValueError(f'Unsupported error handling mode: "{errors}" - must be "strict"')
1819
infile = BytesIO(input)
1920
outfile = BytesIO()
2021
read = infile.read
@@ -35,7 +36,8 @@ def uu_encode(input, errors='strict', filename='<data>', mode=0o666):
3536
return (outfile.getvalue(), len(input))
3637

3738
def uu_decode(input, errors='strict'):
38-
assert errors == 'strict'
39+
if errors != 'strict':
40+
raise ValueError(f'Unsupported error handling mode: "{errors}" - must be "strict"')
3941
infile = BytesIO(input)
4042
outfile = BytesIO()
4143
readline = infile.readline

Lib/encodings/zlib_codec.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,20 @@
88
import codecs
99
import zlib # this codec needs the optional zlib module !
1010

11+
### Codec Helpers
12+
13+
def _assert_strict(errors):
14+
if errors != 'strict':
15+
raise ValueError(f'Unsupported error handling mode: "{errors}" - must be "strict"')
16+
1117
### Codec APIs
1218

1319
def zlib_encode(input, errors='strict'):
14-
assert errors == 'strict'
20+
_assert_strict(errors)
1521
return (zlib.compress(input), len(input))
1622

1723
def zlib_decode(input, errors='strict'):
18-
assert errors == 'strict'
24+
_assert_strict(errors)
1925
return (zlib.decompress(input), len(input))
2026

2127
class Codec(codecs.Codec):
@@ -26,7 +32,7 @@ def decode(self, input, errors='strict'):
2632

2733
class IncrementalEncoder(codecs.IncrementalEncoder):
2834
def __init__(self, errors='strict'):
29-
assert errors == 'strict'
35+
_assert_strict(errors)
3036
self.errors = errors
3137
self.compressobj = zlib.compressobj()
3238

@@ -42,7 +48,7 @@ def reset(self):
4248

4349
class IncrementalDecoder(codecs.IncrementalDecoder):
4450
def __init__(self, errors='strict'):
45-
assert errors == 'strict'
51+
_assert_strict(errors)
4652
self.errors = errors
4753
self.decompressobj = zlib.decompressobj()
4854

Lib/test/test_codecs.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3128,6 +3128,18 @@ def test_uu_invalid(self):
31283128
# Missing "begin" line
31293129
self.assertRaises(ValueError, codecs.decode, b"", "uu-codec")
31303130

3131+
def test_invalid_error_input(self):
3132+
# decoders/encoders require errors == 'strict'
3133+
3134+
for encoding in bytes_transform_encodings:
3135+
with self.subTest(encoding=encoding):
3136+
encoder = codecs.getencoder(encoding)
3137+
decoder = codecs.getdecoder(encoding)
3138+
3139+
self.assertRaises(ValueError, encoder, 'in', errors='notstrict')
3140+
self.assertRaises(ValueError, decoder, 'in', errors='notstrict')
3141+
3142+
31313143

31323144
# The codec system tries to add notes to exceptions in order to ensure
31333145
# the error mentions the operation being performed and the codec involved.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
The ``base64_codec``, ``uu_codec``, ``quopri_codec``, ``hex_codec``,
2+
``zlib_codec`` and ``bz2_codec`` now raise a :exc:`ValueError` when their
3+
decoder/encoder is provided an *errors* parameter that is not equal to
4+
``'strict'``.

0 commit comments

Comments
 (0)