Georg Brandl avatar Georg Brandl committed 19a7b50

Closes #810: fix deprecation warnings with Python 3. What is left over is from nose or docutils.

Comments (0)

Files changed (9)

sphinx/builders/changes.py

 
 import codecs
 from os import path
-from cgi import escape
 
 from sphinx import package_dir
 from sphinx.util import copy_static_entry
 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):
                   '.. deprecated:: %s' % version]
 
         def hl(no, line):
-            line = '<a name="L%s"> </a>' % no + escape(line)
+            line = '<a name="L%s"> </a>' % no + htmlescape(line)
             for x in hltext:
                 if x in line:
                     line = '<span class="hl">%s</span>' % line
         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')
                           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),
                                 '<b>.. %s:: %s</b>' % (directive, version))

sphinx/builders/htmlhelp.py

 """
 
 import os
-import cgi
 import codecs
 from os import path
 
 
 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
                         write_toc(subnode, ullevel)
                 elif isinstance(node, nodes.reference):
                     link = node['refuri']
-                    title = cgi.escape(node.astext()).replace('"','&quot;')
+                    title = htmlescape(node.astext()).replace('"','&quot;')
                     f.write(object_sitemap % (title, link))
                 elif isinstance(node, nodes.bullet_list):
                     if ullevel != 0:
                     item = '    <param name="%s" value="%s">\n' % \
                         (name, value)
                     f.write(item)
-                title = cgi.escape(title)
+                title = htmlescape(title)
                 f.write('<LI> <OBJECT type="text/sitemap">\n')
                 write_param('Keyword', title)
                 if len(refs) == 0:

sphinx/builders/qthelp.py

 import codecs
 import posixpath
 from os import path
-from cgi import escape
 
 from docutils import nodes
 
 from sphinx import addnodes
 from sphinx.builders.html import StandaloneHTMLBuilder
+from sphinx.util.pycompat import htmlescape
 
 
 _idpattern = re.compile(
                        fn.endswith('.html'):
                     filename = path.join(root, fn)[olen:]
                     projectfiles.append(file_template %
-                                        {'filename': escape(filename)})
+                                        {'filename': htmlescape(filename)})
         projectfiles = '\n'.join(projectfiles)
 
         # it seems that the "namespace" may not contain non-alphanumeric
         f = codecs.open(path.join(outdir, outname+'.qhp'), 'w', 'utf-8')
         try:
             f.write(project_template % {
-                'outname': escape(outname),
-                'title': escape(self.config.html_title),
-                'version': escape(self.config.version),
-                'project': escape(self.config.project),
-                'namespace': escape(nspace),
-                'masterdoc': escape(self.config.master_doc),
+                'outname': htmlescape(outname),
+                'title': htmlescape(self.config.html_title),
+                'version': htmlescape(self.config.version),
+                'project': htmlescape(self.config.project),
+                'namespace': htmlescape(nspace),
+                'masterdoc': htmlescape(self.config.master_doc),
                 'sections': sections,
                 'keywords': keywords,
                 'files': projectfiles})
         f = codecs.open(path.join(outdir, outname+'.qhcp'), 'w', 'utf-8')
         try:
             f.write(collection_template % {
-                'outname': escape(outname),
-                'title': escape(self.config.html_short_title),
-                'homepage': escape(homepage),
-                'startpage': escape(startpage)})
+                'outname': htmlescape(outname),
+                'title': htmlescape(self.config.html_short_title),
+                'homepage': htmlescape(homepage),
+                'startpage': htmlescape(startpage)})
         finally:
             f.close()
 
         if self.isdocnode(node):
             refnode = node.children[0][0]
             link = refnode['refuri']
-            title = escape(refnode.astext()).replace('"','&quot;')
+            title = htmlescape(refnode.astext()).replace('"','&quot;')
             item = '<section title="%(title)s" ref="%(ref)s">' % {
                                                                 'title': title,
                                                                 'ref': link}
                 parts.extend(self.write_toc(subnode, indentlevel))
         elif isinstance(node, nodes.reference):
             link = node['refuri']
-            title = escape(node.astext()).replace('"','&quot;')
+            title = htmlescape(node.astext()).replace('"','&quot;')
             item = section_template % {'title': title, 'ref': link}
             item = u' ' * 4 * indentlevel + item
             parts.append(item.encode('ascii', 'xmlcharrefreplace'))
     def build_keywords(self, title, refs, subitems):
         keywords = []
 
-        title = escape(title)
+        title = htmlescape(title)
 #        if len(refs) == 0: # XXX
 #            write_param('See Also', title)
         if len(refs) == 1:

sphinx/highlighting.py

 """
 
 import sys
