Skip to content

Commit b300c6e

Browse files
dbiebercopybara-github
authored andcommitted
Only determine the context if --interactive is set or if no component is supplied.
PiperOrigin-RevId: 254792343 Change-Id: I396e02dc7140ede46e5ce010c504763f65882bbc
1 parent 76f2a87 commit b300c6e

1 file changed

Lines changed: 17 additions & 13 deletions

File tree

‎fire/core.py‎

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -120,16 +120,22 @@ def Fire(component=None, command=None, name=None):
120120
raise ValueError('The command argument must be a string or a sequence of '
121121
'arguments.')
122122

123-
# Determine the calling context.
124-
caller = inspect.stack()[1]
125-
caller_frame = caller[0]
126-
caller_globals = caller_frame.f_globals
127-
caller_locals = caller_frame.f_locals
123+
args, flag_args = parser.SeparateFlagArgs(args)
124+
125+
argparser = parser.CreateParser()
126+
parsed_flag_args, unused_args = argparser.parse_known_args(flag_args)
127+
128128
context = {}
129-
context.update(caller_globals)
130-
context.update(caller_locals)
129+
if parsed_flag_args.interactive or component is None:
130+
# Determine the calling context.
131+
caller = inspect.stack()[1]
132+
caller_frame = caller[0]
133+
caller_globals = caller_frame.f_globals
134+
caller_locals = caller_frame.f_locals
135+
context.update(caller_globals)
136+
context.update(caller_locals)
131137

132-
component_trace = _Fire(component, args, context, name)
138+
component_trace = _Fire(component, args, parsed_flag_args, context, name)
133139

134140
if component_trace.HasError():
135141
_DisplayError(component_trace)
@@ -329,7 +335,7 @@ def _OneLineResult(result):
329335
return str(result).replace('\n', ' ')
330336

331337

332-
def _Fire(component, args, context, name=None):
338+
def _Fire(component, args, parsed_flag_args, context, name=None):
333339
"""Execute a Fire command on a target component using the args supplied.
334340
335341
Arguments that come after a final isolated '--' are treated as Flags, eg for
@@ -361,6 +367,8 @@ def _Fire(component, args, context, name=None):
361367
component: The target component for Fire.
362368
args: A list of args to consume in Firing on the component, usually from
363369
the command line.
370+
parsed_flag_args: The values of the flag args (e.g. --verbose, --separator)
371+
that are part of every Fire CLI.
364372
context: A dict with the local and global variables available at the call
365373
to Fire.
366374
name: Optional. The name of the command. Used in interactive mode and in
@@ -372,10 +380,6 @@ def _Fire(component, args, context, name=None):
372380
ValueError: If there are arguments that cannot be consumed.
373381
ValueError: If --completion is specified but no name available.
374382
"""
375-
args, flag_args = parser.SeparateFlagArgs(args)
376-
377-
argparser = parser.CreateParser()
378-
parsed_flag_args, unused_args = argparser.parse_known_args(flag_args)
379383
verbose = parsed_flag_args.verbose
380384
interactive = parsed_flag_args.interactive
381385
separator = parsed_flag_args.separator

0 commit comments

Comments
 (0)