Skip to content

Commit 3b4e723

Browse files
committed
fix return value for unconnected signals in capture
Key Changes: - Updated ClockedSignal.capture to return the default value for unconnected signals. - Added unit test to verify capture behavior with default values. - Mocked ReadOnlyManager.wait in tests to support capture verification without a simulator.
1 parent 0696550 commit 3b4e723

2 files changed

Lines changed: 24 additions & 1 deletion

File tree

cocotbext/interface/interface.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ async def capture(self, resolver=None, default: Any = None):
112112
else:
113113
logger.warning(f"{self._handle} is not resolvable")
114114
return value
115+
return default
115116

116117
def __getattr__(self, name):
117118
"""Forward other cocotb handle methods (like ._name, etc)"""

tests/test_interface.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232

3333
@pytest.fixture(autouse=True)
3434
def mock_cocotb_scheduler(monkeypatch):
35-
"""Auto-patch cocotb.start_soon to avoid scheduler errors in tests.
35+
"""Auto-patch cocotb.start_soon and ReadOnlyManager to avoid scheduler errors in tests.
3636
3737
Properly closes coroutines to avoid RuntimeWarning about unawaited coroutines.
3838
"""
@@ -42,7 +42,11 @@ def mock_start_soon(coro):
4242
if inspect.iscoroutine(coro):
4343
coro.close()
4444

45+
async def mock_wait():
46+
pass
47+
4548
monkeypatch.setattr("cocotb.start_soon", mock_start_soon)
49+
monkeypatch.setattr("cocotbext.interface.interface.ReadOnlyManager.wait", mock_wait)
4650

4751

4852
# ============================================================================
@@ -577,6 +581,24 @@ async def inner():
577581

578582
asyncio.run(inner())
579583

584+
def test_clocked_signal_capture_default(self, mock_hierarchy):
585+
"""Test capturing with a default value for unconnected signals."""
586+
587+
async def inner():
588+
clk = mock_hierarchy._children["clk"]
589+
# Signal with no handle
590+
cs = ClockedSignal(None, clk, RisingEdge, is_input=True)
591+
592+
# Capture should return the default value
593+
val = await cs.capture(default=123)
594+
assert val == 123
595+
596+
# Capture without default should return None
597+
val_none = await cs.capture()
598+
assert val_none is None
599+
600+
asyncio.run(inner())
601+
580602

581603
# ============================================================================
582604
# Tests: Modport + Clocking Integration

0 commit comments

Comments
 (0)