Index: Modules/posixmodule.c =================================================================== --- Modules/posixmodule.c (révision 85799) +++ Modules/posixmodule.c (copie de travail) @@ -1104,7 +1104,7 @@ } /* Grab GetFinalPathNameByHandle dynamically from kernel32 */ -static int has_GetFinalPathNameByHandle = 0; +static int has_GetFinalPathNameByHandle = -1; static DWORD (CALLBACK *Py_GetFinalPathNameByHandleA)(HANDLE, LPSTR, DWORD, DWORD); static DWORD (CALLBACK *Py_GetFinalPathNameByHandleW)(HANDLE, LPWSTR, DWORD, @@ -1114,7 +1114,7 @@ { HINSTANCE hKernel32; /* only recheck */ - if (!has_GetFinalPathNameByHandle) + if (has_GetFinalPathNameByHandle == -1) { hKernel32 = GetModuleHandle("KERNEL32"); *(FARPROC*)&Py_GetFinalPathNameByHandleA = GetProcAddress(hKernel32, @@ -1134,17 +1134,17 @@ GetFinalPathNameByHandle() */ int code; - HANDLE hFile; + HANDLE hFile = INVALID_HANDLE_VALUE; int buf_size; char *target_path; int result_length; WIN32_FILE_ATTRIBUTE_DATA info; - if(!check_GetFinalPathNameByHandle()) { + if (!check_GetFinalPathNameByHandle()) { /* if the OS doesn't have GetFinalPathNameByHandle, it doesn't have symlinks, so just fall back to the traditional behavior found in lstat. */ - return win32_lstat(path, result); + goto fallback; } hFile = CreateFileA( @@ -1157,7 +1157,7 @@ FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS, NULL); - if(hFile == INVALID_HANDLE_VALUE) { + if (hFile == INVALID_HANDLE_VALUE) { /* Either the target doesn't exist, or we don't have access to get a handle to it. If the former, we need to return an error. If the latter, we can use attributes_from_dir. */ @@ -1181,7 +1181,8 @@ /* We have a good handle to the target, use it to determine the target path name (then we'll call lstat on it). */ buf_size = Py_GetFinalPathNameByHandleA(hFile, 0, 0, VOLUME_NAME_DOS); - if(!buf_size) return -1; + if (!buf_size) + goto fallback; /* Due to a slight discrepancy between GetFinalPathNameByHandleA and GetFinalPathNameByHandleW, we must allocate one more byte than reported. */ @@ -1189,12 +1190,12 @@ result_length = Py_GetFinalPathNameByHandleA(hFile, target_path, buf_size+1, VOLUME_NAME_DOS); - if(!result_length) { + if (!result_length) { free(target_path); - return -1; + goto fallback; } - if(!CloseHandle(hFile)) { + if (!CloseHandle(hFile)) { free(target_path); return -1; } @@ -1205,6 +1206,11 @@ } return code; + +fallback: + if (hFile != INVALID_HANDLE_VALUE) + CloseHandle(hFile); + return win32_lstat(path, result); } static int @@ -1212,17 +1218,17 @@ { /* Traverse the symlink to the target using GetFinalPathNameByHandle() */ int code; - HANDLE hFile; + HANDLE hFile = INVALID_HANDLE_VALUE; int buf_size; wchar_t *target_path; int result_length; WIN32_FILE_ATTRIBUTE_DATA info; - if(!check_GetFinalPathNameByHandle()) { + if (!check_GetFinalPathNameByHandle()) { /* If the OS doesn't have GetFinalPathNameByHandle, it doesn't have symlinks, so just fall back to the traditional behavior found in lstat. */ - return win32_lstat_w(path, result); + goto fallback; } hFile = CreateFileW( @@ -1235,7 +1241,7 @@ FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS, NULL); - if(hFile == INVALID_HANDLE_VALUE) { + if (hFile == INVALID_HANDLE_VALUE) { /* Either the target doesn't exist, or we don't have access to get a handle to it. If the former, we need to return an error. If the latter, we can use attributes_from_dir. */ @@ -1259,19 +1265,19 @@ /* We have a good handle to the target, use it to determine the target path name (then we'll call lstat on it). */ buf_size = Py_GetFinalPathNameByHandleW(hFile, 0, 0, VOLUME_NAME_DOS); - if(!buf_size) - return -1; + if (!buf_size) + goto fallback; target_path = (wchar_t *)malloc((buf_size+1)*sizeof(wchar_t)); result_length = Py_GetFinalPathNameByHandleW(hFile, target_path, buf_size, VOLUME_NAME_DOS); - if(!result_length) { + if (!result_length) { free(target_path); - return -1; + goto fallback; } - if(!CloseHandle(hFile)) { + if (!CloseHandle(hFile)) { free(target_path); return -1; } @@ -1282,6 +1288,11 @@ } return code; + +fallback: + if (hFile != INVALID_HANDLE_VALUE) + CloseHandle(hFile); + return win32_lstat_w(path, result); } static int