comparison mercurial/pycompat.py @ 29584:06587edd1233

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.
author Pulkit Goyal <7895pulkit@gmail.com>
date Sun, 17 Jul 2016 19:48:04 +0530
parents 075146e85bb6
children 594035c1adc7
comparison
equal deleted inserted replaced
29583:6ce870dba6fa 29584:06587edd1233
8 This contains aliases to hide python version-specific details from the core. 8 This contains aliases to hide python version-specific details from the core.
9 """ 9 """
10 10
11 from __future__ import absolute_import 11 from __future__ import absolute_import
12 12
13 try: 13 import sys
14
15 if sys.version_info[0] < 3:
14 import cPickle as pickle 16 import cPickle as pickle
15 pickle.dumps 17 import cStringIO as io
16 except ImportError: 18 import httplib
19 import Queue as _queue
20 import SocketServer as socketserver
21 import urlparse
22 import xmlrpclib
23 else:
24 import http.client as httplib
25 import io
17 import pickle 26 import pickle
18 pickle.dumps # silence pyflakes 27 import queue as _queue
28 import socketserver
29 import urllib.parse as urlparse
30 import xmlrpc.client as xmlrpclib
19 31
20 try: 32 stringio = io.StringIO
21 import httplib
22 httplib.HTTPException
23 except ImportError:
24 import http.client as httplib
25 httplib.HTTPException
26
27 try:
28 import SocketServer as socketserver
29 socketserver.ThreadingMixIn
30 except ImportError:
31 import socketserver
32 socketserver.ThreadingMixIn
33
34 try:
35 import xmlrpclib
36 xmlrpclib.Transport
37 except ImportError:
38 import xmlrpc.client as xmlrpclib
39 xmlrpclib.Transport
40
41 try:
42 import urlparse
43 urlparse.urlparse
44 except ImportError:
45 import urllib.parse as urlparse
46 urlparse.urlparse
47
48 try:
49 import cStringIO as io
50 stringio = io.StringIO
51 except ImportError:
52 import io
53 stringio = io.StringIO
54
55 try:
56 import Queue as _queue
57 _queue.Queue
58 except ImportError:
59 import queue as _queue
60 empty = _queue.Empty 33 empty = _queue.Empty
61 queue = _queue.Queue 34 queue = _queue.Queue
62 35
63 class _pycompatstub(object): 36 class _pycompatstub(object):
64 pass 37 pass