Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Lib/test/test_os/test_os.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,12 @@ def test_access(self):
os.close(f)
self.assertTrue(os.access(os_helper.TESTFN, os.W_OK))

@unittest.skipUnless(hasattr(os, 'truncate'), 'test needs os.truncate()')
def test_truncate_negative_length(self):
create_file(os_helper.TESTFN, b'abc')
with self.assertRaises(OSError):
os.truncate(os_helper.TESTFN, -1)

@unittest.skipIf(
support.is_wasi, "WASI does not support dup."
)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix :func:`os.truncate` not raising :exc:`OSError` on Windows when
truncation fails.
4 changes: 3 additions & 1 deletion Modules/posixmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -13650,8 +13650,10 @@ os_truncate_impl(PyObject *module, path_t *path, Py_off_t length)
else {
result = _chsize_s(fd, length);
close(fd);
if (result < 0)
if (result != 0) {
errno = result;
result = -1;
}
}
#else
result = truncate(path->narrow, length);
Expand Down
Loading