From 59bbb580e34bbc5dce76dacaad9ff94f21fa396f Mon Sep 17 00:00:00 2001 From: David Pursehouse Date: Fri, 17 May 2013 10:49:33 +0900 Subject: Move Python version checking to a separate module Add a new module with methods for checking the Python version. Instead of handling Python3 imports with try...except blocks, first check the python version and then import the relevant modules. This makes the code a bit cleaner and will result in less diff when/if we remove support for Python < 3 later. Use the same mechanism to handle `input` vs. `raw_input` and add suppression of pylint warnings caused by redefinition of the built-in method `input`. Change-Id: Ia403e525b88d77640a741ac50382146e7d635924 Also-by: Chirayu Desai Signed-off-by: Chirayu Desai --- subcmds/init.py | 8 ++++---- subcmds/status.py | 11 ++++++----- subcmds/sync.py | 17 ++++++----------- subcmds/upload.py | 7 ++++--- 4 files changed, 20 insertions(+), 23 deletions(-) (limited to 'subcmds') diff --git a/subcmds/init.py b/subcmds/init.py index 1f915268..a44fb7a9 100644 --- a/subcmds/init.py +++ b/subcmds/init.py @@ -19,11 +19,11 @@ import platform import re import shutil import sys -try: - # For python3 + +from pyversion import is_python3 +if is_python3(): import urllib.parse -except ImportError: - # For python2 +else: import imp import urlparse urllib = imp.new_module('urllib') diff --git a/subcmds/status.py b/subcmds/status.py index 9810337f..41c4429a 100644 --- a/subcmds/status.py +++ b/subcmds/status.py @@ -21,12 +21,13 @@ except ImportError: import dummy_threading as _threading import glob -try: - # For python2 - import StringIO as io -except ImportError: - # For python3 + +from pyversion import is_python3 +if is_python3(): import io +else: + import StringIO as io + import itertools import os import sys diff --git a/subcmds/sync.py b/subcmds/sync.py index 8fb94885..b34787d2 100644 --- a/subcmds/sync.py +++ b/subcmds/sync.py @@ -24,22 +24,17 @@ import socket import subprocess import sys import time -try: - # For python3 + +from pyversion import is_python3 +if is_python3(): import urllib.parse -except ImportError: - # For python2 + import xmlrpc.client +else: import imp import urlparse + import xmlrpclib urllib = imp.new_module('urllib') urllib.parse = urlparse -try: - # For python3 - import xmlrpc.client -except ImportError: - # For python2 - import imp - import xmlrpclib xmlrpc = imp.new_module('xmlrpc') xmlrpc.client = xmlrpclib diff --git a/subcmds/upload.py b/subcmds/upload.py index a34938e5..13b87784 100644 --- a/subcmds/upload.py +++ b/subcmds/upload.py @@ -23,10 +23,11 @@ from editor import Editor from error import HookError, UploadError from project import RepoHook -try: +from pyversion import is_python3 +if not is_python3(): + # pylint:disable=W0622 input = raw_input -except NameError: - pass + # pylint:enable=W0622 UNUSUAL_COMMIT_THRESHOLD = 5 -- cgit v1.2.3-54-g00ecf