Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Close file handles of Popen #4

Merged
merged 5 commits into from
Apr 30, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 36 additions & 19 deletions src/markdown_katex/wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ class KatexError(Exception):


def _write_tex2html(cmd_parts: typ.List[str], tex: str, tmp_output_file: pl.Path) -> None:
# pylint: disable=consider-using-with ; not supported on py27
tmp_input_file = TMP_DIR / tmp_output_file.name.replace(".html", ".tex")
input_data = tex.encode(KATEX_INPUT_ENCODING)

Expand All @@ -202,23 +203,33 @@ def _write_tex2html(cmd_parts: typ.List[str], tex: str, tmp_output_file: pl.Path
fobj.write(input_data)

cmd_parts.extend(["--input", str(tmp_input_file), "--output", str(tmp_output_file)])
proc = sp.Popen(cmd_parts, stdout=sp.PIPE, stderr=sp.PIPE)
ret_code = proc.wait()
if ret_code < 0:
signame = SIG_NAME_BY_NUM[abs(ret_code)]
err_msg = (
f"Error processing '{tex}': "
+ "katex_cli process ended with "
+ f"code {ret_code} ({signame})"
)
raise KatexError(err_msg)
elif ret_code > 0:
stdout = read_output(proc.stdout)
errout = read_output(proc.stderr)
output = (stdout + "\n" + errout).strip()
err_msg = f"Error processing '{tex}': {output}"
raise KatexError(err_msg)

proc = None
try:
proc = sp.Popen(cmd_parts, stdout=sp.PIPE, stderr=sp.PIPE)
ret_code = proc.wait()
if ret_code < 0:
signame = SIG_NAME_BY_NUM[abs(ret_code)]
err_msg = (
f"Error processing '{tex}': "
+ "katex_cli process ended with "
+ f"code {ret_code} ({signame})"
)
raise KatexError(err_msg)
elif ret_code > 0:
stdout = read_output(proc.stdout)
errout = read_output(proc.stderr)
output = (stdout + "\n" + errout).strip()
err_msg = f"Error processing '{tex}': {output}"
raise KatexError(err_msg)
finally:
if proc is not None:
# It might be reasonable that Popen itself raises an
# exception. In such a case, proc would still be None
# and there is nothing to close.
if proc.stdout is not None:
proc.stdout.close()
if proc.stderr is not None:
proc.stderr.close()
tmp_input_file.unlink()


Expand Down Expand Up @@ -279,10 +290,16 @@ def _cleanup_tmp_dir() -> None:


def _get_cmd_help_text() -> str:
# pylint: disable=consider-using-with ; not supported on py27
bin_parts = get_bin_cmd()
cmd_parts = bin_parts + ['--help']
proc = sp.Popen(cmd_parts, stdout=sp.PIPE)
help_text = read_output(proc.stdout)
proc = None
try:
proc = sp.Popen(cmd_parts, stdout=sp.PIPE)
help_text = read_output(proc.stdout)
finally:
if proc is not None and proc.stdout is not None:
proc.stdout.close()
return help_text


Expand Down