From bcfb0220acb9bc589451cc443161bacaa89736b4 Mon Sep 17 00:00:00 2001 From: Aniruddha Date: Wed, 13 Apr 2022 15:26:34 +0530 Subject: [PATCH 1/2] Add check for missing parenthesis at EOF --- isort/core.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/isort/core.py b/isort/core.py index e9a2977b3..9dfaf70f0 100644 --- a/isort/core.py +++ b/isort/core.py @@ -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 @@ -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 From a4f8fb63f226122c24f376dd6f088098d113e7be Mon Sep 17 00:00:00 2001 From: Aniruddha Date: Wed, 13 Apr 2022 16:21:19 +0530 Subject: [PATCH 2/2] Add test to check for infinite loop on unmatched parenthesis --- tests/unit/test_isort.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/tests/unit/test_isort.py b/tests/unit/test_isort.py index 293b99bec..55de0f209 100644 --- a/tests/unit/test_isort.py +++ b/tests/unit/test_isort.py @@ -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"