浏览代码

bitbake: data_smart/utils: Add 'd' to the context used for better_eval in python expansion

If a line like:

foo=${@' '.join([d.getVar('D', True) + x for x in (' '.join([d.getVar('FILES_bash-' + p, True) or '' for p in ['lib', 'dev', 'staticdev', 'doc', 'locale', 'ptest']])).split()])}

is added to a function like do_install, it fails with Exception name 'd'
is not defined. This is due to a change of behaviour in python 3 compared
to python 2. Generator expressions, dict comprehensions  and set comprehensions
are executed in a new scope but list comprehensions in python 2.x are not. In
python 3 they all use a new scope.

To allow these kinds of expressions to work, the easiest approach is
to add 'd' to the global context. To do this, an extra optional parameter
is added to better_eval and we use that to add 'd'.

(Bitbake rev: 8f74881037bb01013d3d439dc0c269909a198c1c)

Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
Richard Purdie 9 年之前
父节点
当前提交
f2e5d88461
共有 2 个文件被更改,包括 9 次插入3 次删除
  1. 1 1
      bitbake/lib/bb/data_smart.py
  2. 8 2
      bitbake/lib/bb/utils.py

+ 1 - 1
bitbake/lib/bb/data_smart.py

@@ -135,7 +135,7 @@ class VariableParse:
                     self.contains[k] = parser.contains[k].copy()
                 else:
                     self.contains[k].update(parser.contains[k])
-            value = utils.better_eval(codeobj, DataContext(self.d))
+            value = utils.better_eval(codeobj, DataContext(self.d), {'d' : self.d})
             return str(value)
 
 

+ 8 - 2
bitbake/lib/bb/utils.py

@@ -37,6 +37,7 @@ import errno
 import signal
 import ast
 import collections
+import copy
 from subprocess import getstatusoutput
 from contextlib import contextmanager
 from ctypes import cdll
@@ -407,8 +408,13 @@ def better_exec(code, context, text = None, realfile = "<code>", pythonexception
 def simple_exec(code, context):
     exec(code, get_context(), context)
 
-def better_eval(source, locals):
-    return eval(source, get_context(), locals)
+def better_eval(source, locals, extraglobals = None):
+    ctx = get_context()
+    if extraglobals:
+        ctx = copy.copy(ctx)
+        for g in extraglobals:
+            ctx[g] = extraglobals[g]
+    return eval(source, ctx, locals)
 
 @contextmanager
 def fileslocked(files):