From bb74aec2b6aa1179868d83134013450c9ff9d4d6 Mon Sep 17 00:00:00 2001 From: Lonami Date: Thu, 5 Oct 2023 16:03:32 +0200 Subject: [PATCH] Fix a crash in the coverage extension when module contents are not found (#11702) Signed-off-by: Stephen Finucane Co-authored-by: Stephen Finucane Authored-by: Stephen Finucane --- CHANGES.rst | 2 ++ sphinx/ext/coverage.py | 14 +++++++++----- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/CHANGES.rst b/CHANGES.rst index a227aec4f26..7f80c70f20c 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -41,6 +41,8 @@ Bugs fixed Patch by Bénédikt Tran. * #11697: HTML Search: add 'noindex' meta robots tag. Patch by James Addison. +* #11678: Fix a possible ``ZeroDivisionError`` in ``sphinx.ext.coverage``. + Patch by Stephen Finucane. Testing ------- diff --git a/sphinx/ext/coverage.py b/sphinx/ext/coverage.py index e3d9745e397..02096afbc29 100644 --- a/sphinx/ext/coverage.py +++ b/sphinx/ext/coverage.py @@ -290,11 +290,15 @@ def _write_py_statistics(self, op: TextIO) -> None: value = 100.0 table.append([module, '%.2f%%' % value, '%d' % len(self.py_undocumented[module])]) - table.append([ - 'TOTAL', - f'{100 * len(all_documented_objects) / len(all_objects):.2f}%', - f'{len(all_objects) - len(all_documented_objects)}', - ]) + + if all_objects: + table.append([ + 'TOTAL', + f'{100 * len(all_documented_objects) / len(all_objects):.2f}%', + f'{len(all_objects) - len(all_documented_objects)}', + ]) + else: + table.append(['TOTAL', '100', '0']) for line in _write_table(table): op.write(f'{line}\n')