Repository navigation
Closed
Labels
Description
| BPO | 41388 |
|---|---|
| Nosy | @terryjreedy, @taleinat, @lrjball |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
assignee = 'https://github.com/terryjreedy'
closed_at = None
created_at = <Date 2020-07-24.19:10:41.733>
labels = ['expert-IDLE', 'type-bug', '3.8', '3.9', '3.10']
title = 'IDLE fails to detect corresponding opening parenthesis'
updated_at = <Date 2020-07-26.23:35:02.207>
user = 'https://bugs.python.org/AlexeyBurdin'bugs.python.org fields:
activity = <Date 2020-07-26.23:35:02.207>
actor = 'Lewis Ball'
assignee = 'terry.reedy'
closed = False
closed_date = None
closer = None
components = ['IDLE']
creation = <Date 2020-07-24.19:10:41.733>
creator = 'Alexey Burdin'
dependencies = []
files = []
hgrepos = []
issue_num = 41388
keywords = []
message_count = 9.0
messages = ['374205', '374218', '374224', '374225', '374226', '374274', '374298', '374301', '374350']
nosy_count = 4.0
nosy_names = ['terry.reedy', 'taleinat', 'Alexey Burdin', 'Lewis Ball']
pr_nums = []
priority = 'normal'
resolution = None
stage = 'test needed'
status = 'open'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue41388'
versions = ['Python 3.8', 'Python 3.9', 'Python 3.10']Linked PRs
- gh-85560: Fix IDLE paren matching with "else" or "yield" at the start of a continuation line #157693
- [3.15] gh-85560: Fix IDLE paren matching with "else" or "yield" at the start of a continuation line (GH-157693) #157959
- [3.14] gh-85560: Fix IDLE paren matching with "else" or "yield" at the start of a continuation line (GH-157693) #157960
- [3.13] gh-85560: Fix IDLE paren matching with "else" or "yield" at the start of a continuation line (GH-157693) #157961
Metadata
Metadata
Assignees
Labels
Projects
- StatusShow more project fieldsDone
Activity
AlexeyBurdin commentedon Jul 24, 2020
when the cursor is placed in line 5 col 31 (between
)and))) hitting Ctrl-0 (Show surrounding parens) produces an error sound, though there's no error, the script works fine.lrjball commentedon Jul 25, 2020
A minimal example of the same issue seems to be:
In this case the parentheses matching also fails. It only ever seems to fail when
elseis the first word on a newline inside brackets, and adding the line break elsewhere in the above statement will not cause the matching to fail.I imagine it stops looking for the matching parentheses when it gets to the
elsestatement, as anelsewhen part of anif/elsecannot be inside parentheses. Obviously the exception to this is the ternary operator.Bizarrely, it only seems to fail the first time it encounters this issue. Any subsequent code with the same pattern seems to be matched fine. So in the following example, the second brackets get matched without an issue:
Hopefully someone more familiar with the source code can shed some light on this!
terryjreedy commentedon Jul 25, 2020
(While I wrote the following, Lewis Ball posted some of the same conclusions.)
I verified the failure with 3.8.5 and current master.
I reduced the failing example to a minimum of 7 chars on 2 lines.
(
else)
1a. Deleting '\n' or any letter of 'else' fixes the problem.
1b. Adding 'else' before '(' fixes the problem.
1a is true for the initial 'else' on line 4 of the original example.
1b is true for 'else' on a new line before the first.
2a. Matching also fails when typing ')'.
2b. Replacing '()' with '[]' or '{}' makes no difference.
2c. Replacing 'else' with 'elif', 'while', 'def', or 'class' makes no difference. This include the addition for 1b above. Replacing 'else' with 'if', 'for', or 'with' fixes the bug.
Bingo!!! Matching uses idlelib.hyperparser, which uses idlelib.pyparse, which has regex _synchre, which finds "what looks like the start of a popular statement". It matches 'elif', etcetera, but not 'if', etcetera. I noticed previously that these 'popular' line beginners were missing and have planned to add them.* But we need to make matched words not disable fence matching.
2d. In the original and minimal examples, moving the offending 'else' to the end of the previous line fixes the problem. So I believe that 'else' at the beginning of the line is seen marking the beginning of the statememt, and matching does not look back further. If 'else' and 'yield' were left in _synchre and 'if' and 'for' were added back, the matcher would have to determine whether the line is a continuation or not. (And I believe that not having 'if' and 'for' degrades the use of pyparse at least for other uses.)
2e. The minimal failing example works in the shell. Why are _synchre keywords not poisonous here? Is it just because the current statement is known to begin just after the '>>> ' prompt, so that else beginning the second line is somehow not seen as beginning a new statement? We need to check if and how fence matching in shell is different other than the presence of the marker where code input begins.
Alexey: in (#65955 (comment)) I claimed that there is no issue with typing closers or ^0 on the same line just before the closer. Thank you for reporting this counter-example. I imagine that you are not the first IDLE user to write a conditional expression on 2 or more lines with else at the beginning of a line. Such expressions always need a closer.
terryjreedy commentedon Jul 25, 2020
Without rereading hyperparser and pyparse, I don't understand why a previous _synchre match prevents subsequent failures. It does suggest that when matching parentheses, a fix would be to temporarily prepend 'else\n' to the text, or otherwise simulating the effect of that.
But the previous 'else' might only work before the 'current' statement. And we should determine whether any other uses of these modules have related bugs. Also, removing 'else' from the _synchre regex did not fix the issue, so there is something else I am missing.
4 remaining items
lrjball commentedon Jul 26, 2020
Okay that makes sense. Removing things from _synchre would slow down the matching slightly, although the matching still seems plenty fast enough when done inside an if clause at the moment, so I'm not sure how noticeable the removal of `else` would be. One thing could be to search for `else:` instead of just `else` in _synchre, as only the former indicates the start of a new statement (although I guess it would actually have to check for `else[\s\\]*:` or something).
Like you say though, if there are other more pressing issues with the matching then maybe it is worth fixing them all at the same time. Happy to help with that if needed.
terryjreedy commentedon Sep 24, 2024
I reproduced the issue with 3.13.0rc2 on macOS and main on Windows.
(\nelse)(with auto indent added) must be the first pair typed since opening the window. Previous text loaded on opening a existing file does not count.pythongh-85560: Fix IDLE paren matching with "else" or "yield" at the…
serhiy-storchaka commentedon Sep 17, 2026
#157693 makes
_synchrematchelseonly when it is followed by a colon, as in anelsestatement, dropsyield, which cannot be told from a yield expression at the start of a continuation line, and addswith,del,global,nonlocal,passandfinally, which cannot start a continuation line.gh-85560: Fix IDLE paren matching with "else" or "yield" at the start…
[3.14] gh-85560: Fix IDLE paren matching with "else" or "yield" at th…
[3.13] gh-85560: Fix IDLE paren matching with "else" or "yield" at th…
[3.15] gh-85560: Fix IDLE paren matching with "else" or "yield" at th…