From 34c5c5d8781a767d35023f2cb5b2bd1e9dac5baf Mon Sep 17 00:00:00 2001 From: holger krekel Date: Mon, 25 Oct 2010 23:09:24 +0200 Subject: [PATCH] fix capturing: properly finalize self-testing of py.test, reducing number of open files during the test run. --HG-- branch : trunk --- pytest/plugin/capture.py | 9 +++++++++ pytest/plugin/pytester.py | 2 ++ pytest/plugin/terminal.py | 28 ++++++++++++++++++---------- 3 files changed, 29 insertions(+), 10 deletions(-) diff --git a/pytest/plugin/capture.py b/pytest/plugin/capture.py index dbfb59662..ff29723b6 100644 --- a/pytest/plugin/capture.py +++ b/pytest/plugin/capture.py @@ -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): diff --git a/pytest/plugin/pytester.py b/pytest/plugin/pytester.py index b5ed3d739..7be9be3af 100644 --- a/pytest/plugin/pytester.py +++ b/pytest/plugin/pytester.py @@ -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"): diff --git a/pytest/plugin/terminal.py b/pytest/plugin/terminal.py index beeecc4fb..686ef96f2 100644 --- a/pytest/plugin/terminal.py +++ b/pytest/plugin/terminal.py @@ -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)