jenkins-bot has submitted this change and it was merged.
Change subject: Make __str__ python3 compatible ......................................................................
Make __str__ python3 compatible
To make a python 2 & 3 compatible code base, we need to have a __str__ function that returns bytes on python 2 and unicode on python 3. This Unicodemixin handles this, by using __unicode__, and encoding that to utf-8 to get bytes.
Change-Id: I7229b884b630c0d05e2ebe171edec81511d7940b --- M pywikibot/exceptions.py M pywikibot/page.py M pywikibot/tools.py 3 files changed, 22 insertions(+), 18 deletions(-)
Approvals: Xqt: Looks good to me, approved jenkins-bot: Verified
diff --git a/pywikibot/exceptions.py b/pywikibot/exceptions.py index f029147..7d4ba61 100644 --- a/pywikibot/exceptions.py +++ b/pywikibot/exceptions.py @@ -10,23 +10,16 @@ __version__ = '$Id$'
-from pywikibot import config +from pywikibot.tools import UnicodeMixin
# TODO: These are copied from wikipedia.py; not certain that all of them # will be needed in the rewrite.
-class Error(Exception): +class Error(Exception, UnicodeMixin): """Wikipedia error""" def __init__(self, arg): self.unicode = arg - try: - self.string = arg.encode(config.console_encoding, "xmlcharrefreplace") - except (AttributeError, TypeError): - self.string = arg.encode("ascii", "xmlcharrefreplace") - - def __str__(self): - return self.string
def __unicode__(self): return self.unicode diff --git a/pywikibot/page.py b/pywikibot/page.py index 1b9ec62..5f68c9c 100644 --- a/pywikibot/page.py +++ b/pywikibot/page.py @@ -42,7 +42,7 @@ # Note: Link objects (defined later on) represent a wiki-page's title, while # Page objects (defined here) represent the page itself, including its contents.
-class Page(object): +class Page(pywikibot.UnicodeMixin): """Page: A MediaWiki page
This object only implements internally methods that do not require @@ -214,12 +214,6 @@
""" return self._link.section - - def __str__(self): - """Return a console representation of the pagelink.""" - return self.title(asLink=True, forceInterwiki=True - ).encode(config.console_encoding, - "xmlcharrefreplace")
def __unicode__(self): return self.title(asLink=True, forceInterwiki=True) diff --git a/pywikibot/tools.py b/pywikibot/tools.py index 299a248..3b5671b 100644 --- a/pywikibot/tools.py +++ b/pywikibot/tools.py @@ -10,7 +10,24 @@ import sys import threading import time -import Queue + +if sys.version_info[0] > 2: + import queue as Queue +else: + import Queue + + +class UnicodeMixin(object): + """Mixin class to handle defining the proper __str__/__unicode__ + methods in Python 2 or 3. + """ + + if sys.version_info[0] >= 3: + def __str__(self): + return self.__unicode__() + else: + def __str__(self): + return self.__unicode__().encode('utf8')
class ThreadedGenerator(threading.Thread): @@ -141,7 +158,7 @@ ...
""" - def __init__(self, limit=sys.maxint, *args): + def __init__(self, limit=128, *args): self.limit = limit list.__init__(self, *args) for item in list(self):
pywikibot-commits@lists.wikimedia.org