diff --git a/Lib/email/charset.py b/Lib/email/charset.py index 9bebf6f3182..ad56c58f809 100644 --- a/Lib/email/charset.py +++ b/Lib/email/charset.py @@ -9,6 +9,7 @@ __all__ = [ 'add_codec', ] +import codecs import email.base64mime import email.quoprimime @@ -209,7 +210,12 @@ class Charset: except UnicodeError: raise errors.CharsetError(input_charset) input_charset = input_charset.lower() - # Set the input charset after filtering through the aliases + # Set the input charset after filtering through the aliases and/or codecs + if not (input_charset in ALIASES or input_charset in CHARSETS): + try: + input_charset = codecs.lookup(input_charset).name + except LookupError: + pass self.input_charset = ALIASES.get(input_charset, input_charset) # We can try to guess which encoding and conversion to use by the # charset_map dictionary. Try that first, but let the user override diff --git a/Lib/email/test/test_email.py b/Lib/email/test/test_email.py index 94eec86bde7..4ce98481369 100644 --- a/Lib/email/test/test_email.py +++ b/Lib/email/test/test_email.py @@ -2868,6 +2868,9 @@ class TestCharset(unittest.TestCase): self.assertEqual(str(charset), 'us-ascii') self.assertRaises(Errors.CharsetError, Charset, 'asc\xffii') + def test_codecs_aliases_accepted(self): + charset = Charset('utf8') + self.assertEqual(str(charset), 'utf-8') # Test multilingual MIME headers. diff --git a/Misc/NEWS b/Misc/NEWS index 0af21571d9f..f82f048bc47 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -46,6 +46,9 @@ C-API Library ------- +- Issue #4487: email now accepts as charset aliases all codec aliases + accepted by the codecs module. + - Issue #6470: Drop UNC prefix in FixTk. - Issue #5610: feedparser no longer eats extra characters at the end of