Skip to content

Fix python instructions collecting method for python 3.9 and older #2693

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "utbot-executor"
version = "1.8.0"
version = "1.8.6"
description = ""
authors = ["Vyacheslav Tamarin <vyacheslav.tamarin@yandex.ru>"]
readme = "README.md"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ def _run_calculate_function_value(
__is_exception = False

_, __start = inspect.getsourcelines(function)
__all_code_stmts = filter_instructions(get_instructions(function.__code__), tracer.mode)
__all_code_stmts = filter_instructions(get_instructions(function), tracer.mode)

__tracer = tracer

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
from __future__ import annotations
import dataclasses
import dis
import enum
import os
import sys
import typing
from contextlib import contextmanager
from types import CodeType
from types import CodeType, MethodType, FunctionType, LambdaType


InstructionType: typing.TypeAlias = MethodType | FunctionType | CodeType | type | LambdaType


class TraceMode(enum.Enum):
Expand Down Expand Up @@ -37,8 +41,19 @@ def suppress_stdout():
sys.stdout = old_stdout


def get_instructions(obj: CodeType) -> list[UtInstruction]:
return [UtInstruction(line, start_offset, True) for start_offset, _, line in obj.co_lines() if None not in {start_offset, line}]
def get_instructions(obj: InstructionType) -> list[UtInstruction]:
if sys.version_info >= (3, 10):
code = obj.__code__
return [UtInstruction(line, start_offset, True) for start_offset, _, line in code.co_lines() if None not in {start_offset, line}]
else:
instructions: list[UtInstruction] = []
current_line = None
for instruction in dis.get_instructions(obj):
if current_line is None and instruction.starts_line:
current_line = instruction.starts_line
if current_line is not None:
instructions.append(UtInstruction(instruction.starts_line, instruction.offset, True))
return instructions


def filter_instructions(
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.8.0
1.8.6