@@ -195,6 +195,13 @@ join(wchar_t *buffer, size_t bufferLength, const wchar_t *fragment)
195
195
}
196
196
197
197
198
+ bool
199
+ split_parent (wchar_t * buffer , size_t bufferLength )
200
+ {
201
+ return SUCCEEDED (PathCchRemoveFileSpec (buffer , bufferLength ));
202
+ }
203
+
204
+
198
205
int
199
206
_compare (const wchar_t * x , int xLen , const wchar_t * y , int yLen )
200
207
{
@@ -414,8 +421,8 @@ typedef struct {
414
421
// if true, treats 'tag' as a non-PEP 514 filter
415
422
bool oldStyleTag ;
416
423
// if true, ignores 'tag' when a high priority environment is found
417
- // gh-92817: This is currently set when a tag is read from configuration or
418
- // the environment, rather than the command line or a shebang line, and the
424
+ // gh-92817: This is currently set when a tag is read from configuration,
425
+ // the environment, or a shebang, rather than the command line, and the
419
426
// only currently possible high priority environment is an active virtual
420
427
// environment
421
428
bool lowPriorityTag ;
@@ -794,6 +801,8 @@ searchPath(SearchInfo *search, const wchar_t *shebang, int shebangLength)
794
801
}
795
802
}
796
803
804
+ debug (L"# Search PATH for %s\n" , filename );
805
+
797
806
wchar_t pathVariable [MAXLEN ];
798
807
int n = GetEnvironmentVariableW (L"PATH" , pathVariable , MAXLEN );
799
808
if (!n ) {
@@ -1031,8 +1040,11 @@ checkShebang(SearchInfo *search)
1031
1040
debug (L"Shebang: %s\n" , shebang );
1032
1041
1033
1042
// Handle shebangs that we should search PATH for
1043
+ int executablePathWasSetByUsrBinEnv = 0 ;
1034
1044
exitCode = searchPath (search , shebang , shebangLength );
1035
- if (exitCode != RC_NO_SHEBANG ) {
1045
+ if (exitCode == 0 ) {
1046
+ executablePathWasSetByUsrBinEnv = 1 ;
1047
+ } else if (exitCode != RC_NO_SHEBANG ) {
1036
1048
return exitCode ;
1037
1049
}
1038
1050
@@ -1067,21 +1079,22 @@ checkShebang(SearchInfo *search)
1067
1079
search -> tagLength = commandLength ;
1068
1080
// If we had 'python3.12.exe' then we want to strip the suffix
1069
1081
// off of the tag
1070
- if (search -> tagLength > 4 ) {
1082
+ if (search -> tagLength >= 4 ) {
1071
1083
const wchar_t * suffix = & search -> tag [search -> tagLength - 4 ];
1072
1084
if (0 == _comparePath (suffix , 4 , L".exe" , -1 )) {
1073
1085
search -> tagLength -= 4 ;
1074
1086
}
1075
1087
}
1076
1088
// If we had 'python3_d' then we want to strip the '_d' (any
1077
1089
// '.exe' is already gone)
1078
- if (search -> tagLength > 2 ) {
1090
+ if (search -> tagLength >= 2 ) {
1079
1091
const wchar_t * suffix = & search -> tag [search -> tagLength - 2 ];
1080
1092
if (0 == _comparePath (suffix , 2 , L"_d" , -1 )) {
1081
1093
search -> tagLength -= 2 ;
1082
1094
}
1083
1095
}
1084
1096
search -> oldStyleTag = true;
1097
+ search -> lowPriorityTag = true;
1085
1098
search -> executableArgs = & command [commandLength ];
1086
1099
search -> executableArgsLength = shebangLength - commandLength ;
1087
1100
if (search -> tag && search -> tagLength ) {
@@ -1095,6 +1108,11 @@ checkShebang(SearchInfo *search)
1095
1108
}
1096
1109
}
1097
1110
1111
+ // Didn't match a template, but we found it on PATH
1112
+ if (executablePathWasSetByUsrBinEnv ) {
1113
+ return 0 ;
1114
+ }
1115
+
1098
1116
// Unrecognised executables are first tried as command aliases
1099
1117
commandLength = 0 ;
1100
1118
while (commandLength < shebangLength && !isspace (shebang [commandLength ])) {
@@ -1765,7 +1783,15 @@ virtualenvSearch(const SearchInfo *search, EnvironmentInfo **result)
1765
1783
return 0 ;
1766
1784
}
1767
1785
1768
- if (INVALID_FILE_ATTRIBUTES == GetFileAttributesW (buffer )) {
1786
+ DWORD attr = GetFileAttributesW (buffer );
1787
+ if (INVALID_FILE_ATTRIBUTES == attr && search -> lowPriorityTag ) {
1788
+ if (!split_parent (buffer , MAXLEN ) || !join (buffer , MAXLEN , L"python.exe" )) {
1789
+ return 0 ;
1790
+ }
1791
+ attr = GetFileAttributesW (buffer );
1792
+ }
1793
+
1794
+ if (INVALID_FILE_ATTRIBUTES == attr ) {
1769
1795
debug (L"Python executable %s missing from virtual env\n" , buffer );
1770
1796
return 0 ;
1771
1797
}
0 commit comments