Patchwork [3,of,7] py3: use pycompat.bytestr() instead of str()

login
register
mail settings
Submitter Pulkit Goyal
Date May 3, 2017, 9:59 a.m.
Message ID <ada413b057f0052a0651.1493805562@pulkit-goyal>
Download mbox | patch
Permalink /patch/20398/
State Accepted
Headers show

Comments

Pulkit Goyal - May 3, 2017, 9:59 a.m.
# HG changeset patch
# User Pulkit Goyal <7895pulkit@gmail.com>
# Date 1493266797 -19800
#      Thu Apr 27 09:49:57 2017 +0530
# Node ID ada413b057f0052a065197295801e24d49799036
# Parent  af5154fdc7d485b090ba1bed3bb42addb7836469
py3: use pycompat.bytestr() instead of str()

This is because str() on python 3 return unicodes
Yuya Nishihara - May 4, 2017, 2:12 a.m.
On Wed, 03 May 2017 15:29:22 +0530, Pulkit Goyal wrote:
> # HG changeset patch
> # User Pulkit Goyal <7895pulkit@gmail.com>
> # Date 1493266797 -19800
> #      Thu Apr 27 09:49:57 2017 +0530
> # Node ID ada413b057f0052a065197295801e24d49799036
> # Parent  af5154fdc7d485b090ba1bed3bb42addb7836469
> py3: use pycompat.bytestr() instead of str()

> --- a/hgext/blackbox.py	Thu Apr 20 19:51:37 2017 +0530
> +++ b/hgext/blackbox.py	Thu Apr 27 09:49:57 2017 +0530
> @@ -45,6 +45,7 @@
>  
>  from mercurial import (
>      cmdutil,
> +    pycompat,
>      ui as uimod,
>      util,
>  )
> @@ -173,7 +174,7 @@
>                  ui._bbinlog = True
>                  date = util.datestr(None, '%Y/%m/%d %H:%M:%S')
>                  user = util.getuser()
> -                pid = str(util.getpid())
> +                pid = pycompat.bytestr(util.getpid())

pid must be an int, so '%d' can be used instead.

> diff -r af5154fdc7d4 -r ada413b057f0 mercurial/cmdutil.py
> --- a/mercurial/cmdutil.py	Thu Apr 20 19:51:37 2017 +0530
> +++ b/mercurial/cmdutil.py	Thu Apr 27 09:49:57 2017 +0530
> @@ -1360,7 +1360,7 @@
>          if rev is None:
>              jrev = jnode = 'null'
>          else:
> -            jrev = str(rev)
> +            jrev = pycompat.bytestr(rev)

Same here. '%d'

> @@ -1607,7 +1607,7 @@
>          if rev in results:
>              ui.status(_("found revision %s from %s\n") %
>                        (rev, util.datestr(results[rev])))

Perhaps this message should be updated, too.

> -            return str(rev)
> +            return pycompat.bytestr(rev)

'%d'

> diff -r af5154fdc7d4 -r ada413b057f0 mercurial/templatefilters.py
> --- a/mercurial/templatefilters.py	Thu Apr 20 19:51:37 2017 +0530
> +++ b/mercurial/templatefilters.py	Thu Apr 27 09:49:57 2017 +0530
> @@ -230,7 +230,7 @@
>      elif obj is True:
>          return 'true'
>      elif isinstance(obj, (int, long, float)):
> -        return str(obj)
> +        return pycompat.bytestr(obj)
>      elif isinstance(obj, str):
>          return '"%s"' % encoding.jsonescape(obj, paranoid=paranoid)
>      elif util.safehasattr(obj, 'keys'):
> @@ -359,7 +359,7 @@
>          return "".join([stringify(t) for t in thing if t is not None])
>      if thing is None:
>          return ""
> -    return str(thing)
> +    return pycompat.bytestr(thing)

I've taken these templatefilters changes, thanks.

Patch

diff -r af5154fdc7d4 -r ada413b057f0 hgext/blackbox.py
--- a/hgext/blackbox.py	Thu Apr 20 19:51:37 2017 +0530
+++ b/hgext/blackbox.py	Thu Apr 27 09:49:57 2017 +0530
@@ -45,6 +45,7 @@ 
 
 from mercurial import (
     cmdutil,
+    pycompat,
     ui as uimod,
     util,
 )
@@ -173,7 +174,7 @@ 
                 ui._bbinlog = True
                 date = util.datestr(None, '%Y/%m/%d %H:%M:%S')
                 user = util.getuser()
-                pid = str(util.getpid())
+                pid = pycompat.bytestr(util.getpid())
                 formattedmsg = msg[0] % msg[1:]
                 rev = '(unknown)'
                 changed = ''
diff -r af5154fdc7d4 -r ada413b057f0 mercurial/cmdutil.py
--- a/mercurial/cmdutil.py	Thu Apr 20 19:51:37 2017 +0530
+++ b/mercurial/cmdutil.py	Thu Apr 27 09:49:57 2017 +0530
@@ -1360,7 +1360,7 @@ 
         if rev is None:
             jrev = jnode = 'null'
         else:
-            jrev = str(rev)
+            jrev = pycompat.bytestr(rev)
             jnode = '"%s"' % hex(ctx.node())
         j = encoding.jsonescape
 
@@ -1607,7 +1607,7 @@ 
         if rev in results:
             ui.status(_("found revision %s from %s\n") %
                       (rev, util.datestr(results[rev])))
-            return str(rev)
+            return pycompat.bytestr(rev)
 
     raise error.Abort(_("revision matching date not found"))
 
diff -r af5154fdc7d4 -r ada413b057f0 mercurial/templatefilters.py
--- a/mercurial/templatefilters.py	Thu Apr 20 19:51:37 2017 +0530
+++ b/mercurial/templatefilters.py	Thu Apr 27 09:49:57 2017 +0530
@@ -230,7 +230,7 @@ 
     elif obj is True:
         return 'true'
     elif isinstance(obj, (int, long, float)):
-        return str(obj)
+        return pycompat.bytestr(obj)
     elif isinstance(obj, str):
         return '"%s"' % encoding.jsonescape(obj, paranoid=paranoid)
     elif util.safehasattr(obj, 'keys'):
@@ -359,7 +359,7 @@ 
         return "".join([stringify(t) for t in thing if t is not None])
     if thing is None:
         return ""
-    return str(thing)
+    return pycompat.bytestr(thing)
 
 @templatefilter('stripdir')
 def stripdir(text):