[PATCH 8 of 9] pycompat: put single line things above class and function definitions

Pulkit Goyal 7895pulkit at gmail.com
Thu Jun 15 17:34:50 EDT 2017


# HG changeset patch
# User Pulkit Goyal <7895pulkit at gmail.com>
# Date 1497561930 -19800
#      Fri Jun 16 02:55:30 2017 +0530
# Node ID c95d3227e37cfbbb2c687dad98bc978d063c624f
# Parent  66940f7bf570ebb3a3a43c556e6887dab28c19a4
pycompat: put single line things above class and function definitions

Earlier, there are some single line assignments initially then a function, then
some more single line assignments, then again few functions. This patch gathers
all those single line assignments at one place and make sure functions are
continuous so that it's easy to read code. Therefore it also moves the wrapper
above the bytestr class.

diff --git a/mercurial/pycompat.py b/mercurial/pycompat.py
--- a/mercurial/pycompat.py
+++ b/mercurial/pycompat.py
@@ -78,6 +78,20 @@
 
     bytechr = struct.Struct('>B').pack
 
+    def _wrapattrfunc(f):
+        @functools.wraps(f)
+        def w(object, name, *args):
+            return f(object, sysstr(name), *args)
+        return w
+
+    # these wrappers are automagically imported by hgloader
+    delattr = _wrapattrfunc(builtins.delattr)
+    getattr = _wrapattrfunc(builtins.getattr)
+    hasattr = _wrapattrfunc(builtins.hasattr)
+    setattr = _wrapattrfunc(builtins.setattr)
+    xrange = builtins.range
+    unicode = str
+
     class bytestr(bytes):
         """A bytes which mostly acts as a Python 2 str
 
@@ -192,20 +206,6 @@
             return doc
         return sysbytes(doc)
 
-    def _wrapattrfunc(f):
-        @functools.wraps(f)
-        def w(object, name, *args):
-            return f(object, sysstr(name), *args)
-        return w
-
-    # these wrappers are automagically imported by hgloader
-    delattr = _wrapattrfunc(builtins.delattr)
-    getattr = _wrapattrfunc(builtins.getattr)
-    hasattr = _wrapattrfunc(builtins.hasattr)
-    setattr = _wrapattrfunc(builtins.setattr)
-    xrange = builtins.range
-    unicode = str
-
     def open(name, mode='r', buffering=-1):
         return builtins.open(name, sysstr(mode), buffering)
 
@@ -262,6 +262,25 @@
     sysstr = identity
     strurl = identity
     bytesurl = identity
+    # 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.
+    fsdecode = identity
+    strkwargs = identity
+    byteskwargs = identity
+    oslinesep = os.linesep
+    osname = os.name
+    ospathsep = os.pathsep
+    ossep = os.sep
+    osaltsep = os.altsep
+    stdin = sys.stdin
+    stdout = sys.stdout
+    stderr = sys.stderr
+    sysplatform = sys.platform
+    getcwd = os.getcwd
+    sysexecutable = sys.executable
+    shlexsplit = shlex.split
+    stringio = cStringIO.StringIO
+    maplist = map
 
     # this can't be parsed on Python 3
     exec('def raisewithtb(exc, tb):\n'
@@ -279,35 +298,14 @@
             raise TypeError(
                 "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.
-    fsdecode = identity
-
     def getdoc(obj):
         return getattr(obj, '__doc__', None)
 
     def getoptb(args, shortlist, namelist):
         return getopt.getopt(args, shortlist, namelist)
 
-    strkwargs = identity
-    byteskwargs = identity
-
-    oslinesep = os.linesep
-    osname = os.name
-    ospathsep = os.pathsep
-    ossep = os.sep
-    osaltsep = os.altsep
-    stdin = sys.stdin
-    stdout = sys.stdout
-    stderr = sys.stderr
     if getattr(sys, 'argv', None) is not None:
         sysargv = sys.argv
-    sysplatform = sys.platform
-    getcwd = os.getcwd
-    sysexecutable = sys.executable
-    shlexsplit = shlex.split
-    stringio = cStringIO.StringIO
-    maplist = map
 
 empty = _queue.Empty
 queue = _queue.Queue


More information about the Mercurial-devel mailing list