fix isinstance() check against a tuple of extension types
authorStefan Behnel <scoder@users.berlios.de>
Sat, 29 Jan 2011 05:37:09 +0000 (06:37 +0100)
committerStefan Behnel <scoder@users.berlios.de>
Sat, 29 Jan 2011 05:37:09 +0000 (06:37 +0100)
Cython/Compiler/Optimize.py
tests/run/isinstance.pyx

index 77fdaac10d43e7ee502e509244aa5b2d6e661443..3139e4005e05998afed8e237bce4556d1e51141e 100644 (file)
@@ -1994,20 +1994,21 @@ class OptimizeBuiltinCalls(Visitor.EnvTransform):
                         builtin_type = entry.type
             if builtin_type and builtin_type is not Builtin.type_type:
                 type_check_function = entry.type.type_check_function(exact=False)
+                if type_check_function in tests:
+                    continue
+                tests.append(type_check_function)
                 type_check_args = [arg]
             elif test_type_node.type is Builtin.type_type:
                 type_check_function = '__Pyx_TypeCheck'
                 type_check_args = [arg, test_type_node]
             else:
                 return node
-            if type_check_function not in tests:
-                tests.append(type_check_function)
-                test_nodes.append(
-                    ExprNodes.PythonCapiCallNode(
-                        test_type_node.pos, type_check_function, self.Py_type_check_func_type,
-                        args = type_check_args,
-                        is_temp = True,
-                        ))
+            test_nodes.append(
+                ExprNodes.PythonCapiCallNode(
+                    test_type_node.pos, type_check_function, self.Py_type_check_func_type,
+                    args = type_check_args,
+                    is_temp = True,
+                    ))
 
         def join_with_or(a,b, make_binop_node=ExprNodes.binop_node):
             or_node = make_binop_node(node.pos, 'or', a, b)
index d11b3771c4d1c9de98975d98733fecdaabfd4010..ce9d0d135d7778d5ba621c646f7078d5f5b18f9d 100644 (file)
@@ -77,6 +77,23 @@ def test_custom():
     assert isinstance(A(), A)
     return True
 
+cdef class B:
+    pass
+
+cdef class C:
+    pass
+
+def test_custom_tuple(obj):
+    """
+    >>> test_custom_tuple(A())
+    True
+    >>> test_custom_tuple(B())
+    True
+    >>> test_custom_tuple(C())
+    False
+    """
+    return isinstance(obj, (A,B))
+
 def test_nested(x):
     """
     >>> test_nested(1)