Skip to content

Commit e13544f

Browse files
[symilar] Fix crash when giving bad options to symilar
1 parent c3e2579 commit e13544f

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 (
@@ -921,10 +921,18 @@ def Run(argv: Sequence[str] | None = None) -> NoReturn:
921921
ignore_docstrings = False
922922
ignore_imports = False
923923
ignore_signatures = False
924-
opts, args = getopt(list(argv), s_opts, l_opts)
924+
try:
925+
opts, args = getopt(list(argv), s_opts, l_opts)
926+
except GetoptError as e:
927+
print(e)
928+
usage(2)
925929
for opt, val in opts:
926930
if opt in {"-d", "--duplicates"}:
927-
min_lines = int(val)
931+
try:
932+
min_lines = int(val)
933+
except ValueError as e:
934+
print(e)
935+
usage(2)
928936
elif opt in {"-h", "--help"}:
929937
usage()
930938
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)