Commit 8d79bfe0 authored by Robert Bradshaw's avatar Robert Bradshaw Committed by Stefan Behnel

Perform typecheck for (typechecking) builtin casts.

parent 6e94a674
...@@ -8022,6 +8022,7 @@ class TypecastNode(ExprNode): ...@@ -8022,6 +8022,7 @@ class TypecastNode(ExprNode):
# operand ExprNode # operand ExprNode
# base_type CBaseTypeNode # base_type CBaseTypeNode
# declarator CDeclaratorNode # declarator CDeclaratorNode
# typecheck boolean
# #
# If used from a transform, one can if wanted specify the attribute # If used from a transform, one can if wanted specify the attribute
# "type" directly and leave base_type and declarator to None # "type" directly and leave base_type and declarator to None
...@@ -8082,7 +8083,7 @@ class TypecastNode(ExprNode): ...@@ -8082,7 +8083,7 @@ class TypecastNode(ExprNode):
else: else:
warning(self.pos, "No conversion from %s to %s, python object pointer used." % (self.type, self.operand.type)) warning(self.pos, "No conversion from %s to %s, python object pointer used." % (self.type, self.operand.type))
elif from_py and to_py: elif from_py and to_py:
if self.typecheck and self.type.is_extension_type: if self.typecheck and self.type.is_pyobject:
self.operand = PyTypeTestNode(self.operand, self.type, env, notnone=True) self.operand = PyTypeTestNode(self.operand, self.type, env, notnone=True)
elif isinstance(self.operand, SliceIndexNode): elif isinstance(self.operand, SliceIndexNode):
# This cast can influence the created type of string slices. # This cast can influence the created type of string slices.
......
...@@ -101,3 +101,14 @@ def test_getFooCast(): ...@@ -101,3 +101,14 @@ def test_getFooCast():
cdef int old_count = count cdef int old_count = count
cdef Foo x = <Foo?>getFoo() cdef Foo x = <Foo?>getFoo()
return count - old_count return count - old_count
def test_builtin_typecheck_cast(maybe_list):
"""
>>> test_builtin_typecheck_cast([])
[]
>>> test_builtin_typecheck_cast({})
Traceback (most recent call last):
...
TypeError: Expected list, got dict
"""
return <list?>maybe_list
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