albertvillanova / WikidataBot

Wikidata bot
http://www.wikidata.org/wiki/User:AVMbot
0 stars 0 forks source link

[pywikibot] BUG while pytest output capture because of logging #4

Open albertvillanova opened 5 years ago

albertvillanova commented 5 years ago

When running pytest (by default it captures stdout/stderr; see pytest doc) there is an exception:

$ python -m pytest tests
============================= test session starts =============================
platform win32 -- Python 3.7.5, pytest-5.2.2, py-1.8.0, pluggy-0.13.0
rootdir: C:\Users\AVI14827\projects\wikidata\WikidataBot
collected 1 item

tests\test_sample.py .                                                   [100%]

============================== 1 passed in 0.59s ==============================
Error in atexit._run_exitfuncs:
Traceback (most recent call last):
  File "C:\Users\AVI14827\AppData\Local\Continuum\anaconda3\envs\wikidata\lib\logging\__init__.py", line 894, in handle
    self.emit(record)
  File "c:\users\avi14827\projects\wikidata\pywikibot\pywikibot\userinterfaces\terminal_interface_base.py", line 519, in emit
    return self.UI.output(text, targetStream=self.stream)
  File "c:\users\avi14827\projects\wikidata\pywikibot\pywikibot\userinterfaces\terminal_interface_base.py", line 244, in output
    self._print(text, targetStream)
  File "c:\users\avi14827\projects\wikidata\pywikibot\pywikibot\userinterfaces\terminal_interface_base.py", line 182, in _print
    self._write(text, target_stream)
  File "c:\users\avi14827\projects\wikidata\pywikibot\pywikibot\userinterfaces\terminal_interface_base.py", line 143, in _write
    target_stream.write(text)
  File "C:\Users\AVI14827\AppData\Local\Continuum\anaconda3\envs\wikidata\lib\site-packages\_pytest\capture.py", line 427, in write
    self.buffer.write(obj)
ValueError: I/O operation on closed file

This happens just because of this line in the test code:

import pywikibot

Apparently, pywikibot sets logging at import at this is not a best practice: https://github.com/pytest-dev/pytest/issues/5502#issuecomment-548013400

Refactoring of pywikibot logging setting is required. I think also terminal user-interface must be refactorized.

albertvillanova commented 5 years ago

For the moment, to avoid this error, pytets capturing must be disabled:

pytest -s
$ ./run_tests.sh
============================= test session starts =============================
platform win32 -- Python 3.7.5, pytest-5.2.2, py-1.8.0, pluggy-0.13.0
rootdir: C:\Users\AVI14827\projects\wikidata\WikidataBot
collected 1 item

tests\test_sample.py .

============================== 1 passed in 0.40s ==============================
CRITICAL: Exiting due to uncaught exception None

Note the last line, logged by pywikibot:

CRITICAL: Exiting due to uncaught exception None