[PATCH seda] [entities] Add a safety belt avoid unexpected error in some corner case

Sylvain Thenault sylvain.thenault at logilab.fr
Tue Dec 12 18:26:35 CET 2017


# HG changeset patch
# User Sylvain Thénault <sylvain.thenault at logilab.fr>
# Date 1513097877 -3600
#      Tue Dec 12 17:57:57 2017 +0100
# Node ID 451328134bee3f42ec84ed06f41202e114c5ef59
# Parent  ae3705aa2ba756e131eba977ffd12cfcf77da8da
# Available At https://hg.logilab.org/review/cubes/seda
#              hg pull https://hg.logilab.org/review/cubes/seda -r 451328134bee
[entities] Add a safety belt avoid unexpected error in some corner case

e.g. while debugging an instance I attempted to display result of 'Any X, C
WHERE X container C' with unexpectedly to container for the entity, but I wasn't
able to display it because rset.one() raised an error since len(rset) was 2.

Even if the caller may be wrong, it seems ok to not call rset.one() if function
is called in an erroneous context and simply return None in such case.

diff --git a/cubicweb_seda/entities/__init__.py b/cubicweb_seda/entities/__init__.py
--- a/cubicweb_seda/entities/__init__.py
+++ b/cubicweb_seda/entities/__init__.py
@@ -76,12 +76,14 @@ def parent_and_container(entity):
 
 def _seda_container_from_context(rset, entity, **kwargs):
     if entity is None:
         if 'row' in kwargs:
             entity = rset.get_entity(kwargs['row'], kwargs.get('col', 0))
+        elif len(rset) == 1:
+            entity = rset.one()
         else:
-            entity = rset.one()
+            return None
     # protect against unrelated entity types
     if not entity.cw_etype.startswith('SEDA'):
         return None
     if entity.cw_etype != 'SEDAArchiveTransfer':
         entity = parent_and_container(entity)[1]


More information about the saem-devel mailing list