pFad - Phone/Frame/Anonymizer/Declutterfier! Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

URL: http://github.com/python/cpython/commit/fe0a41aae425c61364b79c18ca8321dffed3ac40

bd26ec06210f.css" /> Issue #23668: Adds support for os.truncate and os.ftruncate on Windows · python/cpython@fe0a41a · GitHub
Skip to content

Commit fe0a41a

Browse files
committed
Issue #23668: Adds support for os.truncate and os.ftruncate on Windows
1 parent c7d979f commit fe0a41a

File tree

5 files changed

+53
-63
lines changed

5 files changed

+53
-63
lines changed

Doc/library/io.rst

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -339,8 +339,11 @@ I/O Base Classes
339339
if *size* is not specified). The current stream position isn't changed.
340340
This resizing can extend or reduce the current file size. In case of
341341
extension, the contents of the new file area depend on the platform
342-
(on most systems, additional bytes are zero-filled, on Windows they're
343-
undetermined). The new file size is returned.
342+
(on most systems, additional bytes are zero-filled). The new file size
343+
is returned.
344+
345+
.. versionchanged:: 3.5
346+
Windows will now zero-fill files when extending.
344347

345348
.. method:: writable()
346349

Doc/library/os.rst

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -805,8 +805,10 @@ as internal buffering of data.
805805
most *length* bytes in size. As of Python 3.3, this is equivalent to
806806
``os.truncate(fd, length)``.
807807

808-
Availability: Unix.
808+
Availability: Unix, Windows.
809809

810+
.. versionchanged:: 3.5
811+
Added support for Windows
810812

811813
.. function:: get_blocking(fd)
812814

@@ -2492,10 +2494,12 @@ features:
24922494

24932495
This function can support :ref:`specifying a file descriptor <path_fd>`.
24942496

2495-
Availability: Unix.
2497+
Availability: Unix, Windows.
24962498

24972499
.. versionadded:: 3.3
24982500

2501+
.. versionchanged:: 3.5
2502+
Added support for Windows
24992503

25002504
.. function:: unlink(path, *, dir_fd=None)
25012505

Misc/NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,8 @@ Library
286286

287287
- Issue #2052: Add charset parameter to HtmlDiff.make_file().
288288

289+
- Issue #23668: Support os.truncate and os.ftruncate on Windows.
290+
289291
- Issue #23138: Fixed parsing cookies with absent keys or values in cookiejar.
290292
Patch by Demian Brecht.
291293

Modules/_io/fileio.c

Lines changed: 4 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -839,9 +839,7 @@ static PyObject *
839839
fileio_truncate(fileio *self, PyObject *args)
840840
{
841841
PyObject *posobj = NULL; /* the new size wanted by the user */
842-
#ifndef MS_WINDOWS
843842
Py_off_t pos;
844-
#endif
845843
int ret;
846844
int fd;
847845

@@ -864,52 +862,6 @@ fileio_truncate(fileio *self, PyObject *args)
864862
Py_INCREF(posobj);
865863
}
866864

867-
#ifdef MS_WINDOWS
868-
/* MS _chsize doesn't work if newsize doesn't fit in 32 bits,
869-
so don't even try using it. */
870-
{
871-
PyObject *oldposobj, *tempposobj;
872-
HANDLE hFile;
873-
874-
/* we save the file pointer position */
875-
oldposobj = portable_lseek(fd, NULL, 1);
876-
if (oldposobj == NULL) {
877-
Py_DECREF(posobj);
878-
return NULL;
879-
}
880-
881-
/* we then move to the truncation position */
882-
tempposobj = portable_lseek(fd, posobj, 0);
883-
if (tempposobj == NULL) {
884-
Py_DECREF(oldposobj);
885-
Py_DECREF(posobj);
886-
return NULL;
887-
}
888-
Py_DECREF(tempposobj);
889-
890-
/* Truncate. Note that this may grow the file! */
891-
Py_BEGIN_ALLOW_THREADS
892-
errno = 0;
893-
hFile = (HANDLE)_get_osfhandle(fd);
894-
ret = hFile == (HANDLE)-1; /* testing for INVALID_HANDLE value */
895-
if (ret == 0) {
896-
ret = SetEndOfFile(hFile) == 0;
897-
if (ret)
898-
errno = EACCES;
899-
}
900-
Py_END_ALLOW_THREADS
901-
902-
/* we restore the file pointer position in any case */
903-
tempposobj = portable_lseek(fd, oldposobj, 0);
904-
Py_DECREF(oldposobj);
905-
if (tempposobj == NULL) {
906-
Py_DECREF(posobj);
907-
return NULL;
908-
}
909-
Py_DECREF(tempposobj);
910-
}
911-
#else
912-
913865
#if defined(HAVE_LARGEFILE_SUPPORT)
914866
pos = PyLong_AsLongLong(posobj);
915867
#else
@@ -922,11 +874,13 @@ fileio_truncate(fileio *self, PyObject *args)
922874

923875
Py_BEGIN_ALLOW_THREADS
924876
errno = 0;
877+
#ifdef MS_WINDOWS
878+
ret = _chsize_s(fd, pos);
879+
#else
925880
ret = ftruncate(fd, pos);
881+
#endif
926882
Py_END_ALLOW_THREADS
927883

928-
#endif /* !MS_WINDOWS */
929-
930884
if (ret != 0) {
931885
Py_DECREF(posobj);
932886
PyErr_SetFromErrno(PyExc_IOError);

Modules/posixmodule.c

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -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]
87588762
os.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]
87898800
os.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

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad © 2024 Your Company Name. All rights reserved.





Check this box to remove all script contents from the fetched content.



Check this box to remove all images from the fetched content.


Check this box to remove all CSS styles from the fetched content.


Check this box to keep images inefficiently compressed and original size.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy