Skip to content

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

Open
StanFromIreland wants to merge 7 commits into
python:mainfrom
StanFromIreland:codecs-valueerrors
Open

gh-62040: Raise ValueError on invalid *errors* argument in several codecs#136611
StanFromIreland wants to merge 7 commits into
python:mainfrom
StanFromIreland:codecs-valueerrors

Conversation

@StanFromIreland

@StanFromIreland StanFromIreland commented Jul 13, 2025

Copy link
Copy Markdown
Member

@StanFromIreland StanFromIreland changed the title gh-62040: Raise ValueError on invalid *errors* parameter in several codecs gh-62040: Raise ValueError on invalid *errors* argument in several codecs Jul 13, 2025
@efimov-mikhail

Copy link
Copy Markdown
Member

I can see that there's only some changes from last patch:
https://bugs.python.org/file30902/issue17840.patch

For example, I'd like to remove also assert self.errors == 'strict', use _check_strict method in all files, test new changes.

@StanFromIreland

Copy link
Copy Markdown
Member Author

Ah I forgot about the patch and my grep was too strict, I'll fix all the self.errors cases.

@StanFromIreland
StanFromIreland marked this pull request as draft July 14, 2025 13:16
@StanFromIreland
StanFromIreland marked this pull request as ready for review July 24, 2025 08:22
Comment thread Lib/test/test_codecs.py Outdated
Comment thread Lib/test/test_codecs.py Outdated

### Codec Helpers

def _assert_strict(errors):

Copy link
Copy Markdown
Member

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
Copy Markdown
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
Copy Markdown
Member

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
Copy Markdown
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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I don't have a preference. Both are fine.

Comment thread Lib/test/test_codecs.py
@StanFromIreland

This comment was marked as outdated.

@StanFromIreland

Copy link
Copy Markdown
Member Author

Little ping @malemburg :-)

@github-actions

Copy link
Copy Markdown

This PR is stale because it has been open for 30 days with no activity.

@github-actions github-actions Bot added the stale Stale PR or inactive for long period of time. label May 15, 2026

### Codec Helpers

def _assert_strict(errors):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This function is duplicated in six modules. Since all of them already import codecs, would not it be better to add a shared implementation in codecs? Change the name to be clear that it asserts/checks that the error handler is strict only.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Agreed; this helper should live in codecs.py.


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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

"error handling mode" does not occur anywhere in the code or documentation. Only "error handling"/"error handler".

idna raises f"Unsupported error handling: {errors}". This is not a strong precedent, and ValueError is more appropriate than UnicodeError, but take this into account.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

"error handling parameter" would be clearer.

Comment thread Lib/test/test_codecs.py
Comment on lines +3138 to +3139
self.assertRaises(ValueError, encoder, 'in', errors='notstrict')
self.assertRaises(ValueError, decoder, 'in', errors='notstrict')

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Input should be bytes, not str.

@malemburg malemburg left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Please see my last review comment.


### Codec Helpers

def _assert_strict(errors):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I don't have a preference. Both are fine.


### Codec Helpers

def _assert_strict(errors):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Agreed; this helper should live in codecs.py.


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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

"error handling parameter" would be clearer.


def base64_encode(input, errors='strict'):
assert errors == 'strict'
_assert_strict(errors)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Hmm, now that I see where you are using the helper, I'm not so sure that this is the right approach anymore.

"assert" only works when Python is run without -O, so the helper does change semantics. I'm also worried that the extra function call will make things slower.

The change in semantics should be documented and I would prefer to have the checks inlined in all these cases. Yes, it's duplication, but those are only two lines of code and the error message could additionally include information about which encoding caused the problem.

@bedevere-app

bedevere-app Bot commented Aug 1, 2026

Copy link
Copy Markdown

When you're done making the requested changes, leave the comment: I have made the requested changes; please review again.

@serhiy-storchaka

Copy link
Copy Markdown
Member

Note that encoders cannot fail -- every input is valid. The error handler never used, even if it was supported. Why not simply remove assertions in encoders?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting changes stale Stale PR or inactive for long period of time.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants