@@ -2315,6 +2315,10 @@ FTRUNCATE
23152315#endif
23162316/*[python end generated code: output=4bd4f6f7d41267f1 input=80b4c890b6774ea5]*/
23172317
2318+ #ifdef MS_WINDOWS
2319+ #undef PATH_HAVE_FTRUNCATE
2320+ #define PATH_HAVE_FTRUNCATE 1
2321+ #endif
23182322
23192323/*[python input]
23202324
@@ -8753,7 +8757,7 @@ os_makedev_impl(PyModuleDef *module, int major, int minor)
87538757#endif /* HAVE_DEVICE_MACROS */
87548758
87558759
8756- #ifdef HAVE_FTRUNCATE
8760+ #if defined HAVE_FTRUNCATE || defined MS_WINDOWS
87578761/*[clinic input]
87588762os.ftruncate
87598763
@@ -8771,20 +8775,27 @@ os_ftruncate_impl(PyModuleDef *module, int fd, Py_off_t length)
87718775 int result ;
87728776 int async_err = 0 ;
87738777
8778+ if (!_PyVerify_fd (fd ))
8779+ return posix_error ();
8780+
87748781 do {
87758782 Py_BEGIN_ALLOW_THREADS
8783+ #ifdef MS_WINDOWS
8784+ result = _chsize_s (fd , length );
8785+ #else
87768786 result = ftruncate (fd , length );
8787+ #endif
87778788 Py_END_ALLOW_THREADS
87788789 } while (result != 0 && errno == EINTR &&
87798790 !(async_err = PyErr_CheckSignals ()));
87808791 if (result != 0 )
87818792 return (!async_err ) ? posix_error () : NULL ;
87828793 Py_RETURN_NONE ;
87838794}
8784- #endif /* HAVE_FTRUNCATE */
8795+ #endif /* HAVE_FTRUNCATE || MS_WINDOWS */
87858796
87868797
8787- #ifdef HAVE_TRUNCATE
8798+ #if defined HAVE_TRUNCATE || defined MS_WINDOWS
87888799/*[clinic input]
87898800os.truncate
87908801 path: path_t(allow_fd='PATH_HAVE_FTRUNCATE')
@@ -8801,21 +8812,37 @@ os_truncate_impl(PyModuleDef *module, path_t *path, Py_off_t length)
88018812/*[clinic end generated code: output=f60a9e08370e9e2e input=77229cf0b50a9b77]*/
88028813{
88038814 int result ;
8815+ #ifdef MS_WINDOWS
8816+ int fd ;
8817+ #endif
88048818
8805- Py_BEGIN_ALLOW_THREADS
8806- #ifdef HAVE_FTRUNCATE
88078819 if (path -> fd != -1 )
8808- result = ftruncate (path -> fd , length );
8820+ return os_ftruncate_impl (module , path -> fd , length );
8821+
8822+ Py_BEGIN_ALLOW_THREADS
8823+ #ifdef MS_WINDOWS
8824+ if (path -> wide )
8825+ fd = _wopen (path -> wide , _O_WRONLY | _O_BINARY | _O_NOINHERIT );
88098826 else
8827+ fd = _open (path -> narrow , _O_WRONLY | _O_BINARY | _O_NOINHERIT );
8828+ if (fd < 0 )
8829+ result = -1 ;
8830+ else {
8831+ result = _chsize_s (fd , length );
8832+ close (fd );
8833+ if (result < 0 )
8834+ errno = result ;
8835+ }
8836+ #else
8837+ result = truncate (path -> narrow , length );
88108838#endif
8811- result = truncate (path -> narrow , length );
88128839 Py_END_ALLOW_THREADS
88138840 if (result < 0 )
88148841 return path_error (path );
88158842
88168843 Py_RETURN_NONE ;
88178844}
8818- #endif /* HAVE_TRUNCATE */
8845+ #endif /* HAVE_TRUNCATE || MS_WINDOWS */
88198846
88208847
88218848/* Issue #22396: On 32-bit AIX platform, the prototypes of os.posix_fadvise()
@@ -12771,7 +12798,7 @@ static char *have_functions[] = {
1277112798 "HAVE_FSTATVFS" ,
1277212799#endif
1277312800
12774- #ifdef HAVE_FTRUNCATE
12801+ #if defined HAVE_FTRUNCATE || defined MS_WINDOWS
1277512802 "HAVE_FTRUNCATE" ,
1277612803#endif
1277712804
0 commit comments