imaplib: is this really so unwieldy?

Chris Angelico rosuav at gmail.com
Tue May 25 07:46:34 EDT 2021


On Tue, May 25, 2021 at 8:21 PM Cameron Simpson <cs at cskk.id.au> wrote:
> When you go:
>
>     text = str(data)
>
> that is _assuming_ a particular text encoding stored in the data. You
> really ought to specify an encoding here. If you've not specified the
> CHARSET for things, 'ascii' would be a conservative choice. The IMAP RFC
> talks about what to expect in section 4 (Data Formats). There's quite a
> lot of possible response formats and I can understand imaplib not
> getting deeply into decoding these.

Worse than that: what you actually get is the repr of the bytes. That
might happen to look a lot like an ASCII decode, but if the string
contains unprintable characters, quotes, or anything outside of the
ASCII range, it's going to represent it as an escape code.

The best way to turn bytes into text is the decode method:

data.decode("UTF-8")

ChrisA


More information about the Python-list mailing list