Skip to content

Commit 8d33a23

Browse files
[Backport maintenance/4.3.x] Catch OverflowError when inferring "c" format specs in f-strings and str.format() (#3306)
Catch OverflowError when inferring "c" format specs in f-strings and str.format() (#3302) (cherry picked from commit 8dea161) Co-authored-by: Tanmay Mohan <[email protected]>
1 parent 4b0d26a commit 8d33a23

5 files changed

Lines changed: 18 additions & 2 deletions

File tree

‎astroid/brain/brain_builtin_inference.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1142,11 +1142,12 @@ def _infer_str_format_call(
11421142
formatted_string = formatter.format(
11431143
format_template, *pos_values, **keyword_values
11441144
)
1145-
except (AttributeError, IndexError, KeyError, TypeError, ValueError):
1145+
except (AttributeError, IndexError, KeyError, TypeError, ValueError, OverflowError):
11461146
# AttributeError: named field in format string was not found in the arguments
11471147
# IndexError: there are too few arguments to interpolate
11481148
# TypeError: Unsupported format string
11491149
# ValueError: Unknown format code
1150+
# OverflowError: "c" with an out-of-range code point, e.g. "{:c}".format(-1)
11501151
return iter([util.Uninferable])
11511152

11521153
return iter([nodes.const_factory(formatted_string)])

‎astroid/nodes/node_classes.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4770,9 +4770,10 @@ def _infer(
47704770
end_col_offset=self.end_col_offset,
47714771
)
47724772
continue
4773-
except (ValueError, TypeError, MemoryError):
4773+
except (ValueError, TypeError, MemoryError, OverflowError):
47744774
# ValueError/TypeError: invalid format spec
47754775
# MemoryError: format spec with huge width (e.g. f'{0:11111111111}')
4776+
# OverflowError: "c" with an out-of-range code point (e.g. f'{-1:c}')
47764777
yield util.Uninferable
47774778
uninferable_already_generated = True
47784779
continue

‎doc/whatsnew/fragments/3301.bugfix‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Fix a crash when inferring an f-string or ``str.format()`` call whose ``c`` format
2+
spec gets an out-of-range code point, such as ``f"{-1:c}"``. These now infer to
3+
``Uninferable``, like the ``%c`` case fixed in #3254.
4+
5+
Closes #3301

‎tests/brain/test_builtin.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,8 @@ def test_string_format(self, format_string: str) -> None:
125125
pytest.param(
126126
""""{:>{}}".format("x", 2000000000)""", id="oversized-nested-width"
127127
),
128+
pytest.param(""""{:c}".format(-1)""", id="char-negative"),
129+
pytest.param(""""{:c}".format(0x110000)""", id="char-too-large"),
128130
],
129131
)
130132
def test_string_format_uninferable(self, format_string: str) -> None:

‎tests/test_inference.py‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5392,6 +5392,13 @@ def test_fstring_oversized_width_uninferable(code: str) -> None:
53925392
assert list(node.infer()) == [util.Uninferable]
53935393

53945394

5395+
@pytest.mark.parametrize("code", ["f'{-1:c}'", "f'{0x110000:c}'", "f'{2**64:c}'"])
5396+
def test_fstring_char_out_of_range_uninferable(code: str) -> None:
5397+
"""Regression test for https://github.com/pylint-dev/astroid/issues/3301."""
5398+
node = extract_node(code)
5399+
assert list(node.infer()) == [util.Uninferable]
5400+
5401+
53955402
def test_augassign_recursion() -> None:
53965403
"""Make sure inference doesn't throw a RecursionError.
53975404

0 commit comments

Comments
 (0)