Page MenuHomePhabricator

TestPwb.test_one_similar_script: io.StringIO is empty in Python 2.7
Closed, ResolvedPublicBUG REPORT

Description

io.StringIO object in TestPwb.test_one_similar_script is empty in Python 2 and 3.
fasttests always passes. Also in Travis.

https://ci.appveyor.com/project/ladsgroup/pywikibot-g4xqx/build/job/3e3efiqk6k0l5627

C:\pwb\GIT\core>py -3 pwb.py pwb_tests -v
tests: max_retries reduced from 15 to 1
test_env (__main__.TestPwb) ... ok
test_locals (__main__.TestPwb) ... ok
test_one_similar_script (__main__.TestPwb)
Test shell.py script call which gives one similar result. ... ok
test_script_found (__main__.TestPwb)
Test pwb.py script call which is found. ... ok
test_script_not_found (__main__.TestPwb)
Test pwbot.py script call which is not found. ... ok
test_similar_scripts_found (__main__.TestPwb)
Test script call which gives multiple similar results. ... ok
test_unicode (__main__.TestPwb)
Test printing unicode in pywikibot. ... ok

----------------------------------------------------------------------
Ran 7 tests in 13.475s

OK


C:\pwb\GIT\core>py -2 pwb.py pwb_tests -v
tests: max_retries reduced from 15 to 1
test_env (__main__.TestPwb) ... ok
test_locals (__main__.TestPwb) ... ok
test_one_similar_script (__main__.TestPwb)
Test shell.py script call which gives one similar result. ... FAIL
test_script_found (__main__.TestPwb)
Test pwb.py script call which is found. ... ok
test_script_not_found (__main__.TestPwb)
Test pwbot.py script call which is not found. ... ok
test_similar_scripts_found (__main__.TestPwb)
Test script call which gives multiple similar results. ... ok
test_unicode (__main__.TestPwb)
Test printing unicode in pywikibot. ... FAIL

======================================================================
FAIL: test_one_similar_script (__main__.TestPwb)
Test shell.py script call which gives one similar result.
----------------------------------------------------------------------
Traceback (most recent call last):
  File ".\tests\pwb_tests.py", line 102, in test_one_similar_script
    self.assertEqual(stderr.readline().strip(), result[0])
AssertionError: u'' != u'ERROR: hello.py not found! Misspelling?'
+ ERROR: hello.py not found! Misspelling?

======================================================================
FAIL: test_unicode (__main__.TestPwb)
Test printing unicode in pywikibot.
----------------------------------------------------------------------
Traceback (most recent call last):
  File ".\tests\pwb_tests.py", line 75, in test_unicode
    self.assertEqual('Häuser', direct['stderr'].strip())
AssertionError: u'H\xe4user' != u'H\u251c\xf1user'
- H\xe4user
?  ^
+ H\u251c\xf1user
?  ^^


----------------------------------------------------------------------
Ran 7 tests in 11.172s

FAILED (failures=2)


C:\pwb\GIT\core>py -2 pwb.py pwb_tests -v TestPwb.test_one_similar_script

tests: max_retries reduced from 15 to 1
test_one_similar_script (__main__.TestPwb)
Test shell.py script call which gives one similar result. ... FAIL

======================================================================
FAIL: test_one_similar_script (__main__.TestPwb)
Test shell.py script call which gives one similar result.
----------------------------------------------------------------------
Traceback (most recent call last):
  File ".\tests\pwb_tests.py", line 102, in test_one_similar_script
    self.assertEqual(stderr.readline().strip(), result[0])
AssertionError: u'' != u'ERROR: hello.py not found! Misspelling?'
+ ERROR: hello.py not found! Misspelling?

----------------------------------------------------------------------
Ran 1 test in 6.020s

FAILED (failures=1)

C:\pwb\GIT\core>py -3 pwb.py pwb_tests -v TestPwb.test_one_similar_script
tests: max_retries reduced from 15 to 1
test_one_similar_script (__main__.TestPwb)
Test shell.py script call which gives one similar result. ...
======================================================================
FAIL: test_one_similar_script (__main__.TestPwb) (line=0)
Test shell.py script call which gives one similar result.
----------------------------------------------------------------------
Traceback (most recent call last):
  File ".\tests\pwb_tests.py", line 102, in test_one_similar_script
    self.assertEqual(stderr.readline().strip(), result[0])
AssertionError: '' != 'ERROR: hello.py not found! Misspelling?'
+ ERROR: hello.py not found! Misspelling?

