Commits

Anonymous committed 618f949

implement htmlescape so it uses cgi.escape or html.escape depending on python version

This avoids a DeprecationWarning on Python >= 3.2 about using cgi.escape.

Comments (0)

Files changed (2)

 
 # Run time aliasing of Python2/3 differences
 
+def htmlescape(s, quote=True):
+    # this is html.escape reimplemented with cgi.escape,
+    # so it works for python 2.x, 3.0 and 3.1
+    import cgi
+    s = cgi.escape(s, quote)
+    if quote:
+        # python 3.2 also replaces the single quotes:
+        s = s.replace("'", "'")
+    return s
+
 if sys.version_info[0] < 3:
     PY3 = False
 
         else:
             return mv
 
+    try:
+        # for python >= 3.2, avoid DeprecationWarning for cgi.escape
+        from html import escape as htmlescape
+    except ImportError:
+        pass
+
 
 # Implementations missing from older versions of Python
 

src/whoosh/highlight.py

 from __future__ import division
 from collections import deque
 from heapq import nlargest
-from cgi import escape as htmlescape
 
+from whoosh.compat import htmlescape
 from whoosh.analysis import Token
 
 
         self.htmlclass = " ".join((self.classname, self.termclass))
 
     def _text(self, text):
-        return htmlescape(text)
+        return htmlescape(text, quote=False)
 
     def format_token(self, text, token, replace=False):
         seen = self.seen