New submission from Steven D'Aprano <[email protected]>:
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
----------
components: Interpreter Core
messages: 406379
nosy: steven.daprano
priority: normal
severity: normal
status: open
title: Improve error message when source code contains invisible control
characters
type: enhancement
versions: Python 3.11
_______________________________________
Python tracker <[email protected]>
<https://bugs.python.org/issue45811>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com