diff mbox series

[3/6] data_smart: check for python builtins directly for context lookup

Message ID 20230801143813.2206785-3-richard.purdie@linuxfoundation.org
State Accepted, archived
Commit 9976ae50677b333d646ca0fd395468bd2301d03f
Headers show
Series [1/6] tests.data: add test for inline python calling a def'd function | expand

Commit Message

Richard Purdie Aug. 1, 2023, 2:38 p.m. UTC
From: Christopher Larson <kergoth@gmail.com>

This avoids the need to hardcode a list of python builtins. This also slightly changes behavior, in a case like `${@eval("3")}`, this will ensure we always call the builtin, even if the metadata has an 'eval' variable defined.

Signed-off-by: Christopher Larson <kergoth@gmail.com>
---
 lib/bb/data_smart.py | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)
diff mbox series

Patch

diff --git a/lib/bb/data_smart.py b/lib/bb/data_smart.py
index c597dbade8..fe0bacd13b 100644
--- a/lib/bb/data_smart.py
+++ b/lib/bb/data_smart.py
@@ -16,7 +16,10 @@  BitBake build tools.
 #
 # Based on functions from the base bb module, Copyright 2003 Holger Schurig
 
-import copy, re, sys, traceback
+import builtins
+import copy
+import re
+import sys
 from collections.abc import MutableMapping
 import logging
 import hashlib
@@ -150,17 +153,18 @@  class VariableParse:
             value = utils.better_eval(codeobj, DataContext(self.d), {'d' : self.d})
             return str(value)
 
-
 class DataContext(dict):
+    excluded = set([i for i in dir(builtins) if not i.startswith('_')] + ['bb', 'os', 'oe'])
+
     def __init__(self, metadata, **kwargs):
         self.metadata = metadata
         dict.__init__(self, **kwargs)
         self['d'] = metadata
 
     def __missing__(self, key):
-        # Skip commonly accessed invalid variables
-        if key in ['bb', 'oe', 'int', 'bool', 'time', 'str', 'os']:
+        if key in self.excluded:
             raise KeyError(key)
+
         value = self.metadata.getVar(key)
         if value is None or self.metadata.getVarFlag(key, 'func', False):
             raise KeyError(key)