Patchwork [1,of,5] pycompat: provide 'ispy3' constant

login
register
mail settings
Submitter Yuya Nishihara
Date Sept. 29, 2016, 2:50 p.m.
Message ID <b1e134252685a51f3b2c.1475160626@mimosa>
Download mbox | patch
Permalink /patch/16800/
State Accepted
Headers show

Comments

Yuya Nishihara - Sept. 29, 2016, 2:50 p.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1475060483 -32400
#      Wed Sep 28 20:01:23 2016 +0900
# Node ID b1e134252685a51f3b2c5c3c22540f3ec30effb0
# Parent  ff17dff99295e0781b3f147da2c5e5b14d3436e4
pycompat: provide 'ispy3' constant

We compare version_info at several places, which seems enough to define
a constant.
Pulkit Goyal - Sept. 29, 2016, 3:36 p.m.
Is loading the whole pycompat module just to have this constant is not
an overkill, I mean its not necessary and loading a module will have
effects on runtime I guess.

On Thu, Sep 29, 2016 at 8:20 PM, Yuya Nishihara <yuya@tcha.org> wrote:
> # HG changeset patch
> # User Yuya Nishihara <yuya@tcha.org>
> # Date 1475060483 -32400
> #      Wed Sep 28 20:01:23 2016 +0900
> # Node ID b1e134252685a51f3b2c5c3c22540f3ec30effb0
> # Parent  ff17dff99295e0781b3f147da2c5e5b14d3436e4
> pycompat: provide 'ispy3' constant
>
> We compare version_info at several places, which seems enough to define
> a constant.
>
> diff --git a/mercurial/bundle2.py b/mercurial/bundle2.py
> --- a/mercurial/bundle2.py
> +++ b/mercurial/bundle2.py
> @@ -159,6 +159,7 @@ from . import (
>      error,
>      obsolete,
>      pushkey,
> +    pycompat,
>      tags,
>      url,
>      util,
> @@ -996,7 +997,7 @@ class bundlepart(object):
>              outdebug(ui, 'closing payload chunk')
>              # abort current part payload
>              yield _pack(_fpayloadsize, 0)
> -            if sys.version_info[0] >= 3:
> +            if pycompat.ispy3:
>                  raise exc_info[0](exc_info[1]).with_traceback(exc_info[2])
>              else:
>                  exec("""raise exc_info[0], exc_info[1], exc_info[2]""")
> diff --git a/mercurial/encoding.py b/mercurial/encoding.py
> --- a/mercurial/encoding.py
> +++ b/mercurial/encoding.py
> @@ -10,14 +10,14 @@ from __future__ import absolute_import
>  import array
>  import locale
>  import os
> -import sys
>  import unicodedata
>
>  from . import (
>      error,
> +    pycompat,
>  )
>
> -if sys.version_info[0] >= 3:
> +if pycompat.ispy3:
>      unichr = chr
>
>  # These unicode characters are ignored by HFS+ (Apple Technote 1150,
> @@ -27,7 +27,7 @@ if sys.version_info[0] >= 3:
>             "200c 200d 200e 200f 202a 202b 202c 202d 202e "
>             "206a 206b 206c 206d 206e 206f feff".split()]
>  # verify the next function will work
> -if sys.version_info[0] >= 3:
> +if pycompat.ispy3:
>      assert set(i[0] for i in _ignore) == set([ord(b'\xe2'), ord(b'\xef')])
>  else:
>      assert set(i[0] for i in _ignore) == set(["\xe2", "\xef"])
> diff --git a/mercurial/pycompat.py b/mercurial/pycompat.py
> --- a/mercurial/pycompat.py
> +++ b/mercurial/pycompat.py
> @@ -12,7 +12,9 @@ from __future__ import absolute_import
>
>  import sys
>
> -if sys.version_info[0] < 3:
> +ispy3 = (sys.version_info[0] >= 3)
> +
> +if not ispy3:
>      import cPickle as pickle
>      import cStringIO as io
>      import httplib
> @@ -29,7 +31,7 @@ else:
>      import urllib.parse as urlparse
>      import xmlrpc.client as xmlrpclib
>
> -if sys.version_info[0] >= 3:
> +if ispy3:
>      import builtins
>      import functools
>
> @@ -72,7 +74,7 @@ class _pycompatstub(object):
>  httpserver = _pycompatstub()
>  urlreq = _pycompatstub()
>  urlerr = _pycompatstub()
> -if sys.version_info[0] < 3:
> +if not ispy3:
>      import BaseHTTPServer
>      import CGIHTTPServer
>      import SimpleHTTPServer
> diff --git a/mercurial/util.py b/mercurial/util.py
> --- a/mercurial/util.py
> +++ b/mercurial/util.py
> @@ -232,7 +232,7 @@ class digestchecker(object):
>  try:
>      buffer = buffer
>  except NameError:
> -    if sys.version_info[0] < 3:
> +    if not pycompat.ispy3:
>          def buffer(sliceable, offset=0):
>              return sliceable[offset:]
>      else:
Yuya Nishihara - Sept. 29, 2016, 4:04 p.m.
On Thu, 29 Sep 2016 21:06:09 +0530, Pulkit Goyal wrote:
> Is loading the whole pycompat module just to have this constant is not
> an overkill, I mean its not necessary and loading a module will have
> effects on runtime I guess.

