Skip to content

fix(phylo): decode Gblocks stdout before regex match in run_gblocks - #862

Merged
tanghaibao merged 1 commit into
tanghaibao:mainfrom
wyim-pgl:fix/run-gblocks-bytes-regex
Sep 19, 2026
Merged

tanghaibao merged 1 commit into
tanghaibao:mainfrom
wyim-pgl:fix/run-gblocks-bytes-regex

Conversation

@wyim-pgl

Copy link
Copy Markdown
Contributor

Problem

python -m jcvi.apps.phylo build crashes on Python 3 when Gblocks is enabled:

Traceback (most recent call last):
  File "jcvi/apps/phylo.py", line 921, in build
    gb_fasta = run_gblocks(mrtrans_fasta)
  File "jcvi/apps/phylo.py", line 216, in run_gblocks
    alignp = re.sub(
TypeError: cannot use a string pattern on a bytes-like object

Root cause

run_gblocks() applies a str regex to r, but r comes from AbstractCommandline.run() (in jcvi/compara/ks.py), which returns Popen(...).communicate(). Since jcvi.apps.base.Popen does not set text=True/universal_newlines, communicate() returns bytes on Python 3 — a leftover from the Python 2 port. This is independent of the Gblocks version.

Fix

Decode the captured stdout defensively before matching:

r = r.decode("utf-8", errors="replace") if isinstance(r, bytes) else r

Verification

  • python -m py_compile src/jcvi/apps/phylo.py passes
  • Simulated Gblocks 0.91b stdout (Gblocks alignment: 1234 positions (57 %)) as bytes now parses to alignp = 57; the str passthrough branch also works.

Note: the same bytes/str pattern exists in jcvi/compara/ks.py find_synonymous() (e.read().find(...) on a communicate() result), which will need a separate fix.

AbstractCommandline.run() returns raw bytes from Popen.communicate() on
Python 3 (jcvi.apps.base.Popen does not set text=True), so the str
pattern in re.sub() raises:

    TypeError: cannot use a string pattern on a bytes-like object

Decode the captured stdout defensively before matching so
'python -m jcvi.apps.phylo build' works with Gblocks on Python 3.
@tanghaibao
tanghaibao merged commit a44fa61 into tanghaibao:main Sep 19, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants