This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author steven.daprano
Recipients steven.daprano
Date 2021-11-15.23:52:37
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1637020357.3.0.163492557929.issue45811@roundup.psfhosted.org>
In-reply-to
Content
Invisible control characters (aside from white space) are not permitted in source code, but the syntax error we get is confusing and lacks information:

>>> s = 'print\x17("Hello")'
>>> eval(s)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<string>", line 1
    print("Hello")
         ^
SyntaxError: invalid syntax


The caret points to an invisible character. The offending control character is not visible in the traceback, or the source code unless you use a hex editor. Copying and pasting the string from the traceback, or the source code, may remove the control character (depending on the tools you use), making it even harder to track down the problem.

I suggest that the syntax error should state that the problem is an invisible control character, and display it as a standard human-readable code together with its hex code:

SyntaxError: invisible control character ^W (0x17)


Just in case it isn't obvious what the mapping between controls and the human visible string is:

def control(char):
    n = ord(char)
    if 0 <= n <= 0x1F:
        # C0 control codes
        return '^' + chr(ord('@')+n)
    elif n == 0x7F:
        # DEL
        return '^?'
    elif 0x80 <= n <= 0x9F:
        # C1 control codes
        return 'Esc+' + chr(ord('@')+n-0x80)
    else:
        raise ValueError('Not a control character.')


https://en.wikipedia.org/wiki/C0_and_C1_control_codes
History
Date User Action Args
2021-11-15 23:52:37steven.dapranosetrecipients: + steven.daprano
2021-11-15 23:52:37steven.dapranosetmessageid: <1637020357.3.0.163492557929.issue45811@roundup.psfhosted.org>
2021-11-15 23:52:37steven.dapranolinkissue45811 messages
2021-11-15 23:52:37steven.dapranocreate