cpython: 505bf6086ec5 (original) (raw)
Mercurial > cpython
changeset 95551:505bf6086ec5
Issue #23668: Adds support for os.truncate and os.ftruncate on Windows [#23668]
Steve Dower steve.dower@microsoft.com | |
---|---|
date | Fri, 20 Mar 2015 19:50:46 -0700 |
parents | 1e64d57422ee |
children | 32652360d1c3 |
files | Doc/library/io.rst Doc/library/os.rst Misc/NEWS Modules/_io/fileio.c Modules/posixmodule.c |
diffstat | 5 files changed, 57 insertions(+), 67 deletions(-)[+] [-] Doc/library/io.rst 7 Doc/library/os.rst 10 Misc/NEWS 2 Modules/_io/fileio.c 54 Modules/posixmodule.c 51 |
line wrap: on
line diff
--- a/Doc/library/io.rst +++ b/Doc/library/io.rst @@ -339,8 +339,11 @@ I/O Base Classes if size is not specified). The current stream position isn't changed. This resizing can extend or reduce the current file size. In case of extension, the contents of the new file area depend on the platform
(on most systems, additional bytes are zero-filled, on Windows they're[](#l1.7)
undetermined). The new file size is returned.[](#l1.8)
(on most systems, additional bytes are zero-filled). The new file size[](#l1.9)
is returned.[](#l1.10)
--- a/Doc/library/os.rst
+++ b/Doc/library/os.rst
@@ -805,8 +805,10 @@ as internal buffering of data.
most length bytes in size. As of Python 3.3, this is equivalent to
os.truncate(fd, length)
.
.. function:: get_blocking(fd)
@@ -2492,10 +2494,12 @@ features:
This function can support :ref:specifying a file descriptor <path_fd>
.
- Availability: Unix, Windows. .. versionadded:: 3.3
- .. versionchanged:: 3.5
Added support for Windows[](#l2.26)
.. function:: unlink(path, *, dir_fd=None)
--- a/Misc/NEWS +++ b/Misc/NEWS @@ -286,6 +286,8 @@ Library
- Issue #2052: Add charset parameter to HtmlDiff.make_file(). +- Issue #23668: Support os.truncate and os.ftruncate on Windows. +
- Issue #23138: Fixed parsing cookies with absent keys or values in cookiejar. Patch by Demian Brecht.
--- a/Modules/_io/fileio.c +++ b/Modules/_io/fileio.c @@ -839,9 +839,7 @@ static PyObject * fileio_truncate(fileio *self, PyObject *args) { PyObject posobj = NULL; / the new size wanted by the user */ -#ifndef MS_WINDOWS Py_off_t pos; -#endif int ret; int fd; @@ -864,52 +862,6 @@ fileio_truncate(fileio *self, PyObject * Py_INCREF(posobj); } -#ifdef MS_WINDOWS
- /* MS _chsize doesn't work if newsize doesn't fit in 32 bits,
so don't even try using it. */[](#l4.19)
- {
PyObject *oldposobj, *tempposobj;[](#l4.21)
HANDLE hFile;[](#l4.22)
/* we save the file pointer position */[](#l4.24)
oldposobj = portable_lseek(fd, NULL, 1);[](#l4.25)
if (oldposobj == NULL) {[](#l4.26)
Py_DECREF(posobj);[](#l4.27)
return NULL;[](#l4.28)
}[](#l4.29)
/* we then move to the truncation position */[](#l4.31)
tempposobj = portable_lseek(fd, posobj, 0);[](#l4.32)
if (tempposobj == NULL) {[](#l4.33)
Py_DECREF(oldposobj);[](#l4.34)
Py_DECREF(posobj);[](#l4.35)
return NULL;[](#l4.36)
}[](#l4.37)
Py_DECREF(tempposobj);[](#l4.38)
/* Truncate. Note that this may grow the file! */[](#l4.40)
Py_BEGIN_ALLOW_THREADS[](#l4.41)
errno = 0;[](#l4.42)
hFile = (HANDLE)_get_osfhandle(fd);[](#l4.43)
ret = hFile == (HANDLE)-1; /* testing for INVALID_HANDLE value */[](#l4.44)
if (ret == 0) {[](#l4.45)
ret = SetEndOfFile(hFile) == 0;[](#l4.46)
if (ret)[](#l4.47)
errno = EACCES;[](#l4.48)
}[](#l4.49)
Py_END_ALLOW_THREADS[](#l4.50)
/* we restore the file pointer position in any case */[](#l4.52)
tempposobj = portable_lseek(fd, oldposobj, 0);[](#l4.53)
Py_DECREF(oldposobj);[](#l4.54)
if (tempposobj == NULL) {[](#l4.55)
Py_DECREF(posobj);[](#l4.56)
return NULL;[](#l4.57)
}[](#l4.58)
Py_DECREF(tempposobj);[](#l4.59)
- }
-#else - #if defined(HAVE_LARGEFILE_SUPPORT) pos = PyLong_AsLongLong(posobj); #else @@ -922,11 +874,13 @@ fileio_truncate(fileio *self, PyObject * Py_BEGIN_ALLOW_THREADS errno = 0; +#ifdef MS_WINDOWS
+#else ret = ftruncate(fd, pos); +#endif Py_END_ALLOW_THREADS -#endif /* !MS_WINDOWS */ - if (ret != 0) { Py_DECREF(posobj); PyErr_SetFromErrno(PyExc_IOError);
--- a/Modules/posixmodule.c +++ b/Modules/posixmodule.c @@ -2315,6 +2315,10 @@ FTRUNCATE #endif /[python end generated code: output=4bd4f6f7d41267f1 input=80b4c890b6774ea5]/ +#ifdef MS_WINDOWS
+#endif /*[python input] @@ -8753,7 +8757,7 @@ os_makedev_impl(PyModuleDef module, int #endif / HAVE_DEVICE_MACROS / -#ifdef HAVE_FTRUNCATE +#if defined HAVE_FTRUNCATE || defined MS_WINDOWS /[clinic input] os.ftruncate @@ -8771,9 +8775,16 @@ os_ftruncate_impl(PyModuleDef *module, i int result; int async_err = 0;
+ do { Py_BEGIN_ALLOW_THREADS +#ifdef MS_WINDOWS
result = _chsize_s(fd, length);[](#l5.33)
+#else result = ftruncate(fd, length); +#endif Py_END_ALLOW_THREADS } while (result != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); @@ -8781,10 +8792,10 @@ os_ftruncate_impl(PyModuleDef module, i return (!async_err) ? posix_error() : NULL; Py_RETURN_NONE; } -#endif / HAVE_FTRUNCATE / - - -#ifdef HAVE_TRUNCATE +#endif / HAVE_FTRUNCATE || MS_WINDOWS / + + +#if defined HAVE_TRUNCATE || defined MS_WINDOWS /[clinic input] os.truncate path: path_t(allow_fd='PATH_HAVE_FTRUNCATE') @@ -8801,21 +8812,37 @@ os_truncate_impl(PyModuleDef module, pa /[clinic end generated code: output=f60a9e08370e9e2e input=77229cf0b50a9b77]*/ { int result; +#ifdef MS_WINDOWS
Py_BEGIN_ALLOW_THREADS -#ifdef HAVE_FTRUNCATE
- if (path->wide)
else -#endiffd = _wopen(path->wide, _O_WRONLY | _O_BINARY | _O_NOINHERIT);[](#l5.72)
result = truncate(path->narrow, length);[](#l5.75)
fd = _open(path->narrow, _O_WRONLY | _O_BINARY | _O_NOINHERIT);[](#l5.76)
- if (fd < 0)
result = -1;[](#l5.78)
- else {
result = _chsize_s(fd, length);[](#l5.80)
close(fd);[](#l5.81)
if (result < 0)[](#l5.82)
errno = result;[](#l5.83)
- }
+#endif Py_END_ALLOW_THREADS if (result < 0) return path_error(path); Py_RETURN_NONE; } -#endif /* HAVE_TRUNCATE / +#endif / HAVE_TRUNCATE || MS_WINDOWS / / Issue #22396: On 32-bit AIX platform, the prototypes of os.posix_fadvise() @@ -12771,7 +12798,7 @@ static char *have_functions[] = { "HAVE_FSTATVFS", #endif -#ifdef HAVE_FTRUNCATE +#if defined HAVE_FTRUNCATE || defined MS_WINDOWS "HAVE_FTRUNCATE", #endif