-import cgi
 import re
 import textwrap
 
     # parser is not available on Jython
     parser = None
 
+from sphinx.util.pycompat import htmlescape
 from sphinx.util.texescape import tex_hl_escape_map_new
 from sphinx.ext import doctest
 
 
     def unhighlighted(self, source):
         if self.dest == 'html':
-            return '<pre>' + cgi.escape(source) + '</pre>\n'
+            return '<pre>' + htmlescape(source) + '</pre>\n'
         else:
             # first, escape highlighting characters like Pygments does
             source = source.translate(escape_hl_chars)

sphinx/util/inspect.py

 inspect = __import__('inspect')
 
 from sphinx.util import force_decode
+from sphinx.util.pycompat import bytes
 
 
 if sys.version_info >= (2, 5):
         s = repr(object)
     except Exception:
         raise ValueError
-    return force_decode(s, None).replace('\n', ' ')
+    if isinstance(s, bytes):
+        return force_decode(s, None).replace('\n', ' ')
+    return s.replace('\n', ' ')

sphinx/util/pycompat.py

         return s.encode('ascii', 'backslashreplace')
 
 
+try:
+    from html import escape as htmlescape
+except ImportError:
+    from cgi import escape as htmlescape
+
 # ------------------------------------------------------------------------------
 # Missing builtins and itertools in Python < 2.6
 

sphinx/websupport/__init__.py

     :license: BSD, see LICENSE for details.
 """
 
-import cgi
 import sys
 import cPickle as pickle
 import posixpath
 from sphinx.application import Sphinx
 from sphinx.util.osutil import ensuredir
 from sphinx.util.jsonimpl import dumps as dump_json
+from sphinx.util.pycompat import htmlescape
 from sphinx.websupport import errors
 from sphinx.websupport.search import BaseSearch, SEARCH_ADAPTERS
 from sphinx.websupport.storage import StorageBackend
             ret = publish_parts(text, writer_name='html',
                                 settings_overrides=settings)['fragment']
         except Exception:
-            ret = cgi.escape(text)
+            ret = htmlescape(text)
         return ret

sphinx/websupport/storage/differ.py

 """
 
 import re
-from cgi import escape
 from difflib import Differ
 
+from sphinx.util.pycompat import htmlescape
+
 
 class CombinedHtmlDiff(object):
     """Create an HTML representation of the differences between two pieces
     highlight_regex = re.compile(r'([\+\-\^]+)')
 
     def __init__(self, source, proposal):
-        proposal = escape(proposal)
+        proposal = htmlescape(proposal)
 
         differ = Differ()
         self.diff = list(differ.compare(source.splitlines(1),

tests/test_build_html.py

     for fname, paths in HTML_XPATH.iteritems():
         parser = NslessParser()
         parser.entity.update(htmlentitydefs.entitydefs)
-        etree = ET.parse(os.path.join(app.outdir, fname), parser)
+        fp = open(os.path.join(app.outdir, fname))
+        try:
+            etree = ET.parse(fp, parser)
+        finally:
+            fp.close()
         for path, check in paths:
             yield check_xpath, etree, fname, path, check
 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.