question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

open is not skipped for skipped modules

See original GitHub issue

While experimenting with the package, I have found that the open builtin is faked for even for modules that I have excluded from faking via the Patcher context manager.

Traceback (most recent call last):
  File "c:\Users\gilad\Documents\my_module\test.py", line 9, in <module>
    test()
  File "c:\Users\gilad\Documents\my_module\test.py", line 7, in test
    my_func()
  File "c:\Users\gilad\Documents\my_module\my_file.py", line 7, in my_func
    open('test.py')
  File "C:\Users\gilad\AppData\Local\Programs\Python\Python38\lib\site-packages\pyfakefs\fake_filesystem.py", line 4449, in open
    return fake_open(file, mode, buffering, encoding, errors,
  File "C:\Users\gilad\AppData\Local\Programs\Python\Python38\lib\site-packages\pyfakefs\fake_filesystem.py", line 4935, in __call__
    return self.call(*args, **kwargs)
  File "C:\Users\gilad\AppData\Local\Programs\Python\Python38\lib\site-packages\pyfakefs\fake_filesystem.py", line 4983, in call
    file_object = self._init_file_object(file_object,
  File "C:\Users\gilad\AppData\Local\Programs\Python\Python38\lib\site-packages\pyfakefs\fake_filesystem.py", line 5037, in _init_file_object
    self.filesystem.raise_os_error(errno.ENOENT, file_path)
  File "C:\Users\gilad\AppData\Local\Programs\Python\Python38\lib\site-packages\pyfakefs\fake_filesystem.py", line 960, in raise_os_error
    raise OSError(errno, message, filename)
FileNotFoundError: [Errno 2] No such file or directory in the fake filesystem: 'test.py'

How To Reproduce I have the following setup:

.
├── __init__.py
├── __pycache__
│   ├── __init__.cpython-38.pyc
│   ├── my_file.cpython-38.pyc
│   └── test.cpython-38-pytest-6.0.2.pyc
├── my_file.py
└── test.py

test.py:

from pyfakefs.fake_filesystem_unittest import Patcher

from my_file import my_func

def test():
    with Patcher(additional_skip_names=['my_file']):
        my_func()

test()

my_file.py:

import os

def my_func():
    os.open('test.py', os.O_RDONLY, 0o777) # works!

    open('test.py') # fails...

Your enviroment

python -c "import platform; print(platform.platform())"
python -c "import sys; print('Python', sys.version)"
python -c "from pyfakefs.fake_filesystem import __version__; print('pyfakefs', __version__)"

The output is as follows:

Windows-10-10.0.19041-SP0
Python 3.8.3 (tags/v3.8.3:6f8c832, May 13 2020, 22:37:02) [MSC v.1924 64 bit (AMD64)]
pyfakefs 4.1.0

Could a possible solution be patching the builtin open by overriding it only in the modules not listed for skip? It should be checked that the module hadn’t overrode the open by itself, since it may cause overriding it again.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
mrbean-bremencommented, Sep 24, 2020

As a temporary solution I am using add_real_directory for adding the necessary files to the fake fs, but this is not ideal…

Actually I used to do the same for some django-related package (I think tzinfo, which loads the timezone info from disk). I agree that this is not ideal (though it works well enough, and has no real performance penalty), so to fix these things is a good idea.

0reactions
mrbean-bremencommented, Nov 24, 2020

@hard7 - thanks for the report–this is obviously a bug. logsio shall not get skipped just because it ends with io. Can you please write a new issue for this, as it is not related to this one (other than it may have been introduced while fixing this one)? I will try to fix this ASAP.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Managed module skipped · Issue #65 - GitHub
Hello,. I disabled the managed modules because OpenCppCoverage supports only the unmanaged code. For example, there is a crash in this case (See ......
Read more >
Skip requiring a module for tests in Node.js - Stack Overflow
The problem is, that in moduleTwo some code is run that opens a websocket and the test does not finish. How do i...
Read more >
doctest — Test interactive Python examples — Python 3.11.1 ...
Source code: Lib/doctest.py The doctest module searches for pieces of text ... The SKIP flag can also be used for temporarily “commenting out”...
Read more >
How do I prevent a kernel module from loading automatically?
Ensure the module is not configured to get loaded in either /etc/modprobe.conf ... flag to mkinitrd to have it skip the module in...
Read more >
How to use skip and xfail to deal with tests that cannot succeed
You can skip tests on a missing import by using pytest.importorskip at module level, within a test, or test setup function. docutils =...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found