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

Pulkit Goyal 7895pulkit at gmail.com
Fri Mar 3 10:04:11 UTC 2017


# HG changeset patch
# User Pulkit Goyal <7895pulkit at 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.

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


More information about the Mercurial-devel mailing list