From 21f8e09be8e5089bc0189d5609ad6363ac5e7420 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Sun, 5 Jul 2015 22:24:35 +0300 Subject: [PATCH] test.support: Implement helpers required to run test_pep380.py. --- test.support/test/support.py | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/test.support/test/support.py b/test.support/test/support.py index c79663dc..cb9cd6f2 100644 --- a/test.support/test/support.py +++ b/test.support/test/support.py @@ -1,4 +1,8 @@ +import sys +import io import unittest +import gc +import contextlib TESTFN = '@test' @@ -25,3 +29,31 @@ def skip_unless_symlink(test): def create_empty_file(name): open(name, "w").close() + +@contextlib.contextmanager +def disable_gc(): + have_gc = gc.isenabled() + gc.disable() + try: + yield + finally: + if have_gc: + gc.enable() + +def gc_collect(): + gc.collect() + gc.collect() + gc.collect() + +@contextlib.contextmanager +def captured_output(stream_name): + org = getattr(sys, stream_name) + buf = io.StringIO() + setattr(sys, stream_name, buf) + try: + yield buf + finally: + setattr(sys, stream_name, org) + +def captured_stderr(): + return captured_output("stderr")