|
1 | | -# Copyright (C) 2001,2002 Python Software Foundation |
2 | | -# Author: che@debian.org (Ben Gertzfield), barry@zope.com (Barry Warsaw) |
| 1 | +# Copyright (C) 2001-2006 Python Software Foundation |
| 2 | +# Author: che@debian.org (Ben Gertzfield), barry@python.org (Barry Warsaw) |
3 | 3 |
|
4 | 4 | from types import UnicodeType |
5 | 5 | from email.Encoders import encode_7or8bit |
@@ -99,20 +99,13 @@ def _isunicode(s): |
99 | 99 | # of stability and useability. |
100 | 100 |
|
101 | 101 | CODEC_MAP = { |
102 | | - 'euc-jp': 'japanese.euc-jp', |
103 | | - 'iso-2022-jp': 'japanese.iso-2022-jp', |
104 | | - 'shift_jis': 'japanese.shift_jis', |
105 | | - 'euc-kr': 'korean.euc-kr', |
106 | | - 'ks_c_5601-1987': 'korean.cp949', |
107 | | - 'iso-2022-kr': 'korean.iso-2022-kr', |
108 | | - 'johab': 'korean.johab', |
109 | | - 'gb2132': 'eucgb2312_cn', |
110 | | - 'big5': 'big5_tw', |
111 | | - 'utf-8': 'utf-8', |
| 102 | + 'gb2132': 'eucgb2312_cn', |
| 103 | + 'big5': 'big5_tw', |
| 104 | + 'utf-8': 'utf-8', |
112 | 105 | # Hack: We don't want *any* conversion for stuff marked us-ascii, as all |
113 | 106 | # sorts of garbage might be sent to us in the guise of 7-bit us-ascii. |
114 | 107 | # Let that stuff pass through without conversion to/from Unicode. |
115 | | - 'us-ascii': None, |
| 108 | + 'us-ascii': None, |
116 | 109 | } |
117 | 110 |
|
118 | 111 |
|
@@ -165,6 +158,26 @@ def add_codec(charset, codecname): |
165 | 158 | CODEC_MAP[charset] = codecname |
166 | 159 |
|
167 | 160 |
|
| 161 | +def _find_asian_codec(charset, language): |
| 162 | + try: |
| 163 | + unicode('foo', charset) |
| 164 | + return charset |
| 165 | + except LookupError: |
| 166 | + try: |
| 167 | + codec = language + '.' + charset |
| 168 | + unicode('foo', codec) |
| 169 | + return codec |
| 170 | + except LookupError: |
| 171 | + return None |
| 172 | + |
| 173 | + |
| 174 | +for _charset in ('euc-jp', 'iso-2022-jp', 'shift_jis'): |
| 175 | + add_codec(_charset, _find_asian_codec(_charset, 'japanese') or _charset) |
| 176 | + |
| 177 | +for _charset in ('euc-kr', 'cp949', 'iso-2022-kr', 'johab'): |
| 178 | + add_codec(_charset, _find_asian_codec(_charset, 'korean') or _charset) |
| 179 | + |
| 180 | + |
168 | 181 |
|
169 | 182 | class Charset: |
170 | 183 | """Map character sets to their email properties. |
@@ -229,7 +242,7 @@ def __init__(self, input_charset=DEFAULT_CHARSET): |
229 | 242 | self.input_codec = CODEC_MAP.get(self.input_charset, |
230 | 243 | self.input_charset) |
231 | 244 | self.output_codec = CODEC_MAP.get(self.output_charset, |
232 | | - self.input_codec) |
| 245 | + self.input_codec) |
233 | 246 |
|
234 | 247 | def __str__(self): |
235 | 248 | return self.input_charset.lower() |
|
0 commit comments