@@ -729,3 +729,26 @@ class SplitFileChecksums(unittest.TestCase):
self.check("/a:Truex /b:False", ["/a:Truex /b:False"])
self.check(":True :False", [":True", ":False"])
self.check("/a:True:False /b:False", ["/a:True:False", "/b:False"])
+
+class ContextTests(unittest.TestCase):
+ def setUp(self):
+ self.d = bb.data.init()
+ # Not set_context(), so a broken setter cannot leak into other tests.
+ self.saved_context = bb.utils._context
+ bb.utils._context = bb.utils.clean_context()
+
+ def tearDown(self):
+ bb.utils._context = self.saved_context
+
+ def test_set_context(self):
+ context = bb.utils.clean_context()
+ bb.utils.set_context(context)
+ self.assertIs(bb.utils.get_context(), context)
+
+ def test_set_context_drops_methods(self):
+ bb.methodpool.insert_method("testfunc", "def testfunc(d):\n return 'testvalue'\n", "test.bb", 1)
+ self.assertEqual(self.d.expand("${@testfunc(d)}"), "testvalue")
+
+ bb.utils.set_context(bb.utils.clean_context())
+ self.assertNotIn("testfunc", bb.utils.get_context())
+ self.assertRaises(bb.data_smart.ExpansionError, self.d.expand, "${@testfunc(d)}")
@@ -52,6 +52,7 @@ def get_context():
def set_context(ctx):
+ global _context
_context = ctx
# Context used in better_exec, eval
set_context() assigns to a local name, so the global context is never replaced and keeps everything added to it. This has been the case since commit 6c3281a14012 ("cooker/cookerdata/utils: Improve context management") replaced a direct assignment to bb.utils._context with it, which broke the two resets that rely on it: - Parser.parse() resets the context before each recipe, so a parser process accumulates the Python functions of every recipe it parsed before. A recipe calling a function from a class it does not inherit parses or fails depending on which process it lands in. - CookerDataBuilder.__init__() resets the context when the configuration is parsed again, so a long-lived server keeps the functions and modules of the previous configuration. AI-Generated: Uses Claude Code (Claude Opus 5.5) Signed-off-by: Maxime Roussin-Belanger <maxime.roussinbelanger@gmail.com> --- Changes in v2: - Run ContextTests on a clean context instead of a copy of the global one, which already held testfunc when bb.tests.data ran first in the same process (autobuilder failure reported on v1). - New patch 1/2 stops bb.tests.data leaking testfunc into the global context. lib/bb/tests/utils.py | 23 +++++++++++++++++++++++ lib/bb/utils.py | 1 + 2 files changed, 24 insertions(+)