Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.Sign up
Support temporary scope #2869
It is inconvenient to create a lot of single inline functions when controlling the lifecycle of temporary variables in a long function body.
Here is a kind of syntax of proposal, looks like context manager, which will create a C scope in the function body:
cdef c_struct *target_pointer = <c_struct *>PyMem_Malloc(sizeof(c_struct) * vector_of_c_struct.size()) with cython.scope( i=cython.int, # can use c types here b=c_struct, ) as _tmp: # '_tmp' is a dummy object represent the anonymous namespace of scope _tmp.i = 0 for _tmp.b in vector_of_c_struct: target_pointer[_tmp.i] = _tmp.b _tmp.i += 1 # can't use the variables of '_tmp' in outer scope but can create it multiple times
This syntax will also force the iterator object will not remain assignment variable after iteration.
with cython.scope(link=str) as _tmp: for _tmp.link in link_names: ... with cython.scope(link=CLink) as _tmp: for _tmp.link in link_vector: ...