Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Prev Previous commit
Next Next commit
Review
  • Loading branch information
StanFromIreland committed Nov 1, 2025
commit 9d6f06e00ebe87ed5c163e37ce12287c80a8071b
3 changes: 1 addition & 2 deletions Lib/email/_header_value_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -796,14 +796,13 @@ def params(self):
value = urllib.parse.unquote(value, encoding='latin-1')
else:
try:
charset = utils._sanitize_charset_name(charset, 'ascii')
value = value.decode(charset, 'surrogateescape')
except (LookupError, UnicodeEncodeError):
# XXX: there should really be a custom defect for
# unknown character set to make it easy to find,
# because otherwise unknown charset is a silent
# failure.
value = value.decode('ascii', 'surrogateescape')
value = value.decode('us-ascii', 'surrogateescape')
if utils._has_surrogates(value):
param.defects.append(errors.UndecodableBytesDefect())
value_parts.append(value)
Expand Down
11 changes: 1 addition & 10 deletions Lib/email/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -446,16 +446,8 @@ def decode_params(params):
new_params.append((name, '"%s"' % value))
return new_params

_SANITIZE_TABLE = str.maketrans({i: None for i in range(128, 65536)})

def _sanitize_charset_name(charset, fallback_charset):
if not charset:
return charset
sanitized = charset.translate(_SANITIZE_TABLE)
return sanitized if sanitized else fallback_charset

def collapse_rfc2231_value(value, errors='replace',
fallback_charset='ascii'):
fallback_charset='us-ascii'):
if not isinstance(value, tuple) or len(value) != 3:
return unquote(value)
# While value comes to us as a unicode string, we need it to be a bytes
Expand All @@ -466,7 +458,6 @@ 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)
Expand Down
5 changes: 4 additions & 1 deletion Lib/test/test_email/test_email.py
Original file line number Diff line number Diff line change
Expand Up @@ -5717,7 +5717,10 @@ def test_rfc2231_bad_character_in_encoding(self):

"""
msg = email.message_from_string(m)
self.assertEqual(msg.get_filename(), 'myfile.txt')
import warnings
with warnings.catch_warnings():
warnings.simplefilter("ignore", DeprecationWarning)
self.assertEqual(msg.get_filename(), 'myfile.txt')
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.

@bitdancer It was indeed tested.

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.

Ah, I missed that. I guess maybe warnings weren't enabled the way I ran the tests, though I thought I was doing so...

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.

What do you want to do, keep it like so or revert to my backwards-compatible approach?

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 believe I've confirmed that the tests will still pass after lookup starts raising a ValueError, which is what I was hoping would happen. I wonder if we should actually assert the deprecation warning here, though, so that we are reminded to remove the check when the deprecation turns into reality. I'm fine with whatever is standard practice for such cases, though.

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.

Converted to asserts.


def test_rfc2231_single_tick_in_filename_extended(self):
eq = self.assertEqual
Expand Down
Loading