Patchwork [1,of,2] py3: add pycompat.open and replace open() calls

login
register
mail settings
Submitter Pulkit Goyal
Date March 3, 2017, 10:04 a.m.
Message ID <e815f7c4026f7e714b6e.1488535451@pulkit-goyal>
Download mbox | patch
Permalink /patch/18883/
State Accepted
Headers show

Comments

Pulkit Goyal - March 3, 2017, 10:04 a.m.
# HG changeset patch
# User Pulkit Goyal <7895pulkit@gmail.com>
# Date 1488526472 -19800
#      Fri Mar 03 13:04:32 2017 +0530
# Node ID e815f7c4026f7e714b6e9bb59dce90020d9c56f2
# Parent  b4cd912d7704cd976e1bee3a3c927e0e578ec88f
py3: add pycompat.open and replace open() calls

open() requires mode argument as unicodes on Python 3. This patch introduces
pycompat.open() which is inserted to files using transformer and replaces
builtins.open() calls.
Yuya Nishihara - March 3, 2017, 2:14 p.m.
On Fri, 03 Mar 2017 15:34:11 +0530, Pulkit Goyal wrote:
> # HG changeset patch
> # User Pulkit Goyal <7895pulkit@gmail.com>
> # Date 1488526472 -19800
> #      Fri Mar 03 13:04:32 2017 +0530
> # Node ID e815f7c4026f7e714b6e9bb59dce90020d9c56f2
> # Parent  b4cd912d7704cd976e1bee3a3c927e0e578ec88f
> py3: add pycompat.open and replace open() calls

Queued these with slight modification, thanks.

> --- a/mercurial/pycompat.py	Thu Mar 02 10:12:40 2017 -0800
> +++ b/mercurial/pycompat.py	Fri Mar 03 13:04:32 2017 +0530
> @@ -122,6 +122,9 @@
>          dic = dict((k.encode('latin-1'), v) for k, v in dic.iteritems())
>          return dic
>  
> +    def open(name, mode='r', buffering=-1):
> +        return builtins.open(name, sysstr(mode), buffering)
> +
>      # shlex.split() accepts unicodes on Python 3. This function takes bytes
>      # argument, convert it into unicodes, pass into shlex.split(), convert the
>      # returned value to bytes and return that.
> @@ -131,6 +134,8 @@
>          return [a.encode('latin-1') for a in ret]
>  
>  else:
> +    import __builtin__
> +
>      def sysstr(s):
>          return s
>  
> @@ -170,6 +175,7 @@
>      getcwd = os.getcwd
>      sysexecutable = sys.executable
>      shlexsplit = shlex.split
> +    open = __builtin__.open

Since we use the importer hack, pycompat.open() isn't necessary for py2. I've
adjusted this patch to be more like del/get/has/setattr() wrappers.

Patch

diff -r b4cd912d7704 -r e815f7c4026f mercurial/__init__.py
--- a/mercurial/__init__.py	Thu Mar 02 10:12:40 2017 -0800
+++ b/mercurial/__init__.py	Fri Mar 03 13:04:32 2017 +0530
@@ -280,7 +280,7 @@ 
                     continue
                 r, c = t.start
                 l = (b'; from mercurial.pycompat import '
-                     b'delattr, getattr, hasattr, setattr, xrange\n')
+                     b'delattr, getattr, hasattr, setattr, xrange, open\n')
                 for u in tokenize.tokenize(io.BytesIO(l).readline):
                     if u.type in (tokenize.ENCODING, token.ENDMARKER):
                         continue
@@ -327,7 +327,7 @@ 
     # ``replacetoken`` or any mechanism that changes semantics of module
     # loading is changed. Otherwise cached bytecode may get loaded without
     # the new transformation mechanisms applied.
-    BYTECODEHEADER = b'HG\x00\x06'
+    BYTECODEHEADER = b'HG\x00\x07'
 
     class hgloader(importlib.machinery.SourceFileLoader):
         """Custom module loader that transforms source code.
diff -r b4cd912d7704 -r e815f7c4026f mercurial/pycompat.py
--- a/mercurial/pycompat.py	Thu Mar 02 10:12:40 2017 -0800
+++ b/mercurial/pycompat.py	Fri Mar 03 13:04:32 2017 +0530
@@ -122,6 +122,9 @@ 
         dic = dict((k.encode('latin-1'), v) for k, v in dic.iteritems())
         return dic
 
+    def open(name, mode='r', buffering=-1):
+        return builtins.open(name, sysstr(mode), buffering)
+
     # shlex.split() accepts unicodes on Python 3. This function takes bytes
     # argument, convert it into unicodes, pass into shlex.split(), convert the
     # returned value to bytes and return that.
@@ -131,6 +134,8 @@ 
         return [a.encode('latin-1') for a in ret]
 
 else:
+    import __builtin__
+
     def sysstr(s):
         return s
 
@@ -170,6 +175,7 @@ 
     getcwd = os.getcwd
     sysexecutable = sys.executable
     shlexsplit = shlex.split
+    open = __builtin__.open
 
 stringio = io.StringIO
 empty = _queue.Empty