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

Infinite loop for unmatched parenthesis #1919

Merged
merged 2 commits into from
May 11, 2022
Merged
Show file tree
Hide file tree
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
6 changes: 5 additions & 1 deletion isort/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from isort.settings import DEFAULT_CONFIG, Config

from . import output, parse
from .exceptions import FileSkipComment
from .exceptions import ExistingSyntaxErrors, FileSkipComment
from .format import format_natural, remove_whitespace
from .settings import FILE_SKIP_COMMENTS

Expand Down Expand Up @@ -303,6 +303,10 @@ def process(
else:
while ")" not in stripped_line:
line = input_stream.readline()

if not line: # end of file without closing parenthesis
raise ExistingSyntaxErrors("Parenthesis is not closed")

stripped_line = line.strip().split("#")[0]
import_statement += line

Expand Down
18 changes: 18 additions & 0 deletions tests/unit/test_isort.py
Original file line number Diff line number Diff line change
Expand Up @@ -5562,3 +5562,21 @@ def seekable(self):
test_input = NonSeekableTestStream("import m2\n" "import m1\n" "not_import = 7")
identified_imports = list(map(str, api.find_imports_in_stream(test_input)))
assert identified_imports == [":1 import m2", ":2 import m1"]


def test_infinite_loop_in_unmatched_parenthesis() -> None:
test_input = "from os import ("

# ensure a syntax error is raised for unmatched parenthesis
with pytest.raises(ExistingSyntaxErrors):
isort.code(test_input)

test_input = """from os import (
path,

walk
)
"""

# ensure other cases are handled correctly
assert isort.code(test_input) == "from os import path, walk\n"