Skip to content

Commit af2bbb2

Browse files
committed
Remove unneeded check leading to a regression
- the check had been incorrecty introduced to fix an issue ultimately related to reference counting/garbage collection - the fix was incorrect, and that case can probably be ignored, as the code leading to it makes no sense
1 parent 7041a39 commit af2bbb2

File tree

3 files changed

+14
-30
lines changed

3 files changed

+14
-30
lines changed

CHANGES.md

+6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
# pyfakefs Release Notes
22
The released versions correspond to PyPI releases.
33

4+
## Unreleased
5+
6+
### Fixes
7+
* fixed a regression from version 5.4.0 that incorrectly handled files opened twice via file descriptor
8+
(see [#997](../../issues/997))
9+
410
## [Version 5.4.0](https://pypi.python.org/pypi/pyfakefs/5.4.0) (2024-04-07)
511
Improves permission handling.
612

pyfakefs/fake_open.py

-6
Original file line numberDiff line numberDiff line change
@@ -144,12 +144,6 @@ def call(
144144

145145
newline, open_modes = self._handle_file_mode(mode, newline, open_modes)
146146
opened_as_fd = isinstance(file_, int)
147-
if opened_as_fd and not helpers.IS_PYPY:
148-
fd: int = cast(int, file_)
149-
# cannot open the same file descriptor twice, except in PyPy
150-
for f in self.filesystem.get_open_files(fd):
151-
if isinstance(f, FakeFileWrapper) and f.opened_as_fd:
152-
raise OSError(errno.EBADF, "Bad file descriptor")
153147

154148
# the pathlib opener is defined in a Path instance that may not be
155149
# patched under some circumstances; as it just calls standard open(),

pyfakefs/tests/fake_os_test.py

+8-24
Original file line numberDiff line numberDiff line change
@@ -231,18 +231,18 @@ def test_closed_file_descriptor(self):
231231
self.assert_raises_os_error(errno.EBADF, self.os.fdopen, fileno2, **kwargs)
232232

233233
def test_fdopen_twice(self):
234+
# regression test for #997
234235
file_path = self.make_path("some_file1")
235236
self.create_file(file_path, contents="contents here1")
236237
fake_file = self.open(file_path, "r", encoding="utf8")
237238
fd = fake_file.fileno()
238-
self.open(fd, encoding="utf8")
239-
if not IS_PYPY:
240-
with self.assertRaises(OSError) as cm:
241-
self.open(fd, encoding="utf8")
242-
self.assertEqual(errno.EBADF, cm.exception.errno)
243-
else:
244-
self.open(fd, encoding="utf8")
245-
self.os.close(fd)
239+
# note: we need to assign the files to objects,
240+
# otherwise the file will be closed immediately in the CPython implementation
241+
# note that this case is not (and will probably not be) handled in pyfakefs
242+
file1 = self.open(fd, encoding="utf8") # noqa: F841
243+
file2 = self.open(fd, encoding="utf8") # noqa: F841
244+
245+
self.os.close(fd)
246246

247247
def test_open_fd_write_mode_for_ro_file(self):
248248
# Create a writable file handle to a read-only file, see #967
@@ -3159,22 +3159,6 @@ def test_listdir_impossible_without_read_permission(self):
31593159
with self.open(file_path, encoding="utf8") as f:
31603160
assert f.read() == "hey"
31613161

3162-
def test_fdopen_twice(self):
3163-
file_path1 = self.make_path("some_file1")
3164-
file_path2 = self.make_path("SOME_file1")
3165-
self.create_file(file_path1, contents="contents here1")
3166-
3167-
fake_file1 = self.open(file_path2, "r", encoding="utf8")
3168-
fileno1 = fake_file1.fileno()
3169-
self.os.fdopen(fileno1, encoding="utf8")
3170-
if not IS_PYPY:
3171-
with self.assertRaises(OSError) as cm:
3172-
self.open(fileno1, encoding="utf8")
3173-
self.assertEqual(errno.EBADF, cm.exception.errno)
3174-
else:
3175-
self.open(fileno1, encoding="utf8")
3176-
self.os.close(fileno1)
3177-
31783162
def test_stat(self):
31793163
directory = self.make_path("xyzzy")
31803164
directory1 = self.make_path("XYZZY")

0 commit comments

Comments
 (0)