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 --- main.py | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) (limited to 'main.py') diff --git a/main.py b/main.py index 49d24823..e4cdeb0f 100755 --- a/main.py +++ b/main.py @@ -22,13 +22,12 @@ import optparse import os import sys import time -try: - import urllib2 -except ImportError: - # For python3 + +from pyversion import is_python3 +if is_python3(): import urllib.request else: - # For python2 + import urllib2 urllib = imp.new_module('urllib') urllib.request = urllib2 @@ -50,10 +49,10 @@ from pager import RunPager from subcmds import all_commands -try: +if not is_python3(): + # pylint:disable=W0622 input = raw_input -except NameError: - pass + # pylint:enable=W0622 global_options = optparse.OptionParser( usage="repo [-p|--paginate|--no-pager] COMMAND [ARGS]" -- cgit v1.2.3-54-g00ecf