Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exposing a C string without copying to python 3.x code

I have a library written with cython that wraps a C library, and i'm exposing a few c strings into python code. Those strings are large, and static (can't deallocate them) so just making a python string from them (that makes a copy) is not an option - i get OOM errors.

I have the code working for python 2.x currently using the old buffer API, which looks more or less like:

def get_foo():
     return PyBuffer_FromMemory(c_foo_ptr,c_foo_len)

That just works (tm) for python 2.x, but the old buffer API is gone in 3.x and i can't figure out how do i get that with a the new one.

I see that there's PyMemoryView_FromBuffer and PyBuffer_FillInfo which combined will supposedly do the same thing, but PyBuffer_FillInfo wants an object which doesn't exist for me (it's just a module-level function), making a dummy object and passing it just gives me a segfault, so i guess that object should suport the buffer somehow... but where it's documented?

further from experimenting with memoryviews they don't look or act as strings (or bytes) at all, so i'll have to either rewrite all my python code or somehow recreate that functionality.

Am i missing something? is there a simple way to replace PyBuffer_FromMemory in py3k?

Note: I'm using cython, but this is raw c-api stuff, so you can answer without involving cython into it.

like image 214
bdew Avatar asked Feb 16 '11 11:02

bdew


1 Answers

According to this thread the second argument to PyBuffer_FillInfo is optional. Could you pass NULL in its place? If not you could just create a PyBuffer instance yourself and fill the appropriate fields.

like image 167
macedoine Avatar answered Nov 02 '22 06:11

macedoine