python / cpython

The Python programming language
https://www.python.org
Other
62.59k stars 30.04k forks source link

IDLE doesn't know where to start parsing #124451

Open ujan-r opened 5 days ago

ujan-r commented 5 days ago

Bug report

Bug description:

After making a typo in IDLE, I noticed some unexpected calltips and incorrectly matched parentheses. These seem to be caused by the parser not knowing where the current prompt begins. I'll try to send in a PR tonight.

Examples

Parentheses are incorrectly matched:

>>> (1 2
...
SyntaxError: '(' was never closed
>>> ) # IDLE highlights this as the closing paren

Matching parentheses aren't detected (from #85560):

>>> (1 if 0
     else 0) # IDLE beeps here, not detecting the opening paren

Calltip appears unexpectedly (Python 3.12; a new error message hides this issue in the latest version):

>>> (1 2
...
SyntaxError: incomplete input
>>> ( # this opens the calltip for `input`

CPython versions tested on:

3.12, CPython main branch

Operating systems tested on:

macOS

Linked PRs

terryjreedy commented 5 days ago

I verified examples 1 and 3 on macOS and Windows. They are specific to Shell in that only Shell gets SyntaxError messages mixed in with code to be executed. Put the same text (without the prompts*) in an IDLE code editor and the behavior is correct.

The immediate fix is that hyperparser know when it is in Shell and when so, treat the code-begin mark as beginning of file. A different fix that I have already planned it to not mix SyntaxError and new prompts into the new code area. Instead intercept it and display the message in the status bar or a popup. The latter is used (currently) for editor windows, but I have thought of changing for short messages.

I also want to replace using one Text for both history/output and code input, with a movable boundary between, and instead use different widgets for the different functions.