Skip to content

Console loggin problem when using pywinauto with pytest #7059

Open
@aaltat

Description

@aaltat

I have strange console output problem if I have test that uses pytest and pywinauto. The problem can reproduced also by using uiautomation. I think the common underlying nominator is comtypes, but that is just guessing and I lack the skills to confirm that assumption.

The example test in below needs pywinauto and Notepad++ installed with new 1 tab active in the Notepad++ UI:

import subprocess
import time

import pywinauto


def test_pywinauto():
        subprocess.Popen(r'C:/Program Files/Notepad++/notepad++.exe')
        app = pywinauto.Application(backend='uia')
        win = None
        count = 0
        while win is None:
            try:
                app.connect(title_re='new ')
                win = app.window()
            except pywinauto.ElementNotFoundError:
                if count > 5:
                    raise
                time.sleep(1)
            finally:
                count += 1

        win.print_control_identifiers(filename='C:/tmp/print_control_identifiers_1212.log')

When I run this test (In Windows 10, 1909 64bit) using Python 3.8.2:
python -m pytest test_pywinauto.py
then I see endless output in the cmd, see Github gits for part of the log. It looks like test does not end and logging is in some sort of endless loop. I did try pytest versions 5.4.1, 5.2.2, 5.0.1, 5.0.0 and 4.6.9 to see where the problem starts. All of the 5.x series had a similar problem but the 4.6.9 did not suffer from the same problem and test did execute without problems.

Then did do git bisect to try to trace the problem to a specific commit and after few good/bad commands, I did end up seeing this:

$ git bisect bad
a37b902afea21621639b114f087e84f70fb057ba is the first bad commit
commit a37b902afea21621639b114f087e84f70fb057ba
Author: Bruno Oliveira <[email protected]>
Date:   Wed Jun 12 18:49:51 2019 -0300

    Integrate pytest-faulthandler into the core

    * Add pytest-faulthandler files unchanged
    * Adapt imports and tests
    * Add code to skip registration of the external `pytest_faulthandler`
      to avoid conflicts

    Fix #5440

 changelog/5440.feature.rst     |   8 ++++
 doc/en/usage.rst               |  19 +++++++-
 src/_pytest/config/__init__.py |   3 +-
 src/_pytest/deprecated.py      |   8 ++++
 src/_pytest/faulthandler.py    | 102 +++++++++++++++++++++++++++++++++++++++++
 testing/deprecated_test.py     |  23 ++++------
 testing/test_faulthandler.py   |  99 +++++++++++++++++++++++++++++++++++++++
 7 files changed, 245 insertions(+), 17 deletions(-)
 create mode 100644 changelog/5440.feature.rst
 create mode 100644 src/_pytest/faulthandler.py
 create mode 100644 testing/test_faulthandler.py

Now if I run the above test with command (with pytest 5.4.1):
python -m pytest -p no:faulthandler test_pywinauto.py
console output problem is not present and test is executed successfully:

(venv) C:\tmp>python -m pytest -p no:faulthandler test_pywinauto.py
================================================= test session starts =================================================
platform win32 -- Python 3.8.2, pytest-5.4.1, py-1.8.1, pluggy-0.13.1
rootdir: C:\tmp
collected 1 item

test_pywinauto.py .                                                                                              [100%]

================================================== warnings summary ===================================================
venv\lib\site-packages\win32\lib\pywintypes.py:2
  C:\tmp\venv\lib\site-packages\win32\lib\pywintypes.py:2: DeprecationWarning: the imp module is deprecated in favour of importlib; see the module's documentation for alternative uses
    import imp, sys, os

-- Docs: https://docs.pytest.org/en/latest/warnings.html
============================================ 1 passed, 1 warning in 5.30s =============================================

(venv) C:\tmp>

Although there is a workaround available, it would be nice to understand why commit a37b902 causes such problem. This also could be in some way related to #5743 because when faulthandler is disabled, the console does not anymore contain lines saying: Windows fatal exception:....

My pip list looks like:

(venv) C:\tmp\pytest>pip list
Package            Version
------------------ -------
appdirs            1.4.3
atomicwrites       1.3.0
attrs              19.3.0
cfgv               3.1.0
colorama           0.4.3
comtypes           1.1.7
distlib            0.3.0
filelock           3.0.12
identify           1.4.14
importlib-metadata 1.6.0
more-itertools     8.2.0
nodeenv            1.3.5
packaging          20.3
pip                20.0.2
pluggy             0.13.1
pre-commit         2.2.0
py                 1.8.1
pyparsing          2.4.7
pytest             5.4.1
pywin32            227
pywinauto          0.6.8
PyYAML             5.3.1
setuptools         41.2.0
six                1.14.0
toml               0.10.0
tox                3.14.6
virtualenv         20.0.16
wcwidth            0.1.9
zipp               3.1.0

(venv) C:\tmp\pytes>

Metadata

Metadata

Assignees

No one assigned

    Labels

    platform: windowswindows platform-specific problemtype: bugproblem that needs to be addressed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions