Commit 325641c5 authored by 0dminnimda's avatar 0dminnimda Committed by GitHub

Make ctypedefed bint behave like normal bint (GH-4660)

Closes https://github.com/cython/cython/issues/3066
parent 756b7955
......@@ -4929,14 +4929,19 @@ def independent_spanning_type(type1, type2):
type1 = type1.ref_base_type
else:
type2 = type2.ref_base_type
if type1 == type2:
resolved_type1 = type1.resolve()
resolved_type2 = type2.resolve()
if resolved_type1 == resolved_type2:
return type1
elif (type1 is c_bint_type or type2 is c_bint_type) and (type1.is_numeric and type2.is_numeric):
elif ((resolved_type1 is c_bint_type or resolved_type2 is c_bint_type)
and (type1.is_numeric and type2.is_numeric)):
# special case: if one of the results is a bint and the other
# is another C integer, we must prevent returning a numeric
# type so that we do not lose the ability to coerce to a
# Python bool if we have to.
return py_object_type
span_type = _spanning_type(type1, type2)
if span_type is None:
return error_type
......
from __future__ import print_function
from cython cimport typeof
ctypedef bint mybool
cdef mybool mybul = True
cdef bint bul = True
cdef int num = 42
def CondExprNode_to_obj(test):
"""
>>> CondExprNode_to_obj(True)
Python object | Python object
2
>>> CondExprNode_to_obj(False)
Python object | Python object
84
"""
print(typeof(mybul if test else num), "|", typeof(bul if test else num))
return (mybul if test else num) + (bul if test else num)
def BoolBinopNode_to_obj():
"""
>>> BoolBinopNode_to_obj()
Python object | Python object
2
"""
print(typeof(mybul or num), "|", typeof(bul or num))
return (mybul or num) + (bul or num)
cdef int test_bool(mybool arg):
return <int>arg
def CondExprNode_to_bool(test):
"""
>>> CondExprNode_to_bool(True)
bint | bint
0
>>> CondExprNode_to_bool(False)
bint | bint
2
"""
print(typeof(not mybul if test else mybul), "|", typeof(not bul if test else bul))
# test_bool() would silently crash if one of the types is cast
# to Python object and not just assigned.
# It happens when a type is wrongly inferred as Python object
# instead of bint or mybool.
return test_bool(not mybul if test else mybul) + test_bool(not bul if test else bul)
def BoolBinopNode_to_bool():
"""
>>> BoolBinopNode_to_bool()
bint | bint
2
"""
print(typeof(not mybul or mybul), "|", typeof(not bul or bul))
return test_bool(not mybul or mybul) + test_bool(not bul or bul)
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