From e17ed74fe027eb84aaf72ce92c4b1bd8ebf8c049 Mon Sep 17 00:00:00 2001 From: Adam Turner <9087854+aa-turner@users.noreply.github.com> Date: Sat, 4 Jan 2025 06:36:34 +0000 Subject: [PATCH] Remove unneeded content from within ``with`` statements --- sphinx/builders/changes.py | 13 +++---- sphinx/builders/gettext.py | 16 ++++----- sphinx/directives/code.py | 6 ++-- sphinx/ext/autosummary/generate.py | 2 +- sphinx/ext/imgmath.py | 3 +- sphinx/util/docutils.py | 7 ++-- sphinx/util/osutil.py | 4 +-- tests/test_builders/test_build_latex.py | 2 +- tests/test_extensions/test_ext_apidoc.py | 44 ++++++++++++------------ utils/babel_runner.py | 11 +++--- 10 files changed, 56 insertions(+), 52 deletions(-) diff --git a/sphinx/builders/changes.py b/sphinx/builders/changes.py index 49cab2c7a8c..62eb1177c48 100644 --- a/sphinx/builders/changes.py +++ b/sphinx/builders/changes.py @@ -137,15 +137,16 @@ def hl(no: int, line: str) -> str: __('could not read %r for changelog creation'), docname ) continue + text = ''.join(hl(i + 1, line) for (i, line) in enumerate(lines)) + ctx = { + 'filename': str(self.env.doc2path(docname, False)), + 'text': text, + } + rendered = self.templates.render('changes/rstsource.html', ctx) targetfn = os.path.join(self.outdir, 'rst', os_path(docname)) + '.html' ensuredir(os.path.dirname(targetfn)) with open(targetfn, 'w', encoding='utf-8') as f: - text = ''.join(hl(i + 1, line) for (i, line) in enumerate(lines)) - ctx = { - 'filename': str(self.env.doc2path(docname, False)), - 'text': text, - } - f.write(self.templates.render('changes/rstsource.html', ctx)) + f.write(rendered) themectx = { 'theme_' + key: val for (key, val) in self.theme.get_options({}).items() } diff --git a/sphinx/builders/gettext.py b/sphinx/builders/gettext.py index 5274bc77191..c6ac25ed085 100644 --- a/sphinx/builders/gettext.py +++ b/sphinx/builders/gettext.py @@ -198,14 +198,14 @@ def should_write(filepath: str, new_content: str) -> bool: try: with open(filepath, encoding='utf-8') as oldpot: old_content = oldpot.read() - old_header_index = old_content.index('"POT-Creation-Date:') - new_header_index = new_content.index('"POT-Creation-Date:') - old_body_index = old_content.index('"PO-Revision-Date:') - new_body_index = new_content.index('"PO-Revision-Date:') - return ( - old_content[:old_header_index] != new_content[:new_header_index] - or new_content[new_body_index:] != old_content[old_body_index:] - ) + old_header_index = old_content.index('"POT-Creation-Date:') + new_header_index = new_content.index('"POT-Creation-Date:') + old_body_index = old_content.index('"PO-Revision-Date:') + new_body_index = new_content.index('"PO-Revision-Date:') + return ( + old_content[:old_header_index] != new_content[:new_header_index] + or new_content[new_body_index:] != old_content[old_body_index:] + ) except ValueError: pass diff --git a/sphinx/directives/code.py b/sphinx/directives/code.py index 00925debe7d..198b6f006a4 100644 --- a/sphinx/directives/code.py +++ b/sphinx/directives/code.py @@ -224,10 +224,10 @@ def read_file( try: with open(filename, encoding=self.encoding, errors='strict') as f: text = f.read() - if 'tab-width' in self.options: - text = text.expandtabs(self.options['tab-width']) + if 'tab-width' in self.options: + text = text.expandtabs(self.options['tab-width']) - return text.splitlines(True) + return text.splitlines(True) except OSError as exc: msg = __("Include file '%s' not found or reading it failed") % filename raise OSError(msg) from exc diff --git a/sphinx/ext/autosummary/generate.py b/sphinx/ext/autosummary/generate.py index 64dea102b45..4086955d021 100644 --- a/sphinx/ext/autosummary/generate.py +++ b/sphinx/ext/autosummary/generate.py @@ -642,7 +642,7 @@ def find_autosummary_in_files( for filename in filenames: with open(filename, encoding='utf-8', errors='ignore') as f: lines = f.read().splitlines() - documented.extend(find_autosummary_in_lines(lines, filename=filename)) + documented.extend(find_autosummary_in_lines(lines, filename=filename)) return documented diff --git a/sphinx/ext/imgmath.py b/sphinx/ext/imgmath.py index 658d7859e42..d96f852a2f5 100644 --- a/sphinx/ext/imgmath.py +++ b/sphinx/ext/imgmath.py @@ -304,7 +304,8 @@ def render_math( def render_maths_to_base64(image_format: str, generated_path: str) -> str: with open(generated_path, 'rb') as f: - encoded = base64.b64encode(f.read()).decode(encoding='utf-8') + content = f.read() + encoded = base64.b64encode(content).decode(encoding='utf-8') if image_format == 'png': return f'data:image/png;base64,{encoded}' if image_format == 'svg': diff --git a/sphinx/util/docutils.py b/sphinx/util/docutils.py index ba1041d67ea..faa0695c9fe 100644 --- a/sphinx/util/docutils.py +++ b/sphinx/util/docutils.py @@ -403,9 +403,10 @@ def write(self, data: str) -> str: and os.path.exists(self.destination_path) ): with open(self.destination_path, encoding=self.encoding) as f: - # skip writing: content not changed - if f.read() == data: - return data + on_disk = f.read() + # skip writing: content not changed + if on_disk == data: + return data return super().write(data) diff --git a/sphinx/util/osutil.py b/sphinx/util/osutil.py index c3f5aaa139a..d24bbf55b2d 100644 --- a/sphinx/util/osutil.py +++ b/sphinx/util/osutil.py @@ -227,8 +227,8 @@ def close(self) -> None: try: with open(self._path, encoding='utf-8') as old_f: old_content = old_f.read() - if old_content == buf: - return + if old_content == buf: + return except OSError: pass diff --git a/tests/test_builders/test_build_latex.py b/tests/test_builders/test_build_latex.py index fd588600a77..3e63aca7078 100644 --- a/tests/test_builders/test_build_latex.py +++ b/tests/test_builders/test_build_latex.py @@ -100,7 +100,7 @@ def do_GET(self): if self.path == '/sphinx.png': with open('tests/roots/test-local-logo/images/img.png', 'rb') as f: content = f.read() - content_type = 'image/png' + content_type = 'image/png' if content: self.send_response(200, 'OK') diff --git a/tests/test_extensions/test_ext_apidoc.py b/tests/test_extensions/test_ext_apidoc.py index 1e191de9a7b..6cec02562bb 100644 --- a/tests/test_extensions/test_ext_apidoc.py +++ b/tests/test_extensions/test_ext_apidoc.py @@ -86,12 +86,12 @@ def test_custom_templates(make_app, apidoc): # Assert that the legacy filename is discovered with open(builddir / 'mypackage.txt', encoding='utf-8') as f: txt = f.read() - assert 'The legacy package template was found!' in txt + assert 'The legacy package template was found!' in txt # Assert that the new filename is preferred with open(builddir / 'mypackage.mymodule.txt', encoding='utf-8') as f: txt = f.read() - assert 'The Jinja module template was found!' in txt + assert 'The Jinja module template was found!' in txt @pytest.mark.apidoc( @@ -107,17 +107,17 @@ def test_pep_0420_enabled(make_app, apidoc): with open(outdir / 'a.b.c.rst', encoding='utf-8') as f: rst = f.read() - assert 'automodule:: a.b.c.d\n' in rst - assert 'automodule:: a.b.c\n' in rst + assert 'automodule:: a.b.c.d\n' in rst + assert 'automodule:: a.b.c\n' in rst with open(outdir / 'a.b.e.rst', encoding='utf-8') as f: rst = f.read() - assert 'automodule:: a.b.e.f\n' in rst + assert 'automodule:: a.b.e.f\n' in rst with open(outdir / 'a.b.x.rst', encoding='utf-8') as f: rst = f.read() - assert 'automodule:: a.b.x.y\n' in rst - assert 'automodule:: a.b.x\n' not in rst + assert 'automodule:: a.b.x.y\n' in rst + assert 'automodule:: a.b.x\n' not in rst app = make_app('text', srcdir=outdir) app.build() @@ -131,15 +131,15 @@ def test_pep_0420_enabled(make_app, apidoc): with open(builddir / 'a.b.c.txt', encoding='utf-8') as f: txt = f.read() - assert 'a.b.c package\n' in txt + assert 'a.b.c package\n' in txt with open(builddir / 'a.b.e.txt', encoding='utf-8') as f: txt = f.read() - assert 'a.b.e.f module\n' in txt + assert 'a.b.e.f module\n' in txt with open(builddir / 'a.b.x.txt', encoding='utf-8') as f: txt = f.read() - assert 'a.b.x namespace\n' in txt + assert 'a.b.x namespace\n' in txt @pytest.mark.apidoc( @@ -157,15 +157,15 @@ def test_pep_0420_enabled_separate(make_app, apidoc): with open(outdir / 'a.b.c.rst', encoding='utf-8') as f: rst = f.read() - assert '.. toctree::\n :maxdepth: 4\n\n a.b.c.d\n' in rst + assert '.. toctree::\n :maxdepth: 4\n\n a.b.c.d\n' in rst with open(outdir / 'a.b.e.rst', encoding='utf-8') as f: rst = f.read() - assert '.. toctree::\n :maxdepth: 4\n\n a.b.e.f\n' in rst + assert '.. toctree::\n :maxdepth: 4\n\n a.b.e.f\n' in rst with open(outdir / 'a.b.x.rst', encoding='utf-8') as f: rst = f.read() - assert '.. toctree::\n :maxdepth: 4\n\n a.b.x.y\n' in rst + assert '.. toctree::\n :maxdepth: 4\n\n a.b.x.y\n' in rst app = make_app('text', srcdir=outdir) app.build() @@ -181,15 +181,15 @@ def test_pep_0420_enabled_separate(make_app, apidoc): with open(builddir / 'a.b.c.txt', encoding='utf-8') as f: txt = f.read() - assert 'a.b.c package\n' in txt + assert 'a.b.c package\n' in txt with open(builddir / 'a.b.e.f.txt', encoding='utf-8') as f: txt = f.read() - assert 'a.b.e.f module\n' in txt + assert 'a.b.e.f module\n' in txt with open(builddir / 'a.b.x.txt', encoding='utf-8') as f: txt = f.read() - assert 'a.b.x namespace\n' in txt + assert 'a.b.x namespace\n' in txt @pytest.mark.apidoc(coderoot='test-apidoc-pep420/a') @@ -214,9 +214,9 @@ def test_pep_0420_disabled_top_level_verify(make_app, apidoc): with open(outdir / 'c.rst', encoding='utf-8') as f: rst = f.read() - assert 'c package\n' in rst - assert 'automodule:: c.d\n' in rst - assert 'automodule:: c\n' in rst + assert 'c package\n' in rst + assert 'automodule:: c.d\n' in rst + assert 'automodule:: c\n' in rst app = make_app('text', srcdir=outdir) app.build() @@ -238,8 +238,8 @@ def test_trailing_underscore(make_app, apidoc): builddir = outdir / '_build' / 'text' with open(builddir / 'package_.txt', encoding='utf-8') as f: rst = f.read() - assert 'package_ package\n' in rst - assert 'package_.module_ module\n' in rst + assert 'package_ package\n' in rst + assert 'package_.module_ module\n' in rst @pytest.mark.apidoc( @@ -349,7 +349,7 @@ def test_extension_parsed(apidoc): with open(outdir / 'conf.py', encoding='utf-8') as f: rst = f.read() - assert 'sphinx.ext.mathjax' in rst + assert 'sphinx.ext.mathjax' in rst @pytest.mark.apidoc( diff --git a/utils/babel_runner.py b/utils/babel_runner.py index c3a2d030a35..3bb42efc308 100644 --- a/utils/babel_runner.py +++ b/utils/babel_runner.py @@ -84,11 +84,12 @@ def run_extract() -> None: log = _get_logger() with open('sphinx/__init__.py', encoding='utf-8') as f: - for line in f.read().splitlines(): - if line.startswith('__version__ = '): - # remove prefix; strip whitespace; remove quotation marks - sphinx_version = line[14:].strip()[1:-1] - break + lines = f.readlines() + for line in lines: + if line.startswith('__version__ = '): + # remove prefix; strip whitespace; remove quotation marks + sphinx_version = line[14:].strip()[1:-1] + break catalogue = Catalog(project='Sphinx', version=sphinx_version, charset='utf-8')