diff --git a/CHANGES.md b/CHANGES.md index 7ca209de..c145fa86 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -15,6 +15,9 @@ The released versions correspond to PyPI releases. * fixed a crash if the stack limit was set to a low value (see [#1313](https://github.com/pytest-dev/pyfakefs/issues/1313)) +* fixed `os.ftruncate` to inspect the `FakeFileWrapper` returned by + `get_open_file(fd)` + (see [#1331](https://github.com/pytest-dev/pyfakefs/issues/1331)) ## [Version 6.2.0](https://pypi.python.org/pypi/pyfakefs/6.2.0) (2026-04-12) diff --git a/pyfakefs/fake_os.py b/pyfakefs/fake_os.py index 3c3edc03..25764439 100644 --- a/pyfakefs/fake_os.py +++ b/pyfakefs/fake_os.py @@ -1019,11 +1019,10 @@ def ftruncate(self, fd: int, length: int) -> None: Raises: OSError: if the file descriptor is invalid """ - file_object = self.filesystem.get_open_file(fd).get_object() - if isinstance(file_object, FakeFileWrapper): - file_object.size = length - else: + file_handle = self.filesystem.get_open_file(fd) + if not isinstance(file_handle, FakeFileWrapper): self.filesystem.raise_os_error(errno.EBADF) + file_handle.get_object().size = length def access( self, diff --git a/pyfakefs/tests/fake_os_test.py b/pyfakefs/tests/fake_os_test.py index d9232598..104c0e58 100644 --- a/pyfakefs/tests/fake_os_test.py +++ b/pyfakefs/tests/fake_os_test.py @@ -2974,7 +2974,7 @@ def test_ftruncate(self): self.create_file(file_path, contents="0123456789012345") fd = self.os.open(file_path, os.O_RDWR) - self.os.truncate(fd, 10) + self.os.ftruncate(fd, 10) self.assertEqual(10, self.os.stat(file_path).st_size) with self.open(file_path, encoding="utf8") as f: self.assertEqual("0123456789", f.read())