Skip to content

ValueError when calling AlignmentFile.fetch() for Python 3.13, pysam > 0.23.0, and pytest-cov #1350

@fedarko

Description

@fedarko

Hi, and thank you for the very useful library!

I have been developing a Python project that uses pysam. Recently, this project's test suite (covering Python 3.6, 3.7, 3.8, 3.9, 3.10, 3.11, 3.12, and 3.13) started failing only on the Python 3.13 build. I isolated this error to part of my test suite that creates a simple test BAM file.

I am using pytest to test this project, which is the same test runner that pysam uses. Strangely, as shown below, this error only comes up when:

  • I am using Python 3.13.
  • I run pytest using the pytest-cov extension for coverage testing.
  • I am using pysam 0.23.1 or greater.

The descriptions below show how to reproduce this problem in a conda environment using Python 3.13.5, pysam 0.23.3, pytest 8.4.1, and pytest-cov 6.2.1. (Also, the system I am running these tests on is using Ubuntu 22.04.5 LTS x86_64.) Thanks so much, and let me know if I can provide any additional information.

Reproducing the problem

Installing dependencies

Here I install Python using conda into a fresh environment named fetchbug, and then install pysam / pytest / pytest-cov into this environment using its pip. (Per #1349, we can't install pysam using conda into a Python 3.13 environment yet.)

mamba create --yes --name fetchbug python=3.13
conda activate fetchbug
pip install pysam pytest pytest-cov

Python code (test.py)

Here is a minimal test function that reproduces the problem:

import pysam

def test_fetch():
    # Adapted from pysam's documentation on "Creating BAM/CRAM/SAM files from
    # scratch" -- https://pysam.readthedocs.io/en/latest/usage.html
    a1 = pysam.AlignedSegment()
    a1.query_name = "read_A"
    a1.query_sequence = "A" * 35
    a1.flag = 0
    a1.reference_id = 0
    a1.reference_start = 25
    # CIGAR string:
    # 6S 20= 2I 21D 7S
    a1.cigar = ((4, 6), (7, 20), (1, 2), (2, 21), (4, 7))

    header = {
        "HD": {"VN": "1.0"},
        "SQ": [{"LN": 12345, "SN": "genome1"}],
    }
    bam_fp = "out.bam"
    with pysam.AlignmentFile(bam_fp, "wb", header=header) as of:
        of.write(a1)
    pysam.index(bam_fp)
    loaded_bf = pysam.AlignmentFile(bam_fp, "rb")
    # The line below causes a ValueError only (1) on Python 3.13 and (2) when
    # we're using pytest-cov to profile code coverage.
    alns = list(loaded_bf.fetch())

Running just pytest (does not trigger the problem)

$ pytest test.py
============================= test session starts ==============================
platform linux -- Python 3.13.5, pytest-8.4.1, pluggy-1.6.0
rootdir: /home/marcus/Dropbox/Work/PevznerLab/metaLJA
plugins: cov-6.2.1
collected 1 item

test.py .                                                                [100%]

============================== 1 passed in 0.04s ===============================

Running pytest using coverage profiling (triggers the problem)

$ pytest test.py --cov
============================= test session starts ==============================
platform linux -- Python 3.13.5, pytest-8.4.1, pluggy-1.6.0
rootdir: /home/marcus/Dropbox/Work/PevznerLab/metaLJA
plugins: cov-6.2.1
collected 1 item

test.py F                                                                [100%]

=================================== FAILURES ===================================
__________________________________ test_fetch __________________________________

    def test_fetch():
        # Adapted from pysam's documentation on "Creating BAM/CRAM/SAM files from
        # scratch" -- https://pysam.readthedocs.io/en/latest/usage.html
        a1 = pysam.AlignedSegment()
        a1.query_name = "read_A"
        a1.query_sequence = "A" * 35
        a1.flag = 0
        a1.reference_id = 0
        a1.reference_start = 25
        # CIGAR string:
        # 6S 20= 2I 21D 7S
        a1.cigar = ((4, 6), (7, 20), (1, 2), (2, 21), (4, 7))
    
        header = {
            "HD": {"VN": "1.0"},
            "SQ": [{"LN": 12345, "SN": "genome1"}],
        }
        bam_fp = "out.bam"
        with pysam.AlignmentFile(bam_fp, "wb", header=header) as of:
            of.write(a1)
        pysam.index(bam_fp)
        loaded_bf = pysam.AlignmentFile(bam_fp, "rb")
        # The line below causes a ValueError only (1) on Python 3.13 and (2) when
        # we're using pytest-cov to profile code coverage.
>       alns = list(loaded_bf.fetch())
               ^^^^^^^^^^^^^^^^^^^^^^^
E       ValueError: Firing event 10 with no exception set

test.py:27: ValueError
================================ tests coverage ================================
_______________ coverage: platform linux, python 3.13.5-final-0 ________________

Name      Stmts   Miss  Cover
-----------------------------
test.py      16      0   100%
-----------------------------
TOTAL        16      0   100%
=========================== short test summary info ============================
FAILED test.py::test_fetch - ValueError: Firing event 10 with no exception set
============================== 1 failed in 0.08s ===============================

Discussion

  • "Firing event." I searched both the pysam and pytest-cov GitHub repositories for the text Firing event, and didn't find any hits. From searching across all of GitHub for Firing event and no exception set, this error message seems to trace back all the way to the CPython codebase?

  • Segfaults? When I run the entire test suite at once, I sometimes get a segfault (rather than seeing a bunch of individual ValueError messages). I am not sure what the cause of this is, though -- I cannot reliably reproduce it on my laptop, and I assume it is something like a side effect of the above error occurring many times in a row. (Here are screenshots of this happening in our GitHub Actions workflow, in case it is helpful.)

Image
Image

  • pysam versions. I tried repeating the test above with pysam 0.23.3, 0.23.2, 0.23.1, and 0.23.0 (just pip installing different versions). The test remained broken for pysam 0.23.3, 0.23.2, and 0.23.1, but it succeeded with pysam 0.23.0. Maybe the 0.23.0 to 0.23.1 update broke something here?

  • Python versions. This being said, it looks like the Python 3.12 run of our CI also installed pysam 0.23.3, and it worked fine. So, it seems like the specific error really is caused by the three-way combination of python 3.13 + pysam > 0.23.0 + pytest-cov.

  • Importance. On my side, this is not a very urgent error -- for now, I can just disable coverage profiling on the Python 3.13 build. However, I worry it might be a symptom of something subtly problematic in pysam's codebase.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions