diff options
author | da-woods <dw-git@d-woods.co.uk> | 2019-10-24 10:25:13 +0100 |
---|---|---|
committer | da-woods <dw-git@d-woods.co.uk> | 2019-10-24 10:31:22 +0100 |
commit | 1b5b0a514d5e51e85a90dd891190f81d646ff47c (patch) | |
tree | 516ca9c4c746b541da48e203c8f36f05a39e11d6 | |
parent | 758aaad041dddef318f7eb476612d66eb8f7d082 (diff) | |
download | cython-1b5b0a514d5e51e85a90dd891190f81d646ff47c.tar.gz |
Potential fix for GH issue #3203
Gets the specialized type if possible from
NameNode.analyse_as_type
This does introduce a potential new bug:
```
cimport cython
just_float = cython.fused_type(float)
cdef OK1(just_float x):
return just_float in floating
cdef fail1(just_float x, floating y):
return just_float in floating
cdef fail2(floating x):
return floating in floating
def show():
"""
>>> show()
True
True
True
True
"""
print(OK1(1.0))
print(fail1(1.0, 2.0))
print(fail1[float, double](1.0, 2.0))
print(fail2[float](1.0))
```
fail1 and fail2 work before this patch but fail with it. It isn't
clear to me if this should actually be considered a bug. It
works in both versions with `cython.floating`, which possibly
suggests analyse_as_type in AttributeNode should also be changed
-rw-r--r-- | Cython/Compiler/ExprNodes.py | 6 | ||||
-rw-r--r-- | tests/run/fused_cpp.pyx | 16 |
2 files changed, 22 insertions, 0 deletions
diff --git a/Cython/Compiler/ExprNodes.py b/Cython/Compiler/ExprNodes.py index 81b8786b8..d9b754daa 100644 --- a/Cython/Compiler/ExprNodes.py +++ b/Cython/Compiler/ExprNodes.py @@ -1961,6 +1961,12 @@ class NameNode(AtomicExprNode): if not entry: entry = env.lookup(self.name) if entry and entry.is_type: + if entry.type.is_fused and env.fused_to_specific: + try: + return entry.type.specialize(env.fused_to_specific) + except KeyError: + pass # lots of valid reasons why we may not be able to get a specific type + # so don't fail return entry.type else: return None diff --git a/tests/run/fused_cpp.pyx b/tests/run/fused_cpp.pyx index c5a0d7d34..ff361e769 100644 --- a/tests/run/fused_cpp.pyx +++ b/tests/run/fused_cpp.pyx @@ -2,6 +2,8 @@ cimport cython from libcpp.vector cimport vector +from libcpp.typeinfo cimport type_info +from cython.operator cimport typeid def test_cpp_specialization(cython.floating element): """ @@ -14,3 +16,17 @@ def test_cpp_specialization(cython.floating element): cdef vector[cython.floating] *v = new vector[cython.floating]() v.push_back(element) print cython.typeof(v), cython.typeof(element), v.at(0) + +cdef fused C: + int + object + +cdef const type_info* tidint = &typeid(int) +def typeid_call(C x): + """ + For GH issue 3203 + >>> typeid_call(1) + True + """ + cdef const type_info* a = &typeid(C) + return a[0] == tidint[0] |