diff hglib/context.py @ 142:fe74d5599539

hglib: wrap all application string literals in util.b() (issue4520) Conversion also included changing use of string interpolation to string concatenation as bytes interpolation does not exist in Python 3. Indexing related to bytes was also changed to length-1 bytes through slicing as Python 3 returns an int in this instance. Tests have not been switched to using util.b() so that the change to application code can be independently verified as not being broken.
author Brett Cannon <brett@python.org>
date Sun, 08 Mar 2015 13:08:37 -0400
parents a05cdc1579df
children c1b966866ed7
line wrap: on
line diff
--- a/hglib/context.py	Sat Mar 07 10:08:52 2015 -0500
+++ b/hglib/context.py	Sun Mar 08 13:08:37 2015 -0400
@@ -1,16 +1,17 @@
 from hglib.error import CommandError
 import client, util, templates
+from hglib.util import b
 
-_nullcset = ['-1', '0000000000000000000000000000000000000000', '', '',
-             '', '', '']
+_nullcset = [b('-1'), b('0000000000000000000000000000000000000000'), b(''),
+             b(''), b(''), b(''), b('')]
 
 class changectx(object):
     """A changecontext object makes access to data related to a particular
     changeset convenient."""
-    def __init__(self, repo, changeid=''):
+    def __init__(self, repo, changeid=b('')):
         """changeid is a revision number, node, or tag"""
-        if changeid == '':
-            changeid = '.'
+        if changeid == b(''):
+            changeid = b('.')
         self._repo = repo
         if isinstance(changeid, client.revision):
             cset = changeid
@@ -18,7 +19,7 @@
             cset = _nullcset
         else:
             if isinstance(changeid, (long, int)):
-                changeid = 'rev(%d)' % changeid
+                changeid = b('rev(') + str(changeid).encode('latin-1') + b(')')
 
             notfound = False
             try:
@@ -43,7 +44,7 @@
 
         self._tags = self._tags.split()
         try:
-            self._tags.remove('tip')
+            self._tags.remove(b('tip'))
         except ValueError:
             pass
 
@@ -89,10 +90,13 @@
         return self._parsestatus(self._repo.status(change=self))[:4]
 
     def _parsestatus(self, stat):
-        d = dict((c, []) for c in 'MAR!?IC ')
+        d = dict((c, [])
+                 for c in (b('M'), b('A'), b('R'), b('!'), b('?'), b('I'),
+                    b('C'), b(' ')))
         for k, path in stat:
             d[k].append(path)
-        return d['M'], d['A'], d['R'], d['!'], d['?'], d['I'], d['C']
+        return (d[b('M')], d[b('A')], d[b('R')], d[b('!')], d[b('?')],
+                d[b('I')], d[b('C')])
 
     def status(self, ignored=False, clean=False):
         """Explicit status query
@@ -201,7 +205,7 @@
 
     def hidden(self):
         """return True if the changeset is hidden, else False"""
-        return bool(self._repo.log(revrange='%s and hidden()' % self._node,
+        return bool(self._repo.log(revrange=self._node + b(' and hidden()'),
                                    hidden=True))
 
     def phase(self):
@@ -210,19 +214,20 @@
 
     def children(self):
         """return contexts for each child changeset"""
-        for c in self._repo.log('children(%s)' % self._node):
+        for c in self._repo.log(b('children(') + self._node + b(')')):
             yield changectx(self._repo, c)
 
     def ancestors(self):
-        for a in self._repo.log('ancestors(%s)' % self._node):
+        for a in self._repo.log(b('ancestors(') + self._node + b(')')):
             yield changectx(self._repo, a)
 
     def descendants(self):
-        for d in self._repo.log('descendants(%s)' % self._node):
+        for d in self._repo.log(b('descendants(') + self._node + b(')')):
             yield changectx(self._repo, d)
 
     def ancestor(self, c2):
         """
         return the ancestor context of self and c2
         """
-        return changectx(self._repo, 'ancestor(%s, %s)' % (self, c2))
+        return changectx(self._repo,
+                         b('ancestor(') + self + b(', ') + c2 + b(')'))