@@ -161,27 +161,37 @@ reduce(wchar_t *dir)
161
161
static int
162
162
change_ext (wchar_t * dest , const wchar_t * src , const wchar_t * ext )
163
163
{
164
- size_t src_len = wcsnlen_s (src , MAXPATHLEN + 1 );
165
- size_t i = src_len ;
166
- if (i >= MAXPATHLEN + 1 ) {
167
- Py_FatalError ("buffer overflow in getpathp.c's reduce()" );
168
- }
164
+ if (src && src != dest ) {
165
+ size_t src_len = wcsnlen_s (src , MAXPATHLEN + 1 );
166
+ size_t i = src_len ;
167
+ if (i >= MAXPATHLEN + 1 ) {
168
+ Py_FatalError ("buffer overflow in getpathp.c's reduce()" );
169
+ }
169
170
170
- while (i > 0 && src [i ] != '.' && !is_sep (src [i ]))
171
- -- i ;
171
+ while (i > 0 && src [i ] != '.' && !is_sep (src [i ]))
172
+ -- i ;
172
173
173
- if (i == 0 ) {
174
- dest [0 ] = '\0' ;
175
- return -1 ;
176
- }
174
+ if (i == 0 ) {
175
+ dest [0 ] = '\0' ;
176
+ return -1 ;
177
+ }
178
+
179
+ if (is_sep (src [i ])) {
180
+ i = src_len ;
181
+ }
177
182
178
- if (is_sep (src [i ])) {
179
- i = src_len ;
183
+ if (wcsncpy_s (dest , MAXPATHLEN + 1 , src , i )) {
184
+ dest [0 ] = '\0' ;
185
+ return -1 ;
186
+ }
187
+ } else {
188
+ wchar_t * s = wcsrchr (dest , L'.' );
189
+ if (s ) {
190
+ s [0 ] = '\0' ;
191
+ }
180
192
}
181
193
182
- if (wcsncpy_s (dest , MAXPATHLEN + 1 , src , i ) ||
183
- wcscat_s (dest , MAXPATHLEN + 1 , ext ))
184
- {
194
+ if (wcscat_s (dest , MAXPATHLEN + 1 , ext )) {
185
195
dest [0 ] = '\0' ;
186
196
return -1 ;
187
197
}
@@ -337,6 +347,19 @@ search_for_prefix(wchar_t *prefix, const wchar_t *argv0_path, const wchar_t *lan
337
347
}
338
348
339
349
350
+ static int
351
+ get_dllpath (wchar_t * dllpath )
352
+ {
353
+ #ifdef Py_ENABLE_SHARED
354
+ extern HANDLE PyWin_DLLhModule ;
355
+ if (PyWin_DLLhModule && GetModuleFileNameW (PyWin_DLLhModule , dllpath , MAXPATHLEN )) {
356
+ return 0 ;
357
+ }
358
+ #endif
359
+ return -1 ;
360
+ }
361
+
362
+
340
363
#ifdef Py_ENABLE_SHARED
341
364
342
365
/* a string loaded from the DLL at startup.*/
@@ -509,31 +532,6 @@ getpythonregpath(HKEY keyBase, int skipcore)
509
532
#endif /* Py_ENABLE_SHARED */
510
533
511
534
512
- static _PyInitError
513
- get_dll_path (PyCalculatePath * calculate , _PyPathConfig * config )
514
- {
515
- wchar_t dll_path [MAXPATHLEN + 1 ];
516
- memset (dll_path , 0 , sizeof (dll_path ));
517
-
518
- #ifdef Py_ENABLE_SHARED
519
- extern HANDLE PyWin_DLLhModule ;
520
- if (PyWin_DLLhModule ) {
521
- if (!GetModuleFileNameW (PyWin_DLLhModule , dll_path , MAXPATHLEN )) {
522
- dll_path [0 ] = 0 ;
523
- }
524
- }
525
- #else
526
- dll_path [0 ] = 0 ;
527
- #endif
528
-
529
- config -> dll_path = _PyMem_RawWcsdup (dll_path );
530
- if (config -> dll_path == NULL ) {
531
- return _Py_INIT_NO_MEMORY ();
532
- }
533
- return _Py_INIT_OK ();
534
- }
535
-
536
-
537
535
static _PyInitError
538
536
get_program_full_path (const _PyCoreConfig * core_config ,
539
537
PyCalculatePath * calculate , _PyPathConfig * config )
@@ -675,12 +673,11 @@ calculate_init(PyCalculatePath *calculate,
675
673
static int
676
674
get_pth_filename (wchar_t * spbuffer , _PyPathConfig * config )
677
675
{
678
- if (config -> dll_path [0 ]) {
679
- if (!change_ext (spbuffer , config -> dll_path , L"._pth" ) &&
680
- exists (spbuffer ))
681
- {
682
- return 1 ;
683
- }
676
+ if (get_dllpath (spbuffer ) &&
677
+ !change_ext (spbuffer , spbuffer , L"._pth" ) &&
678
+ exists (spbuffer ))
679
+ {
680
+ return 1 ;
684
681
}
685
682
if (config -> program_full_path [0 ]) {
686
683
if (!change_ext (spbuffer , config -> program_full_path , L"._pth" ) &&
@@ -967,11 +964,6 @@ calculate_path_impl(const _PyCoreConfig *core_config,
967
964
{
968
965
_PyInitError err ;
969
966
970
- err = get_dll_path (calculate , config );
971
- if (_Py_INIT_FAILED (err )) {
972
- return err ;
973
- }
974
-
975
967
err = get_program_full_path (core_config , calculate , config );
976
968
if (_Py_INIT_FAILED (err )) {
977
969
return err ;
@@ -992,9 +984,13 @@ calculate_path_impl(const _PyCoreConfig *core_config,
992
984
calculate_pyvenv_file (calculate );
993
985
994
986
/* Calculate zip archive path from DLL or exe path */
995
- change_ext (calculate -> zip_path ,
996
- config -> dll_path [0 ] ? config -> dll_path : config -> program_full_path ,
997
- L".zip" );
987
+ if (get_dllpath (calculate -> zip_path ) ||
988
+ change_ext (calculate -> zip_path , calculate -> zip_path , L".zip" ))
989
+ {
990
+ if (change_ext (calculate -> zip_path , config -> program_full_path , L".zip" )) {
991
+ calculate -> zip_path [0 ] = L'\0' ;
992
+ }
993
+ }
998
994
999
995
calculate_home_prefix (calculate , prefix );
1000
996
@@ -1054,28 +1050,39 @@ int
1054
1050
_Py_CheckPython3 (void )
1055
1051
{
1056
1052
wchar_t py3path [MAXPATHLEN + 1 ];
1057
- wchar_t * s ;
1058
1053
if (python3_checked ) {
1059
1054
return hPython3 != NULL ;
1060
1055
}
1061
1056
python3_checked = 1 ;
1062
1057
1063
1058
/* If there is a python3.dll next to the python3y.dll,
1064
- assume this is a build tree; use that DLL */
1065
- wcscpy (py3path , _Py_path_config .dll_path );
1066
- s = wcsrchr (py3path , L'\\' );
1067
- if (!s ) {
1068
- s = py3path ;
1059
+ use that DLL */
1060
+ if (!get_dllpath (py3path )) {
1061
+ reduce (py3path );
1062
+ join (py3path , PY3_DLLNAME );
1063
+ hPython3 = LoadLibraryExW (py3path , NULL , LOAD_WITH_ALTERED_SEARCH_PATH );
1064
+ if (hPython3 != NULL ) {
1065
+ return 1 ;
1066
+ }
1069
1067
}
1070
- wcscpy (s , L"\\python3.dll" );
1071
- hPython3 = LoadLibraryExW (py3path , NULL , LOAD_WITH_ALTERED_SEARCH_PATH );
1072
- if (hPython3 != NULL ) {
1073
- return 1 ;
1068
+
1069
+ /* If we can locate python3.dll in our application dir,
1070
+ use that DLL */
1071
+ wcscpy (py3path , Py_GetPrefix ());
1072
+ if (py3path [0 ]) {
1073
+ join (py3path , PY3_DLLNAME );
1074
+ hPython3 = LoadLibraryExW (py3path , NULL , LOAD_WITH_ALTERED_SEARCH_PATH );
1075
+ if (hPython3 != NULL ) {
1076
+ return 1 ;
1077
+ }
1074
1078
}
1075
1079
1076
- /* Check sys.prefix\DLLs\python3.dll */
1080
+ /* For back-compat, also search {sys.prefix}\DLLs, though
1081
+ that has not been a normal install layout for a while */
1077
1082
wcscpy (py3path , Py_GetPrefix ());
1078
- wcscat (py3path , L"\\DLLs\\python3.dll" );
1079
- hPython3 = LoadLibraryExW (py3path , NULL , LOAD_WITH_ALTERED_SEARCH_PATH );
1083
+ if (py3path [0 ]) {
1084
+ join (py3path , L"DLLs\\" PY3_DLLNAME );
1085
+ hPython3 = LoadLibraryExW (py3path , NULL , LOAD_WITH_ALTERED_SEARCH_PATH );
1086
+ }
1080
1087
return hPython3 != NULL ;
1081
1088
}
0 commit comments