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

Commit

Permalink
Trac #17693, comment 36, 34: raise exceptions when merge is not possible
Browse files Browse the repository at this point in the history
  • Loading branch information
dkrenn committed Sep 28, 2015
1 parent 3de0a1d commit 485b46f
Showing 1 changed file with 20 additions and 6 deletions.
26 changes: 20 additions & 6 deletions src/sage/data_structures/mutable_poset.py
Original file line number Diff line number Diff line change
Expand Up @@ -1133,14 +1133,14 @@ def merge(self, element, check=True, delete=True):
Merge the given element with the element contained in this
shell.
INPUT:
- ``element`` -- an element (of the poset).
- ``check`` -- (default: ``True``) if set, then the
``can_merge``-function of :class:`MutablePoset` determines
whether the merge is possible.
whether the merge is possible. ``can_merge`` is ``None`` means
that this check is always passed.
- ``delete`` -- (default: ``True``) if set, then ``element``
is removed from the poset after the merge.
Expand Down Expand Up @@ -1180,14 +1180,25 @@ def merge(self, element, check=True, delete=True):
:meth:`MutablePoset.merge`,
:class:`MutablePoset`.
TESTS::
sage: MP([2], merge=operator.add,
....: can_merge=lambda _, __: False).shell(2).merge(1)
Traceback (most recent call last):
...
RuntimeError: Cannot merge 2 with 1.
"""
poset = self.poset
if poset._merge_ is None:
# poset._merge_ is None means no merge (poset._merge_ simply
# returns its first input argument).
return
self_element = self.element
if check and poset._can_merge_ is not None and \
not poset._can_merge_(self_element, element):
return
if check:
if not poset._can_merge_(self_element, element):
raise RuntimeError('Cannot merge %s with %s.' %
(self_element, element))
new = poset._merge_(self_element, element)
if new is None:
poset.discard(poset.get_key(self.element))
Expand Down Expand Up @@ -1341,7 +1352,10 @@ def __init__(self, data=None, key=None, merge=None, can_merge=None):
self._key_ = key

self._merge_ = merge
self._can_merge_ = can_merge
if can_merge is None:
self._can_merge_ = lambda _, __: True
else:
self._can_merge_ = can_merge

if data is not None:
try:
Expand Down

0 comments on commit 485b46f

Please sign in to comment.