(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
This commit is contained in:
George Gevoian 2023-06-13 09:40:55 -04:00
parent 2740884e3c
commit f873b38e8f
2 changed files with 23 additions and 2 deletions

View File

@ -24,6 +24,7 @@ from codebuilder import DOLLAR_REGEX
import depend
import docactions
import docmodel
from fake_std_streams import FakeStdStreams
import gencode
import logger
import match_counter
@ -944,9 +945,11 @@ class Engine(object):
raise depend.CircularRefError("Circular Reference")
if not col.is_formula():
value = col.get_cell_value(int(record), restore=True)
result = col.method(record, table.user_table, value, self._user)
with FakeStdStreams():
result = col.method(record, table.user_table, value, self._user)
else:
result = col.method(record, table.user_table)
with FakeStdStreams():
result = col.method(record, table.user_table)
if self._cell_required_error:
raise self._cell_required_error # pylint: disable=raising-bad-type
self.formula_tracer(col, record)

View File

@ -0,0 +1,18 @@
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