diff options
author | Jon Dufresne <jon.dufresne@gmail.com> | 2018-09-11 04:57:20 -0700 |
---|---|---|
committer | Jon Dufresne <jon.dufresne@gmail.com> | 2018-09-11 05:45:36 -0700 |
commit | 02fea029bfc5bfd64e43de9e810aef2dd3c8cb2c (patch) | |
tree | 8460d233e013fe774e8e9d5a2cc3e3ef2392a37d | |
parent | 844a3a5c226ff8891a1ce139f10ac92157c75da5 (diff) | |
download | sphinx-git-02fea029bfc5bfd64e43de9e810aef2dd3c8cb2c.tar.gz |
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.
-rw-r--r-- | sphinx/builders/applehelp.py | 3 | ||||
-rw-r--r-- | sphinx/builders/changes.py | 13 | ||||
-rw-r--r-- | sphinx/builders/html.py | 16 | ||||
-rw-r--r-- | sphinx/builders/htmlhelp.py | 5 | ||||
-rw-r--r-- | sphinx/builders/linkcheck.py | 4 | ||||
-rw-r--r-- | sphinx/builders/qthelp.py | 7 | ||||
-rw-r--r-- | sphinx/builders/text.py | 3 | ||||
-rw-r--r-- | sphinx/builders/xml.py | 3 | ||||
-rw-r--r-- | sphinx/cmd/quickstart.py | 3 | ||||
-rw-r--r-- | sphinx/directives/code.py | 4 | ||||
-rw-r--r-- | sphinx/ext/autosummary/generate.py | 5 | ||||
-rw-r--r-- | sphinx/ext/doctest.py | 4 | ||||
-rw-r--r-- | sphinx/ext/graphviz.py | 5 | ||||
-rw-r--r-- | sphinx/ext/imgmath.py | 3 | ||||
-rw-r--r-- | sphinx/testing/path.py | 5 | ||||
-rw-r--r-- | sphinx/util/docutils.py | 3 | ||||
-rw-r--r-- | sphinx/util/fileutil.py | 5 | ||||
-rw-r--r-- | sphinx/util/i18n.py | 5 |
18 files changed, 43 insertions, 53 deletions
diff --git a/sphinx/builders/applehelp.py b/sphinx/builders/applehelp.py index 79d57210c..56e9bd6c0 100644 --- a/sphinx/builders/applehelp.py +++ b/sphinx/builders/applehelp.py @@ -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) diff --git a/sphinx/builders/changes.py b/sphinx/builders/changes.py index 3f9bffa0d..d5244747d 100644 --- a/sphinx/builders/changes.py +++ b/sphinx/builders/changes.py @@ -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), diff --git a/sphinx/builders/html.py b/sphinx/builders/html.py index 3c08cc818..b4b570bdc 100644 --- a/sphinx/builders/html.py +++ b/sphinx/builders/html.py @@ -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) diff --git a/sphinx/builders/htmlhelp.py b/sphinx/builders/htmlhelp.py index 52600ca21..4d5172a8f 100644 --- a/sphinx/builders/htmlhelp.py +++ b/sphinx/builders/htmlhelp.py @@ -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 diff --git a/sphinx/builders/linkcheck.py b/sphinx/builders/linkcheck.py index c3fd1e88a..2189411ee 100644 --- a/sphinx/builders/linkcheck.py +++ b/sphinx/builders/linkcheck.py @@ -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)) diff --git a/sphinx/builders/qthelp.py b/sphinx/builders/qthelp.py index 1b666cb4d..22ccae5f0 100644 --- a/sphinx/builders/qthelp.py +++ b/sphinx/builders/qthelp.py @@ -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) diff --git a/sphinx/builders/text.py b/sphinx/builders/text.py index 81209d165..ef8c4a7ac 100644 --- a/sphinx/builders/text.py +++ b/sphinx/builders/text.py @@ -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) diff --git a/sphinx/builders/xml.py b/sphinx/builders/xml.py index 6198532c9..3e0d9224e 100644 --- a/sphinx/builders/xml.py +++ b/sphinx/builders/xml.py @@ -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) diff --git a/sphinx/cmd/quickstart.py b/sphinx/cmd/quickstart.py index 174298579..51285efa5 100644 --- a/sphinx/cmd/quickstart.py +++ b/sphinx/cmd/quickstart.py @@ -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: diff --git a/sphinx/directives/code.py b/sphinx/directives/code.py index a98ab5883..ec389e708 100644 --- a/sphinx/directives/code.py +++ b/sphinx/directives/code.py @@ -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']) diff --git a/sphinx/ext/autosummary/generate.py b/sphinx/ext/autosummary/generate.py index dbadfe8d5..b4b8bcc42 100644 --- a/sphinx/ext/autosummary/generate.py +++ b/sphinx/ext/autosummary/generate.py @@ -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 diff --git a/sphinx/ext/doctest.py b/sphinx/ext/doctest.py index fa6cbed6d..c4898d583 100644 --- a/sphinx/ext/doctest.py +++ b/sphinx/ext/doctest.py @@ -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))) diff --git a/sphinx/ext/graphviz.py b/sphinx/ext/graphviz.py index cf606f3b7..63f7f3dc8 100644 --- a/sphinx/ext/graphviz.py +++ b/sphinx/ext/graphviz.py @@ -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 diff --git a/sphinx/ext/imgmath.py b/sphinx/ext/imgmath.py index 593835554..5be272ab1 100644 --- a/sphinx/ext/imgmath.py +++ b/sphinx/ext/imgmath.py @@ -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 diff --git a/sphinx/testing/path.py b/sphinx/testing/path.py index 585933499..6deda345f 100644 --- a/sphinx/testing/path.py +++ b/sphinx/testing/path.py @@ -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): diff --git a/sphinx/util/docutils.py b/sphinx/util/docutils.py index 547d74c17..96944bd6f 100644 --- a/sphinx/util/docutils.py +++ b/sphinx/util/docutils.py @@ -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 diff --git a/sphinx/util/fileutil.py b/sphinx/util/fileutil.py index fcbc8abe6..85b6d4f47 100644 --- a/sphinx/util/fileutil.py +++ b/sphinx/util/fileutil.py @@ -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) diff --git a/sphinx/util/i18n.py b/sphinx/util/i18n.py index edaebebda..2b557ec93 100644 --- a/sphinx/util/i18n.py +++ b/sphinx/util/i18n.py @@ -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: |