@@ -303,12 +303,6 @@ extern int lstat(const char *, struct stat *);
303
303
#ifdef HAVE_PROCESS_H
304
304
#include <process.h>
305
305
#endif
306
- #ifndef VOLUME_NAME_DOS
307
- #define VOLUME_NAME_DOS 0x0
308
- #endif
309
- #ifndef VOLUME_NAME_NT
310
- #define VOLUME_NAME_NT 0x2
311
- #endif
312
306
#ifndef IO_REPARSE_TAG_SYMLINK
313
307
#define IO_REPARSE_TAG_SYMLINK (0xA000000CL)
314
308
#endif
@@ -3731,11 +3725,10 @@ os__getfinalpathname_impl(PyObject *module, path_t *path)
3731
3725
/*[clinic end generated code: output=621a3c79bc29ebfa input=2b6b6c7cbad5fb84]*/
3732
3726
{
3733
3727
HANDLE hFile ;
3734
- int buf_size ;
3735
- wchar_t * target_path ;
3728
+ wchar_t buf [ MAXPATHLEN ], * target_path = buf ;
3729
+ int buf_size = Py_ARRAY_LENGTH ( buf ) ;
3736
3730
int result_length ;
3737
3731
PyObject * result ;
3738
- const char * err = NULL ;
3739
3732
3740
3733
Py_BEGIN_ALLOW_THREADS
3741
3734
hFile = CreateFileW (
@@ -3747,55 +3740,52 @@ os__getfinalpathname_impl(PyObject *module, path_t *path)
3747
3740
/* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */
3748
3741
FILE_FLAG_BACKUP_SEMANTICS ,
3749
3742
NULL );
3743
+ Py_END_ALLOW_THREADS
3750
3744
3751
3745
if (hFile == INVALID_HANDLE_VALUE ) {
3752
- err = "CreateFileW" ;
3753
- goto done1 ;
3746
+ return win32_error_object ("CreateFileW" , path -> object );
3754
3747
}
3755
3748
3756
3749
/* We have a good handle to the target, use it to determine the
3757
3750
target path name. */
3758
- buf_size = GetFinalPathNameByHandleW (hFile , 0 , 0 , VOLUME_NAME_NT );
3751
+ while (1 ) {
3752
+ Py_BEGIN_ALLOW_THREADS
3753
+ result_length = GetFinalPathNameByHandleW (hFile , target_path ,
3754
+ buf_size , VOLUME_NAME_DOS );
3755
+ Py_END_ALLOW_THREADS
3759
3756
3760
- if (!buf_size ) {
3761
- err = "GetFinalPathNameByHandle" ;
3762
- goto done1 ;
3763
- }
3764
- done1 :
3765
- Py_END_ALLOW_THREADS
3766
- if (err )
3767
- return win32_error_object (err , path - > object );
3757
+ if (!result_length ) {
3758
+ result = win32_error_object ("GetFinalPathNameByHandleW" ,
3759
+ path -> object );
3760
+ goto cleanup ;
3761
+ }
3768
3762
3769
- target_path = PyMem_New ( wchar_t , buf_size + 1 );
3770
- if (! target_path )
3771
- return PyErr_NoMemory ();
3763
+ if ( result_length < buf_size ) {
3764
+ break ;
3765
+ }
3772
3766
3773
- Py_BEGIN_ALLOW_THREADS
3774
- result_length = GetFinalPathNameByHandleW ( hFile , target_path ,
3775
- buf_size , VOLUME_NAME_DOS );
3776
- if (!result_length ) {
3777
- err = "GetFinalPathNameByHandle" ;
3778
- goto done2 ;
3779
- }
3767
+ wchar_t * tmp ;
3768
+ tmp = PyMem_Realloc ( target_path != buf ? target_path : NULL ,
3769
+ result_length * sizeof ( * tmp ) );
3770
+ if (!tmp ) {
3771
+ result = PyErr_NoMemory () ;
3772
+ goto cleanup ;
3773
+ }
3780
3774
3781
- if (!CloseHandle (hFile )) {
3782
- err = "CloseHandle" ;
3783
- goto done2 ;
3784
- }
3785
- done2 :
3786
- Py_END_ALLOW_THREADS
3787
- if (err ) {
3788
- PyMem_Free (target_path );
3789
- return win32_error_object (err , path -> object );
3775
+ buf_size = result_length ;
3776
+ target_path = tmp ;
3790
3777
}
3791
3778
3792
- target_path [result_length ] = 0 ;
3793
3779
result = PyUnicode_FromWideChar (target_path , result_length );
3794
- PyMem_Free (target_path );
3795
3780
if (path -> narrow )
3796
3781
Py_SETREF (result , PyUnicode_EncodeFSDefault (result ));
3797
- return result ;
3798
3782
3783
+ cleanup :
3784
+ if (target_path != buf ) {
3785
+ PyMem_Free (target_path );
3786
+ }
3787
+ CloseHandle (hFile );
3788
+ return result ;
3799
3789
}
3800
3790
3801
3791
/*[clinic input]
0 commit comments