Skip to content

Commit

Permalink
Always evaluate the context file in a separate process
Browse files Browse the repository at this point in the history
The GUI used to have a special case for databases using the default environment
where it would evaluate the context file in the same process. This is bad
because then the context file might not be evaluated in the database directory,
which would break local imports. Now we always evaluate it with
`get_context_file()` in a separate process in the database directory.
  • Loading branch information
JamesWrigley committed Sep 23, 2024
1 parent fa60bbe commit e4dae6d
Showing 1 changed file with 6 additions and 19 deletions.
25 changes: 6 additions & 19 deletions damnit/gui/editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
from pyflakes.api import check as pyflakes_check

from ..backend.extract_data import get_context_file
from ..ctxsupport.ctxrunner import extract_error_info
from ..context import ContextFile


class ContextTestResult(Enum):
Expand All @@ -33,25 +31,14 @@ def __init__(self, code, db_dir, context_python, parent=None):
self.context_python = context_python

def run(self):
error_info = None

# If a different environment is not specified, we can evaluate the
# context file directly.
if self.context_python is None:
try:
ContextFile.from_str(self.code)
except:
# Extract the error information
error_info = extract_error_info(*sys.exc_info())

# Otherwise, write it to a temporary file to evaluate it from another
# Write the context to a temporary file to evaluate it from another
# process.
else:
with NamedTemporaryFile(prefix=".tmp_ctx", dir=self.db_dir) as ctx_file:
ctx_path = Path(ctx_file.name)
ctx_path.write_text(self.code)
with NamedTemporaryFile(prefix=".tmp_ctx", dir=self.db_dir) as ctx_file:
ctx_path = Path(ctx_file.name)
ctx_path.write_text(self.code)

ctx, error_info = get_context_file(ctx_path, self.context_python)
context_python = sys.executable if self.context_python is None else self.context_python
ctx, error_info = get_context_file(ctx_path, context_python)

if error_info is not None:
stacktrace, lineno, offset = error_info
Expand Down

0 comments on commit e4dae6d

Please sign in to comment.