Skip to content

Commit

Permalink
consider subprocess.Popen.comunicate with timeout argument to be comp…
Browse files Browse the repository at this point in the history
…atible with Python2/3
  • Loading branch information
hhatto committed Apr 23, 2020
1 parent e9c5891 commit d6aae6a
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions test/test_autopep8.py
Expand Up @@ -15,7 +15,7 @@
import os
import re
import sys

import time
import contextlib
import io
import shutil
Expand Down Expand Up @@ -7057,9 +7057,21 @@ def autopep8_context(line, options=None):
@contextlib.contextmanager
def autopep8_subprocess(line, options, timeout=None):
with temporary_file_context(line) as filename:
p = Popen(list(AUTOPEP8_CMD_TUPLE) + [filename] + options,
stdout=PIPE)
yield (p.communicate(timeout=timeout)[0].decode('utf-8'), p.returncode)
p = Popen(list(AUTOPEP8_CMD_TUPLE) + [filename] + options, stdout=PIPE)
if timeout is None:
_stdout, _ = p.communicate()
else:
try:
_stdout, _ = p.communicate(timeout=timeout)
except TypeError:
# for Python2
while p.poll() is None and timeout > 0:
time.sleep(0.5)
timeout -= 0.5
if p.poll() is None:
raise Exception("subprocess is timed out")
_stdout, _ = p.communicate()
yield (_stdout.decode('utf-8'), p.returncode)


@contextlib.contextmanager
Expand Down

0 comments on commit d6aae6a

Please sign in to comment.