Remove unneeded content from within `with` statements

This commit is contained in:
Adam Turner 2025-01-04 06:36:34 +00:00
parent f63784310c
commit e17ed74fe0
10 changed files with 56 additions and 52 deletions

View File

@ -137,15 +137,16 @@ class ChangesBuilder(Builder):
__('could not read %r for changelog creation'), docname
)
continue
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))
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:
f.write(rendered)
themectx = {
'theme_' + key: val for (key, val) in self.theme.get_options({}).items()
}

View File

@ -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':

View File

@ -403,8 +403,9 @@ class SphinxFileOutput(FileOutput):
and os.path.exists(self.destination_path)
):
with open(self.destination_path, encoding=self.encoding) as f:
on_disk = f.read()
# skip writing: content not changed
if f.read() == data:
if on_disk == data:
return data
return super().write(data)

View File

@ -84,7 +84,8 @@ def run_extract() -> None:
log = _get_logger()
with open('sphinx/__init__.py', encoding='utf-8') as f:
for line in f.read().splitlines():
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]