Title: unicode error should raise a value error
Type: behavior Stage: resolved
Components: Interpreter Core Versions: Python 3.9
Status: closed Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: shihai1991, steven.daprano
Priority: normal Keywords:

Created on 2019-06-23 13:29 by shihai1991, last changed 2019-06-23 14:15 by shihai1991. This issue is now closed.

Messages (3)
msg346321 - (view) Author: hai shi (shihai1991) * Date: 2019-06-23 13:29
>>> "\x1"
  File "<stdin>", line 1
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 0-2: truncated \xXX escape

I think rasing the valueError would be exact.(pep223 agree this behavior:

And I think the error type could be changed in:
msg346324 - (view) Author: Steven D'Aprano (steven.daprano) * (Python committer) Date: 2019-06-23 14:03
It is a *syntax* error. You have written bad syntax, just as the error message says. Your \xXX code is too short, only 1 hex digit instead of 2. I disagree that this should be a value error: it isn't a bad value, it is bad syntax.

I don't know what the context of the BDFL pronouncement in PEP 223 was, but I think it was decided later that Guido wrong because the error in Python 2.4 and 2.5 was UnicodeDecodeError and then changed to SyntaxError in 2.6 (probably to match Python 3, I guess).

In Python 3, both unicode and bytes strings give a syntax error for this error.
msg346326 - (view) Author: hai shi (shihai1991) * Date: 2019-06-23 14:15
Got it, thank for your detailed explanation, steven.
Date User Action Args
2019-06-23 14:15:14shihai1991setstatus: open -> closed

messages: + msg346326
stage: resolved
2019-06-23 14:03:05steven.dapranosetnosy: + steven.daprano

messages: + msg346324
versions: + Python 3.9
2019-06-23 13:29:20shihai1991create