[med-svn] [Git][med-team/python-pysam][master] 3 commits: Fix faidx output capture and improve pysam's use of -o for redirection

Andreas Tille (@tille) gitlab at salsa.debian.org
Wed Nov 4 10:08:02 GMT 2026



Andreas Tille pushed to branch master at Debian Med / python-pysam


Commits:
a4f319b2 by Andreas Tille at 2026-11-04T10:49:33+01:00
Fix faidx output capture and improve pysam's use of -o for redirection

- - - - -
8299c135 by Andreas Tille at 2026-11-04T10:49:53+01:00
skip-32bit-tests.patch: skip the 64-bit object-size checks and the PileupColumn access-after-iterator test on 32-bit platforms. Closes: #1152973

- - - - -
d30d427b by Andreas Tille at 2026-11-04T10:50:20+01:00
Upload to unstable

- - - - -


4 changed files:

- debian/changelog
- + debian/patches/6261249228.patch
- debian/patches/series
- + debian/patches/skip-32bit-tests.patch


Changes:

=====================================
debian/changelog
=====================================
@@ -1,3 +1,13 @@
+python-pysam (0.24.1+ds-3) unstable; urgency=medium
+
+  * Fix faidx output capture and improve pysam's use of -o for
+    redirection
+  * skip-32bit-tests.patch: skip the 64-bit object-size checks
+    and the PileupColumn access-after-iterator test on 32-bit platforms.
+    Closes: #1152973
+
+ -- Andreas Tille <tille at debian.org>  Wed, 04 Nov 2026 10:50:05 +0100
+
 python-pysam (0.24.1+ds-2) unstable; urgency=medium
 
   * Upload to unstable


