Skip to content

Add missing mode property to fake file wrapper #1163

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 18, 2025

Conversation

mrbean-bremen
Copy link
Member

@mrbean-bremen mrbean-bremen commented Apr 16, 2025

Tasks

  • Unit tests added that reproduce the issue or prove feature is working
  • Fix or feature added
  • Entry to release notes added
  • Pre-commit CI shows no errors
  • Unit tests passing

@@ -890,6 +888,16 @@ def closed(self) -> bool:
"""Simulate the `closed` attribute on file."""
return not self._is_open()

@property
def mode(self) -> str:
if self.open_modes.append:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to clarify how I discovered this bug - it might help you better handle different values for the mode parameter:I came across this code in the imp module (imp.load_source) (Python 3.9):

class _HackedGetData:
    """Compatibility support for 'file' arguments of various load_*()
    functions."""

    def __init__(self, fullname, path, file=None):
        super().__init__(fullname, path)
        self.file = file

    def get_data(self, path):
        """Gross hack to contort loader to deal w/ load_*()'s bad API."""
        if self.file and path == self.path:
            # The contract of get_data() requires us to return bytes. Reopen the
            # file in binary mode if needed.
            if not self.file.closed:
                file = self.file
                if 'b' not in file.mode:
                    file.close()
            if self.file.closed:
                self.file = file = open(self.path, 'rb')

            with file:
                return file.read()
        else:
            return super().get_data(path)

This code relies on checking for "b" (binary) mode.
For completeness, it might make sense to add these values too

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, good point - I forgot that!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, added the check for binary mode, please check!

Copy link

@evgenii-moriakhin evgenii-moriakhin Apr 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

from doc https://docs.python.org/3/library/functions.html#open

Modes 'r+' and 'r+b' open the file with no truncation.

in the current code here

       if self.open_modes.truncate:
            if self._binary:
                return "rb+" if self.open_modes.can_read else "wb"

this is probably a typo

although I see a separate test on r+b below, and maybe I haven't studied how the truncate flag works in the fake file

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All these tests are run both in the real and in the fake fs, so no, this is not a typo - I just reproduced the behavior in the real file system, though that was unexpected to me, too.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also tested this locally, though I haven't found an explanation for this behavior.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I'm merging this then.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BTW, I've been thinking about the r+/w+ behavior, and I think that this is because while there is a difference on opening with r+ and w+ (w+ will truncate the file, r+ will not), after the fie has been opened, the behavior is exactly the same for both modes (e.g. both allow reading and writing). Returning the mode in both cases as "r+" may be a design decision.

@mrbean-bremen mrbean-bremen marked this pull request as draft April 17, 2025 18:17
@mrbean-bremen mrbean-bremen marked this pull request as ready for review April 17, 2025 18:37
@mrbean-bremen mrbean-bremen merged commit a4da98c into pytest-dev:main Apr 18, 2025
121 checks passed
@mrbean-bremen mrbean-bremen deleted the file_mode branch April 18, 2025 16:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Bug: FakeFileOpen objects don't implement the mode property required by typing.io.IO
2 participants