question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Console loggin problem when using pywinauto with pytest

See original GitHub issue

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 <nicoddemus@gmail.com>
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 https://github.com/pytest-dev/pytest/commit/a37b902afea21621639b114f087e84f70fb057ba 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>

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:2
  • Comments:11 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
bluetechcommented, Apr 10, 2020

@aaltat Thanks for the detailed report! We are seeing something similar in pytest CI, which may or may not be related.

What I’d be interested in finding out first is whether this is caused by something pytest does, or if it can be reproduced without it.

What the faulthandler plugin does, when not running under xdist and without setting the faulthandler_timeout pytest.ini option boils down to:

import faulthandler, os, sys

print("BEFORE enable")
stderr_fd_copy = os.dup(sys.stderr.fileno())
faulthandler.enable(file=stderr_fd_copy)
print("AFTER enable")

# Tests run here.

print("BEFORE disable")
faulthandler.disable()
os.close(stderr_fd_copy)
print("AFTER disable")

(I added some printf debugging)

So I wonder what happens if you run the above directly (without pytest), inserting the test_pywinauto code in the middle.

0reactions
shailygajjarcommented, Jun 24, 2022

That worked for me as well. Thanks for the prompt response!

Read more comments on GitHub >

github_iconTop Results From Across the Web

pytest - Windows fatal exception: code 0x8001010d
Have this problem using pytest 6.2.5 and pytest-qt 4.0.2. I tried np8's idea: still got a horrible crash (without message).
Read more >
PyInstaller Documentation - Read the Docs
Your script's use of print and input() are directed here. Error messages from Python and default logging output also appear in the console...
Read more >
Windows Analysis Report Pywinauto_recorder_installer.exe
Replication Through Removable Media, Launchd, Rc.common, Rc.common, Steganography ... File Type: PE32+ executable (DLL) (console) x86-64, for MS Windows.
Read more >
conda-forge feedstocks | community driven packaging for conda
... python-logging-rabbitmq: python-logging-rabbitmq-feedstock · python-foton: cds-crtools- ... pytest-console-scripts: pytest-console-scripts-feedstock ...
Read more >
How to automate apps using python (PyWinAuto Tutorial)
Comment down below what else you want me to do with python !!!
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found