Skip to content

Commit

Permalink
Fixed check for open file
Browse files Browse the repository at this point in the history
- fixed opening of file with file descriptor
- fixes pytest-dev#282
  • Loading branch information
mrbean-bremen committed Aug 9, 2017
1 parent 6fda286 commit 282f242
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
8 changes: 7 additions & 1 deletion fake_filesystem_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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')
Expand Down
7 changes: 3 additions & 4 deletions pyfakefs/fake_filesystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 282f242

Please sign in to comment.