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

patch_open_code doesn't seem to work in Python 3.12 #892

Closed
alexei opened this issue Oct 7, 2023 · 7 comments
Closed

patch_open_code doesn't seem to work in Python 3.12 #892

alexei opened this issue Oct 7, 2023 · 7 comments
Labels

Comments

@alexei
Copy link

alexei commented Oct 7, 2023

I have a program that imports Python modules dynamically and I use patch_open_code in my tests. I tried running it under Python 3.12 just for fun and found the tests are not working.

For example, this works in e.g. 3.11:

import importlib.util
from contextlib import redirect_stdout
from io import StringIO

import pytest
from pyfakefs.fake_filesystem_unittest import Patcher, PatchMode


@pytest.fixture
def fffs():
    with Patcher(patch_open_code=PatchMode.AUTO) as patcher:
        yield patcher.fs


def import_foo():
    spec = importlib.util.spec_from_file_location("bar", "/foo/bar.py")
    mod = importlib.util.module_from_spec(spec)
    spec.loader.exec_module(mod)


def test_foo(fffs):
    fffs.create_file("/foo/bar.py", contents="print('hello')")
    with redirect_stdout(StringIO()) as stdout:
        import_foo()
    assert stdout.getvalue() == "hello\n"

The test passes alright. In 3.12, however, I'm getting:

__________________________________ test_foo ___________________________________

fffs = <pyfakefs.fake_filesystem.FakeFilesystem object at 0xBAADF00D>

    def test_foo(fffs):
        fffs.create_file("/foo/bar.py", contents="print('hello')")
        with redirect_stdout(StringIO()) as stdout:
>           import_foo()

/test_ffs.py:24: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
/test_ffs.py:18: in import_foo
    spec.loader.exec_module(mod)
<frozen importlib._bootstrap_external>:990: in exec_module
    ???
<frozen importlib._bootstrap_external>:1127: in get_code
    ???
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

self = <_frozen_importlib_external.SourceFileLoader object at 0xDEADC0DE>, path = '/foo/bar.py'

>   ???
E   FileNotFoundError: [Errno 2] No such file or directory: '/foo/bar.py'

<frozen importlib._bootstrap_external>:1185: FileNotFoundError

Tested under Python 3.11.6 and 3.12.0, respectively, with pytest 7.4.0 and pyfakefs 5.2.4.

@mrbean-bremen
Copy link
Member

mrbean-bremen commented Oct 8, 2023

Thanks, I will have a look at it. It may be related to #836, which I haven't managed to fix yet. Now that 3.12 is released, this gets a bit more important...

@alexei
Copy link
Author

alexei commented Oct 8, 2023

Let me know what I can do to help. For example, it doesn't look like this use case (i.e. importing modules) is covered with tests. I could open a PR to add the above code to pyfakefs. What are your thoughts on this?

@mrbean-bremen
Copy link
Member

mrbean-bremen commented Oct 8, 2023

Thanks - I can basically just use your code as a test, and I can reproduce the problem.
I think I already found a trivial fix, that hopefully also fixes the other problem.
Edit: Not quite there yet... this caused another problem.

@mrbean-bremen
Copy link
Member

mrbean-bremen commented Oct 8, 2023

I couldn't get it to work under Windows yet (only in the CI, locally in works), but it helped me to fix the other problem with patching open (this was indeed a trivial fix).
For some reason, your test works under Windows only with Python 3.9. I suppose that it didn't work before either, at least in the CI (still have no idea what is different from my local setup). I will have another go some time later this week...

mrbean-bremen added a commit to mrbean-bremen/pyfakefs that referenced this issue Oct 9, 2023
- fixes Windows problem if called from fspath, if
  the real current drive is not C:
- fixes test for pytest-dev#892 under Windows
mrbean-bremen added a commit that referenced this issue Oct 10, 2023
- fixes Windows problem if called from fspath, if
  the real current drive is not C:
- fixes test for #892 under Windows
@mrbean-bremen
Copy link
Member

Should be fixed in main now - please check!

@mrbean-bremen
Copy link
Member

New version with the fix is released.

@alexei
Copy link
Author

alexei commented Oct 29, 2023

@mrbean-bremen thanks for looking into this. I can confirm it's working 👍

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