pycompat.py should be loaded at very early stage because one of the core
module, util.py, depends on it. So importing pycompat would be instant.

Patch

diff --git a/mercurial/bundle2.py b/mercurial/bundle2.py
--- a/mercurial/bundle2.py
+++ b/mercurial/bundle2.py
@@ -159,6 +159,7 @@  from . import (
     error,
     obsolete,
     pushkey,
+    pycompat,
     tags,
     url,
     util,
@@ -996,7 +997,7 @@  class bundlepart(object):
             outdebug(ui, 'closing payload chunk')
             # abort current part payload
             yield _pack(_fpayloadsize, 0)
-            if sys.version_info[0] >= 3:
+            if pycompat.ispy3:
                 raise exc_info[0](exc_info[1]).with_traceback(exc_info[2])
             else:
                 exec("""raise exc_info[0], exc_info[1], exc_info[2]""")
diff --git a/mercurial/encoding.py b/mercurial/encoding.py
--- a/mercurial/encoding.py
+++ b/mercurial/encoding.py
@@ -10,14 +10,14 @@  from __future__ import absolute_import
 import array
 import locale
 import os
-import sys
 import unicodedata
 
 from . import (
     error,
+    pycompat,
 )
 
-if sys.version_info[0] >= 3:
+if pycompat.ispy3:
     unichr = chr
 
 # These unicode characters are ignored by HFS+ (Apple Technote 1150,
@@ -27,7 +27,7 @@  if sys.version_info[0] >= 3:
            "200c 200d 200e 200f 202a 202b 202c 202d 202e "
            "206a 206b 206c 206d 206e 206f feff".split()]
 # verify the next function will work
-if sys.version_info[0] >= 3:
+if pycompat.ispy3:
     assert set(i[0] for i in _ignore) == set([ord(b'\xe2'), ord(b'\xef')])
 else:
     assert set(i[0] for i in _ignore) == set(["\xe2", "\xef"])
diff --git a/mercurial/pycompat.py b/mercurial/pycompat.py
--- a/mercurial/pycompat.py
+++ b/mercurial/pycompat.py
@@ -12,7 +12,9 @@  from __future__ import absolute_import
 
 import sys
 
-if sys.version_info[0] < 3:
+ispy3 = (sys.version_info[0] >= 3)
+
+if not ispy3:
     import cPickle as pickle
     import cStringIO as io
     import httplib
@@ -29,7 +31,7 @@  else:
     import urllib.parse as urlparse
     import xmlrpc.client as xmlrpclib
 
-if sys.version_info[0] >= 3:
+if ispy3:
     import builtins
     import functools
 
@@ -72,7 +74,7 @@  class _pycompatstub(object):
 httpserver = _pycompatstub()
 urlreq = _pycompatstub()
 urlerr = _pycompatstub()
-if sys.version_info[0] < 3:
+if not ispy3:
     import BaseHTTPServer
     import CGIHTTPServer
     import SimpleHTTPServer
diff --git a/mercurial/util.py b/mercurial/util.py
--- a/mercurial/util.py
+++ b/mercurial/util.py
@@ -232,7 +232,7 @@  class digestchecker(object):
 try:
     buffer = buffer
 except NameError:
-    if sys.version_info[0] < 3:
+    if not pycompat.ispy3:
         def buffer(sliceable, offset=0):
             return sliceable[offset:]
     else: