Skip to content

Commit

Permalink
fix: Discard setup commands stdout
Browse files Browse the repository at this point in the history
Reference: #91
  • Loading branch information
pawamoy committed May 6, 2020
1 parent 9fe3b39 commit ea44cea
Showing 1 changed file with 18 additions and 10 deletions.
28 changes: 18 additions & 10 deletions src/mkdocstrings/handlers/python.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,19 +126,27 @@ def __init__(self, setup_commands: Optional[List[str]]) -> None:
"""
log.debug("mkdocstrings.handlers.python: Opening 'pytkdocs' subprocess")
env = os.environ.copy()
env["PYTHONUNBUFFERED"] = "1"

python_str = "; ".join(setup_commands) + "; " if setup_commands else ""
cmd = python_str + "from pytkdocs.cli import main; main(['--line-by-line'])"
if setup_commands:
# prevent the Python interpreter or the setup commands
# from writing to stdout as it would break pytkdocs output
commands = [
"import sys",
"from io import StringIO",
"from pytkdocs.cli import main as pytkdocs",
"sys.stdout = StringIO()", # redirect stdout to memory buffer
*setup_commands,
"sys.stdout.flush()",
"sys.stdout = sys.__stdout__", # restore stdout
"pytkdocs(['--line-by-line'])",
]
cmd = [sys.executable, "-c", "; ".join(commands)]
else:
cmd = ["pytkdocs", "--line-by-line"]

env["PYTHONUNBUFFERED"] = "1"
self.process = Popen( # noqa: S603,S607 (we trust the input, and we don't want to use the absolute path)
[sys.executable, "-c", cmd],
universal_newlines=True,
stderr=PIPE,
stdout=PIPE,
stdin=PIPE,
bufsize=-1,
env=env,
cmd, universal_newlines=True, stderr=PIPE, stdout=PIPE, stdin=PIPE, bufsize=-1, env=env,
)

def collect(self, identifier: str, config: dict) -> Any:
Expand Down

0 comments on commit ea44cea

Please sign in to comment.