diff --git a/sphinx/builders/changes.py b/sphinx/builders/changes.py
index 3e351e6cb..22fcab869 100644
--- a/sphinx/builders/changes.py
+++ b/sphinx/builders/changes.py
@@ -11,7 +11,6 @@
import codecs
from os import path
-from cgi import escape
from sphinx import package_dir
from sphinx.util import copy_static_entry
@@ -20,6 +19,7 @@ from sphinx.theming import Theme
from sphinx.builders import Builder
from sphinx.util.osutil import ensuredir, os_path
from sphinx.util.console import bold
+from sphinx.util.pycompat import htmlescape
class ChangesBuilder(Builder):
@@ -115,7 +115,7 @@ class ChangesBuilder(Builder):
'.. deprecated:: %s' % version]
def hl(no, line):
- line = ' ' % no + escape(line)
+ line = ' ' % no + htmlescape(line)
for x in hltext:
if x in line:
line = '%s' % line
@@ -125,7 +125,10 @@ class ChangesBuilder(Builder):
self.info(bold('copying source files...'))
for docname in self.env.all_docs:
f = codecs.open(self.env.doc2path(docname), 'r', 'latin1')
- lines = f.readlines()
+ try:
+ lines = f.readlines()
+ finally:
+ f.close()
targetfn = path.join(self.outdir, 'rst', os_path(docname)) + '.html'
ensuredir(path.dirname(targetfn))
f = codecs.open(targetfn, 'w', 'latin1')
@@ -148,7 +151,7 @@ class ChangesBuilder(Builder):
self.outdir, self)
def hl(self, text, version):
- text = escape(text)
+ text = htmlescape(text)
for directive in ['versionchanged', 'versionadded', 'deprecated']:
text = text.replace('.. %s:: %s' % (directive, version),
'.. %s:: %s' % (directive, version))
diff --git a/sphinx/builders/htmlhelp.py b/sphinx/builders/htmlhelp.py
index 9a9238f97..fdf25cc81 100644
--- a/sphinx/builders/htmlhelp.py
+++ b/sphinx/builders/htmlhelp.py
@@ -11,7 +11,6 @@
"""
import os
-import cgi
import codecs
from os import path
@@ -19,6 +18,7 @@ from docutils import nodes
from sphinx import addnodes
from sphinx.builders.html import StandaloneHTMLBuilder
+from sphinx.util.pycompat import htmlescape
# Project file (*.hhp) template. 'outname' is the file basename (like
@@ -241,7 +241,7 @@ class HTMLHelpBuilder(StandaloneHTMLBuilder):
write_toc(subnode, ullevel)
elif isinstance(node, nodes.reference):
link = node['refuri']
- title = cgi.escape(node.astext()).replace('"','"')
+ title = htmlescape(node.astext()).replace('"','"')
f.write(object_sitemap % (title, link))
elif isinstance(node, nodes.bullet_list):
if ullevel != 0:
@@ -272,7 +272,7 @@ class HTMLHelpBuilder(StandaloneHTMLBuilder):
item = ' \n' % \
(name, value)
f.write(item)
- title = cgi.escape(title)
+ title = htmlescape(title)
f.write('