Skip to content

Disk full error not being raised appropriately when file is re-opened #660

@victorallume

Description

@victorallume

Describe the bug
There seems to be a bug when a file is re-opened for writing, that disk usage is not calculated correctly, and hence does not throw disk full exceptions at the right place. In the code below, in the first test, the assertion fails. The second test works fine. The third function fails the assertion. I discovered this error in code which re-opens files regularly, and uses random-access read/write mode ('r+'), and seeking within files.

How To Reproduce

class ExampleTestCase(TestCase):
    def setUp(self):
        self.setUpPyfakefs()
        self.fs.set_disk_usage(100)

    def test_disk_full_reopened_160(self):
        with open('bar.txt', 'w') as f:
            f.write('a' * 60)
        with open('bar.txt') as f:
            self.assertEqual(f.read(), 'a' * 60)
        with open('bar.txt', 'w') as f:
            with self.assertRaises(OSError):
                f.write('b' * 160)
                f.flush()

    def test_disk_full_reopened_161(self):
        with open('bar.txt', 'w') as f:
            f.write('a' * 60)
        with open('bar.txt') as f:
            self.assertEqual(f.read(), 'a' * 60)
        with open('bar.txt', 'w') as f:
            with self.assertRaises(OSError):
                f.write('b' * 161)
                f.flush()

    def test_disk_full_reopened_rplus_seek(self):
        with open('bar.txt', 'w') as f:
            f.write('a' * 60)
        with open('bar.txt') as f:
            self.assertEqual(f.read(), 'a' * 60)
        with open('bar.txt', 'r+') as f:
            with self.assertRaises(OSError):
                f.seek(50)
                f.write('b' * 60)
                f.flush()

Your environment
Please run the following and paste the output.

Linux-5.15.11-76051511-generic-x86_64-with-glibc2.34
Python 3.9.7 (default, Sep 10 2021, 14:59:43) 
[GCC 11.2.0]
pyfakefs 4.5.4

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions