Sam Hooke

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\", 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\", 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\", 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\", 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\", 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\", 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\", line 250, in _wrapped_call
INTERNALERROR>     wrap_controller.send(call_outcome)
INTERNALERROR>   File "c:\my_python_package\.tox\py36\lib\site-packages\pytest_cov\", line 235, in pytest_runtestloop
INTERNALERROR>     self.cov_controller.finish()
INTERNALERROR>   File "c:\my_python_package\.tox\py36\lib\site-packages\pytest_cov\", line 154, in finish
INTERNALERROR>     self.cov.stop()
INTERNALERROR>   File "c:\my_python_package\.tox\py36\lib\site-packages\coverage\", line 802, in combine
INTERNALERROR>     self.get_data()
INTERNALERROR>   File "c:\my_python_package\.tox\py36\lib\site-packages\coverage\", line 829, in get_data
INTERNALERROR>     self._post_save_work()
INTERNALERROR>   File "c:\my_python_package\.tox\py36\lib\site-packages\coverage\", 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\", 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\", line 621, in _warn
INTERNALERROR>     sys.stderr.write(" 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 §
deps =
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 §
source =

source =
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.

These are rough notes from whatever I was working on, interested in or thinking about at the time. They vary greatly in quality and length, but prove useful to me, and hopefully to you too!

← Previous: Run Django makemigrations as tox task
Next: Bootstrapping pip →