Skip to content

gh-62040: Raise ValueError on invalid *errors* argument in several codecs #136611

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Remaining asserts
  • Loading branch information
StanFromIreland committed Jul 24, 2025
commit f7d77c48413801499e5b431ee2e3a32de140e226
16 changes: 10 additions & 6 deletions Lib/encodings/base64_codec.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,20 @@
import codecs
import base64

### Codec APIs
### Codec Helpers

def base64_encode(input, errors='strict'):
def _assert_strict(errors):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe _check_strict from the original patch would be better?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer assert_strict since it is closer to what was there before, and it is more obvious, to me at least, that it will fail if errors != 'strict', whereas check_strict, seems like something that will enable more verbose errors or the like.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO, it's not fully correct to name method assert_something and don't make any assert checks or raising an AssertionError.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@malemburg which do you prefer? _check_strict or _assert_strict

if errors != 'strict':
raise ValueError(f'Unsupported error handling mode: "{errors}" - must be "strict"')

### Codec APIs

def base64_encode(input, errors='strict'):
_assert_strict(errors)
return (base64.encodebytes(input), len(input))

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

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

class IncrementalEncoder(codecs.IncrementalEncoder):
def encode(self, input, final=False):
assert self.errors == 'strict'
_assert_strict(self.errors)
return base64.encodebytes(input)

class IncrementalDecoder(codecs.IncrementalDecoder):
def decode(self, input, final=False):
assert self.errors == 'strict'
_assert_strict(self.errors)
return base64.decodebytes(input)

class StreamWriter(Codec, codecs.StreamWriter):
Expand Down
16 changes: 10 additions & 6 deletions Lib/encodings/hex_codec.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,20 @@
import codecs
import binascii

### Codec APIs
### Codec Helpers

def hex_encode(input, errors='strict'):
def _assert_strict(errors):
if errors != 'strict':
raise ValueError(f'Unsupported error handling mode: "{errors}" - must be "strict"')

### Codec APIs

def hex_encode(input, errors='strict'):
_assert_strict(errors)
return (binascii.b2a_hex(input), len(input))

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

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

class IncrementalEncoder(codecs.IncrementalEncoder):
def encode(self, input, final=False):
assert self.errors == 'strict'
_assert_strict(self.errors)
return binascii.b2a_hex(input)

class IncrementalDecoder(codecs.IncrementalDecoder):
def decode(self, input, final=False):
assert self.errors == 'strict'
_assert_strict(self.errors)
return binascii.a2b_hex(input)

class StreamWriter(Codec, codecs.StreamWriter):
Expand Down
12 changes: 9 additions & 3 deletions Lib/encodings/quopri_codec.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,23 @@
import quopri
from io import BytesIO

def quopri_encode(input, errors='strict'):
### Codec Helpers

def _assert_strict(errors):
if errors != 'strict':
raise ValueError(f'Unsupported error handling mode: "{errors}" - must be "strict"')

### Codec APIs

def quopri_encode(input, errors='strict'):
_assert_strict(errors)
f = BytesIO(input)
g = BytesIO()
quopri.encode(f, g, quotetabs=True)
return (g.getvalue(), len(input))

def quopri_decode(input, errors='strict'):
if errors != 'strict':
raise ValueError(f'Unsupported error handling mode: "{errors}" - must be "strict"')
_assert_strict(errors)
f = BytesIO(input)
g = BytesIO()
quopri.decode(f, g)
Expand Down
12 changes: 8 additions & 4 deletions Lib/encodings/uu_codec.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,16 @@
import binascii
from io import BytesIO

### Codec APIs
### Codec Helpers

def uu_encode(input, errors='strict', filename='<data>', mode=0o666):
def _assert_strict(errors):
if errors != 'strict':
raise ValueError(f'Unsupported error handling mode: "{errors}" - must be "strict"')

### Codec APIs

def uu_encode(input, errors='strict', filename='<data>', mode=0o666):
_assert_strict(errors)
infile = BytesIO(input)
outfile = BytesIO()
read = infile.read
Expand All @@ -36,8 +41,7 @@ def uu_encode(input, errors='strict', filename='<data>', mode=0o666):
return (outfile.getvalue(), len(input))

def uu_decode(input, errors='strict'):
if errors != 'strict':
raise ValueError(f'Unsupported error handling mode: "{errors}" - must be "strict"')
_assert_strict(errors)
infile = BytesIO(input)
outfile = BytesIO()
readline = infile.readline
Expand Down
3 changes: 2 additions & 1 deletion Lib/test/test_http_cookies.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,8 +214,9 @@ def test_set_secure_httponly_partitioned_attrs(self):
'Set-Cookie: Customer="WILE_E_COYOTE"; HttpOnly; Partitioned; Secure')

def test_samesite_attrs(self):
samesite_values = ['Strict', 'Lax', 'strict', 'lax']
samesite_values = ['Strict', 'Lax', 'strict', 'lax', 'None', 'none', 'asdasd']
for val in samesite_values:
print(val)
with self.subTest(val=val):
C = cookies.SimpleCookie('Customer="WILE_E_COYOTE"')
C['Customer']['samesite'] = val
Expand Down
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy