Skip to content

Commit e76c216

Browse files
zoobaCopilot
andauthored
Apply suggestions from code review
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> Co-authored-by: Steve Dower <steve.dower@microsoft.com>
1 parent d1e305b commit e76c216

3 files changed

Lines changed: 19 additions & 6 deletions

File tree

src/manage/scriptutils.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -123,14 +123,25 @@ def _find_on_path(cmd, full_cmd):
123123

124124
def _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)

src/pymanager.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636

3737
"shebang_templates": {
3838
"/usr/bin/python": "py",
39-
"/usr/bin/pythonw": "py",
39+
"/usr/bin/pythonw": "pyw",
4040
"/usr/bin/python3": "py",
4141
"/usr/bin/pythonw3": "pyw",
4242
"/usr/local/bin/python": "py",

tests/test_scriptutils.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,7 @@ def test_quote_args(args, expect):
281281

282282
@pytest.mark.parametrize("line, expect_id, expect_line", [pytest.param(*a, id=a[0]) for a in [
283283
("#!/usr/bin/python", "Test1", None),
284+
("#!/usr/bin/python -Es", "Test1", None),
284285
("#! /usr/bin/pythonw", "Test1", None),
285286
("#! /usr/bin/python2", "Test2", None),
286287
("#! /usr/bin/pythonw2", "Test2", None),

0 commit comments

Comments
 (0)