Skip to contentSkip to conversation

codeparser: Skipping typing when inspecting Python modules

PATCH Superseded Pedro Ferreira 3 participants 3 messages Mar 6, 2025

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 codeparser: Skipping typing when inspecting Python modules

From: Pedro Ferreira <[email protected]>

If a custom python module is added thru BBIMPORTS and it uses typing(Any,Tuple,Union...), codeparser will fail because inspect.py raises TypeError exception if the object is a built-in module, class, or function.

---
 lib/bb/codeparser.py | 22 ++++++++++++++--------
 1 file changed, 14 insertions(+), 8 deletions(-)
lib/bb/codeparser.py +14 -8
index d249af326..7f6adaf36 100644
@@ -72,15 +72,21 @@ def add_module_functions(fn, functions, namespace):
             parser.parse_python(None, filename=fn, lineno=1, fixedhash=fixedhash+f)
             #bb.warn("Cached %s" % f)
         except KeyError:
-            targetfn = inspect.getsourcefile(functions[f])
-            if fn != targetfn:
-                # Skip references to other modules outside this file
-                #bb.warn("Skipping %s" % name)
+            try:
+                targetfn = inspect.getsourcefile(functions[f])
+                if fn != targetfn:
+                    # Skip references to other modules outside this file
+                    #bb.warn("Skipping %s" % name)
+                    continue
+                lines, lineno = inspect.getsourcelines(functions[f])
+                src = "".join(lines)
+                parser.parse_python(src, filename=fn, lineno=lineno, fixedhash=fixedhash+f)
+                #bb.warn("Not cached %s" % f)
+            except TypeError:
+                # Since Python inspect wont support special types
+                # Skip references of typing special forms, aliases and tuples
+                bb.warn("Skipping refs to %s : %s" % (f, type(functions[f]).__name__))
                 continue
-            lines, lineno = inspect.getsourcelines(functions[f])
-            src = "".join(lines)
-            parser.parse_python(src, filename=fn, lineno=lineno, fixedhash=fixedhash+f)
-            #bb.warn("Not cached %s" % f)
         execs = parser.execs.copy()
         # Expand internal module exec references
         for e in parser.execs:
Signature · 1 line
2.34.1
#1 lore raw
Quoted 38 lines · Pedro Ferreira via lists.openembedded.org

From: Pedro Ferreira <[email protected]>

If a custom python module is added thru BBIMPORTS and it uses typing(Any,Tuple,Union...), codeparser will fail because inspect.py raises TypeError exception if the object is a built-in module, class, or function.

---
 lib/bb/codeparser.py | 22 ++++++++++++++--------

 1 file changed, 14 insertions(+), 8 deletions(-)
lib/bb/codeparser.py +0 -0
index d249af326..7f6adaf36 100644
@@ -72,15 +72,21 @@ def add_module_functions(fn, functions, namespace):
              parser.parse_python(None, filename=fn, lineno=1, fixedhash=fixedhash+f)
              #bb.warn("Cached %s" % f)

         except KeyError:
-            targetfn = inspect.getsourcefile(functions[f])
-            if fn != targetfn:
-                # Skip references to other modules outside this file
-                #bb.warn("Skipping %s" % name)
+            try:
+                targetfn = inspect.getsourcefile(functions[f])
+                if fn != targetfn:
+                    # Skip references to other modules outside this file
+                    #bb.warn("Skipping %s" % name)
+                    continue
+                lines, lineno = inspect.getsourcelines(functions[f])
+                src = "".join(lines)
+                parser.parse_python(src, filename=fn, lineno=lineno, fixedhash=fixedhash+f) +                #bb.warn("Not cached %s" % f)
+            except TypeError:
+                # Since Python inspect wont support special types
+                # Skip references of typing special forms, aliases and tuples +                bb.warn("Skipping refs to %s : %s" % (f, type(functions[f]).__name__))

Quoted 9 lines

                 continue
-            lines, lineno = inspect.getsourcelines(functions[f])
-            src = "".join(lines)
-            parser.parse_python(src, filename=fn, lineno=lineno, fixedhash=fixedhash+f) -            #bb.warn("Not cached %s" % f)
         execs = parser.execs.copy()
         # Expand internal module exec references
         for e in parser.execs:

This may be a good reason not to use typing in python module code we're importing. Catching and ignoring all TypeErrors like this sounds like it will cause us other problems in the future by hiding real errors :(

Cheers,

Richard

#2 lore raw
Quoted 46 lines · Richard Purdie via lists.openembedded.org
Quoted 44 lines · Pedro Ferreira via lists.openembedded.org

From: Pedro Ferreira <[email protected]>

If a custom python module is added thru BBIMPORTS and it uses typing(Any,Tuple,Union...), codeparser will fail because inspect.py raises TypeError exception if the object is a built-in module, class, or function.

---
 lib/bb/codeparser.py | 22 ++++++++++++++--------
 1 file changed, 14 insertions(+), 8 deletions(-)
lib/bb/codeparser.py +14 -8
index d249af326..7f6adaf36 100644
@@ -72,15 +72,21 @@ def add_module_functions(fn, functions, namespace):
             parser.parse_python(None, filename=fn, lineno=1, fixedhash=fixedhash+f)
             #bb.warn("Cached %s" % f)
         except KeyError:
-            targetfn = inspect.getsourcefile(functions[f])
-            if fn != targetfn:
-                # Skip references to other modules outside this file
-                #bb.warn("Skipping %s" % name)
+            try:
+                targetfn = inspect.getsourcefile(functions[f])
+                if fn != targetfn:
+                    # Skip references to other modules outside this file
+                    #bb.warn("Skipping %s" % name)
+                    continue
+                lines, lineno = inspect.getsourcelines(functions[f])
+                src = "".join(lines)
+                parser.parse_python(src, filename=fn, lineno=lineno, fixedhash=fixedhash+f)
+                #bb.warn("Not cached %s" % f)
+            except TypeError:
+                # Since Python inspect wont support special types
+                # Skip references of typing special forms, aliases and tuples
+                bb.warn("Skipping refs to %s : %s" % (f, type(functions[f]).__name__))
                 continue
-            lines, lineno = inspect.getsourcelines(functions[f])
-            src = "".join(lines)
-            parser.parse_python(src, filename=fn, lineno=lineno, fixedhash=fixedhash+f)
-            #bb.warn("Not cached %s" % f)
         execs = parser.execs.copy()
Quoted 8 lines

lib/bb/codeparser.py def add_module_functions(fn, functions, namespace): · line 92

Quoted 3 lines
lib/bb/codeparser.py +0 -0
         # Expand internal module exec references
         for e in parser.execs:

This may be a good reason not to use typing in python module code we're importing. Catching and ignoring all TypeErrors like this sounds like it will cause us other problems in the future by hiding real errors :(

This could be OK. The API definition of getsourcefile() clearly states it rasies TypeError on a builtin, and None if the file is not found, so we can tell the difference. That said, it would be better to scope it as tightly as possible to prevent hiding other errors in the code

try:
targetfn = inspect.getsourcefile(functions[f])
except TypeError:
# Builtin
continue

Quoted 13 lines

Cheers,

Richard

-=-=-=-=-=-=-=-=-=-=-=-
Links: You receive all messages sent to this group.
View/Reply Online (#17396): https://lists.openembedded.org/g/bitbake-devel/message/17396
Mute This Topic: https://lists.openembedded.org/mt/111549472/3616693
Group Owner: [email protected]
Unsubscribe: https://lists.openembedded.org/g/bitbake-devel/unsub [[email protected]]
-=-=-=-=-=-=-=-=-=-=-=-