Patchwork [1,of,3,V2] py3: byteify strings in pycompat

login
register
mail settings
Submitter Matt Harbison
Date Sept. 15, 2018, 2:39 a.m.
Message ID <26795e6d586896a0449e.1536979165@Envy>
Download mbox | patch
Permalink /patch/34668/
State Accepted
Headers show

Comments

Matt Harbison - Sept. 15, 2018, 2:39 a.m.
# HG changeset patch
# User Matt Harbison <matt_harbison@yahoo.com>
# Date 1536813542 14400
#      Thu Sep 13 00:39:02 2018 -0400
# Node ID 26795e6d586896a0449ea8e7e78fc6ba33a04d68
# Parent  6ecfd12f09cd4fe185ebbc98d254534ebd995b68
py3: byteify strings in pycompat

These surfaced when disabling the source transformer to debug the problems in
win32.py.  ./contrib/byteify-strings.py found a couple false positives, so I
marked them with r'' explicitly (in case I'm wrong).

# skip-blame since this is just b'' and r'' prefixing
Matt Harbison - Sept. 15, 2018, 4:18 a.m.
On Fri, 14 Sep 2018 22:39:25 -0400, Matt Harbison <mharbison72@gmail.com>  
wrote:

> # HG changeset patch
> # User Matt Harbison <matt_harbison@yahoo.com>
> # Date 1536813542 14400
> #      Thu Sep 13 00:39:02 2018 -0400
> # Node ID 26795e6d586896a0449ea8e7e78fc6ba33a04d68
> # Parent  6ecfd12f09cd4fe185ebbc98d254534ebd995b68
> py3: byteify strings in pycompat
>
> diff --git a/mercurial/pycompat.py b/mercurial/pycompat.py
> --- a/mercurial/pycompat.py
> +++ b/mercurial/pycompat.py

> @@ -346,8 +346,8 @@ else:
>      bytesurl = identity
>       # this can't be parsed on Python 3
> -    exec('def raisewithtb(exc, tb):\n'
> -         '    raise exc, None, tb\n')
> +    exec(r'def raisewithtb(exc, tb):\n'
> +         r'    raise exc, None, tb\n')
>       def fsencode(filename):
>          """

Now that I've cycled back around to py2, it looks like this part needs to  
be dropped.
Yuya Nishihara - Sept. 15, 2018, 6:05 a.m.
On Sat, 15 Sep 2018 00:18:32 -0400, Matt Harbison wrote:
> On Fri, 14 Sep 2018 22:39:25 -0400, Matt Harbison <mharbison72@gmail.com>  
> wrote:
> 
> > # HG changeset patch
> > # User Matt Harbison <matt_harbison@yahoo.com>
> > # Date 1536813542 14400
> > #      Thu Sep 13 00:39:02 2018 -0400
> > # Node ID 26795e6d586896a0449ea8e7e78fc6ba33a04d68
> > # Parent  6ecfd12f09cd4fe185ebbc98d254534ebd995b68
> > py3: byteify strings in pycompat
> >
> > diff --git a/mercurial/pycompat.py b/mercurial/pycompat.py
> > --- a/mercurial/pycompat.py
> > +++ b/mercurial/pycompat.py
> 
> > @@ -346,8 +346,8 @@ else:
> >      bytesurl = identity
> >       # this can't be parsed on Python 3
> > -    exec('def raisewithtb(exc, tb):\n'
> > -         '    raise exc, None, tb\n')
> > +    exec(r'def raisewithtb(exc, tb):\n'
> > +         r'    raise exc, None, tb\n')
> >       def fsencode(filename):
> >          """
> 
> Now that I've cycled back around to py2, it looks like this part needs to  
> be dropped.

Dropped this and queued, thanks. r'\n' == '\\n'.

Patch

diff --git a/mercurial/pycompat.py b/mercurial/pycompat.py
--- a/mercurial/pycompat.py
+++ b/mercurial/pycompat.py
@@ -138,7 +138,7 @@  if ispy3:
     if getattr(sys, 'argv', None) is not None:
         sysargv = list(map(os.fsencode, sys.argv))
 
-    bytechr = struct.Struct('>B').pack
+    bytechr = struct.Struct(r'>B').pack
     byterepr = b'%r'.__mod__
 
     class bytestr(bytes):
@@ -282,7 +282,7 @@  if ispy3:
     xrange = builtins.range
     unicode = str
 
-    def open(name, mode='r', buffering=-1, encoding=None):
+    def open(name, mode=b'r', buffering=-1, encoding=None):
         return builtins.open(name, sysstr(mode), buffering, encoding)
 
     safehasattr = _wrapattrfunc(builtins.hasattr)
@@ -346,8 +346,8 @@  else:
     bytesurl = identity
 
     # this can't be parsed on Python 3
-    exec('def raisewithtb(exc, tb):\n'
-         '    raise exc, None, tb\n')
+    exec(r'def raisewithtb(exc, tb):\n'
+         r'    raise exc, None, tb\n')
 
     def fsencode(filename):
         """
@@ -359,7 +359,7 @@  else:
             return filename
         else:
             raise TypeError(
-                "expect str, not %s" % type(filename).__name__)
+                r"expect str, not %s" % type(filename).__name__)
 
     # In Python 2, fsdecode() has a very chance to receive bytes. So it's
     # better not to touch Python 2 part as it's already working fine.
@@ -404,11 +404,11 @@  else:
     rawinput = raw_input
     getargspec = inspect.getargspec
 
-isjython = sysplatform.startswith('java')
+isjython = sysplatform.startswith(b'java')
 
-isdarwin = sysplatform == 'darwin'
-isposix = osname == 'posix'
-iswindows = osname == 'nt'
+isdarwin = sysplatform == b'darwin'
+isposix = osname == b'posix'
+iswindows = osname == b'nt'
 
 def getoptb(args, shortlist, namelist):
     return _getoptbwrapper(getopt.getopt, args, shortlist, namelist)