diff --git a/sphinx/builders/html.py b/sphinx/builders/html.py
index 10a6498e2..d89092766 100644
--- a/sphinx/builders/html.py
+++ b/sphinx/builders/html.py
@@ -88,7 +88,7 @@ def get_stable_hash(obj):
return get_stable_hash(list(obj.items()))
elif isinstance(obj, (list, tuple)):
obj = sorted(get_stable_hash(o) for o in obj)
- return md5(text_type(obj).encode('utf8')).hexdigest()
+ return md5(text_type(obj).encode()).hexdigest()
class Stylesheet(text_type):
diff --git a/sphinx/cmd/quickstart.py b/sphinx/cmd/quickstart.py
index ce66f7352..8d48be298 100644
--- a/sphinx/cmd/quickstart.py
+++ b/sphinx/cmd/quickstart.py
@@ -172,7 +172,7 @@ def term_decode(text):
'and terminal encoding unknown -- assuming '
'UTF-8 or Latin-1.')))
try:
- return text.decode('utf-8')
+ return text.decode()
except UnicodeDecodeError:
return text.decode('latin1')
diff --git a/sphinx/ext/graphviz.py b/sphinx/ext/graphviz.py
index 878e4bfc3..5cbb26147 100644
--- a/sphinx/ext/graphviz.py
+++ b/sphinx/ext/graphviz.py
@@ -73,7 +73,7 @@ class ClickableMapDefinition:
if self.id == '%3':
# graphviz generates wrong ID if graph name not specified
# https://gitlab.com/graphviz/graphviz/issues/1327
- hashed = sha1(dot.encode('utf-8')).hexdigest()
+ hashed = sha1(dot.encode()).hexdigest()
self.id = 'grapviz%s' % hashed[-10:]
self.content[0] = self.content[0].replace('%3', self.id)
@@ -221,7 +221,7 @@ def render_dot(self, code, options, format, prefix='graphviz'):
"""Render graphviz code into a PNG or PDF output file."""
graphviz_dot = options.get('graphviz_dot', self.builder.config.graphviz_dot)
hashkey = (code + str(options) + str(graphviz_dot) +
- str(self.builder.config.graphviz_dot_args)).encode('utf-8')
+ str(self.builder.config.graphviz_dot_args)).encode()
fname = '%s-%s.%s' % (prefix, sha1(hashkey).hexdigest(), format)
relfn = posixpath.join(self.builder.imgpath, fname)
@@ -259,7 +259,7 @@ def render_dot(self, code, options, format, prefix='graphviz'):
try:
# Graphviz may close standard input when an error occurs,
# resulting in a broken pipe on communicate()
- stdout, stderr = p.communicate(code.encode('utf-8'))
+ stdout, stderr = p.communicate(code.encode())
except (OSError, IOError) as err:
if err.errno not in (EPIPE, EINVAL):
raise
diff --git a/sphinx/ext/imgmath.py b/sphinx/ext/imgmath.py
index 5e2f22511..545193498 100644
--- a/sphinx/ext/imgmath.py
+++ b/sphinx/ext/imgmath.py
@@ -235,7 +235,7 @@ def render_math(self, math):
latex = generate_latex_macro(math, self.builder.config)
- filename = "%s.%s" % (sha1(latex.encode('utf-8')).hexdigest(), image_format)
+ filename = "%s.%s" % (sha1(latex.encode()).hexdigest(), image_format)
relfn = posixpath.join(self.builder.imgpath, 'math', filename)
outfn = path.join(self.builder.outdir, self.builder.imagedir, 'math', filename)
if path.isfile(outfn):
diff --git a/sphinx/ext/inheritance_diagram.py b/sphinx/ext/inheritance_diagram.py
index c315ba8d3..c4ad64e34 100644
--- a/sphinx/ext/inheritance_diagram.py
+++ b/sphinx/ext/inheritance_diagram.py
@@ -389,7 +389,7 @@ class InheritanceDiagram(SphinxDirective):
def get_graph_hash(node):
# type: (inheritance_diagram) -> str
- encoded = (node['content'] + str(node['parts'])).encode('utf-8')
+ encoded = (node['content'] + str(node['parts'])).encode()
return md5(encoded).hexdigest()[-10:]
diff --git a/sphinx/pycode/__init__.py b/sphinx/pycode/__init__.py
index e3e80772c..e2538ad27 100644
--- a/sphinx/pycode/__init__.py
+++ b/sphinx/pycode/__init__.py
@@ -55,7 +55,7 @@ class ModuleAnalyzer:
eggpath, relpath = re.split('(?<=\\.egg)/', filename)
try:
with ZipFile(eggpath) as egg:
- code = egg.read(relpath).decode('utf-8')
+ code = egg.read(relpath).decode()
return cls.for_string(code, modname, filename)
except Exception as exc:
raise PycodeError('error opening %r' % filename, exc)
diff --git a/sphinx/pycode/parser.py b/sphinx/pycode/parser.py
index 71d7df781..0d0c3322f 100644
--- a/sphinx/pycode/parser.py
+++ b/sphinx/pycode/parser.py
@@ -480,7 +480,7 @@ class Parser:
def parse_comments(self):
# type: () -> None
"""Parse the code and pick up comments."""
- tree = ast.parse(self.code.encode('utf-8'))
+ tree = ast.parse(self.code.encode())
picker = VariableCommentPicker(self.code.splitlines(True), self.encoding)
picker.visit(tree)
self.comments = picker.comments
diff --git a/sphinx/search/__init__.py b/sphinx/search/__init__.py
index 7d8156bea..a80cc6c0c 100644
--- a/sphinx/search/__init__.py
+++ b/sphinx/search/__init__.py
@@ -289,7 +289,7 @@ class IndexBuilder:
if scoring:
with open(scoring, 'rb') as fp:
- self.js_scorer_code = fp.read().decode('utf-8')
+ self.js_scorer_code = fp.read().decode()
else:
self.js_scorer_code = u''
self.js_splitter_code = splitter_code
diff --git a/sphinx/transforms/post_transforms/images.py b/sphinx/transforms/post_transforms/images.py
index ff8c9f9cc..079b81102 100644
--- a/sphinx/transforms/post_transforms/images.py
+++ b/sphinx/transforms/post_transforms/images.py
@@ -75,12 +75,12 @@ class ImageDownloader(BaseImageConverter):
basename = basename.split('?')[0]
if basename == '' or len(basename) > MAX_FILENAME_LEN:
filename, ext = os.path.splitext(node['uri'])
- basename = sha1(filename.encode("utf-8")).hexdigest() + ext
+ basename = sha1(filename.encode()).hexdigest() + ext
dirname = node['uri'].replace('://', '/').translate({ord("?"): u"/",
ord("&"): u"/"})
if len(dirname) > MAX_FILENAME_LEN:
- dirname = sha1(dirname.encode('utf-8')).hexdigest()
+ dirname = sha1(dirname.encode()).hexdigest()
ensuredir(os.path.join(self.imagedir, dirname))
path = os.path.join(self.imagedir, dirname, basename)
diff --git a/sphinx/util/__init__.py b/sphinx/util/__init__.py
index e1d39718c..18e073955 100644
--- a/sphinx/util/__init__.py
+++ b/sphinx/util/__init__.py
@@ -180,7 +180,7 @@ class DownloadFiles(dict):
def add_file(self, docname, filename):
# type: (str, str) -> None
if filename not in self:
- digest = md5(filename.encode('utf-8')).hexdigest()
+ digest = md5(filename.encode()).hexdigest()
dest = '%s/%s' % (digest, os.path.basename(filename))
self[filename] = (set(), dest)
@@ -266,7 +266,7 @@ def save_traceback(app):
platform.python_implementation(),
docutils.__version__, docutils.__version_details__,
jinja2.__version__, # type: ignore
- last_msgs)).encode('utf-8'))
+ last_msgs)).encode())
if app is not None:
for ext in app.extensions.values():
modfile = getattr(ext.module, '__file__', 'unknown')
@@ -274,8 +274,8 @@ def save_traceback(app):
modfile = modfile.decode(fs_encoding, 'replace')
if ext.version != 'builtin':
os.write(fd, ('# %s (%s) from %s\n' %
- (ext.name, ext.version, modfile)).encode('utf-8'))
- os.write(fd, exc_format.encode('utf-8'))
+ (ext.name, ext.version, modfile)).encode())
+ os.write(fd, exc_format.encode())
os.close(fd)
return path
@@ -490,7 +490,7 @@ def force_decode(string, encoding):
string = string.decode(encoding)
else:
# try decoding with utf-8, should only work for real UTF-8
- string = string.decode('utf-8')
+ string = string.decode()
except UnicodeError:
# last resort -- can't fail
string = string.decode('latin1')
@@ -632,8 +632,8 @@ def encode_uri(uri):
# type: (str) -> str
split = list(urlsplit(uri))
split[1] = split[1].encode('idna').decode('ascii')
- split[2] = quote_plus(split[2].encode('utf-8'), '/')
- query = list((q, v.encode('utf-8')) for (q, v) in parse_qsl(split[3]))
+ split[2] = quote_plus(split[2].encode(), '/')
+ query = list((q, v.encode()) for (q, v) in parse_qsl(split[3]))
split[3] = urlencode(query)
return urlunsplit(split)
diff --git a/sphinx/util/images.py b/sphinx/util/images.py
index 5b3a27d77..41f41c0b1 100644
--- a/sphinx/util/images.py
+++ b/sphinx/util/images.py
@@ -129,7 +129,7 @@ def test_svg(h, f):
# type: (bytes, IO) -> str
"""An additional imghdr library helper; test the header is SVG's or not."""
try:
- if '