Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #40000

Re: stmplib MIMEText charset weirdness

Path csiph.com!usenet.pasdenom.info!news.albasani.net!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'output': 0.04; 'attribute': 0.05; 'none:': 0.05; 'try:': 0.07; 'base64': 0.09; 'bytes,': 0.09; 'charset': 0.09; 'encodes': 0.09; 'given,': 0.09; 'inherited': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'terry': 0.09; 'useless': 0.09; 'bug': 0.10; '3.3,': 0.16; '3.3.': 0.16; '8-bit': 0.16; 'adam': 0.16; 'lying.': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reedy': 0.16; 'wrote:': 0.17; 'jan': 0.18; 'appears': 0.18; 'assumes': 0.22; 'skip:_ 20': 0.22; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'skip:m 30': 0.26; 'skip:1 30': 0.27; 'fixed': 0.28; 'header:X-Complaints-To:1': 0.28; 'question:': 0.29; "skip:' 10": 0.30; 'skip:8 10': 0.32; 'to:addr:python-list': 0.33; "can't": 0.34; 'text': 0.34; 'pm,': 0.35; 'received:org': 0.36; 'except': 0.36; 'explain': 0.36; 'method': 0.36; 'test': 0.36; 'should': 0.36; 'available.': 0.37; 'being': 0.37; 'why': 0.37; 'subject:: ': 0.38; 'object': 0.38; 'to:addr:python.org': 0.39; 'header:Received:5': 0.40; 'your': 0.60; 'skip:u 10': 0.60; 'side': 0.61; 'provide': 0.62; 'due': 0.66; 'discover': 0.72; 'fact.': 0.84; 'received:fios.verizon.net': 0.84; 'text/html;': 0.84
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Terry Reedy <tjreedy@udel.edu>
Subject Re: stmplib MIMEText charset weirdness
Date Tue, 26 Feb 2013 14:46:14 -0500
References <fc332ca1-e77e-4cab-a96f-53b49e734407@googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding quoted-printable
X-Gmane-NNTP-Posting-Host pool-173-75-251-66.phlapa.fios.verizon.net
User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:17.0) Gecko/20130215 Thunderbird/17.0.3
In-Reply-To <fc332ca1-e77e-4cab-a96f-53b49e734407@googlegroups.com>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.2571.1361907991.2939.python-list@python.org> (permalink)
Lines 53
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1361907991 news.xs4all.nl 6971 [2001:888:2000:d::a6]:52272
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:40000

Show key headers only | View raw


On 2/25/2013 11:00 PM, Adam W. wrote:
> Can someone explain to me why I can't set the charset after the fact.

Email was revised to v.6 for 3.3, so the immediate answer to both your 
why questions is 'because email was not revised yet'.

> text = MIMEText('❤¥'.encode('utf-8'), 'html')

In 3.3 this fails immediately with
AttributeError: 'bytes' object has no attribute 'encode'
because when _charset is not given, MIMEText.__init__ test encodes to 
discover what it should be
         if _charset is None:
             try:
                 _text.encode('us-ascii')
                 _charset = 'us-ascii'
             except UnicodeEncodeError:
                 _charset = 'utf-8'

> text = MIMEText('❤¥'.encode('utf-8'), 'html', 'utf-8')

If one provides bytes, one must provide the charset and MIMEText assumes 
you are not lying.

> text.as_string()
> Content-Type: text/html; charset="utf-8"
 > MIME-Version: 1.0
 > Content-Transfer-Encoding: base64
 >
> 4p2kwqU=

> Side question:
> text = MIMEText('❤¥', 'html')
> text.set_charset('utf-8')

This is redundant here. This method is inherited from Message and 
appears pretty useless for the subclass.

> text.as_string()
> 'MIME-Version: 1.0\nContent-Transfer-Encoding: 8bit\nContent-Type:
 > text/html;charset="utf-8"\n\n❤¥'
>
> Why is it now 8-bit encoding?

Bug fixed in 3.3. Output now same as above. Use 3.3 for email unless you 
cannot due to other dependencies not yet being available.

-- 
Terry Jan Reedy

Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

stmplib MIMEText charset weirdness "Adam W." <AWasilenko@gmail.com> - 2013-02-25 20:00 -0800
  Re: stmplib MIMEText charset weirdness Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-02-26 07:10 +0000
    Re: stmplib MIMEText charset weirdness "Adam W." <AWasilenko@gmail.com> - 2013-02-26 07:29 -0800
  Re: stmplib MIMEText charset weirdness Terry Reedy <tjreedy@udel.edu> - 2013-02-26 14:46 -0500

csiph-web