Skip to content

Commit

Permalink
Convert a test to language_level=3.
Browse files Browse the repository at this point in the history
  • Loading branch information
scoder committed Aug 26, 2023
1 parent 7c3d175 commit 310720f
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 42 deletions.
83 changes: 42 additions & 41 deletions tests/run/fused_types.pyx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# mode: run
# ticket: 1772
# cython: language_level=3

cimport cython
from cython.view cimport array
Expand Down Expand Up @@ -29,14 +30,14 @@ def test_pure():
10
"""
mytype = pure_cython.typedef(pure_cython.fused_type(int, long, complex))
print mytype(10)
print(mytype(10))


cdef cdef_func_with_fused_args(fused_type1 x, fused_type1 y, fused_type2 z):
if fused_type1 is string_t:
print x.decode('ascii'), y.decode('ascii'), z.decode('ascii')
print(x.decode('ascii'), y.decode('ascii'), z.decode('ascii'))
else:
print x, y, z.decode('ascii')
print(x, y, z.decode('ascii'))

return x + y

Expand All @@ -50,16 +51,16 @@ def test_cdef_func_with_fused_args():
4.2 8.6 bunny
12.8
"""
print cdef_func_with_fused_args(b'spam', b'ham', b'eggs').decode('ascii')
print cdef_func_with_fused_args(10, 20, b'butter')
print cdef_func_with_fused_args(4.2, 8.6, b'bunny')
print(cdef_func_with_fused_args(b'spam', b'ham', b'eggs').decode('ascii'))
print(cdef_func_with_fused_args(10, 20, b'butter'))
print(cdef_func_with_fused_args(4.2, 8.6, b'bunny'))

cdef fused_type1 fused_with_pointer(fused_type1 *array):
for i in range(5):
if fused_type1 is string_t:
print array[i].decode('ascii')
print(array[i].decode('ascii'))
else:
print array[i]
print(array[i])

obj = array[0] + array[1] + array[2] + array[3] + array[4]
# if cython.typeof(fused_type1) is string_t:
Expand Down Expand Up @@ -113,13 +114,13 @@ def test_fused_with_pointer():
s = strings[i]
string_array[i] = s

print fused_with_pointer(int_array)
print
print fused_with_pointer(long_array)
print
print fused_with_pointer(float_array)
print
print fused_with_pointer(string_array).decode('ascii')
print(fused_with_pointer(int_array))
print()
print(fused_with_pointer(long_array))
print()
print(fused_with_pointer(float_array))
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:
Expand Down Expand Up @@ -148,13 +149,13 @@ def test_fused_pointer_except_null(value):
"""
if isinstance(value, int):
test_int = cython.declare(cython.int, value)
print fused_pointer_except_null(&test_int)[0]
print(fused_pointer_except_null(&test_int)[0])
elif isinstance(value, float):
test_float = cython.declare(cython.float, value)
print fused_pointer_except_null(&test_float)[0]
print(fused_pointer_except_null(&test_float)[0])
elif isinstance(value, bytes):
test_str = cython.declare(string_t, value)
print fused_pointer_except_null(&test_str)[0].decode('ascii')
print(fused_pointer_except_null(&test_str)[0].decode('ascii'))

include "../testsupport/cythonarrayutil.pxi"

Expand Down Expand Up @@ -183,7 +184,7 @@ cdef test_specialize(fused_type1 x, fused_type1 *y, composed_t z, other_t *a):
cdef fused_type1 result

if composed_t is p_double:
print "double pointer"
print("double pointer")

if fused_type1 in floating:
result = x + y[0] + z[0] + a[0]
Expand Down Expand Up @@ -234,7 +235,7 @@ def test_specializations():
# print

cdef opt_args(integral x, floating y = 4.0):
print x, y
print(x, y)

def test_opt_args():
"""
Expand All @@ -251,7 +252,7 @@ def test_opt_args():

class NormalClass(object):
def method(self, cython.integral i):
print cython.typeof(i), i
print(cython.typeof(i), i)

def test_normal_class():
"""
Expand All @@ -270,7 +271,7 @@ def test_normal_class_refcount():
x = NormalClass()
c = sys.getrefcount(x)
x.method[pure_cython.short](10)
print sys.getrefcount(x) - c
print(sys.getrefcount(x) - c)

def test_fused_declarations(cython.integral i, cython.floating f):
"""
Expand All @@ -289,9 +290,9 @@ def test_fused_declarations(cython.integral i, cython.floating f):
assert cython.typeof(squared_int) == cython.typeof(i)
assert cython.typeof(squared_float) == cython.typeof(f)

print cython.typeof(squared_int)
print cython.typeof(squared_float)
print '%d %.2f' % (squared_int, squared_float)
print(cython.typeof(squared_int))
print(cython.typeof(squared_float))
print('%d %.2f' % (squared_int, squared_float))

