server/msplanner.py
branchstable
changeset 2075 933ccfce6e91
parent 1977 606923dff11b
child 2108 b3f6fa7ad59f
--- a/server/msplanner.py	Tue Jun 09 18:03:28 2009 +0200
+++ b/server/msplanner.py	Tue Jun 09 18:04:31 2009 +0200
@@ -158,6 +158,7 @@
 # XXX move functions below to rql ##############################################
 
 def is_ancestor(n1, n2):
+    """return True if n2 is a parent scope of n1"""
     p = n1.parent
     while p is not None:
         if p is n2:
@@ -171,17 +172,14 @@
         newnode.append(part)
     return newnode
 
-def same_scope(var):
-    """return true if the variable is always used in the same scope"""
-    try:
-        return var.stinfo['samescope']
-    except KeyError:
-        for rel in var.stinfo['relations']:
-            if not rel.scope is var.scope:
-                var.stinfo['samescope'] = False
-                return False
-        var.stinfo['samescope'] = True
-        return True
+def used_in_outer_scope(var, scope):
+    """return true if the variable is used in an outer scope of the given scope
+    """
+    for rel in var.stinfo['relations']:
+        rscope = rel.scope
+        if not rscope is scope and is_ancestor(scope, rscope):
+            return True
+    return False
 
 ################################################################################
 
@@ -1227,6 +1225,7 @@
         self.mayneedvar, self.hasvar = {}, {}
         self.use_only_defined = False
         self.scopes = {rqlst: newroot}
+        self.current_scope = rqlst
         if rqlst.where:
             rqlst = self._rqlst_accept(rqlst, rqlst.where, newroot, terms,
                                        newroot.set_where)
@@ -1397,7 +1396,7 @@
             return False
         if var.name in self.extneedsel or var.stinfo['selected']:
             return False
-        if not same_scope(var):
+        if not var in terms or used_in_outer_scope(var, self.current_scope):
             return False
         if any(v for v, _ in var.stinfo['attrvars'] if not v in terms):
             return False