jenkins-bot merged this change.

View Change

Approvals: Matěj Suchánek: Looks good to me, approved jenkins-bot: Verified
[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(-)

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)}

To view, visit change 588001. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-MessageType: merged
Gerrit-Change-Id: I840d0bac0a161c9cfa1678e90752078d22147c22
Gerrit-Change-Number: 588001
Gerrit-PatchSet: 9
Gerrit-Owner: Xqt <info@gno.de>
Gerrit-Reviewer: Matěj Suchánek <matejsuchanek97@gmail.com>
Gerrit-Reviewer: Xqt <info@gno.de>
Gerrit-Reviewer: jenkins-bot (75)
Gerrit-CC: Dvorapa <dvorapa@seznam.cz>