classification
Title: email.Header encode() unicode P2.6
Type: behavior Stage: test needed
Components: Library (Lib) Versions: Python 2.6
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: barry Nosy List: barry, xnovakj (2)
Priority: normal Keywords

Created on 2005-12-13 11:09 by xnovakj, last changed 2009-03-23 10:12 by xnovakj.

Messages (2)
msg27063 - (view) Author: Jan Novak (xnovakj) Date: 2005-12-13 11:09
Python: 2.4
Module: email.Header
Method: encode()
In some cases returns unicode (example on line 5)

1>> from email.Header import Header

2>> Header(unicode('abcá','iso-8859-2'),'utf-8').encode()
'=?utf-8?b?YWJjw6E=?='

3>> Header('abc','utf-8').encode()
'=?utf-8?q?abc?='

4>> Header(u'abc','utf-8').encode()
'abc' ???

5>> Header('abc','iso-8859-2').encode()
u'=?iso-8859-2?q?abc?=' (P2.4)

6>> Header('abc','iso-8859-2').encode()
'=?iso-8859-2?q?abc?=' (P2.3)
msg84003 - (view) Author: Jan Novak (xnovakj) Date: 2009-03-23 10:12
I made some new tests in P2.6.1

>>> import email.charset

>>> c=email.charset.Charset('utf-8')
>>> print c.input_charset, type(c.input_charset)
utf-8 <type 'unicode'>
>>> print c.output_charset, type(c.output_charset)
utf-8 <type 'str'>

but

>>> c=email.charset.Charset('iso-8859-2')
>>> print c.input_charset, type(c.input_charset)
iso-8859-2 <type 'unicode'>
>>> print c.output_charset, type(c.output_charset)
iso-8859-2 <type 'unicode'>

but if you use alias latin-2 it's OK

>>> c=email.charset.Charset('latin-2')
>>> print c.input_charset, type(c.input_charset)
iso-8859-2 <type 'str'>
>>> print c.output_charset, type(c.output_charset)
iso-8859-2 <type 'str'>
>>> 

Error is here for unicode input-charset:
self.input_charset->conv->self.output_charset

module email/charset.py line 219

        if not conv:
            conv = self.input_charset

for the charsets where aren't output conversions

CHARSETS = {
    # input        header enc  body enc output conv
    'iso-8859-1':  (QP,        QP,      None),
    'iso-8859-2':  (QP,        QP,      None),

and if you don't use alias

ALIASES = {
    'latin_1': 'iso-8859-1',
    'latin-1': 'iso-8859-1',
    'latin_2': 'iso-8859-2',
    'latin-2': 'iso-8859-2',

But the realy source of this error is on line 208
 input_charset = unicode(input_charset, 'ascii')

because this construction returns unicode

>>> print type(unicode('iso-8859-2','ascii'))
<type 'unicode'>
History
Date User Action Args
2009-03-30 22:56:23ajaksu2linkissue1685453 dependencies
2009-03-23 10:12:25xnovakjsetmessages: + msg84003
title: email.Header encode() unicode P2.3xP2.4 -> email.Header encode() unicode P2.6
2009-03-20 23:31:10ajaksu2setstage: test needed
type: behavior
components: + Library (Lib), - None
versions: + Python 2.6
2005-12-13 11:09:30xnovakjcreate