Skip to contentSkip to conversation

ast: Make PyLibNode support submodules in BBIMPORTS

PATCH New Peter Kjellerstedt 1 participant 1 message Sep 18

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ast: Make PyLibNode support submodules in BBIMPORTS

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.

---

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(-)
lib/bb/parse/ast.py +3 -2
index a372b3534..5f2aaadfb 100644
@@ -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):