From c988f6e484fb3edff8ef4d88e4118768cb32bd47 Mon Sep 17 00:00:00 2001 From: Stefan Behnel Date: Wed, 21 Aug 2013 15:51:26 +0200 Subject: [PATCH] simplify generated code by converting 'not X in Y' and 'not X is Y' into the equivalent not-in and is-not operators --- Cython/Compiler/Optimize.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/Cython/Compiler/Optimize.py b/Cython/Compiler/Optimize.py index b0ae989..05fc613 100644 --- a/Cython/Compiler/Optimize.py +++ b/Cython/Compiler/Optimize.py @@ -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('-'): -- 2.7.4