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 from __future__ import print_function
import codecs
import pipes import pipes
import plistlib import plistlib
import shlex import shlex
@ -193,7 +192,7 @@ class AppleHelpBuilder(StandaloneHTMLBuilder):
# Build the access page # Build the access page
logger.info(bold(__('building access page...')), nonl=True) 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 % { f.write(access_page_template % {
'toc': htmlescape(toc, quote=True), 'toc': htmlescape(toc, quote=True),
'title': htmlescape(self.config.applehelp_title) 'title': htmlescape(self.config.applehelp_title)

View File

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

View File

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

View File

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

View File

@ -9,7 +9,6 @@
:license: BSD, see LICENSE for details. :license: BSD, see LICENSE for details.
""" """
import codecs
import re import re
import socket import socket
import threading import threading
@ -308,7 +307,8 @@ class CheckExternalLinksBuilder(Builder):
def write_entry(self, what, docname, line, uri): def write_entry(self, what, docname, line, uri):
# type: (unicode, unicode, int, unicode) -> None # 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), output.write("%s:%s: [%s] %s\n" % (self.env.doc2path(docname, None),
line, what, uri)) line, what, uri))

View File

@ -9,7 +9,6 @@
:license: BSD, see LICENSE for details. :license: BSD, see LICENSE for details.
""" """
import codecs
import os import os
import posixpath import posixpath
import re import re
@ -145,7 +144,8 @@ class QtHelpBuilder(StandaloneHTMLBuilder):
nspace = nspace.lower() nspace = nspace.lower()
# write the project file # 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, body = render_file('project.qhp', outname=outname,
title=self.config.html_title, version=self.config.version, title=self.config.html_title, version=self.config.version,
project=self.config.project, namespace=nspace, project=self.config.project, namespace=nspace,
@ -159,7 +159,8 @@ class QtHelpBuilder(StandaloneHTMLBuilder):
startpage = 'qthelp://' + posixpath.join(nspace, 'doc', 'index.html') startpage = 'qthelp://' + posixpath.join(nspace, 'doc', 'index.html')
logger.info(__('writing collection project file...')) 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, body = render_file('project.qhcp', outname=outname,
title=self.config.html_short_title, title=self.config.html_short_title,
homepage=homepage, startpage=startpage) homepage=homepage, startpage=startpage)

View File

@ -9,7 +9,6 @@
:license: BSD, see LICENSE for details. :license: BSD, see LICENSE for details.
""" """
import codecs
from os import path from os import path
from docutils.io import StringOutput from docutils.io import StringOutput
@ -82,7 +81,7 @@ class TextBuilder(Builder):
outfilename = path.join(self.outdir, os_path(docname) + self.out_suffix) outfilename = path.join(self.outdir, os_path(docname) + self.out_suffix)
ensuredir(path.dirname(outfilename)) ensuredir(path.dirname(outfilename))
try: 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) f.write(self.writer.output)
except (IOError, OSError) as err: except (IOError, OSError) as err:
logger.warning(__("error writing file %s: %s"), outfilename, err) logger.warning(__("error writing file %s: %s"), outfilename, err)

View File

@ -9,7 +9,6 @@
:license: BSD, see LICENSE for details. :license: BSD, see LICENSE for details.
""" """
import codecs
from os import path from os import path
from docutils import nodes from docutils import nodes
@ -95,7 +94,7 @@ class XMLBuilder(Builder):
outfilename = path.join(self.outdir, os_path(docname) + self.out_suffix) outfilename = path.join(self.outdir, os_path(docname) + self.out_suffix)
ensuredir(path.dirname(outfilename)) ensuredir(path.dirname(outfilename))
try: 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) f.write(self.writer.output)
except (IOError, OSError) as err: except (IOError, OSError) as err:
logger.warning(__("error writing file %s: %s"), outfilename, err) logger.warning(__("error writing file %s: %s"), outfilename, err)

View File

@ -18,7 +18,6 @@ import re
import sys import sys
import time import time
from collections import OrderedDict from collections import OrderedDict
from io import open
from os import path from os import path
# try to import readline, unix specific enhancement # 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 overwrite or not path.isfile(fpath):
if 'quiet' not in d: if 'quiet' not in d:
print(__('Creating file %s.') % fpath) 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) f.write(content)
else: else:
if 'quiet' not in d: if 'quiet' not in d:

View File

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

View File

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

View File

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

View File

@ -10,7 +10,6 @@
:license: BSD, see LICENSE for details. :license: BSD, see LICENSE for details.
""" """
import codecs
import posixpath import posixpath
import re import re
from hashlib import sha1 from hashlib import sha1
@ -142,7 +141,7 @@ class Graphviz(SphinxDirective):
rel_filename, filename = self.env.relfn2path(argument) rel_filename, filename = self.env.relfn2path(argument)
self.env.note_dependency(rel_filename) self.env.note_dependency(rel_filename)
try: 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() dotcode = fp.read()
except (IOError, OSError): except (IOError, OSError):
return [document.reporter.warning( 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('<p class="warning">%s</p>' % alt)
self.body.append('</object></div>\n') self.body.append('</object></div>\n')
else: 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) imgmap = ClickableMapDefinition(outfn + '.map', mapfile.read(), dot=code)
if imgmap.clickable: if imgmap.clickable:
# has a map # has a map

View File

@ -9,7 +9,6 @@
:license: BSD, see LICENSE for details. :license: BSD, see LICENSE for details.
""" """
import codecs
import posixpath import posixpath
import re import re
import shutil import shutil
@ -123,7 +122,7 @@ def compile_math(latex, builder):
"""Compile LaTeX macros for math to DVI.""" """Compile LaTeX macros for math to DVI."""
tempdir = ensure_tempdir(builder) tempdir = ensure_tempdir(builder)
filename = path.join(tempdir, 'math.tex') 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) f.write(latex)
# build latex command; old versions of latex don't have the # build latex command; old versions of latex don't have the

View File

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

View File

@ -10,7 +10,6 @@
""" """
from __future__ import absolute_import from __future__ import absolute_import
import codecs
import os import os
import re import re
import types import types
@ -314,7 +313,7 @@ class SphinxFileOutput(FileOutput):
# type: (unicode) -> unicode # type: (unicode) -> unicode
if (self.destination_path and self.autoclose and 'b' not in self.mode and 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)): 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 # skip writing: content not changed
if f.read() == data: if f.read() == data:
return data return data

View File

@ -10,7 +10,6 @@
""" """
from __future__ import absolute_import from __future__ import absolute_import
import codecs
import os import os
import posixpath import posixpath
@ -49,10 +48,10 @@ def copy_asset_file(source, destination, context=None, renderer=None):
from sphinx.util.template import SphinxRenderer from sphinx.util.template import SphinxRenderer
renderer = 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'): if destination.lower().endswith('_t'):
destination = destination[:-2] 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)) fdst.write(renderer.render_string(fsrc.read(), context))
else: else:
copyfile(source, destination) copyfile(source, destination)

View File

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