| Message ID | 20261117152318.2560775-1-maxime.roussinbelanger@gmail.com |
|---|---|
| State | New |
| Headers | show |
| Series | utils: Make set_context() replace the global context | expand |
On Tue Nov 17, 2026 at 4:23 PM CET, Maxime Roussin-Bélanger via bitbake-devel wrote: > 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> > --- Hi Maxime, Thanks for your patch. Yet it looks like one of the added test is failing here: FAIL: test_set_context_drops_methods (bb.tests.utils.ContextTests.test_set_context_drops_methods) ---------------------------------------------------------------------- Traceback (most recent call last): File "/srv/pokybuild/yocto-worker/oe-selftest-fedora/build/layers/bitbake/lib/bb/tests/utils.py", line 752, in test_set_context_drops_methods self.assertNotIn("testfunc", bb.utils.get_context()) ~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ AssertionError: 'testfunc' unexpectedly found in {...} https://autobuilder.yoctoproject.org/valkyrie/#/builders/48/builds/5093 https://autobuilder.yoctoproject.org/valkyrie/#/builders/35/builds/5285 Can you have a look at the issue? Thanks, Mathieu
diff --git a/lib/bb/tests/utils.py b/lib/bb/tests/utils.py index 0c3360505..796f8398d 100644 --- a/lib/bb/tests/utils.py +++ b/lib/bb/tests/utils.py @@ -729,3 +729,25 @@ 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() + self.context = bb.utils.get_context().copy() + + def tearDown(self): + # Not set_context(), so a broken setter cannot leak into other tests. + bb.utils._context = self.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(self.context.copy()) + self.assertNotIn("testfunc", bb.utils.get_context()) + self.assertRaises(bb.data_smart.ExpansionError, self.d.expand, "${@testfunc(d)}") diff --git a/lib/bb/utils.py b/lib/bb/utils.py index 0e2677f8e..78cb683b1 100644 --- a/lib/bb/utils.py +++ b/lib/bb/utils.py @@ -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> --- lib/bb/tests/utils.py | 22 ++++++++++++++++++++++ lib/bb/utils.py | 1 + 2 files changed, 23 insertions(+)