Edgewall Software

Changes between Version 2 and Version 3 of UnicodeDecodeError


Ignore:
Timestamp:
Mar 5, 2007, 11:31:19 AM (17 years ago)
Author:
Christian Boos
Comment:

clarify a bit the section about str.encode() triggering a UnicodeDecodeError

Legend:

Unmodified
Added
Removed
Modified
  • UnicodeDecodeError

    v2 v3  
    2828}}}
    2929
    30 A more subtle and confusing way to trigger this error is when trying to ''encode'' a sequence of bytes to a given encoding. Wait... encoding a sequence of bytes? Does that make sense? Well, no, normally it shouldn't, but Python "offers" you the `encode` method on `str` objects, as a shortcut to: `"string".encode() == unicode("string").encode()`. That could be called a `u"cadeau empoisonné"` ;-)
     30A more subtle and confusing way to trigger this error is when trying to ''encode'' a sequence of bytes to a given encoding. Wait... encoding a sequence of bytes? Does that even make sense? Well, normally not, but Python interprets that as a shortcut for encoding the `unicode` object corresponding to this string. So we have the following equivalence:
     31{{{
     32"string".encode(enc) == unicode("string").encode(enc)
     33}}}
     34That could be called a `u"cadeau empoisonné"` ;-)
    3135
    3236Of course, if `"string"` can't be first decoded the naive way in order to produce that temporary `unicode` object, it will trigger the same error we saw above: