Unicode in MIMEText

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Damjan

    #1

    Unicode in MIMEText

    Why doesn't this work:

    from email.MIMEText import MIMEText
    msg = MIMEText(u'\u04 3a\u0438\u0440\ u0438\u043b\u04 38\u0446\u0430' )
    msg.set_charset ('utf-8')
    msg.as_string()
    Traceback (most recent call last):
    File "<stdin>", line 1, in ?
    File "/usr/lib/python2.4/email/Message.py", line 129, in as_string
    g.flatten(self, unixfrom=unixfr om)
    File "/usr/lib/python2.4/email/Generator.py", line 82, in flatten
    self._write(msg )
    File "/usr/lib/python2.4/email/Generator.py", line 113, in _write
    self._dispatch( msg)
    File "/usr/lib/python2.4/email/Generator.py", line 139, in _dispatch
    meth(msg)
    File "/usr/lib/python2.4/email/Generator.py", line 180, in _handle_text
    payload = cset.body_encod e(payload)
    File "/usr/lib/python2.4/email/Charset.py", line 366, in body_encode
    return email.base64MIM E.body_encode(s )
    File "/usr/lib/python2.4/email/base64MIME.py", line 136, in encode
    enc = b2a_base64(s[i:i + max_unencoded])
    UnicodeEncodeEr ror: 'ascii' codec can't encode characters in position 0-7:
    ordinal not in range(128)

    --
    damjan
  • Damjan

    #2
    Re: Unicode in MIMEText

    > Why doesn't this work:[color=blue]
    >
    > from email.MIMEText import MIMEText
    > msg = MIMEText(u'\u04 3a\u0438\u0440\ u0438\u043b\u04 38\u0446\u0430' )
    > msg.set_charset ('utf-8')
    > msg.as_string()[/color]
    ....[color=blue]
    > UnicodeEncodeEr ror: 'ascii' codec can't encode characters in position 0-7:
    > ordinal not in range(128)[/color]

    It's a real shame that unicode support in the python library is very weak
    sometimes...

    Anyway I solved my problem by patching email.Charset

    --- Charset.py~ 2005-11-24 04:20:09.000000 000 +0100
    +++ Charset.py 2005-11-24 04:21:02.000000 000 +0100
    @@ -244,6 +244,8 @@
    """Convert a string from the input_codec to the output_codec."" "
    if self.input_code c <> self.output_cod ec:
    return unicode(s, self.input_code c).encode(self. output_codec)
    + elif isinstance(s, unicode):
    + return s.encode(self.o utput_codec)
    else:
    return s





    --
    damjan

    Comment

    • Steve Holden

      #3
      Re: Unicode in MIMEText

      Damjan wrote:[color=blue][color=green]
      >>Why doesn't this work:
      >>
      >>from email.MIMEText import MIMEText
      >>msg = MIMEText(u'\u04 3a\u0438\u0440\ u0438\u043b\u04 38\u0446\u0430' )
      >>msg.set_chars et('utf-8')
      >>msg.as_string ()[/color]
      >
      > ...
      >[color=green]
      >>UnicodeEncode Error: 'ascii' codec can't encode characters in position 0-7:
      >>ordinal not in range(128)[/color]
      >
      >
      > It's a real shame that unicode support in the python library is very weak
      > sometimes...
      >
      > Anyway I solved my problem by patching email.Charset
      >
      > --- Charset.py~ 2005-11-24 04:20:09.000000 000 +0100
      > +++ Charset.py 2005-11-24 04:21:02.000000 000 +0100
      > @@ -244,6 +244,8 @@
      > """Convert a string from the input_codec to the output_codec."" "
      > if self.input_code c <> self.output_cod ec:
      > return unicode(s, self.input_code c).encode(self. output_codec)
      > + elif isinstance(s, unicode):
      > + return s.encode(self.o utput_codec)
      > else:
      > return s
      >
      >
      >
      >
      >[/color]
      .... and being concerned to improve the library you logged this patch in
      Sourceforge for consideration by the developers?

      That's the only way to guarantee proper consideration of your fix.

      regards
      Steve
      --
      Steve Holden +44 150 684 7255 +1 800 494 3119
      Holden Web LLC www.holdenweb.com
      PyCon TX 2006 www.python.org/pycon/

      Comment

      • Damjan

        #4
        Re: Unicode in MIMEText

        > ... and being concerned to improve the library you logged this patch in[color=blue]
        > Sourceforge for consideration by the developers?
        >
        > That's the only way to guarantee proper consideration of your fix.[/color]

        Ok I will, can you confirm that the patch is correct?
        Maybe I got something wrong?


        --
        damjan

        Comment

        • Steve Holden

          #5
          Re: Unicode in MIMEText

          Damjan wrote:[color=blue][color=green]
          >>... and being concerned to improve the library you logged this patch in
          >>Sourceforge for consideration by the developers?
          >>
          >>That's the only way to guarantee proper consideration of your fix.[/color]
          >
          >
          > Ok I will, can you confirm that the patch is correct?
          > Maybe I got something wrong?
          >
          >[/color]
          I can't confirm its correctness but I can say it looks reasonable enough
          to submit as a path. The fact that you have identified an issue and a
          possible fix is quite enough to allow you to submit the patch.

          The adequacy of the patch will ultimately be decided by the maintainer
          who considers your submission (in all probability Barry Warsaw, but not
          necessarily).

          Thanks for taking the time to improve the quality of the Python library.

          regards
          Steve
          --
          Steve Holden +44 150 684 7255 +1 800 494 3119
          Holden Web LLC www.holdenweb.com
          PyCon TX 2006 www.python.org/pycon/

          Comment

          • Damjan

            #6
            Re: Unicode in MIMEText

            patch submitted...
            [color=blue]
            > Thanks for taking the time to improve the quality of the Python library.[/color]

            Do you think it would be possible to do some kind of an automatic
            comprehensive test of compatibility of the standard library with unicode
            strings?


            --
            damjan

            Comment

            Working...