summaryrefslogtreecommitdiffstats
path: root/Lib/email
diff options
context:
space:
mode:
authorR. David Murray <rdmurray@bitdance.com>2010-06-04 19:51:06 (GMT)
committerR. David Murray <rdmurray@bitdance.com>2010-06-04 19:51:06 (GMT)
commite7e505ba6e2ba1768ba81e9dde652b6aff34c386 (patch)
tree34592df5dc9e94b983f36d379bbcc26a80488054 /Lib/email
parenteba67c0eac27cfafb779372ef82de09aefcca262 (diff)
downloadcpython-e7e505ba6e2ba1768ba81e9dde652b6aff34c386.zip
cpython-e7e505ba6e2ba1768ba81e9dde652b6aff34c386.tar.gz
cpython-e7e505ba6e2ba1768ba81e9dde652b6aff34c386.tar.bz2
#4487: have Charset check with codecs for possible aliases.
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.
Diffstat (limited to 'Lib/email')
-rw-r--r--Lib/email/charset.py8
-rw-r--r--Lib/email/test/test_email.py3
2 files changed, 10 insertions, 1 deletions
diff --git a/Lib/email/charset.py b/Lib/email/charset.py
index 9bebf6f..ad56c58 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 94eec86..4ce9848 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.