jenkins-bot has submitted this change and it was merged.
Change subject: [FIX] ui tests: Support Python 3 ......................................................................
[FIX] ui tests: Support Python 3
This uses BytesIO or StringIO depending on the Python version as in Python 3 the sys.std* are supporting strings instead of bytes. The unix output was encoding the text in anyway so this was also fixed.
Not all tests succeed because the test conditions haven't been fixed yet.
Change-Id: Id79817ac743eb5376c2c8accd028b4e39dfe0e8d --- M pywikibot/userinterfaces/terminal_interface_unix.py M tests/ui_tests.py 2 files changed, 46 insertions(+), 27 deletions(-)
Approvals: John Vandenberg: Looks good to me, approved jenkins-bot: Verified
diff --git a/pywikibot/userinterfaces/terminal_interface_unix.py b/pywikibot/userinterfaces/terminal_interface_unix.py index b980cf6..6b6b282 100755 --- a/pywikibot/userinterfaces/terminal_interface_unix.py +++ b/pywikibot/userinterfaces/terminal_interface_unix.py @@ -47,12 +47,14 @@ # just to be sure, reset the color text += unixColors['default']
- # .encoding does not mean we can write unicode - # to the stream pre-2.7. - if sys.version_info >= (2, 7) and \ - hasattr(targetStream, 'encoding') and \ - targetStream.encoding: - text = text.encode(targetStream.encoding, 'replace').decode(targetStream.encoding) - targetStream.write(text) - else: - targetStream.write(text.encode(self.encoding, 'replace')) + if sys.version_info[0] == 2: + # .encoding does not mean we can write unicode + # to the stream pre-2.7. + if (sys.version_info >= (2, 7) and + hasattr(targetStream, 'encoding') and + targetStream.encoding): + text = text.encode(targetStream.encoding, 'replace').decode( + targetStream.encoding) + else: + text = text.encode(self.encoding, 'replace') + targetStream.write(text) diff --git a/tests/ui_tests.py b/tests/ui_tests.py index f792efc..77d7e62 100644 --- a/tests/ui_tests.py +++ b/tests/ui_tests.py @@ -31,12 +31,28 @@ import os import sys import time -from io import StringIO - -from tests.utils import unittest +import io
if sys.version_info[0] > 2: unicode = str + + +class Stream(object): + + """Handler for a StrigIO or BytesIO instance able to patch itself.""" + + def __init__(self, name): + self._stream = io.StringIO() if sys.version_info[0] > 2 else io.BytesIO() + self._name = 'std{0}'.format(name) + self._original = getattr(sys, self._name) + + def patch(self): + setattr(sys, self._name, self._stream) + self._stream.truncate(0) + self._stream.seek(0) + + def unpatch(self): + setattr(sys, self._name, self._original)
if os.name == "nt": from multiprocessing.managers import BaseManager @@ -91,31 +107,33 @@ s.serve_forever()
if __name__ == "__main__": - oldstderr = sys.stderr - oldstdout = sys.stdout - oldstdin = sys.stdin + strout = Stream('out') + strerr = Stream('err') + strin = Stream('in')
- newstdout = StringIO() - newstderr = StringIO() - newstdin = StringIO() + newstdout = strout._stream + newstderr = strerr._stream + newstdin = strin._stream
def patch(): """Patch standard terminal files.""" - sys.stdout = newstdout - sys.stderr = newstderr - sys.stdin = newstdin + strout.patch() + strerr.patch() + strin.patch()
def unpatch(): """un-patch standard terminal files.""" - sys.stdout = oldstdout - sys.stderr = oldstderr - sys.stdin = oldstdin + strout.unpatch() + strerr.unpatch() + strin.unpatch()
try: patch() import pywikibot finally: unpatch() + + from tests.utils import unittest
from pywikibot.bot import DEBUG, VERBOSE, INFO, STDOUT, INPUT, WARNING, ERROR, CRITICAL
@@ -131,9 +149,6 @@
def setUp(self): patch() - newstdout.truncate(0) - newstderr.truncate(0) - newstdin.truncate(0)
pywikibot.config.colorized_output = True pywikibot.config.transliterate = False @@ -525,6 +540,8 @@ unpatch()
else: + from tests.utils import unittest + class TestTerminalUI(unittest.TestCase):
"""Class to show all tests skipped under unittest."""