Skip to content

Commit

Permalink
Fix mypy errors
Browse files Browse the repository at this point in the history
  • Loading branch information
labbati committed Dec 12, 2024
1 parent dcbb9ad commit bce9590
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 26 deletions.
31 changes: 17 additions & 14 deletions ddtrace/internal/bytecode_injection/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,17 +60,15 @@ def inject_invocation(injection_context: InjectionContext, path: str, package: s
)
seen_lines.extend(nested_lines)

code_attrs = {
"co_code": bytes(new_code),
"co_consts": tuple(new_consts),
"co_linetable": new_linetable,
"co_stacksize": code.co_stacksize + 4, # TODO: Compute the value!
}

if is_python_3_11:
code_attrs["co_exceptiontable"] = new_exctable

code = code.replace(**code_attrs)
if is_python_3_10:
code = code.replace(co_code=bytes(new_code), co_consts=tuple(new_consts), co_linetable=new_linetable,
co_stacksize=code.co_stacksize + 4 # TODO: Compute the value!
)
else:
code = code.replace(co_code=bytes(new_code), co_consts=tuple(new_consts), co_linetable=new_linetable,
co_stacksize=code.co_stacksize + 4, # TODO: Compute the value!
co_exceptiontable=new_exctable # type:ignore[call-arg]
)

return (
code,
Expand Down Expand Up @@ -153,6 +151,8 @@ def inject_invocation(injection_context: InjectionContext, path: str, package: s

def _inject_invocation_nonrecursive(
injection_context: InjectionContext, path: str, package: str


) -> t.Tuple[bytearray, t.List[object], bytes, bytes, t.List[int]]:
"""
Inject invocation of the hook function at the specified source code lines, or more specifically offsets, in the
Expand Down Expand Up @@ -358,7 +358,10 @@ def append_instruction(opcode: int, extended_arg: int) -> bytearray:
arg >>= 8
arg_offset -= 2

exception_table = _generate_exception_table(code, offsets_map, extended_arg_offsets) if is_python_3_11 else b""
if is_python_3_11:
exception_table = _generate_exception_table(code, offsets_map, extended_arg_offsets)
else:
exception_table = b""

return (
new_code,
Expand Down Expand Up @@ -499,7 +502,7 @@ def _generate_adjusted_location_data_3_11(

# Extend the line table record if we added any EXTENDED_ARGs
offset += offset_delta
if ext_arg_offset is not None and original_offset >= ext_arg_offset:
if ext_arg_offset is not None and ext_arg_size is not None and original_offset >= ext_arg_offset:
# if ext_arg_offset is not None and offset > ext_arg_offset:
room = 7 - offset_delta
chunk[0] += min(room, t.cast(int, ext_arg_size))
Expand Down Expand Up @@ -544,7 +547,7 @@ def _generate_exception_table(
For format see:
https://github.com/python/cpython/blob/208b0fb645c0e14b0826c0014e74a0b70c58c9d6/InternalDocs/exception_handling.md#format-of-the-exception-table
"""
parsed_exception_table = dis._parse_exception_table(code)
parsed_exception_table = dis._parse_exception_table(code) # type: ignore[attr-defined]

def calculate_additional_offset(original_offset: int, is_start: bool = False) -> int:
# We want to include the code we add in the exception table, so that the finally block is correctly executed
Expand Down
12 changes: 0 additions & 12 deletions ddtrace/internal/coverage/instrumentation_py3_10.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,6 @@
# NOTE: the "prettier" one-liner version (eg: assert (3,11) <= sys.version_info < (3,12)) does not work for mypy
assert sys.version_info >= (3, 10) and sys.version_info < (3, 11) # nosec

EXTENDED_ARG = dis.EXTENDED_ARG
LOAD_CONST = dis.opmap["LOAD_CONST"]
CALL = dis.opmap["CALL_FUNCTION"]
POP_TOP = dis.opmap["POP_TOP"]
IMPORT_NAME = dis.opmap["IMPORT_NAME"]
IMPORT_FROM = dis.opmap["IMPORT_FROM"]

JUMPS = set(dis.hasjabs + dis.hasjrel)
ABSOLUTE_JUMPS = set(dis.hasjabs)
BACKWARD_JUMPS = set(op for op in dis.hasjrel if "BACKWARD" in dis.opname[op])
FORWARD_JUMPS = set(op for op in dis.hasjrel if "BACKWARD" not in dis.opname[op])


def instrument_all_lines(code: CodeType, hook: HookType, path: str, package: str) -> t.Tuple[CodeType, CoverageLines]:
injection_context = InjectionContext(code, hook, lambda _: [o for o, _ in dis.findlinestarts(code)])
Expand Down

0 comments on commit bce9590

Please sign in to comment.