Skip to content
Closed
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
50 changes: 30 additions & 20 deletions codeflash/verification/codeflash_capture.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import time
import warnings
from enum import Enum
from pathlib import Path
from typing import Callable

import dill as pickle
Expand All @@ -33,6 +32,11 @@ def get_test_info_from_stack(tests_root: str) -> tuple[str, str | None, str, str
test_name: str | None = None
line_id = ""

# Get current frame and skip our own function's frame

# Precompute resolved tests_root as string for fast comparison
tests_root_resolved = os.path.normcase(os.path.abspath(tests_root))

# Get current frame and skip our own function's frame
frame = inspect.currentframe()
if frame is not None:
Expand All @@ -47,9 +51,10 @@ def get_test_info_from_stack(tests_root: str) -> tuple[str, str | None, str, str
# Check if function name indicates a test (e.g., starts with "test_")
if function_name.startswith("test_"):
test_name = function_name
test_module = inspect.getmodule(frame)
if hasattr(test_module, "__name__"):
test_module_name = test_module.__name__
# Faster direct lookup for module name from f_globals
test_module = frame.f_globals.get("__name__", None)
if isinstance(test_module, str):
test_module_name = test_module
line_id = str(lineno)

# Check if it's a method in a class
Expand All @@ -62,23 +67,28 @@ def get_test_info_from_stack(tests_root: str) -> tuple[str, str | None, str, str
break

# Check for instantiation on the module level
if (
"__name__" in frame.f_globals
and frame.f_globals["__name__"].split(".")[-1].startswith("test_")
and Path(filename).resolve().is_relative_to(Path(tests_root))
and function_name == "<module>"
):
test_module_name = frame.f_globals["__name__"]
line_id = str(lineno)

# # Check if it's a method in a class
if (
"self" in frame.f_locals
and hasattr(frame.f_locals["self"], "__class__")
and hasattr(frame.f_locals["self"].__class__, "__name__")
):
test_class_name = frame.f_locals["self"].__class__.__name__
break
# Check for instantiation on the module level
fg = frame.f_globals
fn = fg.get("__name__") if "__name__" in fg else None
if fn is not None and fn.split(".")[-1].startswith("test_") and function_name == "<module>":
# Do a much faster relative path check using string ops
file_abs = os.path.normcase(os.path.abspath(filename))
try:
is_rel = os.path.commonpath([file_abs, tests_root_resolved]) == tests_root_resolved
except ValueError: # If paths are on different drives, .commonpath raises ValueError
is_rel = False
if is_rel:
test_module_name = fg["__name__"]
line_id = str(lineno)

if (
"self" in frame.f_locals
and hasattr(frame.f_locals["self"], "__class__")
and hasattr(frame.f_locals["self"].__class__, "__name__")
):
test_class_name = frame.f_locals["self"].__class__.__name__
break

# Go to the previous frame
frame = frame.f_back
Expand Down
Loading