]> granicus.if.org Git - python/commitdiff
#4487: have Charset check with codecs for possible aliases.
authorR. David Murray <rdmurray@bitdance.com>
Fri, 4 Jun 2010 19:51:06 +0000 (19:51 +0000)
committerR. David Murray <rdmurray@bitdance.com>
Fri, 4 Jun 2010 19:51:06 +0000 (19:51 +0000)
Previously, unexpected results occurred when email was passed, for example,
'utf8' as a charset name, since email would accept it but would *not* use
the 'utf-8' codec for it, even though Python itself recognises that as
an alias for utf-8.  Now Charset checks with codecs for aliases as well
as its own internal table.  Issue 8898 has been opened to change this
further in py3k so that all aliasing is routed through the codecs module.

Lib/email/charset.py
Lib/email/test/test_email.py
Misc/NEWS

index 9bebf6f3182f5870a2a3247f599e4e394fb6837f..ad56c58f809ffad09cb4d9eaba0857a2df74f94e 100644 (file)
@@ -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
index 94eec86bde7d1d4dbb93b205157a8e457a18b3c1..4ce98481369b2263d2edb2a9bb31ef1a170e646c 100644 (file)
@@ -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')
 
 \f
 # Test multilingual MIME headers.
index 0af21571d9f448e224f3204c56e04f8398cda9f2..f82f048bc47ce4ae0b5ad362856c291303d768d8 100644 (file)
--- 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