changeset 8208:9948ed9916c4

py3: work around incompatibility between pytest, py3 inspect, and tg Work around an issue that has been reported on https://github.com/TurboGears/tg2/issues/118 : .../site-packages/_pytest/doctest.py:381: in _mock_aware_unwrap return real_unwrap(obj, stop=_is_mocked) /usr/lib64/python3.7/inspect.py:511: in unwrap while _is_wrapper(func): /usr/lib64/python3.7/inspect.py:505: in _is_wrapper return hasattr(f, '__wrapped__') and not stop(f) .../site-packages/tg/support/objectproxy.py:19: in __getattr__ return getattr(self._current_obj(), attr) .../site-packages/tg/request_local.py:240: in _current_obj return getattr(context, self.name) .../site-packages/tg/support/objectproxy.py:19: in __getattr__ return getattr(self._current_obj(), attr) .../site-packages/tg/support/registry.py:72: in _current_obj 'thread' % self.____name__) E TypeError: No object (name: context) has been registered for this thread pytest's doctest support is (in _mock_aware_unwrap) using py3 inspect. Inside inspect, _is_wrapper will do an innocent looking: hasattr(f, '__wrapped__') But if the code under test has un (unused) import of a tg context (such as tg.request), it is no longer so innocent. tg will throw: TypeError: No object (name: context) has been registered for this thread (which in py2 would have caught by hasattr, but not in py3.) pytest will thus fail already in the "collecting ..." phase. To work around that, use the hack of pushing a tg context in the top level pytest_configure.
author Mads Kiilerich <mads@kiilerich.com>
date Fri, 31 Jan 2020 19:38:09 +0100
parents 141066b8a89a
children 01aca0a4f876
files conftest.py
diffstat 1 files changed, 9 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/conftest.py	Sat Jan 25 20:06:36 2020 +0100
+++ b/conftest.py	Fri Jan 31 19:38:09 2020 +0100
@@ -2,10 +2,19 @@
 
 import mock
 import pytest
+import tg
 
 
 here = os.path.dirname(__file__)
 
+# HACK:
+def pytest_configure():
+    # Register global dummy tg.context to avoid "TypeError: No object (name: context) has been registered for this thread"
+    tg.request_local.context._push_object(tg.util.bunch.Bunch())
+    # could be removed again after use with
+    # tg.request_local.context._pop_object ... but we keep it around forever as
+    # a reasonable sentinel
+
 def pytest_ignore_collect(path):
     # ignore all files outside the 'kallithea' directory
     if not str(path).startswith(os.path.join(here, 'kallithea')):