Skip to content

py.exe launcher ignores [commands] from py.ini #100247

Closed
@cwalther

Description

@cwalther

Bug report

This is a regression between Python 3.11.0 and 3.11.1. Since 46a3cf4 (88297e2, #98692), py.exe no longer observes the “custom commands” mapping under [commands] in py.ini defining additional “virtual commands” (unless the virtual command starts with the same prefix as one of the four predefined virtual commands), but directly tries to launch the virtual command as an executable.

Steps to reproduce:

%WINDIR%\py.ini:

[commands]
/opt/something/bin/my-python2=C:\something\python27\python.exe

test.py:

#!/opt/something/bin/my-python2
import sys
print('hello from', sys.executable)
%WINDIR%\py.exe test.py

Expected result (and actual up to 3.11.0):

('hello from', 'C:\\something\\python27\\python.exe')

Actual result:

Unable to create process using 'C:\opt\something\bin\my-python2  test.py': The system cannot find the file specified.

I seem to be able to fix this as follows, which satisfies the existing tests, however this code is such a complex tangle of special cases that I have no idea whether it is the right thing to do. (The idea is that the loop over shebangTemplates should always find exactly one match, which was previously (before the regression) ensured by the empty template, so that _findCommand() is always called. Checking for tmpl != &shebangTemplates[0] is needed to satisfy test_recursive_search_path, however it might exclude too much – maybe searchPath() should instead report explicitly that it skipped a recursive call.)

diff --git a/PC/launcher2.c b/PC/launcher2.c
index 9b3db04aa4..ad313c10f3 100644
--- a/PC/launcher2.c
+++ b/PC/launcher2.c
@@ -1001,19 +1001,13 @@ checkShebang(SearchInfo *search)
         L"/usr/bin/env ",
         L"/usr/bin/",
         L"/usr/local/bin/",
-        L"python",
+        L"",
         NULL
     };
 
     for (const wchar_t **tmpl = shebangTemplates; *tmpl; ++tmpl) {
         if (_shebangStartsWith(shebang, shebangLength, *tmpl, &command)) {
             commandLength = 0;
-            // Normally "python" is the start of the command, but we also need it
-            // as a shebang prefix for back-compat. We move the command marker back
-            // if we match on that one.
-            if (0 == wcscmp(*tmpl, L"python")) {
-                command -= 6;
-            }
             while (command[commandLength] && !isspace(command[commandLength])) {
                 commandLength += 1;
             }
@@ -1052,18 +1046,20 @@ checkShebang(SearchInfo *search)
                     debug(L"# Treating shebang command '%.*s' as 'py'\n",
                         commandLength, command);
                 }
+            } else if (tmpl != &shebangTemplates[0]) {
+                // Unrecognised commands are joined to the script's directory and treated
+                // as the executable path
+                return _useShebangAsExecutable(search, shebang, shebangLength);
             } else {
                 debug(L"# Found shebang command but could not execute it: %.*s\n",
                     commandLength, command);
             }
             // search is done by this point
-            return 0;
+            break;
         }
     }
 
-    // Unrecognised commands are joined to the script's directory and treated
-    // as the executable path
-    return _useShebangAsExecutable(search, shebang, shebangLength);
+    return 0;
 }
 
 

Your environment

  • CPython versions tested on: 3.9, 3.10, 3.11.0 (good), 3.11.1 (bad)
  • Operating system and architecture: Windows 10 Pro 10.0.19043.2006 AMD64

Linked PRs

Metadata

Metadata

Assignees

Labels

OS-windowstype-bugAn unexpected behavior, bug, or error

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions