pycompat: make pycompat demandimport friendly
authorPulkit Goyal <7895pulkit@gmail.com>
Sun, 17 Jul 2016 19:48:04 +0530
changeset 29584 06587edd1233
parent 29583 6ce870dba6fa
child 29585 6ed452d0f1f1
pycompat: make pycompat demandimport friendly pycompat.py includes hack to import modules whose names are changed in Python 3. We use try-except to load module according to the version of python. But this method forces us to import the modules to raise an ImportError and hence making it demandimport unfriendly. This patch changes the try-except blocks to a single if-else block. To avoid test-check-pyflakes.t complain about unused imports, pycompat.py is excluded from the test.
mercurial/pycompat.py
tests/test-check-pyflakes.t
--- a/mercurial/pycompat.py	Mon Jul 18 08:55:30 2016 +0100
+++ b/mercurial/pycompat.py	Sun Jul 17 19:48:04 2016 +0530
@@ -10,53 +10,26 @@
 
 from __future__ import absolute_import
 
-try:
+import sys
+
+if sys.version_info[0] < 3:
     import cPickle as pickle
-    pickle.dumps
-except ImportError:
-    import pickle
-    pickle.dumps # silence pyflakes
-
-try:
+    import cStringIO as io
     import httplib
-    httplib.HTTPException
-except ImportError:
-    import http.client as httplib
-    httplib.HTTPException
-
-try:
+    import Queue as _queue
     import SocketServer as socketserver
-    socketserver.ThreadingMixIn
-except ImportError:
-    import socketserver
-    socketserver.ThreadingMixIn
-
-try:
+    import urlparse
     import xmlrpclib
-    xmlrpclib.Transport
-except ImportError:
+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
-    xmlrpclib.Transport
-
-try:
-    import urlparse
-    urlparse.urlparse
-except ImportError:
-    import urllib.parse as urlparse
-    urlparse.urlparse
 
-try:
-    import cStringIO as io
-    stringio = io.StringIO
-except ImportError:
-    import io
-    stringio = io.StringIO
-
-try:
-    import Queue as _queue
-    _queue.Queue
-except ImportError:
-    import queue as _queue
+stringio = io.StringIO
 empty = _queue.Empty
 queue = _queue.Queue
 
--- a/tests/test-check-pyflakes.t	Mon Jul 18 08:55:30 2016 +0100
+++ b/tests/test-check-pyflakes.t	Sun Jul 17 19:48:04 2016 +0530
@@ -6,7 +6,9 @@
 run pyflakes on all tracked files ending in .py or without a file ending
 (skipping binary file random-seed)
 
-  $ hg locate 'set:**.py or grep("^#!.*python")' 2>/dev/null \
+  $ hg locate 'set:**.py or grep("^#!.*python")' \
+  > -X mercurial/pycompat.py \
+  > 2>/dev/null \
   > | xargs pyflakes 2>/dev/null | "$TESTDIR/filterpyflakes.py"
   tests/filterpyflakes.py:61: undefined name 'undefinedname'