======================================================================
FAIL: test_one_similar_script (__main__.TestPwb) (line=1)
Test shell.py script call which gives one similar result.
----------------------------------------------------------------------
Traceback (most recent call last):
  File ".\tests\pwb_tests.py", line 107, in test_one_similar_script
    msg='"{}" does not start with "{}"'.format(text, result[1]))
AssertionError: False is not true : "" does not start with "NOTE: Starting the most similar script shell.py"

======================================================================
FAIL: test_one_similar_script (__main__.TestPwb) (line=2)
Test shell.py script call which gives one similar result.
----------------------------------------------------------------------
Traceback (most recent call last):
  File ".\tests\pwb_tests.py", line 109, in test_one_similar_script
    self.assertEqual(stderr.readline().strip(), result[2])
AssertionError: '' != 'in 5.0 seconds; type CTRL-C to stop.'
+ in 5.0 seconds; type CTRL-C to stop.

----------------------------------------------------------------------
Ran 1 test in 6.016s

FAILED (failures=3)
C:\pwb\GIT\core>py -3 pwb.py pwb_tests -v TestPwb.test_one_similar_script
tests: max_retries reduced from 15 to 1
test_one_similar_script (__main__.TestPwb)
Test shell.py script call which gives one similar result. ... ok

----------------------------------------------------------------------
Ran 1 test in 6.022s

OK

Event Timeline

Restricted Application added subscribers: pywikibot-bugs-list, Aklapper. · View Herald Transcript
Xqt triaged this task as High priority.May 26 2019, 10:37 AM

Change 512524 had a related patch set uploaded (by Xqt; owner: Xqt):
[pywikibot/core@master] [tests] Skip TestPwb.test_one_similar_script with Python 2

https://gerrit.wikimedia.org/r/512524

is not deterministic

But why? Which part of the test/script is causing this behaviour?

Xqt renamed this task from TestPwb.test_one_similar_script is not deterministic to TestPwb.test_one_similar_script: io.StringIO is empty in Python 2.7 .Jun 12 2019, 8:49 AM
Xqt updated the task description. (Show Details)

is not deterministic

But why? Which part of the test/script is causing this behaviour?

the result of stream = execute_pwb(['hello'], data_in=chr(3), timeout=6)
is assigned in stderr = io.StringIO(stream['stderr'])
but there is a different behaviour in Python 2 and 3 for print(stderr.read()):

Python 2.7 gives an empty line whereas
Python 3.7 mostly*) gives:

ERROR: hello.py not found! Misspelling?
NOTE: Starting the most similar script shell.py ***
      in 5.0 seconds; type CTRL-C to stop.
Welcome to the Pywikibot interactive shell!

*) In rare cases Python 3.7 also gives an empty line

For me, both Python 2 and 3 works the same. Only the read() works just once (as a stream), so better to copy the captured string.

Python 3.7
$ python pwb.py shell
Welcome to the Pywikibot interactive shell!
>>> from tests.utils import execute_pwb
tests: max_retries reduced from 15 to 1
>>> import io
>>> stream = execute_pwb(['hello'], data_in=chr(3), timeout=6)
>>> stderr = io.StringIO(stream['stderr'])
>>> print(stderr.read())
ERROR: hello.py not found! Misspelling?
NOTE: Starting the most similar script shell.py ***
      in 5.0 seconds; type CTRL-C to stop.
Welcome to the Pywikibot interactive shell!

>>> print(stderr.read())

>>> 
Python 2.7
$ pip2 install --user mock
DEPRECATION: Python 2.7 will reach the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 won't be maintained after that date. A future version of pip will drop support for Python 2.7.
Collecting mock
  Using cached https://files.pythonhosted.org/packages/05/d2/f94e68be6b17f46d2c353564da56e6fb89ef09faeeff3313a046cb810ca9/mock-3.0.5-py2.py3-none-any.whl
Collecting funcsigs>=1; python_version < "3.3" (from mock)
  Using cached https://files.pythonhosted.org/packages/69/cb/f5be453359271714c01b9bd06126eaf2e368f1fddfff30818754b5ac2328/funcsigs-1.0.2-py2.py3-none-any.whl
Requirement already satisfied: six in /usr/lib/python2.7/site-packages (from mock) (1.12.0)
Installing collected packages: funcsigs, mock
Successfully installed funcsigs-1.0.2 mock-3.0.5
$ python2 pwb.py shell
WARNING: /home/pavel/pywikibot/pywikibot/__init__.py:125: FutureWarning: 
Python 2.7.16 will be dropped in 2020.
It is recommended to use Python 3.5 or above.
See T213287 for further information.

  FutureWarning)

