Skip to content
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

tempfile.TemporaryFile does not work on Linux with a fake filesystem #509

Closed
vtsatskin opened this issue Jan 10, 2020 · 3 comments
Closed
Labels

Comments

@vtsatskin
Copy link

vtsatskin commented Jan 10, 2020

Describe the bug
When invoking tempfile.TemporaryFile on a fake filesystem under Linux, a NotImplementedError is raised. This does not occur on macOS.

How To Reproduce

Both of the following tests fail:

from pyfakefs.fake_filesystem import FakeFilesystem
import tempfile


def test_tempfile_defaults(fs: FakeFilesystem) -> None:
    tempfile.TemporaryFile()


def test_tempfile_custom_dir(fs: FakeFilesystem) -> None:
    fs.create_dir("/dir")
    tempfile.TemporaryFile(dir="/dir")

# passes, checking if temporary directory exists
def test_tempfile_get_dir(fs: FakeFilesystem) -> None:
    assert tempfile.gettempdir() == "/tmp"

With the following truncated output:

/home/nvidia/passengerai/device/.venv/python-3.6.9/lib/python3.6/tempfile.py:735: in TemporaryFile
    fd = _os.open(dir, flags2, 0o600)
        fs.create_dir("/dir")
>       tempfile.TemporaryFile(dir="/dir")

/home/nvidia/passengerai/device/tests/unit/media_streaming/storage_management/test_pyfakefs.py:11:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
/home/nvidia/passengerai/device/.venv/python-3.6.9/lib/python3.6/tempfile.py:735: in TemporaryFile
    fd = _os.open(dir, flags2, 0o600)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

self = <pyfakefs.fake_filesystem.FakeOsModule object at 0x7f7e8a0eb8>, path = '/dir'
flags = 4243586, mode = 384, dir_fd = None

    def open(self, path, flags, mode=None, dir_fd=None):
        """Return the file descriptor for a FakeFile.
    
        Args:
            path: the path to the file
            flags: low-level bits to indicate io operation
            mode: bits to define default permissions
                Note: only basic modes are supported, OS-specific modes are
                ignored
            dir_fd: If not `None`, the file descriptor of a directory,
                with `file_path` being relative to this directory.
                New in Python 3.3.
    
        Returns:
            A file descriptor.
    
        Raises:
            IOError: if the path cannot be found
            ValueError: if invalid mode is given
            NotImplementedError: if `os.O_EXCL` is used without `os.O_CREAT`
        """
        path = self._path_with_dir_fd(path, self.open, dir_fd)
        if mode is None:
            if self.filesystem.is_windows_fs:
                mode = 0o666
            else:
                mode = 0o777 & ~self._umask()
    
        open_modes = _OpenModes(
            must_exist=not flags & os.O_CREAT,
            can_read=not flags & os.O_WRONLY,
            can_write=flags & (os.O_RDWR | os.O_WRONLY),
            truncate=flags & os.O_TRUNC,
            append=flags & os.O_APPEND,
            must_not_exist=flags & os.O_EXCL
        )
        if open_modes.must_not_exist and open_modes.must_exist:
            raise NotImplementedError(
>               'O_EXCL without O_CREAT mode is not supported')
E           NotImplementedError: O_EXCL without O_CREAT mode is not supported

/home/nvidia/passengerai/device/.venv/python-3.6.9/lib/python3.6/site-packages/pyfakefs/fake_filesystem.py:3739: NotImplementedError
=================================== 2 failed, 1 passed in 2.63s ===================================

Your enviroment

Linux-4.9.140-tegra-aarch64-with-Ubuntu-18.04-bionic
Python 3.6.9 (default, Nov  7 2019, 10:44:02)
pyfakefs 3.7.1

This also happens on pyfakefs 4.0 from the master branch.

@vtsatskin vtsatskin changed the title tempfile.TemporaryFile does not work on Linux tempfile.TemporaryFile does not work on Linux with a fake filesystem Jan 10, 2020
@mrbean-bremen
Copy link
Member

Thanks for the report! Looks like tempfile is using some open mode that we don't correctly support - I will have a look.

@mrbean-bremen
Copy link
Member

@vtsatskin - please check if current master works for you, and reopen if you still have problems!

@vtsatskin
Copy link
Author

@mrbean-bremen thanks so much for the timely response and fix as usual. I've tested it and found that the issue has been resolved.

Upon further testing with my test suite, I found another bug related to tempfile.TemporaryFile and filed it in #511.

mrbean-bremen added a commit that referenced this issue Mar 2, 2020
- added basic test for TemporaryFile
- fixes #509
- back-ported from master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants