Commit c988f6e4 authored by Stefan Behnel's avatar Stefan Behnel

simplify generated code by converting 'not X in Y' and 'not X is Y' into the...

simplify generated code by converting 'not X in Y' and 'not X is Y' into the equivalent not-in and is-not operators
parent b20c8f5c
......@@ -3050,6 +3050,8 @@ class ConstantFolding(Visitor.VisitorTransform, SkipDeclarations):
def visit_UnopNode(self, node):
self._calculate_const(node)
if node.constant_result is ExprNodes.not_a_constant:
if node.operator == '!':
return self._handle_UnaryNotNode(node)
return node
if not node.operand.is_literal:
return node
......@@ -3066,6 +3068,23 @@ class ConstantFolding(Visitor.VisitorTransform, SkipDeclarations):
return self._handle_UnaryMinusNode(node)
return node
def _handle_UnaryNotNode(self, node):
if isinstance(node.operand, ExprNodes.PrimaryCmpNode):
cmp_node = node.operand
if cmp_node.operator == 'in':
operator = 'not_in'
elif cmp_node.operator == 'is':
operator = 'is_not'
else:
return node
return ExprNodes.PrimaryCmpNode(
cmp_node.pos,
operand1=cmp_node.operand1,
operand2=cmp_node.operand2,
operator=operator,
cascade=cmp_node.cascade)
return node
def _handle_UnaryMinusNode(self, node):
def _negate(value):
if value.startswith('-'):
......
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