=====================================
debian/patches/6261249228.patch
=====================================
@@ -0,0 +1,143 @@
+Origin: https://github.com/pysam-developers/pysam/commit/6261249228
+Author: John Marshall <jmarshall at hey.com>
+Date: Wed, 23 Sep 2026 20:03:50 +1200
+Subject: [PATCH] Fix faidx output capture and improve pysam's use of -o for
+ redirection
+
+Capturing faidx output has been broken since v0.24.0 due to importing
+samtools >=1.22 which newly uses bgzf_open() to write to standard output,
+similarly to the calmd command. Fix this by patching `bgzf_open("-", ...)`
+and using the same MAP_STDOUT_OPTIONS technique as for calmd.
+
+The view -c special case has been unnecessary since samtools 1.4.
+
+If the user-specified arguments use -o FILE themselves, the caller needs
+to use catch_stdout=False to avoid the synthetic -o option used to capture
+stdout clobbering the desired -o option. Make this more robust by prepending
+the synthetic -o option rather than appending it, so that the user's option
+clobbers the synthetic one instead. Fixes #1397.
+
+Add test cases for commands that use special MAP_STDOUT_OPTIONS handling.
+---
+ devtools/import.py       |  3 +++
+ pysam/libcutils.pyx      |  7 +++----
+ samtools/faidx.c.pysam.c |  2 +-
+ tests/samtools_test.py   | 45 +++++++++++++++++++++++++++++++++++-----
+ 4 files changed, 47 insertions(+), 10 deletions(-)
+
+--- a/devtools/import.py
++++ b/devtools/import.py
+@@ -107,6 +107,9 @@ def _update_pysam_files(cf, destdir):
+                     "bam_md.c": (
+                         'sam_open_format("-", mode_w',
+                         'sam_open_format({}_stdout_fn, mode_w'.format(basename)),
++                    "faidx.c": (
++                        'bgzf_open("-", "wu"',
++                        'bgzf_open({}_stdout_fn, "wu"'.format(basename)),
+                     "phase.c": (
+                         'putc("ACGT"[f->seq[j] == 1? (c&3, {}_stdout) : (c>>16&3)]);'.format(basename),
+                         'putc("ACGT"[f->seq[j] == 1? (c&3) : (c>>16&3)], {}_stdout);'.format(basename)),
+--- a/pysam/libcutils.pyx
++++ b/pysam/libcutils.pyx
+@@ -361,6 +361,7 @@ def _pysam_dispatch(collection,
+             "mpileup": "-o {}",
+             "depad": "-o {}",
+             "calmd": "",  # uses pysam_stdout_fn
++            "faidx": "",  # uses pysam_stdout_fn
+         },
+             "bcftools": {}
+         }
+@@ -372,14 +373,12 @@ def _pysam_dispatch(collection,
+             if method not in ("head", "index", "roh", "stats"):
+                 stdout_option = "-o {}"
+         elif method in MAP_STDOUT_OPTIONS[collection]:
+-            # special case - samtools view -c outputs on stdout
+-            if not(method == "view" and "-c" in args):
+-                stdout_option = MAP_STDOUT_OPTIONS[collection][method]
++            stdout_option = MAP_STDOUT_OPTIONS[collection][method]
+ 
+         if stdout_option is not None and not is_usage:
+             os.close(stdout_h)
+             stdout_f_bytes = force_bytes(stdout_f)
+-            args.extend(stdout_option.format(stdout_f).split(" "))
++            args[0:0] = stdout_option.format(stdout_f).split(maxsplit=1)
+             stdout_h = c_open(b"/dev/null", O_WRONLY)
+         else:
+             stdout_f_bytes = None
+--- a/samtools/faidx.c.pysam.c
++++ b/samtools/faidx.c.pysam.c
+@@ -613,7 +613,7 @@ int faidx_core(int argc, char *argv[], e
+             }
+         }
+     } else {
+-        if (!(out.bgzf_fp = bgzf_open("-", "wu"))) {
++        if (!(out.bgzf_fp = bgzf_open(samtools_stdout_fn, "wu"))) {
+             fprintf(samtools_stderr,"[faidx] Cannot open output for writing :%s.\n", strerror(errno) );
+             goto exit2;
+         }
+--- a/tests/samtools_test.py
++++ b/tests/samtools_test.py
+@@ -1,4 +1,5 @@
+ import warnings
++import json
+ import os
+ import re
+ import glob
+@@ -299,11 +300,6 @@ class TestReturnType:
+ class TestStdout:
+     '''test if stdout can be redirected.'''
+ 
+-    def testWithRedirectedStdout(self):
+-        r = pysam.samtools.flagstat(
+-            os.path.join(BAM_DATADIR, "ex1.bam"))
+-        assert len(r) > 0
+-
+     def testWithoutRedirectedStdout(self):
+         r = pysam.samtools.flagstat(
+             os.path.join(BAM_DATADIR, "ex1.bam"),
+@@ -329,6 +325,45 @@ class TestStdout:
+         assert len(r) > 0
+ 
+ 
++class TestSamtoolsCapture:
++    """Test that each subcommand's standard output is correctly captured.
++    """
++    @pytest.mark.skipif(pysam.__version__ < "0.24.0", reason="calmd redirection broken")
++    def test_calmd(self):
++        text = pysam.samtools.calmd("-Q", os.path.join(BAM_DATADIR, "ex1.bam"), os.path.join(BAM_DATADIR, "ex1.fa"))
++        assert "EAS56_57:6:190:289:82" in text
++
++    def test_depad(self):
++        text = pysam.samtools.depad("-s", "-T", os.path.join(BAM_DATADIR, "ex1.fa"), os.path.join(BAM_DATADIR, "ex5.bam"))
++        assert "read_28833_29006_6945" in text
++
++    def test_faidx(self):
++        seq = pysam.samtools.faidx(os.path.join(BAM_DATADIR, "ex1.fa"), "chr2:101-120")
++        assert seq == ">chr2:101-120\nGAACTTGAAGACAAGTCTCT\n"
++
++    def test_flags(self):
++        text = pysam.samtools.flags("16")
++        assert text == "0x10\t16\tREVERSE\n"
++
++    def test_flagstat(self):
++        stats = json.loads(pysam.samtools.flagstat("-Ojson", os.path.join(BAM_DATADIR, "ex1.bam")))
++        assert stats["QC-passed reads"]["properly paired"] == 3124
++
++    def test_mpileup(self):
++        text = pysam.samtools.mpileup("--region", "chr2:500-501", os.path.join(BAM_DATADIR, "ex1.bam"))
++        assert "chr2\t500\tN\t44" in text
++
++    def test_view(self):
++        line = pysam.samtools.view(os.path.join(BAM_DATADIR, "ex1.bam"), "chr2:500-501", split_lines=True)
++        assert len(line) == 47
++        assert "EAS1_93:7:319:280:57" in line[0]
++        assert "EAS192_3:6:45:183:25" in line[-1]
++
++    def test_view_count(self):
++        line = pysam.samtools.view("--count", os.path.join(BAM_DATADIR, "ex1.bam"), "chr2:500-501")
++        assert line.rstrip() == "47"
++
++
+ class TestPysam(TestSamtools):
+     """check access to samtools command in the pysam
+     main package.


=====================================
debian/patches/series
=====================================
@@ -1,2 +1,4 @@
 # skip_test_remote.patch
 clean_less
+6261249228.patch
+skip-32bit-tests.patch


=====================================
debian/patches/skip-32bit-tests.patch
=====================================
@@ -0,0 +1,67 @@
+Description: Skip tests that are invalid or crash on 32-bit platforms
+ TestBinaryCompatibility compares hard-coded object sizes of the 64-bit
+ layouts, and testIteratorOutOfScope deliberately accesses a PileupColumn
+ after its iterator is gone, which is a use-after-free in pysam that
+ typically crashes rather than raising on 32-bit platforms (see
+ https://github.com/pysam-developers/pysam/issues/1151).
+ Upstream guards these with platform.machine(), but that reports the
+ kernel architecture: on i386 builds on a 64-bit kernel it still returns
+ x86_64, so the guard does not take effect and both test sets fail or
+ crash, breaking the build on i386, armhf and any other 32-bit arch.
+ Key both guards on the pointer size of the interpreter running the
+ tests instead.
+Author: Andreas Tille <tille at debian.org>
+Bug-Debian: https://bugs.debian.org/1152973
+Forwarded: https://github.com/pysam-developers/pysam/issues/1151
+Assisted-By: scaleway/deepseek-v4-flash-0731
+
+diff --git a/tests/AlignmentFilePileup_test.py b/tests/AlignmentFilePileup_test.py
+index 0969231..64a13fc 100644
+--- a/tests/AlignmentFilePileup_test.py
++++ b/tests/AlignmentFilePileup_test.py
+@@ -1,6 +1,7 @@
+ """Benchmarking module for AlignmentFile functionality"""
+ import os
+ import pysam
++import struct
+ import sys
+ import pytest
+ 
+@@ -174,8 +175,10 @@ class TestPileupObjects:
+     def teardown_method(self):
+         self.samfile.close()
+ 
+-    @pytest.mark.skipif(sys.version_info[:2] == (3, 11) or sys.platform.startswith("netbsd"),
+-                        reason="exercises invalid accesses, which crashes on Python 3.11 and NetBSD")
++    @pytest.mark.skipif(struct.calcsize("P") < 8
++                        or sys.version_info[:2] == (3, 11)
++                        or sys.platform.startswith("netbsd"),
++                        reason="exercises invalid accesses, which crashes on 32-bit platforms, Python 3.11 and NetBSD")
+     def testIteratorOutOfScope(self):
+         '''test if exception is raised if pileup col is accessed after
+         iterator is exhausted.'''
+diff --git a/tests/compile_test.py b/tests/compile_test.py
+index fbb8760..ddd0873 100644
+--- a/tests/compile_test.py
++++ b/tests/compile_test.py
+@@ -8,7 +8,7 @@ pysam and tabix works.
+ 
+ # clean up previous compilation
+ import os
+-import platform
++import struct
+ import pytest
+ import pysam
+ from TestUtils import make_data_files, BAM_DATADIR, CBCF_DATADIR, TABIX_DATADIR
+@@ -54,7 +54,10 @@ def test_gtf():
+     assert nread == 237
+ 
+ 
+- at pytest.mark.skipif(platform.machine() not in ('aarch64', 'arm64', 'AMD64', 'x86_64'), reason="different scalar sizes")
++# Scalar sizes differ on 32-bit platforms.  platform.machine() reports the
++# kernel architecture, which is still x86_64 in a 32-bit i386 chroot, so
++# check the pointer size of the interpreter actually running the test.
++ at pytest.mark.skipif(struct.calcsize("P") < 8, reason="different scalar sizes")
+ class TestBinaryCompatibility:
+     def test_alignments(self):
+         fp = pysam.AlignmentFile(os.path.join(BAM_DATADIR, "ex1.bam"))



View it on GitLab: https://salsa.debian.org/med-team/python-pysam/-/compare/41448af40b84143b5a890fd7e60d8c74bcfda81b...d30d427b7b1f085302df0bfb601031400a7a17d8

-- 
View it on GitLab: https://salsa.debian.org/med-team/python-pysam/-/compare/41448af40b84143b5a890fd7e60d8c74bcfda81b...d30d427b7b1f085302df0bfb601031400a7a17d8
You're receiving this email because of your account on salsa.debian.org. Manage all notifications: https://salsa.debian.org/-/profile/notifications | Help: https://salsa.debian.org/help


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://alioth-lists.debian.net/pipermail/debian-med-commit/attachments/20261104/6a6b7780/attachment-0001.htm>


More information about the debian-med-commit mailing list