Skip to content

Commit dd76d9d

Browse files
authored
Fix normalizeSelection for Python < 3.8 (#18279)
* Fix normalizeSelection for Python < 3.8 * >= 3.8 instead of > 3.7
1 parent 59a239c commit dd76d9d

2 files changed

Lines changed: 5 additions & 4 deletions

File tree

news/2 Fixes/18258.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix "Run Selection/Line in Python Terminal" for Python < 3.8 when the code includes decorators.

pythonFiles/normalizeSelection.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,11 @@ def _get_statements(selection):
5050
for node in tree.body[1:]:
5151
line_end = node.lineno - 1
5252
# Special handling of decorators:
53-
# In Python 3, decorators are not taken into account in the value returned by lineno,
53+
# In Python 3.8 and higher, decorators are not taken into account in the value returned by lineno,
5454
# and we have to use the length of the decorator_list array to compute the actual start line.
55-
# In Python 2.7, lineno takes into account decorators, so this offset check is unnecessary.
55+
# Before that, lineno takes into account decorators, so this offset check is unnecessary.
5656
# Also, not all AST objects can have decorators.
57-
if hasattr(node, "decorator_list") and sys.version_info.major >= 3:
57+
if hasattr(node, "decorator_list") and sys.version_info >= (3, 8):
5858
# Using getattr instead of node.decorator_list or pyright will complain about an unknown member.
5959
line_end -= len(getattr(node, "decorator_list"))
6060
ends.append(line_end)
@@ -71,7 +71,7 @@ def _get_statements(selection):
7171
start = node.lineno - 1
7272

7373
# Special handling of decorators similar to what's above.
74-
if hasattr(node, "decorator_list") and sys.version_info.major >= 3:
74+
if hasattr(node, "decorator_list") and sys.version_info >= (3, 8):
7575
# Using getattr instead of node.decorator_list or pyright will complain about an unknown member.
7676
start -= len(getattr(node, "decorator_list"))
7777
block = "\n".join(lines[start:end])

0 commit comments

Comments
 (0)