@@ -265,7 +265,21 @@ canonicalize(wchar_t *buffer, const wchar_t *path)
265265 return _PyStatus_NO_MEMORY ();
266266 }
267267
268- if (FAILED (PathCchCanonicalizeEx (buffer , MAXPATHLEN + 1 , path , 0 ))) {
268+ if (PathIsRelativeW (path )) {
269+ wchar_t buff [MAXPATHLEN ];
270+ if (!GetCurrentDirectoryW (MAXPATHLEN , buff )) {
271+ return _PyStatus_ERR ("unable to find current working directory" );
272+ }
273+ if (FAILED (PathCchCombineEx (buff , MAXPATHLEN + 1 , buff , path , PATHCCH_ALLOW_LONG_PATHS ))) {
274+ return INIT_ERR_BUFFER_OVERFLOW ();
275+ }
276+ if (FAILED (PathCchCanonicalizeEx (buffer , MAXPATHLEN + 1 , buff , PATHCCH_ALLOW_LONG_PATHS ))) {
277+ return INIT_ERR_BUFFER_OVERFLOW ();
278+ }
279+ return _PyStatus_OK ();
280+ }
281+
282+ if (FAILED (PathCchCanonicalizeEx (buffer , MAXPATHLEN + 1 , path , PATHCCH_ALLOW_LONG_PATHS ))) {
269283 return INIT_ERR_BUFFER_OVERFLOW ();
270284 }
271285 return _PyStatus_OK ();
@@ -291,6 +305,9 @@ search_for_prefix(wchar_t *prefix, const wchar_t *argv0_path)
291305 /* Search from argv0_path, until LANDMARK is found.
292306 We guarantee 'prefix' is null terminated in bounds. */
293307 wcscpy_s (prefix , MAXPATHLEN + 1 , argv0_path );
308+ if (!prefix [0 ]) {
309+ return 0 ;
310+ }
294311 wchar_t stdlibdir [MAXPATHLEN + 1 ];
295312 wcscpy_s (stdlibdir , Py_ARRAY_LENGTH (stdlibdir ), prefix );
296313 /* We initialize with the longest possible path, in case it doesn't fit.
@@ -925,6 +942,7 @@ calculate_module_search_path(PyCalculatePath *calculate,
925942 the parent of that.
926943 */
927944 if (prefix [0 ] == L'\0' ) {
945+ PyStatus status ;
928946 wchar_t lookBuf [MAXPATHLEN + 1 ];
929947 const wchar_t * look = buf - 1 ; /* 'buf' is at the end of the buffer */
930948 while (1 ) {
@@ -939,6 +957,10 @@ calculate_module_search_path(PyCalculatePath *calculate,
939957 nchars = lookEnd - look ;
940958 wcsncpy (lookBuf , look + 1 , nchars );
941959 lookBuf [nchars ] = L'\0' ;
960+ status = canonicalize (lookBuf , lookBuf );
961+ if (_PyStatus_EXCEPTION (status )) {
962+ return status ;
963+ }
942964 /* Up one level to the parent */
943965 reduce (lookBuf );
944966 if (search_for_prefix (prefix , lookBuf )) {
0 commit comments