Skip to content

Commit

Permalink
Merge d8e85fb into 84cb34c
Browse files Browse the repository at this point in the history
  • Loading branch information
hhatto committed Apr 23, 2020
2 parents 84cb34c + d8e85fb commit 03807d2
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 6 deletions.
7 changes: 6 additions & 1 deletion autopep8.py
Expand Up @@ -819,8 +819,13 @@ def fix_e301(self, result):
def fix_e302(self, result):
"""Add missing 2 blank lines."""
add_linenum = 2 - int(result['info'].split()[-1])
offset = 1
if self.source[result['line'] - 2].strip() == "\\":
offset = 2
cr = '\n' * add_linenum
self.source[result['line'] - 1] = cr + self.source[result['line'] - 1]
self.source[result['line'] - offset] = (
cr + self.source[result['line'] - offset]
)

def fix_e303(self, result):
"""Remove extra blank lines."""
Expand Down
42 changes: 37 additions & 5 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 @@ -5205,6 +5205,25 @@ class CommandLineTests(unittest.TestCase):

maxDiff = None

def test_e122_and_e302_with_backslash(self):
line = """\
import sys
\\
def f():
pass
"""
fixed = """\
import sys
\\
def f():
pass
"""
with autopep8_subprocess(line, [], timeout=3) as (result, retcode):
self.assertEqual(fixed, result)
self.assertEqual(retcode, autopep8.EXIT_CODE_OK)

def test_diff(self):
line = "'abc' \n"
fixed = "-'abc' \n+'abc'\n"
Expand Down Expand Up @@ -7036,11 +7055,24 @@ def autopep8_context(line, options=None):


@contextlib.contextmanager
def autopep8_subprocess(line, options):
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()[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:
p.kill()
raise Exception("subprocess is timed out")
_stdout, _ = p.communicate()
yield (_stdout.decode('utf-8'), p.returncode)


@contextlib.contextmanager
Expand Down

0 comments on commit 03807d2

Please sign in to comment.