diff mbox series

ast: Make PyLibNode support submodules in BBIMPORTS

Message ID 20260918194806.510401-1-pkj@axis.com
State New
Headers show
Series ast: Make PyLibNode support submodules in BBIMPORTS | expand

Commit Message

Peter Kjellerstedt Sept. 18, 2026, 7:48 p.m. UTC
For a given `addpylib <path> <namespace>`, the existing code would
import all modules specified in `BBIMPORTS` with the assumption that
there are no submodules. This adds support for specifying submodules as
well. E.g., `BBIMPORTS = "foo.bar"` will now make `<namespace>.foo.bar`
available.

Signed-off-by: Peter Kjellerstedt <peter.kjellerstedt@axis.com>
---

In theory, one could do `addpylib <lib>/<namespace> foo` and then use
`BBIMPORTS = "bar"`. However, any existing `import <namespace>.foo.bar`
in the code would not match the import made via `BBIMPORTS`.

 lib/bb/parse/ast.py | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
diff mbox series

Patch

diff --git a/lib/bb/parse/ast.py b/lib/bb/parse/ast.py
index a372b3534..5f2aaadfb 100644
--- a/lib/bb/parse/ast.py
+++ b/lib/bb/parse/ast.py
@@ -310,8 +310,9 @@  class PyLibNode(AstNode):
             bb.utils._context[self.namespace] = __import__(self.namespace)
             toimport = getattr(bb.utils._context[self.namespace], "BBIMPORTS", [])
             for i in toimport:
-                bb.utils._context[self.namespace] = __import__(self.namespace + "." + i)
-                mod = getattr(bb.utils._context[self.namespace], i)
+                mod = bb.utils._context[self.namespace] = __import__(self.namespace + "." + i)
+                for submod in i.split("."):
+                    mod = getattr(mod, submod)
                 fn = getattr(mod, "__file__")
                 funcs = {}
                 for f in dir(mod):