From 08a790afc4d5509adc6a14624274ee1e17a4c051 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=A7=E5=8D=ABLYP?= <2431255020@qq.com> Date: Mon, 16 Nov 2026 18:43:52 +0800 Subject: [PATCH 1/2] =?UTF-8?q?fix:=20PNG=20=E8=A3=81=E5=89=AA=E5=A4=B1?= =?UTF-8?q?=E8=B4=A5=E6=97=B6=E5=9B=9E=E9=80=80=E5=88=B0=E6=9C=AA=E8=A3=81?= =?UTF-8?q?=E5=89=AA=E5=9B=BE=E7=89=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 将 PNG 转换从 check_call 改为可检查返回值的 call;转换失败或裁剪器不可用时删除不完整输出,并走已有的未裁剪图片回退逻辑。 --- docs/src/python/plot_utils.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/docs/src/python/plot_utils.py b/docs/src/python/plot_utils.py index 2f18c794..89381617 100644 --- a/docs/src/python/plot_utils.py +++ b/docs/src/python/plot_utils.py @@ -106,7 +106,16 @@ def save_fig(fig, file_name, fmt=None, dpi=300, tight=True): subprocess.call('epstool --bbox --copy %s %s' % (tmp_name, file_name), shell=True) elif fmt == 'png': - subprocess.check_call(['convert', tmp_name, '-trim', file_name]) + try: + result = subprocess.call(['convert', tmp_name, '-trim', file_name]) + except OSError as exc: + logging.getLogger(__name__).warning( + "Could not run PNG trimmer: %s", exc + ) + result = 1 + # Do not let a partial conversion look like a successful result. + if result != 0 and os.path.isfile(file_name): + os.remove(file_name) elif fmt == 'pdf': subprocess.call('pdfcrop %s %s' % (tmp_name, file_name), shell=True) From 7a8c0ad514bc1118423c6b676c488e28a67378cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=A7=E5=8D=ABLYP?= <2431255020@qq.com> Date: Mon, 16 Nov 2026 18:46:59 +0800 Subject: [PATCH 2/2] =?UTF-8?q?test:=20=E6=9B=B4=E6=96=B0=20PNG=20?= =?UTF-8?q?=E8=A3=81=E5=89=AA=E5=A4=B1=E8=B4=A5=E5=9B=9E=E5=BD=92=E6=B5=8B?= =?UTF-8?q?=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 测试新的回退行为:裁剪命令返回非零时保存未裁剪图片,而不是抛出异常。 --- tests/test_plot_utils.py | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/tests/test_plot_utils.py b/tests/test_plot_utils.py index c9439ff3..8fcf147d 100644 --- a/tests/test_plot_utils.py +++ b/tests/test_plot_utils.py @@ -1,7 +1,6 @@ """Regression tests for the figure-saving helper.""" import importlib.util -import subprocess import sys from pathlib import Path from tempfile import TemporaryDirectory @@ -33,22 +32,28 @@ class SaveFigTests(TestCase): def setUpClass(cls): cls.plot_utils = load_plot_utils() - def test_png_path_with_spaces_is_preserved_and_failures_are_raised(self): + def test_png_path_with_spaces_is_preserved_when_trimmer_fails(self): figure = Mock() + figure_data = b"saved figure" + + def write_temporary_figure(path, **kwargs): + Path(path).write_bytes(figure_data) + + figure.savefig.side_effect = write_temporary_figure + with TemporaryDirectory() as temporary: output = Path(temporary) / "target with space.png" - failure = subprocess.CalledProcessError(1, "convert") with patch.object( self.plot_utils.subprocess, - "check_call", - side_effect=failure, - ) as check_call: - with self.assertRaises(subprocess.CalledProcessError): - self.plot_utils.save_fig(figure, str(output)) + "call", + return_value=1, + ) as call: + self.plot_utils.save_fig(figure, str(output)) - check_call.assert_called_once_with([ + call.assert_called_once_with([ "convert", ANY, "-trim", str(output.resolve()) ]) + self.assertEqual(output.read_bytes(), figure_data) if __name__ == "__main__":