Skip to content

Commit fe8deda

Browse files
authored
iw.current_index is now non-reentrant (#829)
1 parent 0de38a7 commit fe8deda

File tree

1 file changed

+37
-24
lines changed

1 file changed

+37
-24
lines changed

fastplotlib/widgets/image_widget/_widget.py

Lines changed: 37 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -195,33 +195,44 @@ def current_index(self, index: dict[str, int]):
195195
if not self._initialized:
196196
return
197197

198-
if not set(index.keys()).issubset(set(self._current_index.keys())):
199-
raise KeyError(
200-
f"All dimension keys for setting `current_index` must be present in the widget sliders. "
201-
f"The dimensions currently used for sliders are: {list(self.current_index.keys())}"
202-
)
198+
if self._reentrant_block:
199+
return
203200

204-
for k, val in index.items():
205-
if not isinstance(val, int):
206-
raise TypeError("Indices for all dimensions must be int")
207-
if val < 0:
208-
raise IndexError("negative indexing is not supported for ImageWidget")
209-
if val > self._dims_max_bounds[k]:
210-
raise IndexError(
211-
f"index {val} is out of bounds for dimension '{k}' "
212-
f"which has a max bound of: {self._dims_max_bounds[k]}"
201+
try:
202+
self._reentrant_block = True # block re-execution until current_index has *fully* completed execution
203+
if not set(index.keys()).issubset(set(self._current_index.keys())):
204+
raise KeyError(
205+
f"All dimension keys for setting `current_index` must be present in the widget sliders. "
206+
f"The dimensions currently used for sliders are: {list(self.current_index.keys())}"
213207
)
214208

215-
self._current_index.update(index)
209+
for k, val in index.items():
210+
if not isinstance(val, int):
211+
raise TypeError("Indices for all dimensions must be int")
212+
if val < 0:
213+
raise IndexError("negative indexing is not supported for ImageWidget")
214+
if val > self._dims_max_bounds[k]:
215+
raise IndexError(
216+
f"index {val} is out of bounds for dimension '{k}' "
217+
f"which has a max bound of: {self._dims_max_bounds[k]}"
218+
)
216219

217-
for i, (ig, data) in enumerate(zip(self.managed_graphics, self.data)):
218-
frame = self._process_indices(data, self._current_index)
219-
frame = self._process_frame_apply(frame, i)
220-
ig.data = frame
220+
self._current_index.update(index)
221221

222-
# call any event handlers
223-
for handler in self._current_index_changed_handlers:
224-
handler(self.current_index)
222+
for i, (ig, data) in enumerate(zip(self.managed_graphics, self.data)):
223+
frame = self._process_indices(data, self._current_index)
224+
frame = self._process_frame_apply(frame, i)
225+
ig.data = frame
226+
227+
# call any event handlers
228+
for handler in self._current_index_changed_handlers:
229+
handler(self.current_index)
230+
except Exception as exc:
231+
# raise original exception
232+
raise exc # current_index setter has raised. The lines above below are probably more relevant!
233+
finally:
234+
# set_value has finished executing, now allow future executions
235+
self._reentrant_block = False
225236

226237
@property
227238
def n_img_dims(self) -> list[int]:
@@ -574,10 +585,12 @@ def __init__(
574585

575586
self.figure.add_gui(self._image_widget_sliders)
576587

577-
self._initialized = True
578-
579588
self._current_index_changed_handlers = set()
580589

590+
self._reentrant_block = False
591+
592+
self._initialized = True
593+
581594
@property
582595
def frame_apply(self) -> dict | None:
583596
return self._frame_apply

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy