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:
parent
5fc87acf9b
commit
34c5c5d878
|
@ -24,11 +24,19 @@ def addouterr(rep, outerr):
|
||||||
def pytest_configure(config):
|
def pytest_configure(config):
|
||||||
config.pluginmanager.register(CaptureManager(), 'capturemanager')
|
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:
|
class NoCapture:
|
||||||
def startall(self):
|
def startall(self):
|
||||||
pass
|
pass
|
||||||
def resume(self):
|
def resume(self):
|
||||||
pass
|
pass
|
||||||
|
def reset(self):
|
||||||
|
pass
|
||||||
def suspend(self):
|
def suspend(self):
|
||||||
return "", ""
|
return "", ""
|
||||||
|
|
||||||
|
@ -39,6 +47,7 @@ class CaptureManager:
|
||||||
def _maketempfile(self):
|
def _maketempfile(self):
|
||||||
f = py.std.tempfile.TemporaryFile()
|
f = py.std.tempfile.TemporaryFile()
|
||||||
newf = py.io.dupfile(f, encoding="UTF-8")
|
newf = py.io.dupfile(f, encoding="UTF-8")
|
||||||
|
f.close()
|
||||||
return newf
|
return newf
|
||||||
|
|
||||||
def _makestringio(self):
|
def _makestringio(self):
|
||||||
|
|
|
@ -351,6 +351,8 @@ class TmpTestdir:
|
||||||
def parseconfigure(self, *args):
|
def parseconfigure(self, *args):
|
||||||
config = self.parseconfig(*args)
|
config = self.parseconfig(*args)
|
||||||
config.pluginmanager.do_configure(config)
|
config.pluginmanager.do_configure(config)
|
||||||
|
self.request.addfinalizer(lambda:
|
||||||
|
config.pluginmanager.do_unconfigure(config))
|
||||||
return config
|
return config
|
||||||
|
|
||||||
def getitem(self, source, funcname="test_func"):
|
def getitem(self, source, funcname="test_func"):
|
||||||
|
|
|
@ -33,9 +33,26 @@ def pytest_configure(config):
|
||||||
if config.option.collectonly:
|
if config.option.collectonly:
|
||||||
reporter = CollectonlyReporter(config)
|
reporter = CollectonlyReporter(config)
|
||||||
else:
|
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')
|
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):
|
def getreportopt(config):
|
||||||
reportopts = ""
|
reportopts = ""
|
||||||
optvalue = config.getvalue("report")
|
optvalue = config.getvalue("report")
|
||||||
|
@ -74,15 +91,6 @@ class TerminalReporter:
|
||||||
self.curdir = py.path.local()
|
self.curdir = py.path.local()
|
||||||
if file is None:
|
if file is None:
|
||||||
file = py.std.sys.stdout
|
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._tw = py.io.TerminalWriter(file)
|
||||||
self.currentfspath = None
|
self.currentfspath = None
|
||||||
self.reportchars = getreportopt(config)
|
self.reportchars = getreportopt(config)
|
||||||
|
|
Loading…
Reference in New Issue