@@ -394,12 +394,35 @@ search_for_exec_prefix(wchar_t *argv0_path, wchar_t *home)
394394 return 1 ;
395395 }
396396
397- /* Check to see if argv[0] is in the build directory */
397+ /* Check to see if argv[0] is in the build directory. "pybuilddir.txt"
398+ is written by setup.py and contains the relative path to the location
399+ of shared library modules. */
398400 wcscpy (exec_prefix , argv0_path );
399- joinpath (exec_prefix , L"Modules/Setup " );
401+ joinpath (exec_prefix , L"pybuilddir.txt " );
400402 if (isfile (exec_prefix )) {
401- reduce (exec_prefix );
402- return -1 ;
403+ FILE * f = _Py_wfopen (exec_prefix , L"rb" );
404+ if (f == NULL )
405+ errno = 0 ;
406+ else {
407+ char buf [MAXPATHLEN + 1 ];
408+ PyObject * decoded ;
409+ wchar_t rel_builddir_path [MAXPATHLEN + 1 ];
410+ size_t n ;
411+ n = fread (buf , 1 , MAXPATHLEN , f );
412+ buf [n ] = '\0' ;
413+ fclose (f );
414+ decoded = PyUnicode_DecodeUTF8 (buf , n , "surrogateescape" );
415+ if (decoded != NULL ) {
416+ n = PyUnicode_AsWideChar (decoded , rel_builddir_path , MAXPATHLEN );
417+ Py_DECREF (decoded );
418+ if (n >= 0 ) {
419+ rel_builddir_path [n ] = L'\0' ;
420+ wcscpy (exec_prefix , argv0_path );
421+ joinpath (exec_prefix , rel_builddir_path );
422+ return -1 ;
423+ }
424+ }
425+ }
403426 }
404427
405428 /* Search from argv0_path, until root is found */
0 commit comments