Correctly handle OP_CONTAINS (#158660)

CPython can fallback to `__iter__` if object doesn't implement
`__contains__`

Pull Request resolved: https://github.com/pytorch/pytorch/pull/158660
Approved by: https://github.com/zou3519
This commit is contained in:
Guilherme Leobas
2025-07-23 10:03:17 -03:00
committed by PyTorch MergeBot
parent 7f649ed4f8
commit b67f97c166
18 changed files with 45 additions and 36 deletions

View File

@ -666,6 +666,11 @@ class LocalGeneratorObjectVariable(VariableTracker):
finally:
counters["unimplemented"] |= counters["inline_call"]
def call_obj_hasattr(self, tx, name):
if name in self.python_type().__dict__:
return ConstantVariable.create(True)
return ConstantVariable.create(False)
def has_unpack_var_sequence(self, tx):
return False
@ -885,12 +890,6 @@ class LocalGeneratorObjectVariable(VariableTracker):
else:
raise_observed_exception(RuntimeError, tracer)
return retval
elif name == "__contains__":
# The generator needs to be lazily consumed here to avoid unintended
# side effects
return variables.UserFunctionVariable(
polyfills.generator___contains__
).call_function(tx, [self, *args], {})
super().call_method(tx, name, args, kwargs)