Skip to content

Coercing a sliced C array to Python should create a list #2159

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

Open
scoder opened this issue Mar 17, 2018 · 3 comments
Open

Coercing a sliced C array to Python should create a list #2159

scoder opened this issue Mar 17, 2018 · 3 comments

Comments

@scoder
Copy link
Contributor

scoder commented Mar 17, 2018

Cython already copies a C array into a Python list automatically on object coercion. It should do the same for sliced C arrays, e.g.

def test():
    cdef int i
    cdef int a[100]
    for i in range(100):
        a[i] = i**2
    return a[:20]  # rejected by type analysis

Currently results in
Cannot convert 'int *' to Python object.

@robertwb
Copy link
Contributor

robertwb commented Mar 17, 2018 via email

@scoder
Copy link
Contributor Author

scoder commented Mar 17, 2018

Too late ;)
We already support this for plain arrays with known size, which coerce into a Python list. And I think a list makes more sense than a tuple because a Python list is the normal representation of a sequence of homogeneous objects. Tuples are more for heterogeneous objects.

@gabrieldemarmiesse
Copy link
Contributor

I'll note here for whoever wants to fix this issue the places in the documentation that will need to be changed accordingly:

return [x for x in my_array[:number]]

result_as_list = [prime for prime in p[:len_p]]

https://github.com/cython/cython/blob/master/docs/src/tutorial/cython_tutorial.rst#primes

https://github.com/cython/cython/blob/master/docs/src/userguide/language_basics.rst#automatic-type-conversions

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants