Skip to content

io.UnsupportedOperation: File is not open for reading on io.TextIOWrapper #1265

@davidlbaird

Description

@davidlbaird

Describe the bug
Found when calling pandas DataFrame.to_csv on an open file handle. An error is raised when
opening a file with mode='w'.

self = <pyfakefs.tests.fake_open_test.FakeFileOpenTest testMethod=test_io_text_wrapper>

    def test_io_text_wrapper(self):
        file_path = self.make_path("foo")
        with self.open(file_path, mode="w") as f:
>         handle = self.fake_io_module.TextIOWrapper(f)

[tests/fake_open_test.py:1014](redacted): 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
fake_file.py:1219: in read_error
    self._raise("File is not open for reading.")
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

self = <pyfakefs.fake_file.FakeFileWrapper object at 0x336ebe0cf5c0>
message = 'File is not open for reading.'

    def _raise(self, message: str) -> NoReturn:
        if self.raw_io:
            self._filesystem.raise_os_error(errno.EBADF, self.file_path)
>       raise io.UnsupportedOperation(message)
E       io.UnsupportedOperation: File is not open for reading.

fake_file.py:823: UnsupportedOperation

The error is not raised if opening the file with mode='w+'.

How To Reproduce
I added a test function in fake_open_test.py

class FakeFileOpenTest(FakeFileOpenTestBase):

  def test_io_text_wrapper(self):
        file_path = self.make_path("foo")
        with self.open(file_path, mode="w") as f:
          handle = self.fake_io_module.TextIOWrapper(f)
          handle.write("test")

The test passes when the "w" is changed to "w+'.

Your environment

Linux-6.16.12-1rodete2-amd64-x86_64-with-glibc2.27
Python 3.12.11 (stable, redacted, redacted) [Clang 9999.0.0 (7d381f2a5634d1e41b61299839d652cc4a021898)]
pyfakefs 6.0.0
pytest 8.3.4

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions