mirror of
https://github.com/PyCQA/flake8.git
synced 2026-04-10 14:54:17 +00:00
Fix logical checks which report position out of bounds
This commit is contained in:
parent
0c3b8045a7
commit
609010ce7a
2 changed files with 28 additions and 2 deletions
|
|
@ -523,8 +523,9 @@ class FileChecker(object):
|
||||||
self.processor.update_checker_state_for(plugin)
|
self.processor.update_checker_state_for(plugin)
|
||||||
results = self.run_check(plugin, logical_line=logical_line) or ()
|
results = self.run_check(plugin, logical_line=logical_line) or ()
|
||||||
for offset, text in results:
|
for offset, text in results:
|
||||||
offset = find_offset(offset, mapping)
|
line_number, column_offset = find_offset(offset, mapping)
|
||||||
line_number, column_offset = offset
|
if line_number == column_offset == 0:
|
||||||
|
LOG.warning("position of error out of bounds: %s", plugin)
|
||||||
self.report(
|
self.report(
|
||||||
error_code=None,
|
error_code=None,
|
||||||
line_number=line_number,
|
line_number=line_number,
|
||||||
|
|
@ -674,4 +675,7 @@ def find_offset(offset, mapping):
|
||||||
if offset <= token_offset:
|
if offset <= token_offset:
|
||||||
position = token[1]
|
position = token[1]
|
||||||
break
|
break
|
||||||
|
else:
|
||||||
|
position = (0, 0)
|
||||||
|
offset = token_offset = 0
|
||||||
return (position[0], position[1] + offset - token_offset)
|
return (position[0], position[1] + offset - token_offset)
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,7 @@ import pytest
|
||||||
from flake8 import checker
|
from flake8 import checker
|
||||||
from flake8._compat import importlib_metadata
|
from flake8._compat import importlib_metadata
|
||||||
from flake8.plugins import manager
|
from flake8.plugins import manager
|
||||||
|
from flake8.processor import FileProcessor
|
||||||
|
|
||||||
PHYSICAL_LINE = "# Physical line content"
|
PHYSICAL_LINE = "# Physical line content"
|
||||||
|
|
||||||
|
|
@ -159,6 +160,27 @@ def test_line_check_results(plugin_target, len_results):
|
||||||
assert file_checker.results == expected
|
assert file_checker.results == expected
|
||||||
|
|
||||||
|
|
||||||
|
def test_logical_line_offset_out_of_bounds():
|
||||||
|
"""Ensure that logical line offsets that are out of bounds do not crash."""
|
||||||
|
|
||||||
|
@plugin_func
|
||||||
|
def _logical_line_out_of_bounds(logical_line):
|
||||||
|
yield 10000, 'L100 test'
|
||||||
|
|
||||||
|
file_checker = mock_file_checker_with_plugin(_logical_line_out_of_bounds)
|
||||||
|
|
||||||
|
logical_ret = (
|
||||||
|
'',
|
||||||
|
'print("xxxxxxxxxxx")',
|
||||||
|
[(0, (1, 0)), (5, (1, 5)), (6, (1, 6)), (19, (1, 19)), (20, (1, 20))],
|
||||||
|
)
|
||||||
|
with mock.patch.object(
|
||||||
|
FileProcessor, 'build_logical_line', return_value=logical_ret,
|
||||||
|
):
|
||||||
|
file_checker.run_logical_checks()
|
||||||
|
assert file_checker.results == [('L100', 0, 0, 'test', None)]
|
||||||
|
|
||||||
|
|
||||||
PLACEHOLDER_CODE = 'some_line = "of" * code'
|
PLACEHOLDER_CODE = 'some_line = "of" * code'
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue