Skip to content

Commit

Permalink
Fix TokenError from multiline statements
Browse files Browse the repository at this point in the history
Handle more exceptions raised by tokenize._tokenize
  • Loading branch information
athompson673 authored Oct 25, 2024
1 parent 973d3a3 commit 759a8e5
Showing 1 changed file with 17 additions and 11 deletions.
28 changes: 17 additions & 11 deletions spyder/widgets/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import sys
import textwrap
from token import NUMBER
from tokenize import generate_tokens
from tokenize import generate_tokens, TokenError

# Third party imports
from packaging.version import parse
Expand Down Expand Up @@ -1516,17 +1516,23 @@ def mouseDoubleClickEvent(self, event):
# an EOF error trying to double click a line with opening or closing
# triple quotes as well.
text = text.replace('"', ' ').replace("'", ' ')
readline = StringIO(text).read

for t_type, _, start, end, _ in generate_tokens(StringIO(text).read):
if t_type == NUMBER and start[1] <= pos_in_block <= end[1]:
cursor.setPosition(pos + start[1])
cursor.setPosition(
pos + end[1], QTextCursor.MoveMode.KeepAnchor
)
self.setTextCursor(cursor)
return
elif start[1] > pos_in_block:
break
try:
for t_type, _, start, end, _ in generate_tokens(readline):
if t_type == NUMBER and start[1] <= pos_in_block <= end[1]:
cursor.setPosition(pos + start[1])
cursor.setPosition(
pos + end[1], QTextCursor.MoveMode.KeepAnchor
)
self.setTextCursor(cursor)
return
elif start[1] > pos_in_block:
break
except TokenError:
# Ignore 'EOF in multi-line statement' from tokenize._tokenize
# IndentationError should be impossible from tokenizing one line
pass

if isinstance(self, QPlainTextEdit):
QPlainTextEdit.mouseDoubleClickEvent(self, event)
Expand Down

0 comments on commit 759a8e5

Please sign in to comment.