You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Different error messages for same error - invalid string prefixes #84427
After some investigating, I found out that this is happening because when we execute fur'' in the interactive interpreter there is an implicit newline, which means that EOF does not get reached.
OTOH there is no newline when passing it as a string inside an eval call. After the tokenizer encounters the first quote, it calls tok_nextc twice more, in order to differentiate between a single-quoted and a triple-quoted string, thus reaching EOF and proapgating an EOF error up through the perrdetail struct.
So a slightly shorter example uses ru''. This is an error because you can't combine the r prefix and the u prefix (in fact you can't combine anything with the r prefix).
I declare that this is a bug to report EOF here and the code should *first* check for a valid combination (e.g. fr'' or rf'') and only *then* try to distinguish between single and triple quotes.
): When the tokenizer sees a valid identifier start, it goes into a loop that checks for a valid combination of string prefixes. If the combination is valid and it sees a quote directly after that, it goto's to the STRING-handling code. If not, then it breaks out of the loop and returns a NAME node.
I have working code that checks if there is a quotation mark right after an identifier. Here is an example:
╰─ ./python
Python 3.9.0a5+ (heads/pegen-dirty:502dfb719e, Apr 11 2020, 14:43:12)
[GCC 9.2.1 20191008] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> ur''
File "<stdin>", line 1
ur''
^
SyntaxError: invalid string prefix
One observation about this is that it has precedence over an EOL error:
I have working code that checks if there is a quotation mark right after
an identifier. Here is an example:
╰─ ./python
Python 3.9.0a5+ (heads/pegen-dirty:502dfb719e, Apr 11 2020, 14:43:12)
[GCC 9.2.1 20191008] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> ur''
File "<stdin>", line 1
ur''
^
SyntaxError: invalid string prefix
One observation about this is that it has precedence over an EOL error:
New changeset 41d5b94 by Lysandros Nikolaou in branch 'master': bpo-40246: Report a better error message for invalid string prefixes (GH-19476) 41d5b94
26 remaining items
hroncok commented on May 4, 2020
hroncokmannequin
Mannequin
More actions
I will soon come back with what Fedora package were affected by the problem. That could give some data about how to handle this.
hroncok commented on May 4, 2020
hroncokmannequin
Mannequin
More actions
Not that many:
cpython itself (fixed via PR)
demjson (fixed via PR)
asn1crypto (fixed via PR)
dnf (fixed via PR)
freeipa (fixed via PR)
I gave up sending PRs at this point.
waf
weasyprint
virt-who
thrift
salt
wxpython4
rosdistro
mne
pycairo
libstoragemgmt (possibly via bundled lsm/external/xmltodict)
dput-ng
ddupdate
aubio (via bundled waf)
bpo-40246: Revert reporting of invalid string prefixes (GH-19888)
Due to backwards compatibility concerns regarding keywords immediately followed by a string without whitespace between them (like in `bg="#d00" if clear else"#fca"`) will fail to parse,
commit 41d5b94af44e34ac05d4cd57460ed104ccf96628 has to be reverted.
$ ./python -X oldparser -m test -v test_fstring
(...)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/vstinner/python/master/Lib/test/test_fstring.py", line 29, in assertAllRaiseeval(str)
AssertionError: "unexpected EOF while parsing" does not match "invalid syntax (<string>, line 1)"
(...)
The >else"#fca"< code was added 6 years ago (commit 8b95d5e)
That was my typo in turtledemo.__main__ (similar lines before had the space) and I wish that it had been caught then.
Why did compileall in the test suite not catch this SyntaxError before 3.6.0a6 was released? Does it intentionally skip .__main__ files? Should I add a test_turtledemo file (with checks for the presence of tkinter and turtle)?
Activity
lysnikolaou commentedon Apr 10, 2020
While testing pegen, we found this out:
lysnikolaou commentedon Apr 10, 2020
After some investigating, I found out that this is happening because when we execute
fur''in the interactive interpreter there is an implicit newline, which means that EOF does not get reached.OTOH there is no newline when passing it as a string inside an
evalcall. After the tokenizer encounters the first quote, it callstok_nextctwice more, in order to differentiate between a single-quoted and a triple-quoted string, thus reaching EOF and proapgating an EOF error up through theperrdetailstruct.gvanrossum commentedon Apr 10, 2020
So a slightly shorter example uses ru''. This is an error because you can't combine the r prefix and the u prefix (in fact you can't combine anything with the r prefix).
I declare that this is a bug to report EOF here and the code should *first* check for a valid combination (e.g. fr'' or rf'') and only *then* try to distinguish between single and triple quotes.
lysnikolaou commentedon Apr 10, 2020
At the moment, if the prefix is invalid, it gets tokenized as a NAME node and the string following is tokenized as a STRING node with no prefix.
╰─ echo "ur''" | ./python -m tokenize
1,0-1,2: NAME 'ur'
1,2-1,4: STRING "''"
1,4-1,5: NEWLINE '\n'
2,0-2,0: ENDMARKER ''
╰─ echo "f''" | ./python -m tokenize
1,0-1,3: STRING "f''"
1,3-1,4: NEWLINE '\n'
2,0-2,0: ENDMARKER ''
gvanrossum commentedon Apr 11, 2020
But that is the tokenize.py module. It does not have 100% the same behavior as the builtin tokenizer.c module. In this case it probably doesn't.
lysnikolaou commentedon Apr 11, 2020
In my understanding of the C code that's what the C tokenizer is doing as well.
Here's the relevant snippet of the tokenizer (
cpython/Parser/tokenizer.c
Line 1358 in 4b222c9
Am I missing something?
lysnikolaou commentedon Apr 11, 2020
I have working code that checks if there is a quotation mark right after an identifier. Here is an example:
╰─ ./python Python 3.9.0a5+ (heads/pegen-dirty:502dfb719e, Apr 11 2020, 14:43:12) [GCC 9.2.1 20191008] on linux Type "help", "copyright", "credits" or "license" for more information. >>> ur'' File "<stdin>", line 1 ur'' ^ SyntaxError: invalid string prefixOne observation about this is that it has precedence over an EOL error:
>>> ur' File "<stdin>", line 1 ur' ^ SyntaxError: invalid string prefixWould that be acceptable?
gvanrossum commentedon Apr 11, 2020
yes
On Sat, Apr 11, 2020 at 04:46 Lysandros Nikolaou <report@bugs.python.org>
wrote:
--
--Guido (mobile)
pablogsal commentedon Apr 12, 2020
New changeset 41d5b94 by Lysandros Nikolaou in branch 'master':
bpo-40246: Report a better error message for invalid string prefixes (GH-19476)
41d5b94
26 remaining items
hroncok commentedon May 4, 2020
I will soon come back with what Fedora package were affected by the problem. That could give some data about how to handle this.
hroncok commentedon May 4, 2020
Not that many:
cpython itself (fixed via PR)
demjson (fixed via PR)
asn1crypto (fixed via PR)
dnf (fixed via PR)
freeipa (fixed via PR)
I gave up sending PRs at this point.
waf
weasyprint
virt-who
thrift
salt
wxpython4
rosdistro
mne
pycairo
libstoragemgmt (possibly via bundled lsm/external/xmltodict)
dput-ng
ddupdate
aubio (via bundled waf)
vstinner commentedon May 4, 2020
Lysandros:
If someone cares of that, I suggest to open an issue in pylint, pyflakes and similar tools to emit a warning in linters.
lysnikolaou commentedon May 5, 2020
Ok, I'm closing this, after consulting with Guido.
vstinner commentedon May 15, 2020
The following change broke test_fstring when using the old parser. I reopen the issue.
commit 846d8b2 (refs/bisect/bad)
Author: Lysandros Nikolaou <lisandrosnik@gmail.com>
Date: Mon May 4 14:32:18 2020 +0300
$ ./python -X oldparser -m test -v test_fstring (...)======================================================================
FAIL: test_invalid_string_prefixes (test.test_fstring.TestCase) (str='BF""')
----------------------------------------------------------------------
File "<string>", line 1
BF""
^
SyntaxError: invalid syntax
vstinner commentedon May 15, 2020
The error can be seen on the new cool AMD64 Arch Linux VintageParser 3.x, vintage is the new cool:
https://buildbot.python.org/all/#builders/648/builds/185
gvanrossum commentedon May 15, 2020
We should run the tests with the old parser in at least one build.
terryjreedy commentedon May 16, 2020
That was my typo in turtledemo.__main__ (similar lines before had the space) and I wish that it had been caught then.
Why did compileall in the test suite not catch this SyntaxError before 3.6.0a6 was released? Does it intentionally skip .__main__ files? Should I add a test_turtledemo file (with checks for the presence of tkinter and turtle)?
vstinner commentedon May 18, 2020
"make install" ignores compileall errors no? If I recall correctly, only an error is logged into stdlib, "make install" doesn't fail.
pablogsal commentedon May 26, 2020
New changeset 6cb0ad2 by Lysandros Nikolaou in branch '3.9':
bpo-40246: Fix test_fstring when run with the old parser (GH-20402)
6cb0ad2