[selectors] use before-registry-reset event to init is_instance cache: cleaner code and avoid reloading bug (making hooks test fail for instance)
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Mon, 05 Jul 2010 13:35:15 +0200
changeset 5881 57387070f612
parent 5880 a022dcbed0d3
child 5882 4c7a0b139830
[selectors] use before-registry-reset event to init is_instance cache: cleaner code and avoid reloading bug (making hooks test fail for instance)
__init__.py
hooks/syncschema.py
selectors.py
--- a/__init__.py	Mon Jul 05 13:34:29 2010 +0200
+++ b/__init__.py	Mon Jul 05 13:35:15 2010 +0200
@@ -17,8 +17,8 @@
 # with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
 """CubicWeb is a generic framework to quickly build applications which describes
 relations between entitites.
+"""
 
-"""
 __docformat__ = "restructuredtext en"
 
 # ignore the pygments UserWarnings
--- a/hooks/syncschema.py	Mon Jul 05 13:34:29 2010 +0200
+++ b/hooks/syncschema.py	Mon Jul 05 13:35:15 2010 +0200
@@ -1050,7 +1050,7 @@
 class AfterUpdateCWRDefHook(SyncSchemaHook):
     __regid__ = 'syncaddcwattribute'
     __select__ = SyncSchemaHook.__select__ & is_instance('CWAttribute',
-                                                        'CWRelation')
+                                                         'CWRelation')
     events = ('before_update_entity',)
 
     def __call__(self):
--- a/selectors.py	Mon Jul 05 13:34:29 2010 +0200
+++ b/selectors.py	Mon Jul 05 13:35:15 2010 +0200
@@ -202,7 +202,8 @@
 
 from yams import BASE_TYPES
 
-from cubicweb import Unauthorized, NoSelectableObject, NotAnEntity, role
+from cubicweb import (Unauthorized, NoSelectableObject, NotAnEntity,
+                      CW_EVENT_MANAGER, role)
 # even if not used, let yes here so it's importable through this module
 from cubicweb.appobject import Selector, objectify_selector, lltrace, yes
 from cubicweb.schema import split_expression
@@ -698,6 +699,10 @@
             score += score_interface(etypesreg, cls_or_inst, cls, iface)
         return score
 
+def _reset_is_instance_cache(vreg):
+    vreg._is_instance_selector_cache = {}
+
+CW_EVENT_MANAGER.bind('before-registry-reset', _reset_is_instance_cache)
 
 class is_instance(EClassSelector):
     """Return non-zero score for entity that is an instance of the one of given
@@ -728,10 +733,7 @@
 
     def score_etypes(self, req, cls_or_inst, cls):
         # cache on vreg to avoid reloading issues
-        try:
-            cache = req.vreg.__is_instance_cache
-        except AttributeError:
-            cache = req.vreg.__is_instance_cache = {}
+        cache = req.vreg._is_instance_selector_cache
         try:
             expected_eclasses = cache[self]
         except KeyError: