[Cython] Type inference and C++
Vitja Makarov
vitja.makarov at gmail.com
Mon Jul 23 21:28:48 CEST 2012
2012/7/23 Vitja Makarov <vitja.makarov at gmail.com>:
> Here is part of original testcase:
>
> from cython.operator cimport dereference as d
> from cython.operator cimport preincrement as incr
> from libcpp.vector cimport vector
>
> def reverse_iteration_test(L):
> v = new vector[int]()
> for a in L:
> v.push_back(a)
> it = v.rbegin()
> while it != v.rend():
> a = d(it)
> incr(it)
> print(a)
>
> It doesn't work with local type inference enabled since `a` in
> for-loop is infered as pyobject and in while loop it's inferred as
> reverse_iterator.
>
> Then I tried to comment out for loop and compile it with
> upstream/master, same error here:
>
> note: cpp_stl_vector.pyx:8:6: inferred 'v' to be of type 'vector[int] *'
> note: cpp_stl_vector.pyx:11:7: inferred 'it' to be of type 'reverse_iterator'
> note: cpp_stl_vector.pyx:13:10: inferred 'a' to be of type 'reverse_iterator'
>
> Error compiling Cython file:
> ------------------------------------------------------------
> ...
> v = new vector[int]()
> #for a in L:
> # v.push_back(a)
> it = v.rbegin()
> while it != v.rend():
> a = d(it)
> ^
> ------------------------------------------------------------
>
> cpp_stl_vector.pyx:13:13: Cannot assign type 'int &' to 'reverse_iterator'
>
> Error compiling Cython file:
> ------------------------------------------------------------
> ...
> # v.push_back(a)
> it = v.rbegin()
> while it != v.rend():
> a = d(it)
> incr(it)
> print(a)
> ^
> ------------------------------------------------------------
>
> cpp_stl_vector.pyx:15:15: Cannot convert 'reverse_iterator' to Python object
>
>
> I think it's not correct to infer `a` as reverse_iterator because it's
> not an iterator it's vector's item.
>
It seems that dereference is the only way we can get iterator's value,
but UnopNode doesn't respect overloaded operator*()
--
vitja.
More information about the cython-devel
mailing list