Skip to content

Commit 86a5313

Browse files
[symilar] Fix crash when giving bad options to symilar
1 parent d5a6d87 commit 86a5313

File tree

3 files changed

+43
-3
lines changed

3 files changed

+43
-3
lines changed

doc/whatsnew/fragments/9343.bugfix

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fixed a crash in ``symilar`` when the ``-d`` option was not properly recognized.
2+
3+
Closes #9343

pylint/checkers/similar.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
import warnings
4040
from collections import defaultdict
4141
from collections.abc import Callable, Generator, Iterable, Sequence
42-
from getopt import getopt
42+
from getopt import GetoptError, getopt
4343
from io import BufferedIOBase, BufferedReader, BytesIO
4444
from itertools import chain
4545
from typing import (
@@ -920,10 +920,18 @@ def Run(argv: Sequence[str] | None = None) -> NoReturn:
920920
ignore_docstrings = False
921921
ignore_imports = False
922922
ignore_signatures = False
923-
opts, args = getopt(list(argv), s_opts, l_opts)
923+
try:
924+
opts, args = getopt(list(argv), s_opts, l_opts)
925+
except GetoptError as e:
926+
print(e)
927+
usage(2)
924928
for opt, val in opts:
925929
if opt in {"-d", "--duplicates"}:
926-
min_lines = int(val)
930+
try:
931+
min_lines = int(val)
932+
except ValueError as e:
933+
print(e)
934+
usage(2)
927935
elif opt in {"-h", "--help"}:
928936
usage()
929937
elif opt in {"-i", "--ignore-comments"}:

tests/checkers/unittest_similar.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -497,3 +497,32 @@ def test_set_duplicate_lines_to_zero() -> None:
497497
similar.Run(["--duplicates=0", SIMILAR1, SIMILAR2])
498498
assert ex.value.code == 0
499499
assert output.getvalue() == ""
500+
501+
502+
@pytest.mark.parametrize("v", ["d", "i"])
503+
def test_bad_equal_short_form_option(v: str) -> None:
504+
"""Regression test for https://github.com/pylint-dev/pylint/issues/9343"""
505+
output = StringIO()
506+
with redirect_stdout(output), pytest.raises(SystemExit) as ex:
507+
similar.Run([f"-{v}=0", SIMILAR1, SIMILAR2])
508+
assert ex.value.code == 2
509+
assert "option -= not recognized" in output.getvalue()
510+
511+
512+
@pytest.mark.parametrize("v", ["i", "d"])
513+
def test_space_short_form_option(v: str) -> None:
514+
"""Regression test for https://github.com/pylint-dev/pylint/issues/9343"""
515+
output = StringIO()
516+
with redirect_stdout(output), pytest.raises(SystemExit) as ex:
517+
similar.Run([f"-{v} 2", SIMILAR1, SIMILAR2])
518+
assert ex.value.code == 2
519+
assert "option - not recognized" in output.getvalue()
520+
521+
522+
def test_bad_short_form_option() -> None:
523+
"""Regression test for https://github.com/pylint-dev/pylint/issues/9343"""
524+
output = StringIO()
525+
with redirect_stdout(output), pytest.raises(SystemExit) as ex:
526+
similar.Run(["-j=0", SIMILAR1, SIMILAR2])
527+
assert ex.value.code == 2
528+
assert "option -j not recognized" in output.getvalue()

0 commit comments

Comments
 (0)