[py-svn] pytest commit 1c3eb86502b3: fix issue8 : avoid errors caused by logging module wanting to close already closed streams.

commits-noreply at bitbucket.org commits-noreply at bitbucket.org
Mon Dec 6 16:57:23 CET 2010


# HG changeset patch -- Bitbucket.org
# Project pytest
# URL http://bitbucket.org/hpk42/pytest/overview
# User holger krekel <holger at merlinux.eu>
# Date 1291650972 -3600
# Node ID 1c3eb86502b30a52e8adf798967b7d547edd8f43
# Parent  fb3765d0cc9e9e44f1b2d7a5c99d3c389d9ee214
fix issue8 : avoid errors caused by logging module wanting to close already closed streams.

The issue arose if logging was initialized while capturing was enabled
and then capturing streams were closed before process exit, leading
to the logging module to complain.

--- a/testing/test_capture.py
+++ b/testing/test_capture.py
@@ -306,6 +306,26 @@ class TestLoggingInteraction:
             # verify proper termination
             assert "closed" not in s
 
+    def test_logging_initialized_in_test(self, testdir):
+        p = testdir.makepyfile("""
+            import sys
+            def test_something():
+                # pytest does not import logging
+                assert 'logging' not in sys.modules
+                import logging
+                logging.basicConfig()
+                logging.warn("hello432")
+                assert 0
+        """)
+        result = testdir.runpytest(p, "--traceconfig",
+            "-p", "no:capturelog")
+        assert result.ret != 0
+        result.stdout.fnmatch_lines([
+            "*hello432*",
+        ])
+        assert 'operation on closed file' not in result.stderr.str()
+
+
 class TestCaptureFuncarg:
     def test_std_functional(self, testdir):
         reprec = testdir.inline_runsource("""

--- a/_pytest/capture.py
+++ b/_pytest/capture.py
@@ -26,7 +26,9 @@ def pytest_unconfigure(config):
     capman = config.pluginmanager.getplugin('capturemanager')
     while capman._method2capture:
         name, cap = capman._method2capture.popitem()
-        cap.reset()
+        # XXX logging module may wants to close it itself on process exit
+        # otherwise we could do finalization here and call "reset()".
+        cap.suspend()
 
 class NoCapture:
     def startall(self):

--- a/_pytest/core.py
+++ b/_pytest/core.py
@@ -151,15 +151,15 @@ class PluginManager(object):
         except ImportError:
             return # XXX issue a warning
         for ep in iter_entry_points('pytest11'):
-            if ep.name in self._name2plugin:
+            name = ep.name
+            if name.startswith("pytest_"):
+                name = name[7:]
+            if ep.name in self._name2plugin or name in self._name2plugin:
                 continue
             try:
                 plugin = ep.load()
             except DistributionNotFound:
                 continue
-            name = ep.name
-            if name.startswith("pytest_"):
-                name = name[7:]
             self.register(plugin, name=name)
 
     def consider_preparse(self, args):

--- a/tox.ini
+++ b/tox.ini
@@ -73,3 +73,4 @@ rsyncdirs=tox.ini pytest.py _pytest test
 python_files=test_*.py *_test.py
 python_classes=Test Acceptance
 python_functions=test
+pep8ignore = E401

--- a/CHANGELOG
+++ b/CHANGELOG
@@ -1,6 +1,7 @@
 Changes between 2.0.0 and 2.0.1.dev1
 ----------------------------------------------
 
+- fix issue8: no logging errors at process exit
 - refinements to "collecting" output on non-ttys
 - refine internal plugin registration and --traceconfig output
 - introduce a mechanism to prevent/unregister plugins from the



More information about the pytest-commit mailing list