gh-122379: Make REPL completions match only syntactically valid keywords and values #122380
+62
−25
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The implementation is running
compile()
withflags=PyCF_ALLOW_INCOMPLETE_INPUT
for each potential matched word preceded by the console code buffer (in the above example, source would bematch x:\n case
), and iif it raises no exceptions or it raises_IncompleteInputError
, the match is considered to be syntactically valid and kept.It seems that the new REPL can run several statements together, e.g.
Contrary to the old REPL,
I see the implementation of the new REPL is (hide irrelevant details):
It runs the first several statements with
"exec"
except the last one with"single"
.This means that I have to use
mode="exec"
forInteractiveColoredConsole
andmode="single"
for regular ones.mode="single"
withPyCF_ALLOW_INCOMPLETE_INPUT
works as expected, butmode="exec" with
PyCF_ALLOW_INCOMPLETE_INPUT` can give unexpected results. For example,This is because as in
helpers.c
_PyTokenizer_translate_newlines()
,which makes
"if True"
become"if True\n"
which is not incomplete.In my PR, I changed it to not adding a newline for exec input only when flag includes
PyCF_ALLOW_INCOMPLETE_INPUT
and excludesPyCF_DONT_IMPLY_DEDENT
. I am not sure if this change is okay.codeop
is usingPyCF_ALLOW_INCOMPLETE_INPUT | PyCF_DONT_IMPLY_DEDENT
and I have make this case unchanged. The backward-incompatible case would be usingPyCF_ALLOW_INCOMPLETE_INPUT
withoutPyCF_DONT_IMPLY_DEDENT
, which I didn't find any in the standard library. Or should I add a new flag to make it completely backward-compatible, or should it be considered as a bug so that it is okay t fix it?I am sorry for the long paragraphs above, and I hope I described the problem clearly.