Skip to content

The old co_filename is used when test files/folders are moved #14552

@15r10nk

Description

@15r10nk

Problem:

I use co_filename of code objects to find the sourcefile of the calling function in inline-snapshot and have some issues where the file (reported by co_filename) does not exist.

It appears that it has been moved for differend reasons (CI artefacts, docker).

The problem is that the old file location is still stored inside the .pyc file which is generated by pytest.

Repro:

mkdir -p test1

cat > test1/test_a.py << EOF
from inspect import currentframe, getfile

def test_a():
    assert getfile(test_a)==__file__
    assert currentframe().f_code.co_filename == __file__
EOF


echo first run
uvx pytest@9.0.3 -s test1/test_a.py

mv test1 test2

echo
echo second run
uvx pytest@9.0.3 -s test2/test_a.py

rm -r test2

output:

first run
========================================== test session starts ===========================================
platform linux -- Python 3.14.3, pytest-9.0.3, pluggy-1.6.0
rootdir: /home/frank/projects/tmp/repro_bytecode_move
collected 1 item                                                                                         

test1/test_a.py .

=========================================== 1 passed in 0.01s ============================================

second run
========================================== test session starts ===========================================
platform linux -- Python 3.14.3, pytest-9.0.3, pluggy-1.6.0
rootdir: /home/frank/projects/tmp/repro_bytecode_move
collected 1 item                                                                                         

test2/test_a.py F

================================================ FAILURES ================================================
_________________________________________________ test_a _________________________________________________

>   ???
E   AssertionError: assert '/home/frank/...st1/test_a.py' == '/home/frank/...st2/test_a.py'
E     
E     Skipping 39 identical leading characters in diff, use -v to show
E     - _move/test2/test_a.py
E     ?           ^
E     + _move/test1/test_a.py
E     ?           ^

/home/frank/projects/tmp/repro_bytecode_move/test1/test_a.py:4: AssertionError
======================================== short test summary info =========================================
FAILED test2/test_a.py::test_a - AssertionError: assert '/home/frank/...st1/test_a.py' == '/home/frank/...
st2/test_a.py'
=========================================== 1 failed in 0.04s ============================================

I have already created an PR #14551

pytest: 9.0.3
os: linux

  • a detailed description of the bug or problem you are having
  • output of pip list from the virtual environment you are using
  • pytest and operating system versions
  • minimal example if possible

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions