Skip to content

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

@alexei

Description

@alexei

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions