Try another tactic to figure out entry point.
authorScott Gasch <[email protected]>
Sat, 12 Feb 2022 19:20:27 +0000 (11:20 -0800)
committerScott Gasch <[email protected]>
Sat, 12 Feb 2022 19:20:27 +0000 (11:20 -0800)
bootstrap.py
config.py

index 50af84407f57e504632e868a1b1e2de32a19a671..9bffaf535bf48e310a2006a489ac43242c20f99a 100644 (file)
@@ -241,10 +241,16 @@ def initialize(entry_point):
         # Try to figure out the name of the program entry point.  Then
         # parse configuration (based on cmdline flags, environment vars
         # etc...)
-        if '__globals__' in entry_point.__dict__ and '__file__' in entry_point.__globals__:
-            config.parse(entry_point.__globals__['__file__'])
-        else:
-            config.parse(None)
+        entry_filename = None
+        entry_descr = None
+        try:
+            entry_filename = entry_point.__code__.co_filename
+            entry_descr = entry_point.__code__.__repr__()
+        except Exception:
+            if '__globals__' in entry_point.__dict__ and '__file__' in entry_point.__globals__:
+                entry_filename = entry_point.__globals__['__file__']
+                entry_descr = entry_filename
+        config.parse(entry_filename)
 
         if config.config['trace_memory']:
             import tracemalloc
@@ -287,7 +293,7 @@ def initialize(entry_point):
         random.seed(random_seed)
 
         # Do it, invoke the user's code.  Pay attention to how long it takes.
-        logger.debug('Starting %s (program entry point)', entry_point.__name__)
+        logger.debug('Starting %s (program entry point)', entry_descr)
         ret = None
         import stopwatch
 
@@ -307,7 +313,7 @@ def initialize(entry_point):
             with stopwatch.Timer() as t:
                 ret = entry_point(*args, **kwargs)
 
-        logger.debug('%s (program entry point) returned %s.', entry_point.__name__, ret)
+        logger.debug('%s (program entry point) returned %s.', entry_descr)
 
         if config.config['trace_memory']:
             snapshot = tracemalloc.take_snapshot()
index b2e62c0d0a6e0893e372c8d3334868f2de4ed5ee..46caad15c9db04e44a37e4acd5ccad74161b0643 100644 (file)
--- a/config.py
+++ b/config.py
@@ -172,7 +172,7 @@ def is_flag_already_in_argv(var: str):
     return False
 
 
-def reorder_arg_action_groups(entry_module: Optional[str]):
+def reorder_arg_action_groups_before_help(entry_module: Optional[str]):
     reordered_action_groups = []
     for grp in ARGS._action_groups:
         if entry_module is not None and entry_module in grp.title:  # type: ignore
@@ -269,7 +269,9 @@ def parse(entry_module: Optional[str]) -> Dict[str, Any]:
     # screen w/o scrolling.
     for arg in sys.argv:
         if arg in ('--help', '-h'):
-            ARGS._action_groups = reorder_arg_action_groups(entry_module)
+            if entry_module is not None:
+                entry_module = os.path.basename(entry_module)
+            ARGS._action_groups = reorder_arg_action_groups_before_help(entry_module)
 
     # Examine the environment for variables that match known flags.
     # For a flag called --example_flag the corresponding environment