From 26fc3a8d3dd67ad0e38869a46eda1f7ce449dfb6 Mon Sep 17 00:00:00 2001 From: Edwin Balani Date: Fri, 15 May 2026 23:43:02 +0100 Subject: [PATCH] sim: Ensure custom import machinery doesn't hide builtin modules The UnderscoreFinder is intended to override the import-time finding of selected modules, but it is implemented by completely wrapping the original BuiltinImporter that would be found in sys.meta_path at startup. Therefore the UnderscoreFinder is obliged to pass through all find_spec() calls that it doesn't otherwise override, to avoid accidentally breaking importation of other modules. (It doesn't have to consult the wrapped PathFinder by default, because we add another PathFinder to sys.meta_path.) --- sim/run.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/sim/run.py b/sim/run.py index 45908e2..ede99d0 100755 --- a/sim/run.py +++ b/sim/run.py @@ -43,10 +43,14 @@ def __init__(self, builtin, pathfinder): self.pathfinder = pathfinder def find_spec(self, fullname, path, target=None): + # Our fake 'time' module wants to import _time as a backdoor to the + # original time module, which is a builtin C module. Invoke the + # BuiltinImporter with "time" to satisfy this. if fullname == "_time": return self.builtin.find_spec("time", path, target) - if fullname in ["random", "math"]: - return self.builtin.find_spec(fullname, path, target) + + # Suppress the simulator's sys.path prefixed entries temporarily while + # looking for 'json' or 'tarfile'. if fullname in ["json", "tarfile"]: sys_path_saved = sys.path sys.path = sys_path_orig @@ -54,6 +58,11 @@ def find_spec(self, fullname, path, target=None): sys.path = sys_path_saved return res + # Provide a general fallback to other builtin (C) modules. + # It's OK to do this indiscriminately as the BuiltinImporter won't find + # modules that aren't builtin. + return self.builtin.find_spec(fullname, path, target) + # sys.meta_path.insert(0, Hook())