Commit 27b5c525 authored by Xavier Thompson's avatar Xavier Thompson

Support membership tests on cypclass objects with '__contains__'

parent 5d3a0e5e
......@@ -13239,6 +13239,11 @@ class PrimaryCmpNode(ExprNode, CmpNode):
type1 = self.operand1.type
type2 = self.operand2.type
self.is_pycmp = False
if type2.is_cyp_class and self.operator in ("in", "not_in"):
# swap operands
self.operand1, self.operand2 = self.operand2, self.operand1
entry = env.lookup_operator("__contains__", [self.operand1, self.operand2])
else:
entry = env.lookup_operator(self.operator, [self.operand1, self.operand2])
if entry is None:
if self.operator in ("is", "is_not")\
......@@ -13345,7 +13350,12 @@ class PrimaryCmpNode(ExprNode, CmpNode):
result1, result2 = operand1.pythran_result(), operand2.pythran_result()
else:
result1, result2 = operand1.result(), operand2.result()
if operand1.type.is_cyp_class and self.operator not in ("is", "is_not"):
if operand1.type.is_cyp_class and self.operator in ("in", "not_in"):
return "(%s%s->operator__contains__(%s))" % (
"!" if self.operator is "not_in" else "",
result1,
result2)
elif operand1.type.is_cyp_class and self.operator not in ("is", "is_not"):
result1 = "(*%s)" % result1
if self.is_memslice_nonecheck:
if operand1.type.is_memoryviewslice:
......
......@@ -2807,7 +2807,8 @@ class CppClassScope(Scope):
'__le__': '<=',
'__ge__': '>=',
'__call__':'()',
'__getitem__':'[]'
'__getitem__':'[]',
"__contains__":"__contains__" # not actually a c++ operator: will generate a normal function named "operator__contains__"
}
def __init__(self, name, outer_scope, templates=None):
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment