Commit cb440212 authored by scoder's avatar scoder Committed by GitHub

Merge pull request #2177 from AntyMew/master

Allow `NULL` to be coerced to a pointer to a fused type
parents 8ff9c50d f76f35ba
......@@ -850,6 +850,9 @@ class ExprNode(Node):
if src_type.is_fused:
error(self.pos, "Type is not specialized")
elif src_type.is_null_ptr and dst_type.is_ptr:
# NULL can be implicitly cast to any pointer type
return self
else:
error(self.pos, "Cannot coerce to a type that is not specialized")
......
......@@ -119,6 +119,38 @@ def test_fused_with_pointer():
print
print fused_with_pointer(string_array).decode('ascii')
cdef fused_type1* fused_pointer_except_null(fused_type1 x) except NULL:
if fused_type1 is string_t:
assert(bool(x))
else:
assert(x < 10)
return &x
def test_fused_pointer_except_null(value):
"""
>>> test_fused_pointer_except_null(1)
1
>>> test_fused_pointer_except_null(2.0)
2.0
>>> test_fused_pointer_except_null(b'foo')
foo
>>> test_fused_pointer_except_null(16)
Traceback (most recent call last):
AssertionError
>>> test_fused_pointer_except_null(15.1)
Traceback (most recent call last):
AssertionError
>>> test_fused_pointer_except_null(b'')
Traceback (most recent call last):
AssertionError
"""
if isinstance(value, int):
print fused_pointer_except_null(<cython.int>value)[0]
elif isinstance(value, float):
print fused_pointer_except_null(<cython.float>value)[0]
elif isinstance(value, bytes):
print fused_pointer_except_null(<string_t>value)[0].decode('ascii')
include "cythonarrayutil.pxi"
cpdef cython.integral test_fused_memoryviews(cython.integral[:, ::1] a):
......
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