Skip to content

Commit

Permalink
fix issue8 : avoid errors caused by logging module wanting to close a…
Browse files Browse the repository at this point in the history
…lready 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.
  • Loading branch information
hpk42 committed Dec 6, 2010
1 parent c753170 commit 2e80512
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 5 deletions.
1 change: 1 addition & 0 deletions 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
Expand Down
4 changes: 3 additions & 1 deletion _pytest/capture.py
Expand Up @@ -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):
Expand Down
8 changes: 4 additions & 4 deletions _pytest/core.py
Expand Up @@ -151,15 +151,15 @@ def consider_setuptools_entrypoints(self):
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):
Expand Down
20 changes: 20 additions & 0 deletions testing/test_capture.py
Expand Up @@ -306,6 +306,26 @@ def teardown_module(function):
# 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("""
Expand Down
1 change: 1 addition & 0 deletions tox.ini
Expand Up @@ -73,3 +73,4 @@ rsyncdirs=tox.ini pytest.py _pytest testing
python_files=test_*.py *_test.py
python_classes=Test Acceptance
python_functions=test
pep8ignore = E401

0 comments on commit 2e80512

Please sign in to comment.