fix capturing: properly finalize self-testing of py.test, reducing number of open files during the

test run.

--HG--
branch : trunk
This commit is contained in:
holger krekel 2010-10-25 23:09:24 +02:00
parent 5fc87acf9b
commit 34c5c5d878
3 changed files with 29 additions and 10 deletions

View File

@ -24,11 +24,19 @@ def addouterr(rep, outerr):
def pytest_configure(config):
config.pluginmanager.register(CaptureManager(), 'capturemanager')
def pytest_unconfigure(config):
capman = config.pluginmanager.getplugin('capturemanager')
while capman._method2capture:
name, cap = capman._method2capture.popitem()
cap.reset()
class NoCapture:
def startall(self):
pass
def resume(self):
pass
def reset(self):
pass
def suspend(self):
return "", ""
@ -39,6 +47,7 @@ class CaptureManager:
def _maketempfile(self):
f = py.std.tempfile.TemporaryFile()
newf = py.io.dupfile(f, encoding="UTF-8")
f.close()
return newf
def _makestringio(self):

View File

@ -351,6 +351,8 @@ class TmpTestdir:
def parseconfigure(self, *args):
config = self.parseconfig(*args)
config.pluginmanager.do_configure(config)
self.request.addfinalizer(lambda:
config.pluginmanager.do_unconfigure(config))
return config
def getitem(self, source, funcname="test_func"):

View File

@ -33,9 +33,26 @@ def pytest_configure(config):
if config.option.collectonly:
reporter = CollectonlyReporter(config)
else:
reporter = TerminalReporter(config)
# we try hard to make printing resilient against
# later changes on FD level.
stdout = py.std.sys.stdout
if hasattr(os, 'dup') and hasattr(stdout, 'fileno'):
try:
newfd = os.dup(stdout.fileno())
#print "got newfd", newfd
except ValueError:
pass
else:
stdout = os.fdopen(newfd, stdout.mode, 1)
config._toclose = stdout
reporter = TerminalReporter(config, stdout)
config.pluginmanager.register(reporter, 'terminalreporter')
def pytest_unconfigure(config):
if hasattr(config, '_toclose'):
#print "closing", config._toclose, config._toclose.fileno()
config._toclose.close()
def getreportopt(config):
reportopts = ""
optvalue = config.getvalue("report")
@ -74,15 +91,6 @@ class TerminalReporter:
self.curdir = py.path.local()
if file is None:
file = py.std.sys.stdout
# we try hard to make printing resilient against
# later changes on FD level.
if hasattr(os, 'dup') and hasattr(file, 'fileno'):
try:
newfd = os.dup(file.fileno())
except ValueError:
pass
else:
file = os.fdopen(newfd, file.mode, 1)
self._tw = py.io.TerminalWriter(file)
self.currentfspath = None
self.reportchars = getreportopt(config)