[PATCH] pycompat: make pycompat demandimport friendly

Pulkit Goyal 7895pulkit at gmail.com
Sun Jul 17 14:18:31 UTC 2016


# HG changeset patch
# User Pulkit Goyal <7895pulkit at gmail.com>
# Date 1468765084 -19800
#      Sun Jul 17 19:48:04 2016 +0530
# Node ID f0f412e7b4eb6b40105f8c3681e355c4d7fb4139
# Parent  1c22400db72de4bbeb992fde534c0abbe1367b03
pycompat: make pycompat demandimport friendly

pycompat.py includes hack to import modules whose names are changed in Python 3.
 We uses 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.

diff -r 1c22400db72d -r f0f412e7b4eb mercurial/pycompat.py
--- a/mercurial/pycompat.py	Mon Jul 04 11:18:03 2016 -0700
+++ 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 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
-    pickle.dumps # silence pyflakes
+    import queue as _queue
+    import socketserver
+    import urllib.parse as urlparse
+    import xmlrpc.client as xmlrpclib
 
-try:
-    import httplib
-    httplib.HTTPException
-except ImportError:
-    import http.client as httplib
-    httplib.HTTPException
-
-try:
-    import SocketServer as socketserver
-    socketserver.ThreadingMixIn
-except ImportError:
-    import socketserver
-    socketserver.ThreadingMixIn
-
-try:
-    import xmlrpclib
-    xmlrpclib.Transport
-except ImportError:
-    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
 
diff -r 1c22400db72d -r f0f412e7b4eb tests/test-check-pyflakes.t
--- a/tests/test-check-pyflakes.t	Mon Jul 04 11:18:03 2016 -0700
+++ b/tests/test-check-pyflakes.t	Sun Jul 17 19:48:04 2016 +0530
@@ -7,6 +7,7 @@
 (skipping binary file random-seed)
 
   $ hg locate 'set:**.py or grep("^#!.*python")' 2>/dev/null \
+  > -X mercurial/pycompat.py \
   > | xargs pyflakes 2>/dev/null | "$TESTDIR/filterpyflakes.py"
   tests/filterpyflakes.py:61: undefined name 'undefinedname'
   


More information about the Mercurial-devel mailing list