XZise has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/182135

Change subject: [FIX] Fix issues by flake8 on Python 3
......................................................................

[FIX] Fix issues by flake8 on Python 3

Some scripts were still using Python 2 specific code which is now
removed. If possible they were rewritten to support both versions
without extra conditions.

Unfortunately flake8 doesn't detect that certain code is never run in
Python 3. Those lines are just marked with '# noqa' to ignore them
(unfortunately it's not possible to # noqa them just for Python 3).

Change-Id: Ie1e676056ffce9fdabc203442f4b86a88013fe6f
---
M pywikibot/exceptions.py
M pywikibot/page.py
M pywikibot/userinterfaces/gui.py
M pywikibot/userinterfaces/terminal_interface_base.py
M scripts/archivebot.py
M scripts/featured.py
M scripts/protect.py
M scripts/solve_disambiguation.py
M scripts/weblinkchecker.py
M scripts/welcome.py
M tests/ui_tests.py
11 files changed, 26 insertions(+), 16 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/pywikibot/core 
refs/changes/35/182135/1

diff --git a/pywikibot/exceptions.py b/pywikibot/exceptions.py
index 64a93ac..2a948c5 100644
--- a/pywikibot/exceptions.py
+++ b/pywikibot/exceptions.py
@@ -53,9 +53,13 @@
 #
 __version__ = '$Id$'
 
+import sys
 
 from pywikibot.tools import UnicodeMixin
 
+if sys.version_info[0] > 2:
+    unicode = str
+
 
 class Error(UnicodeMixin, Exception):  # noqa
 
diff --git a/pywikibot/page.py b/pywikibot/page.py
index 918d5da..f8f2c47 100644
--- a/pywikibot/page.py
+++ b/pywikibot/page.py
@@ -36,6 +36,7 @@
     from urllib.parse import quote_from_bytes, unquote_to_bytes
     from urllib.request import urlopen
 else:
+    chr = unichr  # noqa
     import htmlentitydefs
     from urllib import quote as quote_from_bytes, unquote as unquote_to_bytes
     from urllib import urlopen
@@ -4512,7 +4513,7 @@
     entityR = re.compile(
         
r'&(?:amp;)?(#(?P<decimal>\d+)|#x(?P<hex>[0-9a-fA-F]+)|(?P<name>[A-Za-z]+));')
     # These characters are Html-illegal, but sadly you *can* find some of
-    # these and converting them to unichr(decimal) is unsuitable
+    # these and converting them to chr(decimal) is unsuitable
     convertIllegalHtmlEntities = {
         128: 8364,  # €
         130: 8218,  # ‚
@@ -4543,7 +4544,7 @@
         159: 376    # Ÿ
     }
     # ensuring that illegal &#129; &#141; and &#157, which have no known 
values,
-    # don't get converted to unichr(129), unichr(141) or unichr(157)
+    # don't get converted to chr(129), chr(141) or chr(157)
     ignore = set(ignore) | set([129, 141, 157])
     result = u''
     i = 0
@@ -4568,14 +4569,12 @@
             except KeyError:
                 pass
             if unicodeCodepoint and unicodeCodepoint not in ignore:
-                if sys.version_info[0] > 2:
-                    result += chr(unicodeCodepoint)
-                elif unicodeCodepoint > sys.maxunicode:
+                if unicodeCodepoint > sys.maxunicode:
                     # solve narrow Python 2 build exception (UTF-16)
                     unicode_literal = lambda n: eval(r"u'\U%08x'" % n)
                     result += unicode_literal(unicodeCodepoint)
                 else:
-                    result += unichr(unicodeCodepoint)
+                    result += chr(unicodeCodepoint)
             else:
                 # Leave the entity unchanged
                 result += text[match.start():match.end()]
diff --git a/pywikibot/userinterfaces/gui.py b/pywikibot/userinterfaces/gui.py
index 0d8123f..8c6e173 100644
--- a/pywikibot/userinterfaces/gui.py
+++ b/pywikibot/userinterfaces/gui.py
@@ -386,8 +386,8 @@
         # if the editbox contains ASCII characters only, get() will
         # return string, otherwise unicode (very annoying). We only want
         # it to return unicode, so we work around this.
-        if isinstance(self.text, str):
-            self.text = unicode(self.text)
+        if sys.version[0] == 2 and isinstance(self.text, str):
+            self.text = unicode(self.text)  # noqa
         self.parent.destroy()
 
     def debug(self, event=None):
diff --git a/pywikibot/userinterfaces/terminal_interface_base.py 
b/pywikibot/userinterfaces/terminal_interface_base.py
index bc6fc08..6d2ceea 100755
--- a/pywikibot/userinterfaces/terminal_interface_base.py
+++ b/pywikibot/userinterfaces/terminal_interface_base.py
@@ -189,7 +189,7 @@
         if sys.version_info[0] > 2:
             return input()
         else:
-            return raw_input()
+            return raw_input()  # noqa
 
     def input(self, question, password=False):
         """
diff --git a/scripts/archivebot.py b/scripts/archivebot.py
index e0e607e..588e3be 100644
--- a/scripts/archivebot.py
+++ b/scripts/archivebot.py
@@ -635,7 +635,7 @@
             for pg in generate_transclusions(site, a, ns):
                 pagelist.append(pg)
         if filename:
-            for pg in file(filename, 'r').readlines():
+            for pg in open(filename, 'r').readlines():
                 pagelist.append(pywikibot.Page(site, pg, ns=10))
         if pagename:
             pagelist.append(pywikibot.Page(site, pagename, ns=3))
diff --git a/scripts/featured.py b/scripts/featured.py
index 522bae4..7f9f8bd 100644
--- a/scripts/featured.py
+++ b/scripts/featured.py
@@ -592,7 +592,7 @@
                 pywikibot.output(u"(already removed)")
         if changed:
             comment = i18n.twtranslate(tosite, 'featured-' + task,
-                                       {'page': unicode(source)})
+                                       {'page': source})
             try:
                 dest.put(text, comment)
             except pywikibot.LockedPage:
diff --git a/scripts/protect.py b/scripts/protect.py
index 65f9787..c8503bd 100644
--- a/scripts/protect.py
+++ b/scripts/protect.py
@@ -123,7 +123,7 @@
                     first_char.append(c)
                     break
             else:
-                first_char.append(unicode(num))
+                first_char.append(str(num))
                 num += 1
             if level == default:
                 default_char = first_char[-1]
diff --git a/scripts/solve_disambiguation.py b/scripts/solve_disambiguation.py
index ef9ab3a..83d1050 100644
--- a/scripts/solve_disambiguation.py
+++ b/scripts/solve_disambiguation.py
@@ -1004,7 +1004,7 @@
             self.makeAlternativesUnique()
             # sort possible choices
             if config.sort_ignore_case:
-                self.alternatives.sort(lambda x, y: cmp(x.lower(), y.lower()))
+                self.alternatives.sort(key=lambda x: x.lower())
             else:
                 self.alternatives.sort()
             self.listAlternatives()
diff --git a/scripts/weblinkchecker.py b/scripts/weblinkchecker.py
index 5cc4cf8..4bc522f 100644
--- a/scripts/weblinkchecker.py
+++ b/scripts/weblinkchecker.py
@@ -114,6 +114,7 @@
     import urllib.request as urllib
     import http.client as httplib
     basestring = (str, )
+    unicode = str
 else:
     import urlparse
     import urllib
@@ -413,7 +414,7 @@
             wasRedirected = self.resolveRedirect(useHEAD=useHEAD)
         except UnicodeError as error:
             return False, u'Encoding Error: %s (%s)' % (
-                error.__class__.__name__, unicode(error))
+                error.__class__.__name__, error)
         except httplib.error as error:
             return False, u'HTTP Error: %s' % error.__class__.__name__
         except socket.error as error:
diff --git a/scripts/welcome.py b/scripts/welcome.py
index 0ae8350..3bf43a4 100644
--- a/scripts/welcome.py
+++ b/scripts/welcome.py
@@ -190,6 +190,9 @@
 from pywikibot import i18n
 from pywikibot import config
 
+if sys.version_info[0] > 2:
+    unicode = str
+
 locale.setlocale(locale.LC_ALL, '')
 
 # Script uses the method i18n.translate() to find the right
@@ -835,8 +838,8 @@
                                           time.gmtime()),
                             locale.getlocale()[1])
                     else:
-                        strfstr = unicode(time.strftime(
-                            u"%d %b %Y %H:%M:%S (UTC)", time.gmtime()))
+                        strfstr = time.strftime(
+                            u"%d %b %Y %H:%M:%S (UTC)", time.gmtime())
                     pywikibot.output(u'Sleeping %d seconds before rerun. %s'
                                      % (globalvar.timeRecur, strfstr))
                     time.sleep(globalvar.timeRecur)
diff --git a/tests/ui_tests.py b/tests/ui_tests.py
index e5a053e..f792efc 100644
--- a/tests/ui_tests.py
+++ b/tests/ui_tests.py
@@ -35,6 +35,9 @@
 
 from tests.utils import unittest
 
+if sys.version_info[0] > 2:
+    unicode = str
+
 if os.name == "nt":
     from multiprocessing.managers import BaseManager
     import threading

-- 
To view, visit https://gerrit.wikimedia.org/r/182135
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ie1e676056ffce9fdabc203442f4b86a88013fe6f
Gerrit-PatchSet: 1
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Owner: XZise <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to