Skip to content

Commit

Permalink
✨ Pass random seed value to ipdb instance
Browse files Browse the repository at this point in the history
  • Loading branch information
michprev committed Sep 17, 2024
1 parent b865aa0 commit 27ed187
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 28 deletions.
5 changes: 3 additions & 2 deletions wake/cli/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ def run_no_pytest(
import importlib.util
import inspect
import shutil
from functools import partial

from wake.development.globals import (
attach_debugger,
Expand Down Expand Up @@ -88,7 +89,7 @@ def run_no_pytest(
console.print(f"Using random seed {random_seeds[0].hex()}")

if debug:
set_exception_handler(attach_debugger)
set_exception_handler(partial(attach_debugger, seed=random_seeds[0]))

if coverage:
set_coverage_handler(CoverageHandler(config))
Expand All @@ -99,7 +100,7 @@ def run_no_pytest(
func()
except Exception:
if debug:
attach_debugger(*sys.exc_info())
attach_debugger(*sys.exc_info(), seed=random_seeds[0])
raise
reset_exception_handled()
finally:
Expand Down
11 changes: 8 additions & 3 deletions wake/development/globals.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ def attach_debugger(
e_type: Optional[Type[BaseException]],
e: Optional[BaseException],
tb: Optional[TracebackType],
seed: Optional[bytes] = None,
):
global _exception_handled

Expand Down Expand Up @@ -93,9 +94,13 @@ def attach_debugger(
break
frames_up += 1

p = _init_pdb(
commands=["up %d" % frames_up] if frames_up > 0 else [],
)
commands = []
if frames_up > 0:
commands.append("up %d" % frames_up)
if seed is not None:
commands.append(f"random_seed = bytes.fromhex('{seed.hex()}')")

p = _init_pdb(commands=commands)
p.reset()
p.interaction(None, tb)

Expand Down
2 changes: 1 addition & 1 deletion wake/testing/fuzzing/fuzzer.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def exception_handler(
attach: bool = err_child_conn.recv()
if attach:
sys.stdin = os.fdopen(0)
attach_debugger(e_type, e, tb)
attach_debugger(e_type, e, tb, seed=random_seed)
finally:
finished_event.set()

Expand Down
3 changes: 1 addition & 2 deletions wake/testing/pytest_plugin_multiprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,7 @@ def _exception_handler(
try:
if attach:
sys.stdin = os.fdopen(0)
console.print(f"Used random seed '{self._random_seed.hex()}'")
attach_debugger(e_type, e, tb)
attach_debugger(e_type, e, tb, seed=self._random_seed)
finally:
self._setup_stdio()
self._conn.send(("exception_handled",))
Expand Down
30 changes: 10 additions & 20 deletions wake/testing/pytest_plugin_single.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
from typing import Iterable, List, Optional, Type
from functools import partial
from typing import Iterable, List, Optional

from pytest import Session

from types import TracebackType

from wake.cli.console import console
from wake.config import WakeConfig
from wake.development.globals import (
Expand All @@ -27,7 +26,6 @@ class PytestWakePluginSingle:
_cov_proc_count: Optional[int]
_random_seeds: List[bytes]
_debug: bool
_exception_handled: bool

def __init__(
self,
Expand All @@ -43,22 +41,15 @@ def __init__(

def pytest_runtest_setup(self, item):
reset_exception_handled()
self._exception_handled = False

def pytest_exception_interact(self, node, call, report):
if self._debug and not self._exception_handled:
self._exception_handler(call.excinfo.type, call.excinfo.value, call.excinfo.tb)


def _exception_handler(
self,
e_type: Optional[Type[BaseException]],
e: Optional[BaseException],
tb: Optional[TracebackType],
) -> None:
self._exception_handled = True
console.print(f"Used random seed '{self._random_seeds[0].hex()}'")
attach_debugger(e_type, e, tb)
if self._debug:
attach_debugger(
call.excinfo.type,
call.excinfo.value,
call.excinfo.tb,
seed=self._random_seeds[0],
)

def pytest_runtestloop(self, session: Session):
if (
Expand All @@ -76,10 +67,9 @@ def pytest_runtestloop(self, session: Session):
coverage = self._cov_proc_count == 1 or self._cov_proc_count == -1

random.seed(self._random_seeds[0])
console.print(f"Using random seed '{self._random_seeds[0].hex()}'")

if self._debug:
set_exception_handler(self._exception_handler)
set_exception_handler(partial(attach_debugger, seed=self._random_seeds[0]))
if coverage:
set_coverage_handler(CoverageHandler(self._config))

Expand Down

0 comments on commit 27ed187

Please sign in to comment.