Commit 1fb5c964 authored by Robert Bradshaw's avatar Robert Bradshaw

Fix #632 isinstance bugs.

parent 5b7fac4a
...@@ -579,6 +579,8 @@ class ModuleNode(Nodes.Node, Nodes.BlockNode): ...@@ -579,6 +579,8 @@ class ModuleNode(Nodes.Node, Nodes.BlockNode):
#define PySet_CheckExact(obj) (Py_TYPE(obj) == &PySet_Type) #define PySet_CheckExact(obj) (Py_TYPE(obj) == &PySet_Type)
#endif #endif
#define __Pyx_TypeCheck(obj, type) PyObject_TypeCheck(obj, (PyTypeObject *)type)
#if PY_MAJOR_VERSION >= 3 #if PY_MAJOR_VERSION >= 3
#define PyIntObject PyLongObject #define PyIntObject PyLongObject
#define PyInt_Type PyLong_Type #define PyInt_Type PyLong_Type
......
...@@ -1986,20 +1986,26 @@ class OptimizeBuiltinCalls(Visitor.EnvTransform): ...@@ -1986,20 +1986,26 @@ class OptimizeBuiltinCalls(Visitor.EnvTransform):
test_nodes = [] test_nodes = []
env = self.current_env() env = self.current_env()
for test_type_node in types: for test_type_node in types:
if not test_type_node.entry: builtin_type = None
return node if isinstance(test_type_node, ExprNodes.NameNode):
entry = env.lookup(test_type_node.entry.name) if test_type_node.entry:
if not entry or not entry.type or not entry.type.is_builtin_type: entry = env.lookup(test_type_node.entry.name)
return node if entry and entry.type and entry.type.is_builtin_type:
type_check_function = entry.type.type_check_function(exact=False) builtin_type = entry.type
if not type_check_function: if builtin_type and builtin_type is not Builtin.type_type:
type_check_function = entry.type.type_check_function(exact=False)
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 return node
if type_check_function not in tests: if type_check_function not in tests:
tests.append(type_check_function) tests.append(type_check_function)
test_nodes.append( test_nodes.append(
ExprNodes.PythonCapiCallNode( ExprNodes.PythonCapiCallNode(
test_type_node.pos, type_check_function, self.Py_type_check_func_type, test_type_node.pos, type_check_function, self.Py_type_check_func_type,
args = [arg], args = type_check_args,
is_temp = True, is_temp = True,
)) ))
......
...@@ -5,8 +5,6 @@ from cpython.bool cimport bool ...@@ -5,8 +5,6 @@ from cpython.bool cimport bool
cdef class A: cdef class A:
pass pass
cdef an_A = A()
@cython.test_assert_path_exists('//SimpleCallNode//SimpleCallNode') @cython.test_assert_path_exists('//SimpleCallNode//SimpleCallNode')
@cython.test_fail_if_path_exists('//SimpleCallNode//PythonCapiCallNode', @cython.test_fail_if_path_exists('//SimpleCallNode//PythonCapiCallNode',
'//PythonCapiCallNode//SimpleCallNode') '//PythonCapiCallNode//SimpleCallNode')
...@@ -50,11 +48,11 @@ def test_optimised(): ...@@ -50,11 +48,11 @@ def test_optimised():
assert isinstance(complex(), complex) assert isinstance(complex(), complex)
assert not isinstance(u"foo", int) assert not isinstance(u"foo", int)
assert isinstance(A, type) assert isinstance(A, type)
assert isinstance(an_A, A) assert isinstance(A(), A)
cdef type typed_type = A cdef type typed_type = A
assert isinstance(an_A, typed_type) assert isinstance(A(), typed_type)
cdef object untyped_type = A cdef object untyped_type = A
assert isinstance(an_A, <type>untyped_type) assert isinstance(A(), <type>untyped_type)
return True return True
@cython.test_assert_path_exists('//PythonCapiCallNode') @cython.test_assert_path_exists('//PythonCapiCallNode')
...@@ -66,13 +64,11 @@ def test_optimised_tuple(): ...@@ -66,13 +64,11 @@ def test_optimised_tuple():
>>> test_optimised_tuple() >>> test_optimised_tuple()
True True
""" """
assert isinstance(int(), (int, long, float, bytes, str, unicode, tuple, list, dict, set, slice)) assert isinstance(int(), (int, long, float, bytes, str, unicode, tuple, list, dict, set, slice, A))
assert isinstance(list(), (int, long, float, bytes, str, unicode, tuple, list, dict, set, slice)) assert isinstance(list(), (int, long, float, bytes, str, unicode, tuple, list, dict, set, slice, A))
assert isinstance(A(), (int, long, float, bytes, str, unicode, tuple, list, dict, set, slice, A))
return True return True
@cython.test_assert_path_exists('//SimpleCallNode//SimpleCallNode')
@cython.test_fail_if_path_exists('//SimpleCallNode//PythonCapiCallNode',
'//PythonCapiCallNode//SimpleCallNode')
def test_custom(): def test_custom():
""" """
>>> test_custom() >>> test_custom()
......
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