def test_sizeof_fused_type(fused_type1 b):
"""
Expand Down Expand Up @@ -336,8 +337,8 @@ def test_fused_memslice_dtype(cython.floating[:] array):
print(cython.typeof(array))
return
cdef cython.floating[:] otherarray = array[0:100:1]
print cython.typeof(array), cython.typeof(otherarray), \
array[5], otherarray[6]
print(cython.typeof(array), cython.typeof(otherarray),
array[5], otherarray[6])
cdef cython.floating value;
cdef cython.floating[:] test_cast = <cython.floating[:1:1]>&value

Expand All @@ -356,7 +357,7 @@ def test_fused_memslice_dtype_repeated(cython.floating[:] array1, cython.floatin
Traceback (most recent call last):
ValueError: Buffer dtype mismatch, expected 'double' but got 'float'
"""
print cython.typeof(array1), cython.typeof(array2)
print(cython.typeof(array1), cython.typeof(array2))

def test_fused_memslice_dtype_repeated_2(cython.floating[:] array1, cython.floating[:] array2,
fused_type3[:] array3):
Expand All @@ -373,7 +374,7 @@ def test_fused_memslice_dtype_repeated_2(cython.floating[:] array1, cython.float
>>> test_fused_memslice_dtype_repeated_2(get_array(4, 'f'), get_array(4, 'f'), get_intc_array())
float[:] float[:] int[:]
"""
print cython.typeof(array1), cython.typeof(array2), cython.typeof(array3)
print(cython.typeof(array1), cython.typeof(array2), cython.typeof(array3))

def test_fused_const_memslice_dtype_repeated(const cython.floating[:] array1, cython.floating[:] array2):
"""Test fused types memory view with one being const
Expand All @@ -389,7 +390,7 @@ def test_fused_const_memslice_dtype_repeated(const cython.floating[:] array1, cy
Traceback (most recent call last):
ValueError: Buffer dtype mismatch, expected 'double' but got 'float'
"""
print cython.typeof(array1), cython.typeof(array2)
print(cython.typeof(array1), cython.typeof(array2))

def test_cython_numeric(cython.numeric arg):
"""
Expand All @@ -399,7 +400,7 @@ def test_cython_numeric(cython.numeric arg):
>>> test_cython_numeric(10.0 + 1j)
double complex (10+1j)
"""
print cython.typeof(arg), arg
print(cython.typeof(arg), arg)


cdef fused int_t:
Expand Down Expand Up @@ -429,15 +430,15 @@ def test_pylong(int_t i):
Traceback (most recent call last):
KeyError: ...
"""
print cython.typeof(i)
print(cython.typeof(i))


cdef fused ints_t:
int
long

cdef _test_index_fused_args(cython.floating f, ints_t i):
print cython.typeof(f), cython.typeof(i)
print(cython.typeof(f), cython.typeof(i))

def test_index_fused_args(cython.floating f, ints_t i):
"""
Expand All @@ -448,7 +449,7 @@ def test_index_fused_args(cython.floating f, ints_t i):
_test_index_fused_args[cython.floating, ints_t](f, i)

cdef _test_index_const_fused_args(const cython.floating f, const ints_t i):
print(cython.typeof(f), cython.typeof(i))
print((cython.typeof(f), cython.typeof(i)))

def test_index_const_fused_args(const cython.floating f, const ints_t i):
"""Test indexing function implementation with const fused type args
Expand Down Expand Up @@ -478,9 +479,9 @@ def test_composite(fused_composite x):
cdef cdef_func_const_fused_arg(const cython.floating val,
const fused_type1 * ptr_to_const,
const (cython.floating *) const_ptr):
print(val, cython.typeof(val))
print(ptr_to_const[0], cython.typeof(ptr_to_const[0]))
print(const_ptr[0], cython.typeof(const_ptr[0]))
print((val, cython.typeof(val)))
print((ptr_to_const[0], cython.typeof(ptr_to_const[0])))
print((const_ptr[0], cython.typeof(const_ptr[0])))

ptr_to_const = NULL # pointer is not const, value is const
const_ptr[0] = 0.0 # pointer is const, value is not const
Expand Down Expand Up @@ -615,13 +616,13 @@ def test_null_default():
cdef double xd = 2.
cdef double xd_minus_1
result = null_default(xd, &xd_minus_1)
print result, xd_minus_1
print(result, xd_minus_1)
result = null_default(xd)
print result
print(result)

cdef float xf = 2.
cdef float xf_minus_1
result = null_default(xf, &xf_minus_1)
print result, xf_minus_1
print(result, xf_minus_1)
result = null_default(xf)
print result
print(result)
2 changes: 1 addition & 1 deletion tests/testsupport/cythonarrayutil.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ cimport cython
from cython.view cimport array

cdef void callback(void *data) noexcept:
print "callback called"
print("callback called")
free(data)

def create_array(shape, mode, use_callback=False):
Expand Down

0 comments on commit 310720f

Please sign in to comment.