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(..., dir=dir) does not raise FileNotFoundError on Linux when directory does not exist #511

Closed
vtsatskin opened this issue Jan 13, 2020 · 2 comments
Labels

Comments

@vtsatskin
Copy link

Describe the bug
When invoking tempfile.TemporaryFile(..., dir=dir) when dir does not exist on a fake filesystem under Linux, a FileNotFoundError is not raised. This does not occur on macOS.

How To Reproduce

import pytest
from pyfakefs.fake_filesystem import FakeFilesystem
import tempfile

def test_file_not_found(fs: FakeFilesystem) -> None:
    with pytest.raises(FileNotFoundError):
        tempfile.TemporaryFile(dir="/bogus")

This test fails because FileNotFoundError is not raised. When running the test on macOS, it passes.

Checking the expected behaviour using the real filesystem:

# macOS
Python 3.6.7 (v3.6.7:6ec5cf24b7, Oct 20 2018, 03:02:14) 
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import tempfile
>>> tempfile.TemporaryFile(dir="/bogus")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/tempfile.py", line 622, in TemporaryFile
    (fd, name) = _mkstemp_inner(dir, prefix, suffix, flags, output_type)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/tempfile.py", line 262, in _mkstemp_inner
    fd = _os.open(file, flags, 0o600)
FileNotFoundError: [Errno 2] No such file or directory: '/bogus/tmp9fiv_v7v'
# Linux
Python 3.6.9 (default, Nov  7 2019, 10:44:02) 
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import tempfile
>>> tempfile.TemporaryFile(dir="/bogus")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.6/tempfile.py", line 761, in TemporaryFile
    (fd, name) = _mkstemp_inner(dir, prefix, suffix, flags, output_type)
  File "/usr/lib/python3.6/tempfile.py", line 401, in _mkstemp_inner
    fd = _os.open(file, flags, 0o600)
FileNotFoundError: [Errno 2] No such file or directory: '/bogus/tmpnkiv2m_v'

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 4.0
@mrbean-bremen
Copy link
Member

Thanks - I will probably have a look at this later tomorrow.

@mrbean-bremen
Copy link
Member

mrbean-bremen commented Jan 14, 2020

Ok, please check with master - hopefully this fixes the problem. It was caused by the same linux-specific O_TMPFILE flag as the previous one.

mrbean-bremen added a commit that referenced this issue Mar 2, 2020
- create unique file name for memory-based file
  created by that flag
- fixes #511
- 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