@@ -123,14 +123,25 @@ def _find_on_path(cmd, full_cmd):
123123
124124def _replace_templates (cmd , line , windowed ):
125125 # Override can be the entire line or just the first argument
126- shebang = re .match (r"#!\s*(.+)(.*)" , line ) or re .match (r"#!\s*([^\s]+)(.*)" , line )
126+ # Override can be the entire line (including args) or just the first argument
127+ m = re .match (r"^#!\s*([^\s]+)(.*)$" , line )
127128
128- if not shebang or shebang . group ( 1 ) not in cmd . shebang_templates :
129+ if not m :
129130 return None , None
130131
131- new_cmd = cmd .shebang_templates [shebang .group (1 )]
132+ full_key = (m .group (1 ) + m .group (2 )).strip ()
133+ if full_key in cmd .shebang_templates :
134+ template_key = full_key
135+ suffix = ""
136+ elif m .group (1 ) in cmd .shebang_templates :
137+ template_key = m .group (1 )
138+ suffix = m .group (2 )
139+ else :
140+ return None , None
141+
142+ new_cmd = cmd .shebang_templates [template_key ]
132143 LOGGER .verbose ("Using '%s' from configuration file in place of shebang '%s'" ,
133- new_cmd , shebang . group ( 1 ) )
144+ new_cmd , template_key )
134145 install = None
135146 if new_cmd .startswith ("py -V:" ):
136147 install = cmd .get_install_to_run (new_cmd [6 :], windowed = windowed )
@@ -146,7 +157,7 @@ def _replace_templates(cmd, line, windowed):
146157 install = cmd .get_install_to_run (windowed = True )
147158 else :
148159 # Recreate the shebang with the alternate command and continue.
149- line = f"#!{ new_cmd } { shebang . group ( 2 ) } "
160+ line = f"#!{ new_cmd } { suffix } "
150161 return install , line
151162
152163
@@ -237,6 +248,7 @@ def _parse_shebang(cmd, line, *, windowed=None):
237248 except LookupError as ex :
238249 LOGGER .error ("Could not launch '%s'. Using default interpreter "
239250 "instead." , full_cmd )
251+ raise
240252 else :
241253 LOGGER .warn ("A shebang '%s' was found, but could not be matched "
242254 "to an installed runtime." , full_cmd )
0 commit comments