The repr of a Page instance uses the console encoding to encode the title. This causes some operations in Python 2 to fail when it tries to decode the representation back and assumes it's in ASCII encoding.
https://code.djangoproject.com/ticket/18063 provides a very good explanation of the problem and approach being taken by Pywikibot.
Hasten the day of Python 2 being de-supported; until then, we give the best possible output for users of Python 2.
Examples:
>>> import pywikibot >>> p = pywikibot.Page(pywikibot.Site(), u'öäöä') >>> p.title() u'\xf6\xe4\xf6\xe4' >>> '%r' % ([p],) '[Page(\xc3\xb6\xc3\xa4\xc3\xb6\xc3\xa4)]' >>> u'%r' % ([p],) Traceback (most recent call last): File "<stdin>", line 1, in <module> UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 6: ordinal not in range(128)
Here all results using repr or str directly or indirectly on Python 2:
>>> p = pywikibot.Page(s, u'Ümlaut') >>> print(u'Hello: %r!' % [p]) Traceback (most recent call last): File "<stdin>", line 1, in <module> UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 6: ordinal not in range(128) >>> print(u'Hello: %s!' % [p]) Traceback (most recent call last): File "<stdin>", line 1, in <module> UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 6: ordinal not in range(128) >>> print(u'Hello: %r!' % p) Traceback (most recent call last): File "<stdin>", line 1, in <module> UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 5: ordinal not in range(128) >>> print(u'Hello: %s!' % p) Hello: [[test:Ümlaut]]!
The same on Python 3:
>>> p = pywikibot.Page(s, 'Ümlaut') >>> print('Hello: %r!' % [p]) Hello: [Page(b'\xc3\x9cmlaut')]! >>> print('Hello: %s!' % [p]) Hello: [Page(b'\xc3\x9cmlaut')]! >>> print('Hello: %r!' % p) Hello: Page(b'\xc3\x9cmlaut')! >>> print('Hello: %s!' % p) Hello: [[test:Ümlaut]]!
On Windows 7 it can also fail when the console encoding doesn't even support the characters:
>>> import pywikibot as py >>> s = py.Site('af') >>> p = py.Page(s, 'user:xqt') >>> p Page(Gebruiker:Xqt) >>> s = py.Site('fa') >>> p = py.Page(s, 'user:xqt') >>> p Traceback (most recent call last): File "<pyshell#12>", line 1, in <module> p File "pywikibot\page.py", line 224, in __repr__ self.title().encode(config.console_encoding)) File "C:\Python27\lib\encodings\cp850.py", line 12, in encode return codecs.charmap_encode(input,errors,encoding_map) UnicodeEncodeError: 'charmap' codec can't encode characters in position 0-4: character maps to <undefined>