Commit 7c89ca3e authored by Stefan Behnel's avatar Stefan Behnel

simplify and extend in/not-in and is/is-not optimisation, add test

parent c988f6e4
...@@ -19,6 +19,7 @@ from StringEncoding import EncodedString, BytesLiteral ...@@ -19,6 +19,7 @@ from StringEncoding import EncodedString, BytesLiteral
from Errors import error from Errors import error
from ParseTreeTransforms import SkipDeclarations from ParseTreeTransforms import SkipDeclarations
import copy
import codecs import codecs
try: try:
...@@ -3068,21 +3069,21 @@ class ConstantFolding(Visitor.VisitorTransform, SkipDeclarations): ...@@ -3068,21 +3069,21 @@ class ConstantFolding(Visitor.VisitorTransform, SkipDeclarations):
return self._handle_UnaryMinusNode(node) return self._handle_UnaryMinusNode(node)
return node return node
_negate_operator = {
'in': 'not_in',
'not_in': 'in',
'is': 'is_not',
'is_not': 'is'
}.get
def _handle_UnaryNotNode(self, node): def _handle_UnaryNotNode(self, node):
if isinstance(node.operand, ExprNodes.PrimaryCmpNode): operand = node.operand
cmp_node = node.operand if isinstance(operand, ExprNodes.PrimaryCmpNode):
if cmp_node.operator == 'in': operator = self._negate_operator(operand.operator)
operator = 'not_in' if operator:
elif cmp_node.operator == 'is': node = copy.copy(operand)
operator = 'is_not' node.operator = operator
else: node = self.visit_PrimaryCmpNode(node)
return node
return ExprNodes.PrimaryCmpNode(
cmp_node.pos,
operand1=cmp_node.operand1,
operand2=cmp_node.operand2,
operator=operator,
cascade=cmp_node.cascade)
return node return node
def _handle_UnaryMinusNode(self, node): def _handle_UnaryMinusNode(self, node):
......
# mode: run
# tag: is_not
cimport cython
@cython.test_fail_if_path_exists('//NotNode')
def is_not(a, b):
"""
>>> is_not(1, 2)
True
>>> x = 1
>>> is_not(x, x)
False
"""
return a is not b
@cython.test_fail_if_path_exists('//NotNode')
def not_is_not(a, b):
"""
>>> not_is_not(1, 2)
False
>>> x = 1
>>> not_is_not(x, x)
True
"""
return not a is not b
@cython.test_fail_if_path_exists('//NotNode')
def not_is(a, b):
"""
>>> not_is(1, 2)
True
>>> x = 1
>>> not_is(x, x)
False
"""
return not a is b
@cython.test_fail_if_path_exists('//NotNode')
def is_not_None(a):
"""
>>> is_not_None(1)
True
>>> is_not_None(None)
False
"""
return a is not None
@cython.test_fail_if_path_exists('//NotNode')
def not_is_not_None(a):
"""
>>> not_is_not_None(1)
False
>>> not_is_not_None(None)
True
"""
return not a is not None
@cython.test_fail_if_path_exists('//NotNode')
def not_is_None(a):
"""
>>> not_is_None(1)
True
>>> not_is_None(None)
False
"""
return not a is 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