Skip to content
This repository has been archived by the owner on Oct 11, 2023. It is now read-only.

Commit

Permalink
Merge pull request #36 from bluesky/fix_segfault
Browse files Browse the repository at this point in the history
Fix segfault
  • Loading branch information
rosesyrett authored Aug 8, 2023
2 parents 59ca796 + 44cbaa1 commit 23d4c46
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 17 deletions.
42 changes: 27 additions & 15 deletions src/ophyd_epics_devices/panda.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import atexit
import re
from enum import Enum
from typing import (
Expand Down Expand Up @@ -31,8 +32,6 @@
)
from p4p.client.thread import Context

ctxt = Context("pva")


class PulseBlock(Device):
delay: SignalRW[float]
Expand Down Expand Up @@ -98,14 +97,8 @@ def block_name_number(block_name: str) -> Tuple[str, int]:
return name, int(num or 1)


# in sim mode, this should be called with a timeout of 0.0.
async def pvi_get(pv: str, timeout: float = 5.0) -> Dict[str, PVIEntry]:
pv_info: Dict[str, Dict[str, str]] = {}
try:
pv_info = ctxt.get(pv, timeout=timeout).get("pvi").todict()
except TimeoutError:
# log here that it couldn't access it.
raise Exception("Cannot get the PV.")
async def pvi_get(pv: str, ctxt: Context, timeout: float = 5.0) -> Dict[str, PVIEntry]:
pv_info = ctxt.get(pv, timeout=timeout).get("pvi").todict()

result = {}

Expand All @@ -115,13 +108,14 @@ async def pvi_get(pv: str, timeout: float = 5.0) -> Dict[str, PVIEntry]:


class PandA(Device):
_ctxt: Optional[Context] = None

pulse: DeviceVector[PulseBlock]
seq: DeviceVector[SeqBlock]
pcap: PcapBlock

def __init__(self, prefix: str, name: str = "") -> None:
self._init_prefix = prefix

self.pvi_mapping: Dict[FrozenSet[str], Callable[..., Signal]] = {
frozenset({"r", "w"}): lambda dtype, rpv, wpv: epics_signal_rw(
dtype, rpv, wpv
Expand All @@ -132,6 +126,20 @@ def __init__(self, prefix: str, name: str = "") -> None:
frozenset({"x"}): lambda dtype, rpv, wpv: epics_signal_x(wpv),
}

@property
def ctxt(self) -> Context:
if PandA._ctxt is None:
PandA._ctxt = Context("pva", nt=False)

@atexit.register
def _del_ctxt():
# If we don't do this we get messages like this on close:
# Error in sys.excepthook:
# Original exception was:
PandA._ctxt = None

return PandA._ctxt

def verify_block(self, name: str, num: int):
"""Given a block name and number, return information about a block."""
anno = get_type_hints(self).get(name)
Expand All @@ -156,7 +164,7 @@ async def _make_block(self, name: str, num: int, block_pv: str, sim: bool = Fals
block = self.verify_block(name, num)

field_annos = get_type_hints(block)
block_pvi = await pvi_get(block_pv) if not sim else None
block_pvi = await pvi_get(block_pv, self.ctxt) if not sim else None

# finds which fields this class actually has, e.g. delay, width...
for sig_name, sig_type in field_annos.items():
Expand Down Expand Up @@ -201,7 +209,7 @@ async def _make_untyped_block(self, block_pv: str):
included dynamically anyway.
"""
block = Device()
block_pvi = await pvi_get(block_pv)
block_pvi = await pvi_get(block_pv, self.ctxt)

for signal_name, signal_pvi in block_pvi.items():
signal_factory = self.pvi_mapping[frozenset(signal_pvi.keys())]
Expand Down Expand Up @@ -236,8 +244,12 @@ async def connect(self, sim=False) -> None:
If there's no pvi information, that's because we're in sim mode. In that case,
makes all required blocks.
"""
pvi = await pvi_get(self._init_prefix + ":PVI") if not sim else None
hints = get_type_hints(self)
pvi = await pvi_get(self._init_prefix + ":PVI", self.ctxt) if not sim else None
hints = {
attr_name: attr_type
for attr_name, attr_type in get_type_hints(self).items()
if not attr_name.startswith("_")
}

# create all the blocks pvi says it should have,
if pvi:
Expand Down
4 changes: 2 additions & 2 deletions tests/test_panda.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ async def sim_panda():
yield sim_panda


def test_panda_names_correct(pva, sim_panda: PandA):
def test_panda_names_correct(sim_panda: PandA):
assert sim_panda.seq[1].name == "sim_panda-seq-1"
assert sim_panda.pulse[1].name == "sim_panda-pulse-1"


async def test_panda_children_connected(pva, sim_panda: PandA):
async def test_panda_children_connected(sim_panda: PandA):
# try to set and retrieve from simulated values...
table = SeqTable(
repeats=np.array([1, 1, 1, 32]).astype(np.uint16),
Expand Down

0 comments on commit 23d4c46

Please sign in to comment.