Skip to content

Commit 0bbb527

Browse files
authored
Fix python instructions collecting method for python 3.9 and older (#2693)
1 parent 355647c commit 0bbb527

File tree

4 files changed

+21
-6
lines changed

4 files changed

+21
-6
lines changed

utbot-python-executor/src/main/python/utbot_executor/pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "utbot-executor"
3-
version = "1.8.0"
3+
version = "1.8.6"
44
description = ""
55
authors = ["Vyacheslav Tamarin <vyacheslav.tamarin@yandex.ru>"]
66
readme = "README.md"

utbot-python-executor/src/main/python/utbot_executor/utbot_executor/executor.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ def _run_calculate_function_value(
182182
__is_exception = False
183183

184184
_, __start = inspect.getsourcelines(function)
185-
__all_code_stmts = filter_instructions(get_instructions(function.__code__), tracer.mode)
185+
__all_code_stmts = filter_instructions(get_instructions(function), tracer.mode)
186186

187187
__tracer = tracer
188188

utbot-python-executor/src/main/python/utbot_executor/utbot_executor/utils.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
from __future__ import annotations
22
import dataclasses
3+
import dis
34
import enum
45
import os
56
import sys
67
import typing
78
from contextlib import contextmanager
8-
from types import CodeType
9+
from types import CodeType, MethodType, FunctionType, LambdaType
10+
11+
12+
InstructionType: typing.TypeAlias = MethodType | FunctionType | CodeType | type | LambdaType
913

1014

1115
class TraceMode(enum.Enum):
@@ -37,8 +41,19 @@ def suppress_stdout():
3741
sys.stdout = old_stdout
3842

3943

40-
def get_instructions(obj: CodeType) -> list[UtInstruction]:
41-
return [UtInstruction(line, start_offset, True) for start_offset, _, line in obj.co_lines() if None not in {start_offset, line}]
44+
def get_instructions(obj: InstructionType) -> list[UtInstruction]:
45+
if sys.version_info >= (3, 10):
46+
code = obj.__code__
47+
return [UtInstruction(line, start_offset, True) for start_offset, _, line in code.co_lines() if None not in {start_offset, line}]
48+
else:
49+
instructions: list[UtInstruction] = []
50+
current_line = None
51+
for instruction in dis.get_instructions(obj):
52+
if current_line is None and instruction.starts_line:
53+
current_line = instruction.starts_line
54+
if current_line is not None:
55+
instructions.append(UtInstruction(instruction.starts_line, instruction.offset, True))
56+
return instructions
4257

4358

4459
def filter_instructions(
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1.8.0
1+
1.8.6

0 commit comments

Comments
 (0)