[entities] Add a safety belt avoid unexpected error in some corner case
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Tue, 12 Dec 2017 17:57:57 +0100
changeset 2915 451328134bee
parent 2914 ae3705aa2ba7
child 2924 919faf33df18
[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.
cubicweb_seda/entities/__init__.py
--- a/cubicweb_seda/entities/__init__.py	Tue Dec 12 17:55:38 2017 +0100
+++ b/cubicweb_seda/entities/__init__.py	Tue Dec 12 17:57:57 2017 +0100
@@ -78,8 +78,10 @@
     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