tests.data: add test for inline python calling a def'd function
PATCH 1/6 tests.data: add test for inline python calling a def'd function
From: Christopher Larson <[email protected]>
This is a test for an issue seen long ago, to avoid regressions, where a reference to a def'd function in the metadata would return the string value from the metadata rather than the function in inline python.
--- lib/bb/tests/data.py | 9 +++++++++ 1 file changed, 9 insertions(+)
lib/bb/tests/data.py +9 -0
@@ -77,6 +77,15 @@ class DataExpansions(unittest.TestCase): val = self.d.expand("${@d.getVar('foo') + ' ${bar}'}") self.assertEqual(str(val), "value_of_foo value_of_bar") + def test_python_snippet_function_reference(self): + self.d.setVar("TESTVAL", "testvalue") + self.d.setVar("testfunc", 'd.getVar("TESTVAL")') + self.d.setVarFlag("testfunc", "func", "1") + context = bb.utils.get_context() + context["testfunc"] = lambda d: d.getVar("TESTVAL") + val = self.d.expand("${@testfunc(d)}") + self.assertEqual(str(val), "testvalue") + def test_python_unexpanded(self): self.d.setVar("bar", "${unsetvar}") val = self.d.expand("${@d.getVar('foo') + ' ${bar}'}")
Signature · 1 line
2.39.2
PATCH 2/6 tests.codeparser: add test for exec of builtin from inline python
From: Christopher Larson <[email protected]>
This ensures that any change to the presence of builtins in inline python execs will be noticed.
--- lib/bb/tests/codeparser.py | 6 ++++++ 1 file changed, 6 insertions(+)
lib/bb/tests/codeparser.py +6 -0
@@ -44,6 +44,7 @@ class VariableReferenceTest(ReferenceTest): def parseExpression(self, exp): parsedvar = self.d.expandWithRefs(exp, None) self.references = parsedvar.references + self.execs = parsedvar.execs def test_simple_reference(self): self.setEmptyVars(["FOO"])
@@ -61,6 +62,11 @@ class VariableReferenceTest(ReferenceTest): self.parseExpression("${@d.getVar('BAR') + 'foo'}") self.assertReferences(set(["BAR"])) + def test_python_exec_reference(self): + self.parseExpression("${@eval('3 * 5')}") + self.assertReferences(set()) + self.assertExecs(set(["eval"])) + class ShellReferenceTest(ReferenceTest): def parseExpression(self, exp):
Signature · 1 line
2.39.2
PATCH 3/6 data_smart: check for python builtins directly for context lookup
In reply to Richard PurdieFrom: Christopher Larson <[email protected]>
This avoids the need to hardcode a list of python builtins. This also slightly changes behavior, in a case like ${@eval("3")}, this will ensure we always call the builtin, even if the metadata has an 'eval' variable defined.
--- lib/bb/data_smart.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-)
lib/bb/data_smart.py +8 -4
@@ -16,7 +16,10 @@ BitBake build tools. # # Based on functions from the base bb module, Copyright 2003 Holger Schurig -import copy, re, sys, traceback +import builtins +import copy +import re +import sys from collections.abc import MutableMapping import logging import hashlib
@@ -150,17 +153,18 @@ class VariableParse: value = utils.better_eval(codeobj, DataContext(self.d), {'d' : self.d}) return str(value) - class DataContext(dict): + excluded = set([i for i in dir(builtins) if not i.startswith('_')] + ['bb', 'os', 'oe']) + def __init__(self, metadata, **kwargs): self.metadata = metadata dict.__init__(self, **kwargs) self['d'] = metadata def __missing__(self, key): - # Skip commonly accessed invalid variables - if key in ['bb', 'oe', 'int', 'bool', 'time', 'str', 'os']: + if key in self.excluded: raise KeyError(key) + value = self.metadata.getVar(key) if value is None or self.metadata.getVarFlag(key, 'func', False): raise KeyError(key)
Signature · 1 line
2.39.2
PATCH 4/6 tests.data: add test for builtin preferred over metadata value
In reply to Richard PurdieFrom: Christopher Larson <[email protected]>
This test makes sure that '${@eval()}' calls the eval builtin, even if an 'eval' variable is defined in the metadata.
--- lib/bb/tests/data.py | 4 ++++ 1 file changed, 4 insertions(+)
lib/bb/tests/data.py +4 -0
@@ -86,6 +86,10 @@ class DataExpansions(unittest.TestCase): val = self.d.expand("${@testfunc(d)}") self.assertEqual(str(val), "testvalue") + def test_python_snippet_builtin_metadata(self): + self.d.setVar("eval", "INVALID") + self.d.expand("${@eval('3')}") + def test_python_unexpanded(self): self.d.setVar("bar", "${unsetvar}") val = self.d.expand("${@d.getVar('foo') + ' ${bar}'}")
Signature · 1 line
2.39.2
PATCH 5/6 data_smart: directly check for methodpool functions in context lookup
In reply to Richard PurdieFrom: Christopher Larson <[email protected]>
We previously checked for the existence of the 'func' flag to determine if we should avoid looking up in the metadata. This was done to ensure the user gets the function for 'def' python functions rather than their string contents. We can sidestep the metadata lookup and check our function context directly, instead.
--- lib/bb/data_smart.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)
lib/bb/data_smart.py +4 -3
@@ -154,19 +154,20 @@ class VariableParse: return str(value) class DataContext(dict): - excluded = set([i for i in dir(builtins) if not i.startswith('_')] + ['bb', 'os', 'oe']) + excluded = set([i for i in dir(builtins) if not i.startswith('_')] + ['oe']) def __init__(self, metadata, **kwargs): self.metadata = metadata dict.__init__(self, **kwargs) self['d'] = metadata + self.context = set(bb.utils.get_context()) def __missing__(self, key): - if key in self.excluded: + if key in self.excluded or key in self.context: raise KeyError(key) value = self.metadata.getVar(key) - if value is None or self.metadata.getVarFlag(key, 'func', False): + if value is None: raise KeyError(key) else: return value
Signature · 1 line
2.39.2
PATCH 6/6 bb.tests.data: don't require the func flag for context functions
In reply to Richard PurdieFrom: Christopher Larson <[email protected]>
Update test_python_snippet_function_reference to not require the 'func' flag, now that we know the real function will be returned for context functions without the flag.
--- lib/bb/tests/data.py | 1 - 1 file changed, 1 deletion(-)
lib/bb/tests/data.py +0 -1
@@ -80,7 +80,6 @@ class DataExpansions(unittest.TestCase): def test_python_snippet_function_reference(self): self.d.setVar("TESTVAL", "testvalue") self.d.setVar("testfunc", 'd.getVar("TESTVAL")') - self.d.setVarFlag("testfunc", "func", "1") context = bb.utils.get_context() context["testfunc"] = lambda d: d.getVar("TESTVAL") val = self.d.expand("${@testfunc(d)}")
Signature · 1 line
2.39.2