Prefer builtin open() over io.open() and codecs.open()

In Python3, the functions io.open() is an alias of the builtin open()
and codecs.open() is functionally equivalent. To reduce indirection,
number of imports, and number of patterns, always prefer the builtin.

https://docs.python.org/3/library/io.html#high-level-module-interface

> io.open()
>
> This is an alias for the builtin open() function.
This commit is contained in:
Jon Dufresne 2018-09-11 04:57:20 -07:00
parent 844a3a5c22
commit 02fea029bf
18 changed files with 43 additions and 53 deletions

View File

@ -10,7 +10,6 @@
"""
from __future__ import print_function
import codecs
import pipes
import plistlib
import shlex
@ -193,7 +192,7 @@ class AppleHelpBuilder(StandaloneHTMLBuilder):
# Build the access page
logger.info(bold(__('building access page...')), nonl=True)
with codecs.open(path.join(language_dir, '_access.html'), 'w') as f: # type: ignore
with open(path.join(language_dir, '_access.html'), 'w') as f:
f.write(access_page_template % {
'toc': htmlescape(toc, quote=True),
'title': htmlescape(self.config.applehelp_title)

View File

@ -9,7 +9,6 @@
:license: BSD, see LICENSE for details.
"""
import codecs
from os import path
from typing import cast
@ -115,9 +114,11 @@ class ChangesBuilder(Builder):
'show_copyright': self.config.html_show_copyright,
'show_sphinx': self.config.html_show_sphinx,
}
with codecs.open(path.join(self.outdir, 'index.html'), 'w', 'utf8') as f: # type: ignore # NOQA
with open(path.join(self.outdir, 'index.html'), 'w', # type: ignore
encoding='utf8') as f:
f.write(self.templates.render('changes/frameset.html', ctx))
with codecs.open(path.join(self.outdir, 'changes.html'), 'w', 'utf8') as f: # type: ignore # NOQA
with open(path.join(self.outdir, 'changes.html'), 'w', # type: ignore
encoding='utf8') as f:
f.write(self.templates.render('changes/versionchanges.html', ctx))
hltext = ['.. versionadded:: %s' % version,
@ -135,8 +136,8 @@ class ChangesBuilder(Builder):
logger.info(bold(__('copying source files...')))
for docname in self.env.all_docs:
with codecs.open(self.env.doc2path(docname), 'r', # type: ignore
self.env.config.source_encoding) as f:
with open(self.env.doc2path(docname), 'r', # type: ignore
encoding=self.env.config.source_encoding) as f:
try:
lines = f.readlines()
except UnicodeDecodeError:
@ -144,7 +145,7 @@ class ChangesBuilder(Builder):
continue
targetfn = path.join(self.outdir, 'rst', os_path(docname)) + '.html'
ensuredir(path.dirname(targetfn))
with codecs.open(targetfn, 'w', 'utf-8') as f: # type: ignore
with open(targetfn, 'w', encoding='utf-8') as f: # type: ignore
text = ''.join(hl(i + 1, line) for (i, line) in enumerate(lines))
ctx = {
'filename': self.env.doc2path(docname, None),

View File

@ -9,7 +9,6 @@
:license: BSD, see LICENSE for details.
"""
import codecs
import posixpath
import re
import sys
@ -959,9 +958,9 @@ class StandaloneHTMLBuilder(Builder):
try:
searchindexfn = path.join(self.outdir, self.searchindex_filename)
if self.indexer_dumps_unicode:
f = codecs.open(searchindexfn, 'r', encoding='utf-8') # type: ignore
f = open(searchindexfn, 'r', encoding='utf-8') # type: ignore
else:
f = open(searchindexfn, 'rb') # type: ignore
f = open(searchindexfn, 'rb')
with f:
self.indexer.load(f, self.indexer_format)
except (IOError, OSError, ValueError):
@ -1140,7 +1139,8 @@ class StandaloneHTMLBuilder(Builder):
# outfilename's path is in general different from self.outdir
ensuredir(path.dirname(outfilename))
try:
with codecs.open(outfilename, 'w', ctx['encoding'], 'xmlcharrefreplace') as f: # type: ignore # NOQA
with open(outfilename, 'w', # type: ignore
encoding=ctx['encoding'], errors='xmlcharrefreplace') as f:
f.write(output)
except (IOError, OSError) as err:
logger.warning(__("error writing file %s: %s"), outfilename, err)
@ -1177,9 +1177,9 @@ class StandaloneHTMLBuilder(Builder):
# first write to a temporary file, so that if dumping fails,
# the existing index won't be overwritten
if self.indexer_dumps_unicode:
f = codecs.open(searchindexfn + '.tmp', 'w', encoding='utf-8') # type: ignore
f = open(searchindexfn + '.tmp', 'w', encoding='utf-8') # type: ignore
else:
f = open(searchindexfn + '.tmp', 'wb') # type: ignore
f = open(searchindexfn + '.tmp', 'wb')
with f:
self.indexer.dump(f, self.indexer_format)
movefile(searchindexfn + '.tmp', searchindexfn)
@ -1436,9 +1436,9 @@ class SerializingHTMLBuilder(StandaloneHTMLBuilder):
def dump_context(self, context, filename):
# type: (Dict, unicode) -> None
if self.implementation_dumps_unicode:
f = codecs.open(filename, 'w', encoding='utf-8') # type: ignore
f = open(filename, 'w', encoding='utf-8') # type: ignore
else:
f = open(filename, 'wb') # type: ignore
f = open(filename, 'wb')
with f:
self.implementation.dump(context, f, *self.additional_dump_args)

View File

@ -11,7 +11,6 @@
"""
from __future__ import print_function
import codecs
import os
from os import path
@ -208,8 +207,8 @@ class HTMLHelpBuilder(StandaloneHTMLBuilder):
def open_file(self, outdir, basename, mode='w'):
# type: (unicode, unicode, unicode) -> IO
# open a file with the correct encoding for the selected language
return codecs.open(path.join(outdir, basename), mode, # type: ignore
self.encoding, 'xmlcharrefreplace')
return open(path.join(outdir, basename), mode, # type: ignore
encoding=self.encoding, errors='xmlcharrefreplace')
def update_page_context(self, pagename, templatename, ctx, event_arg):
# type: (unicode, unicode, Dict, unicode) -> None

View File

@ -9,7 +9,6 @@
:license: BSD, see LICENSE for details.
"""
import codecs
import re
import socket
import threading
@ -308,7 +307,8 @@ class CheckExternalLinksBuilder(Builder):
def write_entry(self, what, docname, line, uri):
# type: (unicode, unicode, int, unicode) -> None
with codecs.open(path.join(self.outdir, 'output.txt'), 'a', 'utf-8') as output: # type: ignore # NOQA
with open(path.join(self.outdir, 'output.txt'), 'a', # type: ignore
encoding='utf-8') as output:
output.write("%s:%s: [%s] %s\n" % (self.env.doc2path(docname, None),
line, what, uri))

View File

@ -9,7 +9,6 @@
:license: BSD, see LICENSE for details.
"""
import codecs
import os
import posixpath
import re
@ -145,7 +144,8 @@ class QtHelpBuilder(StandaloneHTMLBuilder):
nspace = nspace.lower()
# write the project file
with codecs.open(path.join(outdir, outname + '.qhp'), 'w', 'utf-8') as f: # type: ignore # NOQA
with open(path.join(outdir, outname + '.qhp'), 'w', # type: ignore
encoding='utf-8') as f:
body = render_file('project.qhp', outname=outname,
title=self.config.html_title, version=self.config.version,
project=self.config.project, namespace=nspace,
@ -159,7 +159,8 @@ class QtHelpBuilder(StandaloneHTMLBuilder):
startpage = 'qthelp://' + posixpath.join(nspace, 'doc', 'index.html')
logger.info(__('writing collection project file...'))
with codecs.open(path.join(outdir, outname + '.qhcp'), 'w', 'utf-8') as f: # type: ignore # NOQA
with open(path.join(outdir, outname + '.qhcp'), 'w', # type: ignore
encoding='utf-8') as f:
body = render_file('project.qhcp', outname=outname,
title=self.config.html_short_title,
homepage=homepage, startpage=startpage)

View File

@ -9,7 +9,6 @@
:license: BSD, see LICENSE for details.
"""
import codecs
from os import path
from docutils.io import StringOutput
@ -82,7 +81,7 @@ class TextBuilder(Builder):
outfilename = path.join(self.outdir, os_path(docname) + self.out_suffix)
ensuredir(path.dirname(outfilename))
try:
with codecs.open(outfilename, 'w', 'utf-8') as f: # type: ignore
with open(outfilename, 'w', encoding='utf-8') as f: # type: ignore
f.write(self.writer.output)
except (IOError, OSError) as err:
logger.warning(__("error writing file %s: %s"), outfilename, err)

View File

@ -9,7 +9,6 @@
:license: BSD, see LICENSE for details.
"""
import codecs
from os import path
from docutils import nodes
@ -95,7 +94,7 @@ class XMLBuilder(Builder):
outfilename = path.join(self.outdir, os_path(docname) + self.out_suffix)
ensuredir(path.dirname(outfilename))
try:
with codecs.open(outfilename, 'w', 'utf-8') as f: # type: ignore
with open(outfilename, 'w', encoding='utf-8') as f: # type: ignore
f.write(self.writer.output)
except (IOError, OSError) as err:
logger.warning(__("error writing file %s: %s"), outfilename, err)

View File

@ -18,7 +18,6 @@ import re
import sys
import time
from collections import OrderedDict
from io import open
from os import path
# try to import readline, unix specific enhancement
@ -445,7 +444,7 @@ def generate(d, overwrite=True, silent=False, templatedir=None):
if overwrite or not path.isfile(fpath):
if 'quiet' not in d:
print(__('Creating file %s.') % fpath)
with open(fpath, 'wt', encoding='utf-8', newline=newline) as f:
with open(fpath, 'wt', encoding='utf-8', newline=newline) as f: # type: ignore
f.write(content)
else:
if 'quiet' not in d:

View File

@ -7,7 +7,6 @@
:license: BSD, see LICENSE for details.
"""
import codecs
import sys
import warnings
from difflib import unified_diff
@ -213,7 +212,8 @@ class LiteralIncludeReader(object):
def read_file(self, filename, location=None):
# type: (unicode, Any) -> List[unicode]
try:
with codecs.open(filename, 'r', self.encoding, errors='strict') as f: # type: ignore # NOQA
with open(filename, 'r', # type: ignore
encoding=self.encoding, errors='strict') as f:
text = f.read() # type: unicode
if 'tab-width' in self.options:
text = text.expandtabs(self.options['tab-width'])

View File

@ -20,7 +20,6 @@
from __future__ import print_function
import argparse
import codecs
import locale
import os
import pydoc
@ -249,8 +248,8 @@ def find_autosummary_in_files(filenames):
"""
documented = [] # type: List[Tuple[unicode, unicode, unicode]]
for filename in filenames:
with codecs.open(filename, 'r', encoding='utf-8', # type: ignore
errors='ignore') as f:
with open(filename, 'r', encoding='utf-8', # type: ignore
errors='ignore') as f:
lines = f.read().splitlines()
documented.extend(find_autosummary_in_lines(lines, filename=filename))
return documented

View File

@ -318,8 +318,8 @@ class DocTestBuilder(Builder):
date = time.strftime('%Y-%m-%d %H:%M:%S')
self.outfile = None # type: IO
self.outfile = codecs.open(path.join(self.outdir, 'output.txt'), # type: ignore
'w', encoding='utf-8')
self.outfile = open(path.join(self.outdir, 'output.txt'), # type: ignore
'w', encoding='utf-8')
self.outfile.write(('Results of doctest builder run on %s\n'
'==================================%s\n') %
(date, '=' * len(date)))

View File

@ -10,7 +10,6 @@
:license: BSD, see LICENSE for details.
"""
import codecs
import posixpath
import re
from hashlib import sha1
@ -142,7 +141,7 @@ class Graphviz(SphinxDirective):
rel_filename, filename = self.env.relfn2path(argument)
self.env.note_dependency(rel_filename)
try:
with codecs.open(filename, 'r', 'utf-8') as fp: # type: ignore
with open(filename, 'r', encoding='utf-8') as fp: # type: ignore
dotcode = fp.read()
except (IOError, OSError):
return [document.reporter.warning(
@ -309,7 +308,7 @@ def render_dot_html(self, node, code, options, prefix='graphviz',
self.body.append('<p class="warning">%s</p>' % alt)
self.body.append('</object></div>\n')
else:
with codecs.open(outfn + '.map', 'r', encoding='utf-8') as mapfile: # type: ignore
with open(outfn + '.map', 'r', encoding='utf-8') as mapfile: # type: ignore
imgmap = ClickableMapDefinition(outfn + '.map', mapfile.read(), dot=code)
if imgmap.clickable:
# has a map

View File

@ -9,7 +9,6 @@
:license: BSD, see LICENSE for details.
"""
import codecs
import posixpath
import re
import shutil
@ -123,7 +122,7 @@ def compile_math(latex, builder):
"""Compile LaTeX macros for math to DVI."""
tempdir = ensure_tempdir(builder)
filename = path.join(tempdir, 'math.tex')
with codecs.open(filename, 'w', 'utf-8') as f: # type: ignore
with open(filename, 'w', encoding='utf-8') as f: # type: ignore
f.write(latex)
# build latex command; old versions of latex don't have the

View File

@ -9,7 +9,6 @@
import os
import shutil
import sys
from io import open
from six import PY2, text_type
@ -161,7 +160,7 @@ class path(text_type):
"""
if isinstance(text, bytes):
text = text.decode(encoding)
with open(self, 'w', encoding=encoding, **kwargs) as f:
with open(self, 'w', encoding=encoding, **kwargs) as f: # type: ignore
f.write(text)
def text(self, encoding='utf-8', **kwargs):
@ -170,7 +169,7 @@ class path(text_type):
Returns the text in the file.
"""
mode = 'rU' if PY2 else 'r'
with open(self, mode=mode, encoding=encoding, **kwargs) as f:
with open(self, mode=mode, encoding=encoding, **kwargs) as f: # type: ignore
return f.read()
def bytes(self):

View File

@ -10,7 +10,6 @@
"""
from __future__ import absolute_import
import codecs
import os
import re
import types
@ -314,7 +313,7 @@ class SphinxFileOutput(FileOutput):
# type: (unicode) -> unicode
if (self.destination_path and self.autoclose and 'b' not in self.mode and
self.overwrite_if_changed and os.path.exists(self.destination_path)):
with codecs.open(self.destination_path, encoding=self.encoding) as f:
with open(self.destination_path, encoding=self.encoding) as f: # type: ignore
# skip writing: content not changed
if f.read() == data:
return data

View File

@ -10,7 +10,6 @@
"""
from __future__ import absolute_import
import codecs
import os
import posixpath
@ -49,10 +48,10 @@ def copy_asset_file(source, destination, context=None, renderer=None):
from sphinx.util.template import SphinxRenderer
renderer = SphinxRenderer()
with codecs.open(source, 'r', encoding='utf-8') as fsrc: # type: ignore
with open(source, 'r', encoding='utf-8') as fsrc: # type: ignore
if destination.lower().endswith('_t'):
destination = destination[:-2]
with codecs.open(destination, 'w', encoding='utf-8') as fdst: # type: ignore
with open(destination, 'w', encoding='utf-8') as fdst: # type: ignore
fdst.write(renderer.render_string(fsrc.read(), context))
else:
copyfile(source, destination)

View File

@ -9,7 +9,6 @@
:license: BSD, see LICENSE for details.
"""
import gettext
import io
import os
import re
import warnings
@ -69,14 +68,14 @@ class CatalogInfo(LocaleFileInfoBase):
def write_mo(self, locale):
# type: (unicode) -> None
with io.open(self.po_path, 'rt', encoding=self.charset) as file_po:
with open(self.po_path, 'rt', encoding=self.charset) as file_po: # type: ignore
try:
po = read_po(file_po, locale)
except Exception as exc:
logger.warning(__('reading error: %s, %s'), self.po_path, exc)
return
with io.open(self.mo_path, 'wb') as file_mo:
with open(self.mo_path, 'wb') as file_mo:
try:
write_mo(file_mo, po)
except Exception as exc: