Skip to content

Commit

Permalink
fix: _remove_element failing silently when child objects left over
Browse files Browse the repository at this point in the history
  • Loading branch information
iisakkirotko committed Apr 17, 2024
1 parent d6e022f commit e49f97c
Showing 1 changed file with 15 additions and 9 deletions.
24 changes: 15 additions & 9 deletions reacton/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1476,7 +1476,7 @@ def format(reason: RerenderReason):
for el in self._shared_elements_next:
logger.debug("\t%r %x", el, id(el))

logger.debug("Current elements:")
logger.debug("Current elements: %r", self._shared_elements)
for el in self._shared_elements:
logger.debug("\t %r %x", el, id(el))
if self.context_root.exceptions_children:
Expand Down Expand Up @@ -1564,7 +1564,10 @@ def format(reason: RerenderReason):

value = html.escape(error)
from . import ipywidgets as w
from pprint import pprint

print("EXCEPTION FROM REACTON", value, "\n")
pprint(self.container)
return self.render(w.HTML(value="<pre>" + value + "</pre>", layout=w.Layout(overflow="auto")), self.container)
else:
raise exc
Expand Down Expand Up @@ -2129,6 +2132,17 @@ def _remove_element(self, el: Element, default_key: str, parent_key):
new_parent_key = join_key(parent_key, key)
self._remove_element(self.context.root_element, "/", parent_key=new_parent_key)
finally:
try:
assert not child_context.elements, f"left over elements {child_context.elements}"
assert not child_context.element_to_widget, f"left over element_to_widget {child_context.element_to_widget}"
assert not child_context.widgets, f"left over widgets {child_context.widgets}"
assert not child_context.children, f"left over children {child_context.children}"
assert not child_context.owns, f"left over owns {child_context.owns}"
# TODO: this is not the case when an exception occurs
# assert not child_context.children_next, f"left over children {child_context.children_next}"
except Exception as e:
child_context.exceptions_self.append(e)

# restore context
self.context = context
if child_context.exceptions_self or child_context.exceptions_children and not child_context.exception_handler:
Expand Down Expand Up @@ -2163,14 +2177,6 @@ def _remove_element(self, el: Element, default_key: str, parent_key):
if el in context.element_to_widget:
del context.element_to_widget[el]
del context.elements[key]
if isinstance(el.component, ComponentFunction):
assert not child_context.elements, f"left over elements {child_context.elements}"
assert not child_context.element_to_widget, f"left over element_to_widget {child_context.element_to_widget}"
assert not child_context.widgets, f"left over widgets {child_context.widgets}"
assert not child_context.children, f"left over children {child_context.children}"
assert not child_context.owns, f"left over owns {child_context.owns}"
# TODO: this is not the case when an exception occurs
# assert not child_context.children_next, f"left over children {child_context.children_next}"

def _visit_children(self, el: Element, default_key: str, parent_key: str, f: Callable):
assert self.context is not None
Expand Down

0 comments on commit e49f97c

Please sign in to comment.