From 75aaea21aa6db77cd2d516a955e9068e96b15de1 Mon Sep 17 00:00:00 2001 From: Sam Stern Date: Sun, 6 Aug 2023 13:04:28 -0400 Subject: [PATCH] Added parts of an explanation --- scalene/scalene_profiler.py | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/scalene/scalene_profiler.py b/scalene/scalene_profiler.py index c5f48d0a7..dac72306a 100644 --- a/scalene/scalene_profiler.py +++ b/scalene/scalene_profiler.py @@ -2071,14 +2071,17 @@ def run_profiler( the_globals = __main__.__dict__ # Splice in the name of the file being executed instead of the profiler. the_globals["__file__"] = os.path.abspath(progs[0]) - # Some mysterious module foo to make this work the same with -m as with `scalene`. - # the_globals["__spec__"] = spec - # print("NAME", spec.name) + # This part works because of the order in which Python attempts to resolve names-- + # Within a given context, it first tries to look for __package__, and then for __spec__. + # __spec__ is a ModuleSpec object that carries a lot of extra machinery and requires + # extra effort to create (it seems, at least). + # + # __spec__ was originally set to none because the __globals__ here has the Scalene ModuleSpec + # but it doesn't seem like that was enough. Setting the __package__, as below, seems to be enough to make + # it look in the right place + the_globals["__spec__"] = None if spec is not None: - name = spec.name - # MAIN_MOD = '.__name__' - # if name.endswith(MAIN_MOD): - # name = name[:-len(MAIN_MOD)] + name = spec.name the_globals['__package__'] = name.split('.')[0] # Do a GC before we start. gc.collect()