summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnthony Sottile <asottile@umich.edu>2020-05-07 11:13:23 -0700
committerAnthony Sottile <asottile@umich.edu>2020-05-07 11:16:00 -0700
commit609010ce7aeecc22a3c104e5c0eb39b3746ace7a (patch)
treeeafb1fff770c50068685100974f4697dd0dc1fb6
parent0c3b8045a7b51aec7abf19dea94d5292cebeeea0 (diff)
downloadflake8-609010ce7aeecc22a3c104e5c0eb39b3746ace7a.tar.gz
Fix logical checks which report position out of bounds
-rw-r--r--src/flake8/checker.py8
-rw-r--r--tests/integration/test_checker.py22
2 files changed, 28 insertions, 2 deletions
diff --git a/src/flake8/checker.py b/src/flake8/checker.py
index 572c153..30d44b8 100644
--- a/src/flake8/checker.py
+++ b/src/flake8/checker.py
@@ -523,8 +523,9 @@ class FileChecker(object):
self.processor.update_checker_state_for(plugin)
results = self.run_check(plugin, logical_line=logical_line) or ()
for offset, text in results:
- offset = find_offset(offset, mapping)
- line_number, column_offset = offset
+ line_number, column_offset = find_offset(offset, mapping)
+ if line_number == column_offset == 0:
+ LOG.warning("position of error out of bounds: %s", plugin)
self.report(
error_code=None,
line_number=line_number,
@@ -674,4 +675,7 @@ def find_offset(offset, mapping):
if offset <= token_offset:
position = token[1]
break
+ else:
+ position = (0, 0)
+ offset = token_offset = 0
return (position[0], position[1] + offset - token_offset)
diff --git a/tests/integration/test_checker.py b/tests/integration/test_checker.py
index 096b350..eaab5c3 100644
--- a/tests/integration/test_checker.py
+++ b/tests/integration/test_checker.py
@@ -5,6 +5,7 @@ import pytest
from flake8 import checker
from flake8._compat import importlib_metadata
from flake8.plugins import manager
+from flake8.processor import FileProcessor
PHYSICAL_LINE = "# Physical line content"
@@ -159,6 +160,27 @@ def test_line_check_results(plugin_target, len_results):
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'