Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support temporary scope #2869

Open
KmolYuan opened this Issue Mar 1, 2019 · 0 comments

Comments

Projects
None yet
1 participant
@KmolYuan
Copy link

KmolYuan commented Mar 1, 2019

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:
        ...

Any suggestions?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.