Commit 9cb557c3 authored by da-woods's avatar da-woods Committed by GitHub

Handle `for x in cpp_function_call()` (GH-3667)

Fixes https://github.com/cython/cython/issues/3663

This ensures that rvalues here are saved as temps, while keeping the
existing behaviour for `for x in deref(vec)`, where the pointer for vec
is copied, meaning it doesn't crash if vec is reassigned.

The bit of this change liable to have the biggest effect is that I've
changed the result type of dereference(x) and x[0] (where x is a c++
type) to a reference rather than value type. I think this is OK because
it matches what C++ does. If that isn't a sensible change then I can
probably inspect the loop sequence more closely to try to detect this.
parent c42ad917
This diff is collapsed.
......@@ -140,3 +140,40 @@ def test_iteration_in_generator_reassigned():
if vint is not orig_vint:
del vint
del orig_vint
cdef extern from *:
"""
std::vector<int> make_vec1() {
std::vector<int> vint;
vint.push_back(1);
vint.push_back(2);
return vint;
}
"""
cdef vector[int] make_vec1() except +
cdef vector[int] make_vec2() except *:
return make_vec1()
cdef vector[int] make_vec3():
try:
return make_vec1()
except:
pass
def test_iteration_from_function_call():
"""
>>> test_iteration_from_function_call()
1
2
1
2
1
2
"""
for i in make_vec1():
print(i)
for i in make_vec2():
print(i)
for i in make_vec3():
print(i)
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