Patchwork [3,of,3] util: make util.py more demandimport friendly

login
register
mail settings
Submitter Pulkit Goyal
Date July 14, 2016, 9:05 a.m.
Message ID <98d10404151317b62c84.1468487109@pulkit-goyal.Home>
Download mbox | patch
Permalink /patch/15848/
State Changes Requested
Headers show

Comments

Pulkit Goyal - July 14, 2016, 9:05 a.m.
# HG changeset patch
# User Pulkit Goyal <7895pulkit@gmail.com>
# Date 1468438157 -19800
#      Thu Jul 14 00:59:17 2016 +0530
# Node ID 98d10404151317b62c84832111f851e89c29214f
# Parent  9f7d3de1e00bb71977e58990d637ceb590993fee
util: make util.py more demandimport friendly

There are modules which are renamed it Python 3, we used to add a hack in pycompat.py
and then import it in util, now we have a if-else which checks the python version and imports
 the required module.
Yuya Nishihara - July 14, 2016, 2:52 p.m.
On Thu, 14 Jul 2016 14:35:09 +0530, Pulkit Goyal wrote:
> # HG changeset patch
> # User Pulkit Goyal <7895pulkit@gmail.com>
> # Date 1468438157 -19800
> #      Thu Jul 14 00:59:17 2016 +0530
> # Node ID 98d10404151317b62c84832111f851e89c29214f
> # Parent  9f7d3de1e00bb71977e58990d637ceb590993fee
> util: make util.py more demandimport friendly
> 
> There are modules which are renamed it Python 3, we used to add a hack in pycompat.py
> and then import it in util, now we have a if-else which checks the python version and imports
>  the required module.
> 
> diff -r 9f7d3de1e00b -r 98d104041513 mercurial/util.py
> --- a/mercurial/util.py	Wed Jul 13 23:38:29 2016 +0530
> +++ b/mercurial/util.py	Thu Jul 14 00:59:17 2016 +0530
> @@ -45,19 +45,37 @@
>      pycompat,
>  )
>  
> +if sys.version_info[0] < 3:
> +    import cPickle as pickle
> +    import cStringIO as io
> +    import httplib
> +    import Queue as _queue
> +    import SocketServer as socketserver
> +    import urlparse
> +    import xmlrpclib
> +else:
> +    import http.client as httplib
> +    import io
> +    import pickle
> +    import queue as _queue
> +    import socketserver
> +    import urllib.parse as urlparse
> +    import xmlrpc.client as xmlrpclib

Perhaps we want this in pycompat.py.

> +empty = _queue.Empty
> +queue = _queue.Queue
> +stringio = io.StringIO
> +pickle.dumps
> +httplib.HTTPException
> +socketserver.ThreadingMixIn
> +xmlrpclib.Transport
> +urlparse.urlparse

And these lines are the reason why pycompat.py is demandimport unfriendly.
They do import modules.

Patch

diff -r 9f7d3de1e00b -r 98d104041513 mercurial/util.py
--- a/mercurial/util.py	Wed Jul 13 23:38:29 2016 +0530
+++ b/mercurial/util.py	Thu Jul 14 00:59:17 2016 +0530
@@ -45,19 +45,37 @@ 
     pycompat,
 )
 
+if sys.version_info[0] < 3:
+    import cPickle as pickle
+    import cStringIO as io
+    import httplib
+    import Queue as _queue
+    import SocketServer as socketserver
+    import urlparse
+    import xmlrpclib
+else:
+    import http.client as httplib
+    import io
+    import pickle
+    import queue as _queue
+    import socketserver
+    import urllib.parse as urlparse
+    import xmlrpc.client as xmlrpclib
+
+empty = _queue.Empty
+queue = _queue.Queue
+stringio = io.StringIO
+pickle.dumps
+httplib.HTTPException
+socketserver.ThreadingMixIn
+xmlrpclib.Transport
+urlparse.urlparse
+
 for attr in (
-    'empty',
-    'httplib',
     'httpserver',
-    'pickle',
-    'queue',
     'urlerr',
-    'urlparse',
     # we do import urlreq, but we do it outside the loop
     #'urlreq',
-    'stringio',
-    'socketserver',
-    'xmlrpclib',
 ):
     globals()[attr] = getattr(pycompat, attr)