jenkins-bot has submitted this change and it was merged. ( https://gerrit.wikimedia.org/r/588001 )
Change subject: [bugfix] Fix subprocess results in Python 3 ......................................................................
[bugfix] Fix subprocess results in Python 3
- returncode for a killed process is 1; add it to auto-run exit_codes - don't raise TimeoutError with Python 2 - replace timeout_handler with p.kill - kill process in Python 3 after TimeoutExpired and read stdout_data, stderr_data and returncode in Python 3 - reorder imports - enable lonelypages script tests
Bug: T95385 Change-Id: I840d0bac0a161c9cfa1678e90752078d22147c22 --- M tests/script_tests.py M tests/utils.py 2 files changed, 15 insertions(+), 16 deletions(-)
Approvals: Matěj Suchánek: Looks good to me, approved jenkins-bot: Verified
diff --git a/tests/script_tests.py b/tests/script_tests.py index dac48de..ec11198 100644 --- a/tests/script_tests.py +++ b/tests/script_tests.py @@ -273,7 +273,8 @@
else: # auto-run - exit_codes = [0, -9] + # returncode is 1 if the process is killed + exit_codes = [0, 1, -9] if not out_result and not err_result: unittest_print(' auto-run script unresponsive after ' '{} seconds'.format(timeout), end=' ') @@ -391,7 +392,6 @@ 'imageharvest', # T167726 'misspelling', # T94681 'watchlist', # T77965 - 'lonelypages', # T94680: uses exit code 1 ]
_arguments = '-simulate' diff --git a/tests/utils.py b/tests/utils.py index 89456be..64242f0 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -7,20 +7,14 @@ # from __future__ import absolute_import, division, unicode_literals
-from contextlib import contextmanager import inspect import json import os -from subprocess import PIPE, Popen import sys import warnings
-try: - from collections.abc import Mapping -except ImportError: # Python 2.7 - from collections import Mapping - from multiprocessing import TimeoutError - from threading import Timer +from contextlib import contextmanager +from subprocess import PIPE, Popen from types import ModuleType
try: @@ -39,11 +33,16 @@ PY2, PYTHON_VERSION, UnicodeType as unicode, ) + from tests import _pwb_py, unittest
if not PY2: + from collections.abc import Mapping import six + from subprocess import TimeoutExpired else: + from collections import Mapping + from threading import Timer ResourceWarning = None
@@ -658,18 +657,18 @@ p.stdin.flush() # _communicate() otherwise has a broken pipe
if PY2: # subprocess.communicate does not support timeout - def timeout_handler(): - p.kill() - raise TimeoutError - - timer = Timer(timeout, timeout_handler) + timer = Timer(timeout, p.kill) timer.start() try: stdout_data, stderr_data = p.communicate() finally: timer.cancel() else: - stdout_data, stderr_data = p.communicate(timeout=timeout) + try: + stdout_data, stderr_data = p.communicate(timeout=timeout) + except TimeoutExpired: + p.kill() + stdout_data, stderr_data = p.communicate() return {'exit_code': p.returncode, 'stdout': stdout_data.decode(config.console_encoding), 'stderr': stderr_data.decode(config.console_encoding)}
pywikibot-commits@lists.wikimedia.org