Wednesday, November 29, 2017

UnicodeDecode SyntaxError

When executing a bytecode for the '+' operation, an invalid byte will raise UnicodeDecodeError.  However, when concatenating adjacent string and unicode constants, it will be a SyntaxError.  (I guess because there is not byte-code executing this is happening at compile time.)

>>> u'a' + '\xff'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
UnicodeDecodeError: 'ascii' codec can't decode byte 0xff in position 0: ordinal not in range(128)


>>> u'a' '\xff'
  File "<stdin>", line 1
SyntaxError: (unicode error) 'ascii' codec can't decode byte 0xff in position 0: ordinal not in range(128)

4 comments:

  1. This is because the concatenation of adjacent strings is a preprocessor operation

    ReplyDelete
  2. Admiring the commitment you put into your website and in depth information you offer. Write My Essay Cheap It’s good to come across a blog every once in a while that isn’t the same old rehashed information. Fantastic read! I’ve saved your site and I’m including your RSS feeds to my Google account.

    ReplyDelete
  3. Especially if you’re unfamiliar with the different types jackets and coats than we are here only to provide you our classic mens brown biker real leather jacket at your doorstep worldwide.

    ReplyDelete