-
-
Notifications
You must be signed in to change notification settings - Fork 34.5k
gh-136702: Deprecate passing non-ascii *encoding* (str) to encodings.normalize_encoding
#140030
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
Changes from 1 commit
c8fc658
5b50daa
95f2e65
fad52cd
3ac0804
9d6f06e
16697dc
e4036f8
b8fc5f4
7592af8
8c59899
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -446,6 +446,15 @@ def decode_params(params): | |
| new_params.append((name, '"%s"' % value)) | ||
| return new_params | ||
|
|
||
| def _sanitize_charset_name(charset, fallback_charset): | ||
| if not charset: | ||
| return charset | ||
| sanitized = ''.join( | ||
| c for c in charset | ||
| if (ord(c) < 0xDC80 or ord(c) > 0xDCFF) and c.isascii() | ||
| ) | ||
|
StanFromIreland marked this conversation as resolved.
Outdated
|
||
| return sanitized if sanitized else fallback_charset | ||
|
|
||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is the trigger for this change? Do I actually have a test that uses a non-ascii charset name? If I did it should be an error case, since non-ascii is not permitted in charset names per the RFCs. I'm surprised I don't appear to be registering a defect for that, though I didn't go through the code enough to be sure I don't ;) Regardless it isn't clear to me that 'sanitizing' is a useful operation. It isn't likely to produce a valid charset name, we should just be falling back to ascii at that point. What led you to choose this approach?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is currently done by
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK. emal doesn't call lookup directly and no tests fail without the changes. I presume you did this to preserve backward compatibility. Unless I'm missing something, I don't think we should bother to do that. Given a non-ascii charset name, there are two possible outcomes from the current code: the name after sanitizing is not a valid codec name, or it is. If it is valid after sanitizing, there are two cases: the sanitized name results in successful decoding, or it does not. It is only the first of these second two cases that would be affected by the post-deprecation change. How often would that case occur in reality? I would guess it would be a vanishingly small number of cases, if it ever occurs at all. I think it will be better to remove the changes to the email package from this PR. If anyone sees the deprecation warning maybe they'll open an issue, but I'm betting nobody ever sees it from the email package. The behavior after the deprecation is over is the behavior we want: if the codec name contains non-ascii it is not a valid codec name, so any non-ascii in the text being decoded using that charset name will ultimately get turned into the 'unknown character' glyph when decoded by the email package.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Yes, I'm no email expert and I did not dig into the specifications, so I did this to not change any behaviour. I can remove it.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What't the conclusion here ? I still see the email package changes in place, but they look pretty harmless to me. |
||
| def collapse_rfc2231_value(value, errors='replace', | ||
| fallback_charset='us-ascii'): | ||
| if not isinstance(value, tuple) or len(value) != 3: | ||
|
|
@@ -458,6 +467,7 @@ def collapse_rfc2231_value(value, errors='replace', | |
| # Issue 17369: if charset/lang is None, decode_rfc2231 couldn't parse | ||
| # the value, so use the fallback_charset. | ||
| charset = fallback_charset | ||
| charset = _sanitize_charset_name(charset, fallback_charset) | ||
| rawbytes = bytes(text, 'raw-unicode-escape') | ||
| try: | ||
| return str(rawbytes, charset, errors) | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.