Welcome to the Pywikibot interactive shell!
>>> from tests.utils import execute_pwb
tests: max_retries reduced from 15 to 1
>>> import io
>>> stream = execute_pwb(['hello'], data_in=chr(3), timeout=6)
>>> stderr = io.StringIO(stream['stderr'])
>>> print(stderr.read())
ERROR: hello.py not found! Misspelling?
NOTE: Starting the most similar script shell.py ***
      in 5.0 seconds; type CTRL-C to stop.

>>> print(stderr.read())

>>> 

Probably a windows problem?

C:\pwb\GIT\core>pwb shell
Welcome to the Pywikibot interactive shell!
>>> from tests.utils import execute_pwb
tests: max_retries reduced from 15 to 1
>>> import io
>>> stream = execute_pwb(['hello'], data_in=chr(3), timeout=6)
>>> stderr = io.StringIO(stream['stderr'])
>>> print(stderr.read())

>>> import sys
>>> sys.version
'3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:06:47) [MSC v.1914 32 bit (Intel)]'
>>> stream = execute_pwb(['hello'], data_in=chr(3), timeout=6)
>>> stderr = io.StringIO(stream['stderr'])
>>> print(stderr.read())

>>>
C:\pwb\GIT\core>py -2 pwb.py shell
WARNING: C:\pwb\GIT\core\pywikibot\tools\ip.py:71: ImportWarning: Importing ipad
dr.IPAddress failed: No module named ipaddr
Importing ipaddress.ip_address failed: No module named ipaddress
Please install ipaddr 2.1.10+ or ipaddress.
  % (_ipaddr_e, _ipaddress_e), ImportWarning)

WARNING: C:\pwb\GIT\core\pywikibot\__init__.py:125: FutureWarning:
Python 2.7.13 will be dropped in 2020.
It is recommended to use Python 3.5 or above.
See T213287 for further information.

  FutureWarning)

Welcome to the Pywikibot interactive shell!
>>> from tests.utils import execute_pwb
tests: max_retries reduced from 15 to 1
>>> import io
>>> stream = execute_pwb(['hello'], data_in=chr(3), timeout=6)
>>> stderr = io.StringIO(stream['stderr'])
>>> print(stderr.read())

>>> import sys
>>> sys.version
'2.7.13 (v2.7.13:a06454b1afa1, Dec 17 2016, 20:42:59) [MSC v.1500 32 bit (Intel)
]'
>>>

On IDLE:

>>> from tests.utils import execute_pwb
tests: max_retries reduced from 15 to 1
>>> import io
>>> stream = execute_pwb(['hello'], data_in=chr(3), timeout=6)
>>> stderr = io.StringIO(stream['stderr'])
>>> print(stderr.read())

>>> import sys
>>> sys.version
'2.7.13 (v2.7.13:a06454b1afa1, Dec 17 2016, 20:42:59) [MSC v.1500 32 bit (Intel)]'
>>>
>>> import pwb
>>> import pywikibot
>>> from tests.utils import execute_pwb
tests: max_retries reduced from 15 to 1
>>> import io
>>> stream = execute_pwb(['hello'], data_in=chr(3), timeout=6)
>>> stderr = io.StringIO(stream['stderr'])
>>> print(stderr.read())

>>> import sys
>>> sys.version
'3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:06:47) [MSC v.1914 32 bit (Intel)]'
>>>

But it works via batch with Python 3.7 but fails with Python 2.7. Anyway we have the same problem at Appveyor. Therefore I proposed to skip the test for Python 2 (until w have a better solution)

Change 512524 merged by jenkins-bot:
[pywikibot/core@master] [tests] Skip TestPwb.test_one_similar_script with Python 2

https://gerrit.wikimedia.org/r/512524

Xqt lowered the priority of this task from High to Lowest.Jul 7 2019, 4:58 PM
Xqt raised the priority of this task from Lowest to High.Jul 19 2019, 6:24 AM
Xqt removed Xqt as the assignee of this task.Jul 19 2019, 7:17 AM
Xqt changed the subtype of this task from "Task" to "Bug Report".

Change 524641 had a related patch set uploaded (by Dalba; owner: dalba):
[pywikibot/core@master] test(utils.py): redesign the timeout workflow

https://gerrit.wikimedia.org/r/524641

Change 524641 merged by jenkins-bot:
[pywikibot/core@master] test(utils.py): redesign the timeout workflow

https://gerrit.wikimedia.org/r/524641