smhk

Invalid handle error from Coverage inside Tox

My tox py36 task was failing with a The handle is invalid error, and the traceback showed it was somewhere inside Coverage v4.2.2:

INTERNALERROR> Traceback (most recent call last):
INTERNALERROR>   File "c:\my_python_package\.tox\py36\lib\site-packages\_pytest\main.py", line 98, in wrap_session
INTERNALERROR>     session.exitstatus = doit(config, session) or 0
INTERNALERROR>   File "c:\my_python_package\.tox\py36\lib\site-packages\_pytest\main.py", line 133, in _main
INTERNALERROR>     config.hook.pytest_runtestloop(session=session)
INTERNALERROR>   File "c:\my_python_package\.tox\py36\lib\site-packages\_pytest\vendored_packages\pluggy.py", line 745, in __call__
INTERNALERROR>     return self._hookexec(self, self._nonwrappers + self._wrappers, kwargs)
INTERNALERROR>   File "c:\my_python_package\.tox\py36\lib\site-packages\_pytest\vendored_packages\pluggy.py", line 339, in _hookexec
INTERNALERROR>     return self._inner_hookexec(hook, methods, kwargs)
INTERNALERROR>   File "c:\my_python_package\.tox\py36\lib\site-packages\_pytest\vendored_packages\pluggy.py", line 334, in <lambda>
INTERNALERROR>     _MultiCall(methods, kwargs, hook.spec_opts).execute()
INTERNALERROR>   File "c:\my_python_package\.tox\py36\lib\site-packages\_pytest\vendored_packages\pluggy.py", line 613, in execute
INTERNALERROR>     return _wrapped_call(hook_impl.function(*args), self.execute)
INTERNALERROR>   File "c:\my_python_package\.tox\py36\lib\site-packages\_pytest\vendored_packages\pluggy.py", line 250, in _wrapped_call
INTERNALERROR>     wrap_controller.send(call_outcome)
INTERNALERROR>   File "c:\my_python_package\.tox\py36\lib\site-packages\pytest_cov\plugin.py", line 235, in pytest_runtestloop
INTERNALERROR>     self.cov_controller.finish()
INTERNALERROR>   File "c:\my_python_package\.tox\py36\lib\site-packages\pytest_cov\engine.py", line 154, in finish
INTERNALERROR>     self.cov.stop()
INTERNALERROR>   File "c:\my_python_package\.tox\py36\lib\site-packages\coverage\control.py", line 802, in combine
INTERNALERROR>     self.get_data()
INTERNALERROR>   File "c:\my_python_package\.tox\py36\lib\site-packages\coverage\control.py", line 829, in get_data
INTERNALERROR>     self._post_save_work()
INTERNALERROR>   File "c:\my_python_package\.tox\py36\lib\site-packages\coverage\control.py", line 844, in _post_save_work
INTERNALERROR>     self._warn_about_unmeasured_code(pkg)
INTERNALERROR>   File "c:\my_python_package\.tox\py36\lib\site-packages\coverage\control.py", line 873, in _warn_about_unmeasured_code
INTERNALERROR>     self._warn("Module %s was never imported." % pkg, slug="module-not-imported")
INTERNALERROR>   File "c:\my_python_package\.tox\py36\lib\site-packages\coverage\control.py", line 621, in _warn
INTERNALERROR>     sys.stderr.write("Coverage.py warning: %s\n" % msg)
INTERNALERROR> OSError: [WinError 6] The handle is invalid

My tox.ini looked something like this (trimmed down for necessary context):

tox.ini
[testenv]
deps =
  -r{toxinidir}/requirements.txt
commands =
  pytest --cov --cov-config=.coveragerc -vv {posargs}

The problem turned out to be that in my .coveragerc file, I had misspelt the name of my Python package:

.coveragerc
[paths]
source =
  src/my_python_packge
  */site-packages/my_python_packge

[run]
source =
  my_python_packge
  tests
parallel = true

Correcting my_python_packge to my_python_package in .coveragerc stopped Coverage from raising the “Module %s was never imported” warning, which stopped the invalid handle error.