Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid expensive list/tuple multiplication operations #2228

Merged
merged 5 commits into from
Jun 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,10 @@ Release date: TBA

Closes pylint-dev/pylint#8749

* Avoid expensive list/tuple multiplication operations that would result in ``MemoryError``.

Closes pylint-dev/pylint#8748


What's New in astroid 2.15.5?
=============================
Expand Down
14 changes: 10 additions & 4 deletions astroid/protocols.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,16 +163,19 @@ def const_infer_binary_op(
def _multiply_seq_by_int(
self: _TupleListNodeT,
opnode: nodes.AugAssign | nodes.BinOp,
other: nodes.Const,
value: int,
context: InferenceContext,
) -> _TupleListNodeT:
node = self.__class__(parent=opnode)
if value > 1e8:
node.elts = [util.Uninferable]
return node
filtered_elts = (
helpers.safe_infer(elt, context) or util.Uninferable
for elt in self.elts
if not isinstance(elt, util.UninferableBase)
)
node.elts = list(filtered_elts) * other.value
node.elts = list(filtered_elts) * value
return node


Expand Down Expand Up @@ -221,14 +224,17 @@ def tl_infer_binary_op(
if not isinstance(other.value, int):
yield not_implemented
return
yield _multiply_seq_by_int(self, opnode, other, context)
yield _multiply_seq_by_int(self, opnode, other.value, context)
elif isinstance(other, bases.Instance) and operator == "*":
# Verify if the instance supports __index__.
as_index = helpers.class_instance_as_index(other)
if not as_index:
yield util.Uninferable
elif not isinstance(as_index.value, int): # pragma: no cover
# already checked by class_instance_as_index() but faster than casting
jacobtylerwalls marked this conversation as resolved.
Show resolved Hide resolved
raise AssertionError("Please open a bug report.")
else:
yield _multiply_seq_by_int(self, opnode, as_index, context)
yield _multiply_seq_by_int(self, opnode, as_index.value, context)
else:
yield not_implemented

Expand Down
7 changes: 7 additions & 0 deletions tests/test_protocols.py
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,13 @@ def test_uninferable_exponents() -> None:
parsed = extract_node("None ** 2")
assert parsed.inferred() == [Uninferable]

@staticmethod
def test_uninferable_list_multiplication() -> None:
"""Attempting to calculate the result is prohibitively expensive."""
parsed = extract_node("[0] * 123456789")
element = parsed.inferred()[0].elts[0]
assert element.value is Uninferable


def test_named_expr_inference() -> None:
code = """
Expand Down
Loading