@@ -4970,6 +4970,128 @@ def tearDown(self):
49704970 except KeyError :
49714971 pass
49724972
4973+ def test_attributes (self ):
4974+ from email import charset
4975+ c = Charset ()
4976+ self .assertEqual (c .input_charset , 'us-ascii' )
4977+ self .assertEqual (c .header_encoding , None )
4978+ self .assertEqual (c .body_encoding , None )
4979+ self .assertEqual (c .output_charset , 'us-ascii' )
4980+ self .assertEqual (c .input_codec , None )
4981+ self .assertEqual (c .output_codec , None )
4982+
4983+ c = Charset ('us-ascii' )
4984+ self .assertEqual (c .input_charset , 'us-ascii' )
4985+ self .assertEqual (c .header_encoding , None )
4986+ self .assertEqual (c .body_encoding , None )
4987+ self .assertEqual (c .output_charset , 'us-ascii' )
4988+ self .assertEqual (c .input_codec , None )
4989+ self .assertEqual (c .output_codec , None )
4990+
4991+ c = Charset ('utf8' )
4992+ self .assertEqual (c .input_charset , 'utf-8' )
4993+ self .assertEqual (c .header_encoding , charset .SHORTEST )
4994+ self .assertEqual (c .body_encoding , charset .BASE64 )
4995+ self .assertEqual (c .output_charset , 'utf-8' )
4996+ self .assertEqual (c .input_codec , 'utf-8' )
4997+ self .assertEqual (c .output_codec , 'utf-8' )
4998+
4999+ c = Charset ('latin1' )
5000+ self .assertEqual (c .input_charset , 'iso-8859-1' )
5001+ self .assertEqual (c .header_encoding , charset .QP )
5002+ self .assertEqual (c .body_encoding , charset .QP )
5003+ self .assertEqual (c .output_charset , 'iso-8859-1' )
5004+ self .assertEqual (c .input_codec , 'iso-8859-1' )
5005+ self .assertEqual (c .output_codec , 'iso-8859-1' )
5006+
5007+ c = Charset ('latin9' )
5008+ self .assertEqual (c .input_charset , 'iso-8859-15' )
5009+ self .assertEqual (c .header_encoding , charset .QP )
5010+ self .assertEqual (c .body_encoding , charset .QP )
5011+ self .assertEqual (c .output_charset , 'iso-8859-15' )
5012+ self .assertEqual (c .input_codec , 'iso-8859-15' )
5013+ self .assertEqual (c .output_codec , 'iso-8859-15' )
5014+
5015+ c = Charset ('cyrillic' )
5016+ self .assertEqual (c .input_charset , 'iso-8859-5' )
5017+ self .assertEqual (c .header_encoding , charset .SHORTEST )
5018+ self .assertEqual (c .body_encoding , charset .BASE64 )
5019+ self .assertEqual (c .output_charset , 'iso-8859-5' )
5020+ self .assertEqual (c .input_codec , 'iso-8859-5' )
5021+ self .assertEqual (c .output_codec , 'iso-8859-5' )
5022+
5023+ c = Charset ('cp1251' )
5024+ self .assertEqual (c .input_charset , 'windows-1251' )
5025+ self .assertEqual (c .header_encoding , charset .SHORTEST )
5026+ self .assertEqual (c .body_encoding , charset .BASE64 )
5027+ self .assertEqual (c .output_charset , 'windows-1251' )
5028+ self .assertEqual (c .input_codec , 'windows-1251' )
5029+ self .assertEqual (c .output_codec , 'windows-1251' )
5030+
5031+ c = Charset ('cp1252' )
5032+ self .assertEqual (c .input_charset , 'windows-1252' )
5033+ self .assertEqual (c .header_encoding , charset .QP )
5034+ self .assertEqual (c .body_encoding , charset .QP )
5035+ self .assertEqual (c .output_charset , 'windows-1252' )
5036+ self .assertEqual (c .input_codec , 'windows-1252' )
5037+ self .assertEqual (c .output_codec , 'windows-1252' )
5038+
5039+ c = Charset ('eucjp' )
5040+ self .assertEqual (c .input_charset , 'euc-jp' )
5041+ self .assertEqual (c .header_encoding , charset .BASE64 )
5042+ self .assertEqual (c .body_encoding , None )
5043+ self .assertEqual (c .output_charset , 'iso-2022-jp' )
5044+ self .assertEqual (c .input_codec , 'euc-jp' )
5045+ self .assertEqual (c .output_codec , 'iso-2022-jp' )
5046+
5047+ c = Charset ('cp949' )
5048+ self .assertEqual (c .input_charset , 'ks_c_5601-1987' )
5049+ self .assertEqual (c .header_encoding , charset .SHORTEST )
5050+ self .assertEqual (c .body_encoding , charset .BASE64 )
5051+ self .assertEqual (c .output_charset , 'ks_c_5601-1987' )
5052+ self .assertEqual (c .input_codec , 'ks_c_5601-1987' )
5053+ self .assertEqual (c .output_codec , 'ks_c_5601-1987' )
5054+
5055+ c = Charset ('gb2312' )
5056+ self .assertEqual (c .input_charset , 'gb2312' )
5057+ self .assertEqual (c .header_encoding , charset .BASE64 )
5058+ self .assertEqual (c .body_encoding , charset .BASE64 )
5059+ self .assertEqual (c .output_charset , 'gb2312' )
5060+ self .assertEqual (c .input_codec , 'gb2312' )
5061+ self .assertEqual (c .output_codec , 'gb2312' )
5062+
5063+ c = Charset ('big5' )
5064+ self .assertEqual (c .input_charset , 'big5' )
5065+ self .assertEqual (c .header_encoding , charset .BASE64 )
5066+ self .assertEqual (c .body_encoding , charset .BASE64 )
5067+ self .assertEqual (c .output_charset , 'big5' )
5068+ self .assertEqual (c .input_codec , 'big5' )
5069+ self .assertEqual (c .output_codec , 'big5' )
5070+
5071+ def test_user_charsets (self ):
5072+ from email import charset
5073+ c = Charset ('fake0' )
5074+ self .assertEqual (c .input_charset , 'fake0' )
5075+ self .assertEqual (c .header_encoding , charset .SHORTEST )
5076+ self .assertEqual (c .body_encoding , charset .BASE64 )
5077+ self .assertEqual (c .output_charset , 'fake0' )
5078+ self .assertEqual (c .input_codec , 'fake0' )
5079+ self .assertEqual (c .output_codec , 'fake0' )
5080+
5081+ charset .add_alias ('fake1' , 'mime-fake' )
5082+ charset .add_alias ('output-mime-fake' , 'output-mime-fake-alias' )
5083+ charset .add_codec ('mime-fake' , 'fakecodec' )
5084+ charset .add_codec ('output-mime-fake-alias' , 'outputfakecodec' )
5085+ charset .add_charset ('mime-fake' , charset .QP , None , 'output-mime-fake' )
5086+
5087+ c = Charset ('fake1' )
5088+ self .assertEqual (c .input_charset , 'mime-fake' )
5089+ self .assertEqual (c .header_encoding , charset .QP )
5090+ self .assertEqual (c .body_encoding , None )
5091+ self .assertEqual (c .output_charset , 'output-mime-fake-alias' )
5092+ self .assertEqual (c .input_codec , 'fakecodec' )
5093+ self .assertEqual (c .output_codec , 'outputfakecodec' )
5094+
49735095 def test_codec_encodeable (self ):
49745096 eq = self .assertEqual
49755097 # Make sure us-ascii = no Unicode conversion
@@ -5010,6 +5132,11 @@ def test_unicode_charset_name(self):
50105132 self .assertEqual (str (charset ), 'us-ascii' )
50115133 self .assertRaises (errors .CharsetError , Charset , 'asc\xff ii' )
50125134
5135+ def test_bytes_charset_name (self ):
5136+ charset = Charset (b'us-ascii' )
5137+ self .assertEqual (str (charset ), 'us-ascii' )
5138+ self .assertRaises (errors .CharsetError , Charset , b'asc\xff ii' )
5139+
50135140
50145141
50155142# Test multilingual MIME headers.
0 commit comments