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

Allow bytearray as input #35

Open
Downchuck opened this issue Sep 25, 2015 · 5 comments
Open

Allow bytearray as input #35

Downchuck opened this issue Sep 25, 2015 · 5 comments

Comments

@Downchuck
Copy link

Currently the bytes_to_slice does not support bytearray; bytearray is just a standard/compatible pointer.

batch.put(key, bytearray("data"))
@Downchuck
Copy link
Author

Possibly related:
https://github.com/cython/cython/blob/master/Cython/Includes/cpython/bytes.pxd#L77

Seems to suggest that arbitrary binary data would be converted to Unicode.
EDIT: Doesn't seem to be an issue. Still, it seems that a direct cast to char* would work fine.

@Downchuck
Copy link
Author

Not sure how safe it is, but this lets me pass in bytearray:

+++ b/rocksdb/_rocksdb.pyx
 cdef Slice bytes_to_slice(ob) except *:
-    return Slice(PyBytes_AsString(ob), PyBytes_Size(ob))
+    return Slice(<char*>ob, len(ob))

@Downchuck
Copy link
Author

@stephan-hof What are your thoughts on this one?
You could do an if(isinstance(ob, bytearray)) to support it.

@stephan-hof
Copy link
Owner

Hi @Downchuck I think its a good idea to support byte-array (and possibly memoryview, buffer, ...).
I have also seen that you opened/commented on other issues, so I guess you are currently working with the stuff.
Unfortunately I'm currently super busy with other things, so I don't have much time to dive into the issues right now. I'll have a look at them end of next week.

@Downchuck
Copy link
Author

I tried to support memoryview in another cython project -- I did not succeed.

bytearray was easy, as it was just a raw char*. It seems like memoryview and buffer both need their own work, futher, I was trying to pass a memoryviewslice from another cython build (via runcython).

Anyway, that single-line item seems to handle python bytes() and bytearray().

mosquito pushed a commit to mosquito/python-rocksdb-static that referenced this issue Aug 16, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants