From dbba0ae7deded30793c68958d035f52d2e037394 Mon Sep 17 00:00:00 2001 From: Takeshi KOMIYA Date: Tue, 23 Nov 2021 18:35:49 +0900 Subject: [PATCH] Fix #9868: imgmath: Crashed if the dvisvgm command failed to convert equation --- CHANGES | 1 + sphinx/ext/imgmath.py | 10 +++++----- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/CHANGES b/CHANGES index 6ef683b8e7a..db9e2a8f837 100644 --- a/CHANGES +++ b/CHANGES @@ -25,6 +25,7 @@ Bugs fixed having invalid __doc__ atribute * #9872: html: Class namespace collision between autodoc signatures and docutils-0.17 +* #9868: imgmath: Crashed if the dvisvgm command failed to convert equation * #9864: mathjax: Failed to render equations via MathJax v2. The loading method of MathJax is back to "async" method again diff --git a/sphinx/ext/imgmath.py b/sphinx/ext/imgmath.py index a0f4ddcc343..1c22dec921b 100644 --- a/sphinx/ext/imgmath.py +++ b/sphinx/ext/imgmath.py @@ -12,7 +12,6 @@ import re import shutil import subprocess -import sys import tempfile from os import path from subprocess import PIPE, CalledProcessError @@ -43,11 +42,11 @@ class MathExtError(SphinxError): category = 'Math extension error' - def __init__(self, msg: str, stderr: bytes = None, stdout: bytes = None) -> None: + def __init__(self, msg: str, stderr: str = None, stdout: str = None) -> None: if stderr: - msg += '\n[stderr]\n' + stderr.decode(sys.getdefaultencoding(), 'replace') + msg += '\n[stderr]\n' + stderr if stdout: - msg += '\n[stdout]\n' + stdout.decode(sys.getdefaultencoding(), 'replace') + msg += '\n[stdout]\n' + stdout super().__init__(msg) @@ -135,7 +134,8 @@ def compile_math(latex: str, builder: Builder) -> str: command.append('math.tex') try: - subprocess.run(command, stdout=PIPE, stderr=PIPE, cwd=tempdir, check=True) + subprocess.run(command, stdout=PIPE, stderr=PIPE, cwd=tempdir, check=True, + encoding='ascii') return path.join(tempdir, 'math.dvi') except OSError as exc: logger.warning(__('LaTeX command %r cannot be run (needed for math '