D1066: url: add cgi.escape equivalent for bytestrings

durin42 (Augie Fackler) phabricator at mercurial-scm.org
Sat Oct 14 07:03:24 UTC 2017


durin42 created this revision.
Herald added a subscriber: mercurial-devel.
Herald added a reviewer: hg-reviewers.

REVISION SUMMARY
  This seems like a sensible enough place to put it.

REPOSITORY
  rHG Mercurial

REVISION DETAIL
  https://phab.mercurial-scm.org/D1066

AFFECTED FILES
  mercurial/url.py

CHANGE DETAILS

diff --git a/mercurial/url.py b/mercurial/url.py
--- a/mercurial/url.py
+++ b/mercurial/url.py
@@ -30,6 +30,21 @@
 urlerr = util.urlerr
 urlreq = util.urlreq
 
+def escape(s, quote=None):
+    '''Replace special characters "&", "<" and ">" to HTML-safe sequences.
+    If the optional flag quote is true, the quotation mark character (")
+    is also translated.
+
+    This is the same as cgi.escape in Python, but always operates on
+    bytes, whereas cgi.escape in Python 3 only works on unicodes.
+    '''
+    s = s.replace(b"&", b"&")
+    s = s.replace(b"<", b"<")
+    s = s.replace(b">", b">")
+    if quote:
+        s = s.replace(b'"', b""")
+    return s
+
 class passwordmgr(object):
     def __init__(self, ui, passwddb):
         self.ui = ui



To: durin42, #hg-reviewers
Cc: mercurial-devel


More information about the Mercurial-devel mailing list