diff mbox series

[v2,2/2] utils: Make set_context() replace the global context

Message ID 20261117203716.3276852-2-maxime.roussinbelanger@gmail.com
State New
Headers show
Series [v2,1/2] tests/data: Remove testfunc from the global context after use | expand

Commit Message

Maxime Roussin-Belanger Nov. 17, 2026, 8:37 p.m. UTC
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(+)
diff mbox series

Patch

diff --git a/lib/bb/tests/utils.py b/lib/bb/tests/utils.py
index 0c3360505..4dd643e56 100644
--- a/lib/bb/tests/utils.py
+++ b/lib/bb/tests/utils.py
@@ -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)}")
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