Skip to content

Commit

Permalink
Remove stdout/stderr from subprocesses (redirected to /dev/null)
Browse files Browse the repository at this point in the history
This means that the subprocess should now not crash anymore because of people
writing to stdout in c modules and stderr should be empty.

Fixes #793.
  • Loading branch information
davidhalter committed Mar 17, 2018
1 parent 5f0b34a commit 094affa
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
6 changes: 5 additions & 1 deletion jedi/evaluate/compiled/subprocess/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,6 @@ def _process(self):
args,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
# stderr=subprocess.PIPE
)

def run(self, evaluator, function, args=(), kwargs={}):
Expand Down Expand Up @@ -266,6 +265,11 @@ def _run(self, evaluator_id, function, args, kwargs):

def listen(self):
stdout = sys.stdout
# Mute stdout/stderr. Nobody should actually be able to write to those,
# because stdout is used for IPC and stderr will just be annoying if it
# leaks (on module imports).
sys.stdout = open(os.devnull, 'w')
sys.stderr = open(os.devnull, 'w')
stdin = sys.stdin
if sys.version_info[0] > 2:
stdout = stdout.buffer
Expand Down
14 changes: 13 additions & 1 deletion jedi/evaluate/compiled/subprocess/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import os

from jedi._compatibility import find_module, cast_path, force_unicode, \
iter_modules, all_suffixes
iter_modules, all_suffixes, print_to_stderr
from jedi.evaluate.compiled import access
from jedi import parser_utils

Expand Down Expand Up @@ -85,6 +85,18 @@ def _test_raise_error(evaluator, exception_type):
raise exception_type


def _test_print(evaluator, stderr=None, stdout=None):
"""
Force some prints in the subprocesses. This exists for unit tests.
"""
if stderr is not None:
print_to_stderr(stderr)
sys.stderr.flush()
if stdout is not None:
print(stdout)
sys.stdout.flush()


def _get_init_path(directory_path):
"""
The __init__ file can be searched in a directory. If found return it, else
Expand Down
5 changes: 5 additions & 0 deletions test/test_api/test_environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ def test_error_in_environment(evaluator, Script):
assert def_.name == 'str'


def test_stdout_in_subprocess(evaluator, Script):
evaluator.compiled_subprocess._test_print(stdout='.')
Script('1').goto_definitions()


def test_killed_subprocess(evaluator, Script):
# Just kill the subprocess.
evaluator.compiled_subprocess._compiled_subprocess._process.kill()
Expand Down

0 comments on commit 094affa

Please sign in to comment.