diff --git a/fake_filesystem_test.py b/fake_filesystem_test.py index 50dcddd9..021c09d5 100755 --- a/fake_filesystem_test.py +++ b/fake_filesystem_test.py @@ -4599,7 +4599,7 @@ def testReadWithRplus(self): fake_file.close() def testAccessingClosedFileRaises(self): - self.filesystem.is_windows_fs = False + # self.filesystem.is_windows_fs = False self.filesystem.CreateFile(self.file_path, contents=b'test') fake_file = self.open(self.file_path, 'r') fake_file.close() @@ -4610,6 +4610,12 @@ def testAccessingClosedFileRaises(self): self.assertRaises(ValueError, lambda: fake_file.tell()) self.assertRaises(ValueError, lambda: fake_file.seek(1)) + def testAccessingOpenFileWithAnotherHandleRaises(self): + self.os.open(self.file_path, os.O_CREAT | os.O_WRONLY | os.O_TRUNC) + fake_file = self.open(self.file_path, 'r') + fake_file.close() + self.assertRaises(ValueError, lambda: fake_file.read(1)) + self.assertRaises(ValueError, lambda: fake_file.write(1)) class OpenWithFileDescriptorTest(FakeFileOpenTestBase): @unittest.skipIf(sys.version_info < (3, 0), 'only tested on 3.0 or greater') diff --git a/pyfakefs/fake_filesystem.py b/pyfakefs/fake_filesystem.py index aa900645..d312aafc 100644 --- a/pyfakefs/fake_filesystem.py +++ b/pyfakefs/fake_filesystem.py @@ -4302,10 +4302,7 @@ def write_error(*args, **kwargs): return getattr(self._io, name) def _check_open_file(self): - open_files = [wrapper.GetObject() for wrapper - in self._filesystem.open_files if wrapper] - is_open = self._file_object in open_files - if not self._is_stream and not is_open: + if not self._is_stream and not self in self._filesystem.open_files: raise ValueError('I/O operation on closed file') def __iter__(self): @@ -4467,6 +4464,8 @@ def Call(self, file_, mode='r', buffering=-1, encoding=None, raw_io=self.raw_io) if filedes is not None: fakefile.filedes = filedes + # replace the file wrapper + self.filesystem.open_files[filedes] = fakefile else: fakefile.filedes = self.filesystem.AddOpenFile(fakefile) return fakefile