jenkins-bot has submitted this change and it was merged.
Change subject: [IMPROV] Check version via > 2 or == 2 ......................................................................
[IMPROV] Check version via > 2 or == 2
This unifies the Python version check. Instead of different kind of checks (> 2, == 3, >= 3 or == 2, < 3, <= 2) this always uses the first variant (> 2 and == 2) and if it's like a switch it always does the Python 3 specific code first and the Python 2 specific code in the else branch.
Change-Id: I826fd50b7343ff5f6e775936957cdbb5e18cd40f --- M generate_family_file.py M pwb.py M pywikibot/__init__.py M pywikibot/comms/http.py M pywikibot/comms/threadedhttp.py M pywikibot/data/wikidataquery.py M pywikibot/family.py M pywikibot/i18n.py M pywikibot/page.py M pywikibot/textlib.py M pywikibot/tools.py M pywikibot/userinterfaces/terminal_interface_base.py M pywikibot/weblib.py M tests/aspects.py M tests/http_tests.py M tests/weblib_tests.py 16 files changed, 62 insertions(+), 68 deletions(-)
Approvals: John Vandenberg: Looks good to me, approved jenkins-bot: Verified
diff --git a/generate_family_file.py b/generate_family_file.py index 78e663f..f98de19 100644 --- a/generate_family_file.py +++ b/generate_family_file.py @@ -21,19 +21,20 @@ import codecs from collections import defaultdict from distutils.version import LooseVersion as V - -if sys.version_info[0] == 3: - raw_input = input +import json
# creating & retrieving urls -if sys.version_info[0] == 2: - from urlparse import urlparse, urljoin - import urllib2 - from urllib2 import HTTPError -else: +if sys.version_info[0] > 2: from urllib.parse import urlparse, urljoin from urllib.error import HTTPError import urllib.request as urllib2 + from html.parser import HTMLParser + raw_input = input +else: + from urlparse import urlparse, urljoin + import urllib2 + from urllib2 import HTTPError + from HTMLParser import HTMLParser
def urlopen(url): @@ -43,20 +44,13 @@ ' - https://www.mediawiki.org/wiki/Pywikibot%27%7D) uo = urllib2.urlopen(req) try: - if sys.version_info[0] == 2: - uo.charset = uo.headers.getfirstmatchingheader('Content-Type')[0].strip().split('charset=')[1] - else: + if sys.version_info[0] > 2: uo.charset = uo.headers.get_content_charset() + else: + uo.charset = uo.headers.getfirstmatchingheader('Content-Type')[0].strip().split('charset=')[1] except IndexError: uo.charset = 'latin-1' return uo - -# parsing response data -import json -if sys.version_info[0] == 2: - from HTMLParser import HTMLParser -else: - from html.parser import HTMLParser
class WikiHTMLPageParser(HTMLParser): diff --git a/pwb.py b/pwb.py index 16efbca..3a53ef2 100644 --- a/pwb.py +++ b/pwb.py @@ -56,10 +56,10 @@ main_mod = imp.new_module('__main__') sys.modules['__main__'] = main_mod main_mod.__file__ = filename - if sys.version_info[0] == 2: - main_mod.__builtins__ = sys.modules['__builtin__'] - else: + if sys.version_info[0] > 2: main_mod.builtins = sys.modules['builtins'] + else: + main_mod.__builtins__ = sys.modules['__builtin__']
# Set sys.argv and the first path element properly. old_argv = sys.argv diff --git a/pywikibot/__init__.py b/pywikibot/__init__.py index d70dc86..3dbfb2d 100644 --- a/pywikibot/__init__.py +++ b/pywikibot/__init__.py @@ -16,11 +16,11 @@ import threading import json
-if sys.version_info[0] == 2: - from Queue import Queue -else: +if sys.version_info[0] > 2: from queue import Queue long = int +else: + from Queue import Queue
# Use pywikibot. prefix for all in-package imports; this is to prevent # confusion with similarly-named modules in version 1 framework, for users diff --git a/pywikibot/comms/http.py b/pywikibot/comms/http.py index 8822c6f..92eeb5d 100644 --- a/pywikibot/comms/http.py +++ b/pywikibot/comms/http.py @@ -42,7 +42,14 @@ httplib2.__file__) sys.exit(1)
-if sys.version_info[0] == 2: +if sys.version_info[0] > 2: + from ssl import SSLError as SSLHandshakeError + SSL_CERT_VERIFY_FAILED_MSG = "SSL: CERTIFICATE_VERIFY_FAILED" + import queue as Queue + import urllib.parse as urlparse + from http import cookiejar as cookielib + from urllib.parse import quote +else: if 'SSLHandshakeError' in httplib2.__dict__: from httplib2 import SSLHandshakeError elif httplib2.__version__ == '0.6.0': @@ -57,13 +64,6 @@ import urlparse import cookielib from urllib2 import quote -else: - from ssl import SSLError as SSLHandshakeError - SSL_CERT_VERIFY_FAILED_MSG = "SSL: CERTIFICATE_VERIFY_FAILED" - import queue as Queue - import urllib.parse as urlparse - from http import cookiejar as cookielib - from urllib.parse import quote
from pywikibot import config from pywikibot.exceptions import FatalServerError, Server504Error diff --git a/pywikibot/comms/threadedhttp.py b/pywikibot/comms/threadedhttp.py index c63daa4..4279454 100644 --- a/pywikibot/comms/threadedhttp.py +++ b/pywikibot/comms/threadedhttp.py @@ -25,12 +25,12 @@ import re import threading
-if sys.version_info[0] == 2: - import cookielib - from urllib import splittype, splithost, unquote -else: +if sys.version_info[0] > 2: from http import cookiejar as cookielib from urllib.parse import splittype, splithost, unquote +else: + import cookielib + from urllib import splittype, splithost, unquote
import pywikibot from pywikibot import config diff --git a/pywikibot/data/wikidataquery.py b/pywikibot/data/wikidataquery.py index f808053..4e4f583 100644 --- a/pywikibot/data/wikidataquery.py +++ b/pywikibot/data/wikidataquery.py @@ -7,19 +7,19 @@
import json import sys -if sys.version_info[0] == 2: - from urllib2 import quote -else: +if sys.version_info[0] > 2: from urllib.parse import quote -from pywikibot.comms import http +else: + from urllib2 import quote import pickle import os import hashlib import time import tempfile
-from pywikibot.page import ItemPage, PropertyPage, Claim import pywikibot +from pywikibot.comms import http +from pywikibot.page import ItemPage, PropertyPage, Claim from pywikibot import config
diff --git a/pywikibot/family.py b/pywikibot/family.py index 2beb963..645138a 100644 --- a/pywikibot/family.py +++ b/pywikibot/family.py @@ -14,10 +14,10 @@ import collections import imp
-if sys.version_info[0] == 2: - from urlparse import urlparse -else: +if sys.version_info[0] > 2: from urllib.parse import urlparse +else: + from urlparse import urlparse
import pywikibot from pywikibot import config2 as config diff --git a/pywikibot/i18n.py b/pywikibot/i18n.py index 180b9f4..7e5b2db 100644 --- a/pywikibot/i18n.py +++ b/pywikibot/i18n.py @@ -21,7 +21,7 @@ import pywikibot from . import config2 as config
-if sys.version_info[0] == 3: +if sys.version_info[0] > 2: basestring = (str, )
PLURAL_PATTERN = '{{PLURAL:(?:%()?([^)]*?)(?:)d)?|(.*?)}}' diff --git a/pywikibot/page.py b/pywikibot/page.py index ef678d0..7025a9a 100644 --- a/pywikibot/page.py +++ b/pywikibot/page.py @@ -29,16 +29,16 @@ except ImportError: from ordereddict import OrderedDict
-if sys.version_info[0] == 2: - import htmlentitydefs - from urllib import quote as quote_from_bytes, unquote as unquote_to_bytes - from urllib import urlopen -else: +if sys.version_info[0] > 2: unicode = basestring = str long = int from html import entities as htmlentitydefs from urllib.parse import quote_from_bytes, unquote_to_bytes from urllib.request import urlopen +else: + import htmlentitydefs + from urllib import quote as quote_from_bytes, unquote as unquote_to_bytes + from urllib import urlopen
import pywikibot from pywikibot import config diff --git a/pywikibot/textlib.py b/pywikibot/textlib.py index 87824e3..5c02e4e 100644 --- a/pywikibot/textlib.py +++ b/pywikibot/textlib.py @@ -22,12 +22,12 @@ import re import sys
-if sys.version_info[0] == 2: - from HTMLParser import HTMLParser -else: +if sys.version_info[0] > 2: from html.parser import HTMLParser basestring = (str,) unicode = str +else: + from HTMLParser import HTMLParser
from pywikibot import config2 as config import pywikibot diff --git a/pywikibot/tools.py b/pywikibot/tools.py index 6064d86..f4e3479 100644 --- a/pywikibot/tools.py +++ b/pywikibot/tools.py @@ -41,7 +41,7 @@
"""Mixin class to add __str__ method in Python 2 or 3."""
- if sys.version_info[0] >= 3: + if sys.version_info[0] > 2: def __str__(self): return self.__unicode__() else: diff --git a/pywikibot/userinterfaces/terminal_interface_base.py b/pywikibot/userinterfaces/terminal_interface_base.py index f86859f..ee234e2 100755 --- a/pywikibot/userinterfaces/terminal_interface_base.py +++ b/pywikibot/userinterfaces/terminal_interface_base.py @@ -103,7 +103,7 @@ line, count = colorTagR.subn('', line) if count > 0: line += ' ***' - if sys.version_info[0] < 3: + if sys.version_info[0] == 2: line = line.encode(self.encoding, 'replace') targetStream.write(line)
@@ -173,7 +173,7 @@ self._print(text, targetStream)
def _raw_input(self): - if sys.version_info[0] >= 3: + if sys.version_info[0] > 2: return input() else: return raw_input() @@ -201,7 +201,7 @@ text = self._raw_input() except KeyboardInterrupt: raise pywikibot.QuitKeyboardInterrupt() - if sys.version_info[0] < 3: + if sys.version_info[0] == 2: text = text.decode(self.encoding) return text
diff --git a/pywikibot/weblib.py b/pywikibot/weblib.py index 15b53ef..b608e6d 100644 --- a/pywikibot/weblib.py +++ b/pywikibot/weblib.py @@ -8,10 +8,10 @@ __version__ = '$Id$'
import sys -if sys.version_info[0] == 2: - from urllib import urlencode -else: +if sys.version_info[0] > 2: from urllib.parse import urlencode +else: + from urllib import urlencode
from pywikibot.comms import http
diff --git a/tests/aspects.py b/tests/aspects.py index 0f3b9f1..eb418d9 100644 --- a/tests/aspects.py +++ b/tests/aspects.py @@ -47,7 +47,7 @@
"""Base class for all tests."""
- if sys.version_info[0] < 3: + if sys.version_info[0] == 2: def assertRaisesRegex(self, *args, **kwargs): """ Wrapper of unittest.assertRaisesRegexp for Python 2 unittest. diff --git a/tests/http_tests.py b/tests/http_tests.py index 19c8909..28bb87a 100644 --- a/tests/http_tests.py +++ b/tests/http_tests.py @@ -14,7 +14,7 @@ from tests.aspects import unittest, TestCase from tests.utils import expectedFailureIf
-if sys.version_info[0] == 3: +if sys.version_info[0] > 2: unicode = str
@@ -43,7 +43,7 @@ site=None, uri='https://www.omegawiki.org/')
- @expectedFailureIf(sys.version_info[0] == 3) # bug 72236 + @expectedFailureIf(sys.version_info[0] > 2) # bug 72236 def test_https_ignore_cert_error(self): """Test http request function ignoring ssl bad certificate.""" # As the connection is cached, the above test will cause @@ -102,7 +102,7 @@ self.assertIsInstance(r[0]['status'], str) self.assertEqual(r[0]['status'], '200')
- self.assertIsInstance(r[1], bytes if sys.version_info[0] >= 3 else str) + self.assertIsInstance(r[1], bytes if sys.version_info[0] > 2 else str) self.assertIn(b'<html lang="mul"', r[1]) self.assertEqual(int(r[0]['content-length']), len(r[1]))
@@ -116,7 +116,7 @@ self.assertIsInstance(r[0]['status'], str) self.assertEqual(r[0]['status'], '200')
- self.assertIsInstance(r[1], bytes if sys.version_info[0] >= 3 else str) + self.assertIsInstance(r[1], bytes if sys.version_info[0] > 2 else str) self.assertIn(b'<html lang="mul"', r[1]) self.assertEqual(int(r[0]['content-length']), len(r[1]))
diff --git a/tests/weblib_tests.py b/tests/weblib_tests.py index 14e338a..f4916e7 100644 --- a/tests/weblib_tests.py +++ b/tests/weblib_tests.py @@ -9,10 +9,10 @@
import os import sys -if sys.version_info[0] == 2: - from urlparse import urlparse -else: +if sys.version_info[0] > 2: from urllib.parse import urlparse +else: + from urlparse import urlparse
import pywikibot.weblib as weblib from tests.aspects import unittest, TestCase
pywikibot-commits@lists.wikimedia.org