pytest-dev / pyfakefs

Provides a fake file system that mocks the Python file system modules.
https://pytest-pyfakefs.readthedocs.io
Apache License 2.0
627 stars 88 forks source link

[WIP] Make fake filesystem, fake `os` and fake `os.path` respect `additional_skip_names` #1025

Open sassanh opened 1 month ago

sassanh commented 1 month ago

Describe the changes

Add a call stack check for fake filesystem, os and os.path methods in their handle_original_call wrapper to see if they are being called from a module listed in skip_modules.

Tasks

sassanh commented 1 month ago

This is mostly brainstorm material to see if we want to go in this direction.

Implementation details: Most of the job is done as a filter in wrapper functions, is_called_from_skipped_module starts from the end of the call stack and goes up until it reaches a module that is:

  1. Is not a frozen importlib
  2. outside of Python's stdlib
  3. outside of pyfakefs library itself (except for tests/ and pytest_tests/ directories) This module is considered the module that is calling the wrapped function and if it is listed in skip_names, then it will try to run the original function instead of the fake wrapped function.

It is working for Python 3.12, 3.11 and mostly 3.10, but 3.9, 3.8 and 3.7 have yet different internals for pathlib.

sassanh commented 3 weeks ago

@mrbean-bremen When you got the time, please check it and let me know if you approve this approach. If it looks good to you, I will work on tests for other Python versions and Windows.