Skip to content

Commit f7d77c4

Browse files
Remaining asserts
1 parent cb8197a commit f7d77c4

File tree

5 files changed

+39
-20
lines changed

5 files changed

+39
-20
lines changed

Lib/encodings/base64_codec.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,20 @@
88
import codecs
99
import base64
1010

11-
### Codec APIs
11+
### Codec Helpers
1212

13-
def base64_encode(input, errors='strict'):
13+
def _assert_strict(errors):
1414
if errors != 'strict':
1515
raise ValueError(f'Unsupported error handling mode: "{errors}" - must be "strict"')
16+
17+
### Codec APIs
18+
19+
def base64_encode(input, errors='strict'):
20+
_assert_strict(errors)
1621
return (base64.encodebytes(input), len(input))
1722

1823
def base64_decode(input, errors='strict'):
19-
if errors != 'strict':
20-
raise ValueError(f'Unsupported error handling mode: "{errors}" - must be "strict"')
24+
_assert_strict(errors)
2125
return (base64.decodebytes(input), len(input))
2226

2327
class Codec(codecs.Codec):
@@ -28,12 +32,12 @@ def decode(self, input, errors='strict'):
2832

2933
class IncrementalEncoder(codecs.IncrementalEncoder):
3034
def encode(self, input, final=False):
31-
assert self.errors == 'strict'
35+
_assert_strict(self.errors)
3236
return base64.encodebytes(input)
3337

3438
class IncrementalDecoder(codecs.IncrementalDecoder):
3539
def decode(self, input, final=False):
36-
assert self.errors == 'strict'
40+
_assert_strict(self.errors)
3741
return base64.decodebytes(input)
3842

3943
class StreamWriter(Codec, codecs.StreamWriter):

Lib/encodings/hex_codec.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,20 @@
88
import codecs
99
import binascii
1010

11-
### Codec APIs
11+
### Codec Helpers
1212

13-
def hex_encode(input, errors='strict'):
13+
def _assert_strict(errors):
1414
if errors != 'strict':
1515
raise ValueError(f'Unsupported error handling mode: "{errors}" - must be "strict"')
16+
17+
### Codec APIs
18+
19+
def hex_encode(input, errors='strict'):
20+
_assert_strict(errors)
1621
return (binascii.b2a_hex(input), len(input))
1722

1823
def hex_decode(input, errors='strict'):
19-
if errors != 'strict':
20-
raise ValueError(f'Unsupported error handling mode: "{errors}" - must be "strict"')
24+
_assert_strict(errors)
2125
return (binascii.a2b_hex(input), len(input))
2226

2327
class Codec(codecs.Codec):
@@ -28,12 +32,12 @@ def decode(self, input, errors='strict'):
2832

2933
class IncrementalEncoder(codecs.IncrementalEncoder):
3034
def encode(self, input, final=False):
31-
assert self.errors == 'strict'
35+
_assert_strict(self.errors)
3236
return binascii.b2a_hex(input)
3337

3438
class IncrementalDecoder(codecs.IncrementalDecoder):
3539
def decode(self, input, final=False):
36-
assert self.errors == 'strict'
40+
_assert_strict(self.errors)
3741
return binascii.a2b_hex(input)
3842

3943
class StreamWriter(Codec, codecs.StreamWriter):

Lib/encodings/quopri_codec.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,23 @@
77
import quopri
88
from io import BytesIO
99

10-
def quopri_encode(input, errors='strict'):
10+
### Codec Helpers
11+
12+
def _assert_strict(errors):
1113
if errors != 'strict':
1214
raise ValueError(f'Unsupported error handling mode: "{errors}" - must be "strict"')
15+
16+
### Codec APIs
17+
18+
def quopri_encode(input, errors='strict'):
19+
_assert_strict(errors)
1320
f = BytesIO(input)
1421
g = BytesIO()
1522
quopri.encode(f, g, quotetabs=True)
1623
return (g.getvalue(), len(input))
1724

1825
def quopri_decode(input, errors='strict'):
19-
if errors != 'strict':
20-
raise ValueError(f'Unsupported error handling mode: "{errors}" - must be "strict"')
26+
_assert_strict(errors)
2127
f = BytesIO(input)
2228
g = BytesIO()
2329
quopri.decode(f, g)

Lib/encodings/uu_codec.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,16 @@
1111
import binascii
1212
from io import BytesIO
1313

14-
### Codec APIs
14+
### Codec Helpers
1515

16-
def uu_encode(input, errors='strict', filename='<data>', mode=0o666):
16+
def _assert_strict(errors):
1717
if errors != 'strict':
1818
raise ValueError(f'Unsupported error handling mode: "{errors}" - must be "strict"')
19+
20+
### Codec APIs
21+
22+
def uu_encode(input, errors='strict', filename='<data>', mode=0o666):
23+
_assert_strict(errors)
1924
infile = BytesIO(input)
2025
outfile = BytesIO()
2126
read = infile.read
@@ -36,8 +41,7 @@ def uu_encode(input, errors='strict', filename='<data>', mode=0o666):
3641
return (outfile.getvalue(), len(input))
3742

3843
def uu_decode(input, errors='strict'):
39-
if errors != 'strict':
40-
raise ValueError(f'Unsupported error handling mode: "{errors}" - must be "strict"')
44+
_assert_strict(errors)
4145
infile = BytesIO(input)
4246
outfile = BytesIO()
4347
readline = infile.readline

Lib/test/test_http_cookies.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,8 +214,9 @@ def test_set_secure_httponly_partitioned_attrs(self):
214214
'Set-Cookie: Customer="WILE_E_COYOTE"; HttpOnly; Partitioned; Secure')
215215

216216
def test_samesite_attrs(self):
217-
samesite_values = ['Strict', 'Lax', 'strict', 'lax']
217+
samesite_values = ['Strict', 'Lax', 'strict', 'lax', 'None', 'none', 'asdasd']
218218
for val in samesite_values:
219+
print(val)
219220
with self.subTest(val=val):
220221
C = cookies.SimpleCookie('Customer="WILE_E_COYOTE"')
221222
C['Customer']['samesite'] = val

0 commit comments

Comments
 (0)