gristlabs_grist-core/sandbox/grist/fake_std_streams.py
George Gevoian f873b38e8f (core) Use fake stdout/stderr when evaluating formulas
Summary:
In the future, it may be helpful to capture some output to display in the
formula editor (e.g. the result of calling print()), but for now it's best
to redirect stdout/stderr to avoid excessive logging.

Test Plan: Tested manually.

Reviewers: alexmojaki

Reviewed By: alexmojaki

Subscribers: alexmojaki

Differential Revision: https://phab.getgrist.com/D3919
2023-06-14 18:24:27 -04:00

19 lines
387 B
Python

import sys
import six
class FakeStdStreams(object):
"""
Redirects stdout and stderr to StringIO.
"""
def __enter__(self):
self._orig_stdout = sys.stdout
self._orig_stderr = sys.stderr
sys.stdout = six.StringIO()
sys.stderr = six.StringIO()
def __exit__(self, exc_type, exc_val, exc_tb):
sys.stdout = self._orig_stdout
sys.stderr = self._orig_stderr