Author: Amaury Forgeot d'Arc <[email protected]>
Branch: move-apptest-support
Changeset: r58726:0d4cf3d45ce7
Date: 2012-11-05 01:31 +0100
http://bitbucket.org/pypy/pypy/changeset/0d4cf3d45ce7/

Log:    Found a nice way to isolate leakfinder from the rest of conftest.

diff --git a/pypy/conftest.py b/pypy/conftest.py
--- a/pypy/conftest.py
+++ b/pypy/conftest.py
@@ -1,6 +1,5 @@
 import py, pytest, sys, os, textwrap
 from inspect import isclass
-from pypy.tool import leakfinder
 
 # pytest settings
 rsyncdirs = ['.', '../lib-python', '../lib_pypy', '../demo']
@@ -25,6 +24,12 @@
 def pytest_report_header():
     return "pytest-%s from %s" %(pytest.__version__, pytest.__file__)
 
+
+def pytest_addhooks(pluginmanager):
+    from pypy.tool.pytest.plugins import LeakFinder
+    pluginmanager.register(LeakFinder())
+
+
 def pytest_configure(config):
     global option
     option = config.option
@@ -210,28 +215,9 @@
 
     __multicall__.execute()
 
-    if isinstance(item, py.test.collect.Function):
-        if not getattr(item.obj, 'dont_track_allocations', False):
-            leakfinder.start_tracking_allocations()
-
-def pytest_runtest_call(__multicall__, item):
-    __multicall__.execute()
-    item._success = True
-
 def pytest_runtest_teardown(__multicall__, item):
     __multicall__.execute()
 
-    if isinstance(item, py.test.collect.Function):
-        if (not getattr(item.obj, 'dont_track_allocations', False)
-            and leakfinder.TRACK_ALLOCATIONS):
-            item._pypytest_leaks = leakfinder.stop_tracking_allocations(False)
-        else:            # stop_tracking_allocations() already called
-            item._pypytest_leaks = None
-
-        # check for leaks, but only if the test passed so far
-        if getattr(item, '_success', False) and item._pypytest_leaks:
-            raise leakfinder.MallocMismatch(item._pypytest_leaks)
-
     if 'pygame' in sys.modules:
         assert option.view, ("should not invoke Pygame "
                              "if conftest.option.view is False")
diff --git a/pypy/tool/pytest/plugins.py b/pypy/tool/pytest/plugins.py
new file mode 100644
--- /dev/null
+++ b/pypy/tool/pytest/plugins.py
@@ -0,0 +1,36 @@
+# pytest hooks, installed by pypy.conftest.
+
+import py
+from pypy.tool import leakfinder
+
+class LeakFinder:
+    """Track memory allocations during test execution.
+    
+    So far, only used by the function lltype.malloc(flavor='raw').
+    """
+    def pytest_runtest_setup(self, __multicall__, item):
+        __multicall__.execute()
+        if not isinstance(item, py.test.collect.Function):
+            return
+        if not getattr(item.obj, 'dont_track_allocations', False):
+            leakfinder.start_tracking_allocations()
+
+    def pytest_runtest_call(self, __multicall__, item):
+        __multicall__.execute()
+        if not isinstance(item, py.test.collect.Function):
+            return
+        item._success = True
+
+    def pytest_runtest_teardown(self, __multicall__, item):
+        __multicall__.execute()
+        if not isinstance(item, py.test.collect.Function):
+            return
+        if (not getattr(item.obj, 'dont_track_allocations', False)
+            and leakfinder.TRACK_ALLOCATIONS):
+            item._pypytest_leaks = leakfinder.stop_tracking_allocations(False)
+        else:            # stop_tracking_allocations() already called
+            item._pypytest_leaks = None
+
+        # check for leaks, but only if the test passed so far
+        if getattr(item, '_success', False) and item._pypytest_leaks:
+            raise leakfinder.MallocMismatch(item._pypytest_leaks)
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to