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

cython array in pure python mode #2678

Closed
h5rdly opened this Issue Oct 23, 2018 · 9 comments

Comments

Projects
None yet
2 participants
@h5rdly

h5rdly commented Oct 23, 2018

Cheers for this awesome project.

I read the Pure Mode tutorial, but couldn't find this specific item - how can i from cpython cimport array in pure python mode, inside a ,py file that has import cython?

Thanks,
Eli

@scoder

This comment has been minimized.

Contributor

scoder commented Oct 23, 2018

Could you explain your use case? The C interface and the Python interface to the array.array type are not really interchangeable, so writing Python code that uses the C interface when compiled is probably not easy. You might be able to get away with memory views, though.

@h5rdly

This comment has been minimized.

h5rdly commented Oct 23, 2018

Hi,

My case is unpacking same-type data coming from a socket - I'm recieving bytestreams of ints, floats etc.
I'm trying to do better than struct.unpack(), and thought cython arrays might be the way to go, but the infrastructure is such that other people are around the code as well, so I prefer use pure mode if possible.

Are you talking about using cython memoryviews with python arrays?

@scoder

This comment has been minimized.

Contributor

scoder commented Oct 23, 2018

Yes, that's what I meant. In Py3, they should work with array.array without further setup, also from Python code. And indexing/slicing should also work nicely for both environments, with the same syntax.

@h5rdly

This comment has been minimized.

h5rdly commented Oct 23, 2018

What if we have 2.7 at work? Anything to be done?

@scoder

This comment has been minimized.

Contributor

scoder commented Oct 24, 2018

I recall that array.array doesn't support the PEP 3118 buffer interface in Py2.x, so memory views can't work there (without a bunch of special-casing code in Cython – we used that for NumPy at some point, before it got native support, many releases back).

@h5rdly

This comment has been minimized.

h5rdly commented Oct 27, 2018

Guess I wait until we upgrade to 3.7. Still sound odd that you can declare all regular c types in pure python mode with @cython.loclas, but can't declare c arrays, would be super beneficial

@h5rdly

This comment has been minimized.

h5rdly commented Oct 31, 2018

How about ctypes arrays? Can they be used with cython memoryview?

@scoder

This comment has been minimized.

Contributor

scoder commented Nov 1, 2018

The PEP 3118 buffer interface was originally only added for Py3 and then backported to Py2, but never applied to the standard data types there, let alone the standard library. The support in Py3 was still improved over the first couple of CPython 3.x releases. I'd be surprised if ctypes had any support for it at all, even in Py3.

@scoder

This comment has been minimized.

Contributor

scoder commented Nov 18, 2018

I'll close this as a question that was answered. Not something to do on Cython side.

@scoder scoder closed this Nov 18, 2018

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