2

I found another SO addressing a similar problem but no one gave an answer.

My problem is the following (and a bit simpler to reproduce), create a file called test_file.py with:


import psutil
import xlwings

def test() -> None:
    my_function()

def my_function() -> None:
    # Create a new workbook
    wb = xlwings.Book()
    # Save it
    wb.save('test.xlsx')
    # Close it
    wb.close()

    # Kill zombie processes
    for proc in psutil.process_iter():
        if proc.name() == "EXCEL.EXE":
            proc.kill()


if __name__ == "__main__":
    my_function()

When doing python test_file.py, everything goes smoothly. When doing pytest to do my unit tests, I have this error:

test_file.py Windows fatal exception: code 0x800706ba

The traceback is the following:

  File "C:\Users\jason\Desktop\so\test_file.py", line 6 in test
  File "C:\Users\jason\AppData\Local\Programs\Python\Python311\Lib\site-packages\_pytest\python.py", line 195 in pytest_pyfunc_call
  File "C:\Users\jason\AppData\Local\Programs\Python\Python311\Lib\site-packages\pluggy\_callers.py", line 39 in _multicall
  File "C:\Users\jason\AppData\Local\Programs\Python\Python311\Lib\site-packages\pluggy\_manager.py", line 80 in _hookexec
  File "C:\Users\jason\AppData\Local\Programs\Python\Python311\Lib\site-packages\pluggy\_hooks.py", line 265 in __call__
  File "C:\Users\jason\AppData\Local\Programs\Python\Python311\Lib\site-packages\_pytest\python.py", line 1789 in runtest
  File "C:\Users\jason\AppData\Local\Programs\Python\Python311\Lib\site-packages\_pytest\runner.py", line 167 in pytest_runtest_call
  File "C:\Users\jason\AppData\Local\Programs\Python\Python311\Lib\site-packages\pluggy\_callers.py", line 39 in _multicall
  File "C:\Users\jason\AppData\Local\Programs\Python\Python311\Lib\site-packages\pluggy\_manager.py", line 80 in _hookexec
  File "C:\Users\jason\AppData\Local\Programs\Python\Python311\Lib\site-packages\pluggy\_hooks.py", line 265 in __call__
  File "C:\Users\jason\AppData\Local\Programs\Python\Python311\Lib\site-packages\_pytest\runner.py", line 260 in <lambda>
  File "C:\Users\jason\AppData\Local\Programs\Python\Python311\Lib\site-packages\_pytest\runner.py", line 339 in from_call
  File "C:\Users\jason\AppData\Local\Programs\Python\Python311\Lib\site-packages\_pytest\runner.py", line 259 in call_runtest_hook
  File "C:\Users\jason\AppData\Local\Programs\Python\Python311\Lib\site-packages\_pytest\runner.py", line 220 in call_and_report
  File "C:\Users\jason\AppData\Local\Programs\Python\Python311\Lib\site-packages\_pytest\runner.py", line 131 in runtestprotocol
  File "C:\Users\jason\AppData\Local\Programs\Python\Python311\Lib\site-packages\_pytest\runner.py", line 112 in pytest_runtest_protocol
  File "C:\Users\jason\AppData\Local\Programs\Python\Python311\Lib\site-packages\pluggy\_callers.py", line 39 in _multicall
  File "C:\Users\jason\AppData\Local\Programs\Python\Python311\Lib\site-packages\pluggy\_manager.py", line 80 in _hookexec
  File "C:\Users\jason\AppData\Local\Programs\Python\Python311\Lib\site-packages\pluggy\_hooks.py", line 265 in __call__
  File "C:\Users\jason\AppData\Local\Programs\Python\Python311\Lib\site-packages\_pytest\main.py", line 349 in pytest_runtestloop
  File "C:\Users\jason\AppData\Local\Programs\Python\Python311\Lib\site-packages\pluggy\_callers.py", line 39 in _multicall
  File "C:\Users\jason\AppData\Local\Programs\Python\Python311\Lib\site-packages\pluggy\_manager.py", line 80 in _hookexec
  File "C:\Users\jason\AppData\Local\Programs\Python\Python311\Lib\site-packages\pluggy\_hooks.py", line 265 in __call__
  File "C:\Users\jason\AppData\Local\Programs\Python\Python311\Lib\site-packages\_pytest\main.py", line 324 in _main
  File "C:\Users\jason\AppData\Local\Programs\Python\Python311\Lib\site-packages\_pytest\main.py", line 270 in wrap_session
  File "C:\Users\jason\AppData\Local\Programs\Python\Python311\Lib\site-packages\_pytest\main.py", line 317 in pytest_cmdline_main
  File "C:\Users\jason\AppData\Local\Programs\Python\Python311\Lib\site-packages\pluggy\_callers.py", line 39 in _multicall
  File "C:\Users\jason\AppData\Local\Programs\Python\Python311\Lib\site-packages\pluggy\_manager.py", line 80 in _hookexec
  File "C:\Users\jason\AppData\Local\Programs\Python\Python311\Lib\site-packages\pluggy\_hooks.py", line 265 in __call__
  File "C:\Users\jason\AppData\Local\Programs\Python\Python311\Lib\site-packages\_pytest\config\__init__.py", line 167 in main
  File "C:\Users\jason\AppData\Local\Programs\Python\Python311\Lib\site-packages\_pytest\config\__init__.py", line 190 in console_main
  File "C:\Users\jason\AppData\Local\Programs\Python\Python311\Scripts\pytest.exe\__main__.py", line 7 in <module>
  File "<frozen runpy>", line 88 in _run_code
  File "<frozen runpy>", line 198 in _run_module_as_main

It must be a problem between pytest, xlwings, psutil because if I'm not using the three simultaneously I cannot reproduce the bug.

I'm using Python 3.11.1, xlwings==0.28.6, pytest==7.2.2 and psutil==5.9.4.

Using a context manager as suggested by @moken doesn't work either:

import xlwings

def test() -> None:
    my_function()

def my_function() -> None:
    with xlwings.App(visible=True):
        # Create a new workbook
        wb = xlwings.Book()
        # Save it
        wb.save('test.xlsx')
        # Close it
        wb.close()


if __name__ == "__main__":
    my_function()

raises the exact same error (when using pytest, when not, everything works fine obviously). enter image description here

  • It's the zombie process check that causes the error right? Why do that? like the referenced SO post suggests. Use a context manager to avoid zombie Excel processes. – moken May 03 '23 at 00:59
  • The context manager raises the same exception for me. I've edited the main post. – IHopeItWontBeAStupidQuestion May 03 '23 at 12:25
  • Have you tried using `unittest.mock` to mock Excel interactions during testing to avoid the issue with (pytest, xlwings, and psutil)? As long as you know xlwings works fine with excel; this approach may work better. – Grimlock May 06 '23 at 22:44
  • 1
    The `.xlsx` which you are using in the pytest, make sure it's been closed. And also you can try running the pytest in a separate `subprocess`. – Pravash Panigrahi May 07 '23 at 12:00
  • https://github.com/xlwings/xlwings/issues/1494 ? – mrxra May 09 '23 at 08:12
  • @mrxra Thanks for the github issue reference! the github link you sent says: "While I agree that that for internal unit testing, you can potentially disable faulthandling, I would advise against making use of -p no:faulthandler the official recommendation in your documentation, since users of the package (like me) would be unintentionally silencing all faulthandling -- not just the faulthandling for tests involving xlwings API calls.". Is it really safe to disable segmentation errors handling? – IHopeItWontBeAStupidQuestion May 15 '23 at 07:18
  • @mrxra About the stackoverflow you linked, I literally linked it and am actively talking about it in the post. I have no idea why you commented that. – IHopeItWontBeAStupidQuestion May 15 '23 at 07:24

0 Answers0