开发者

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 (ca开发者_如何转开发n'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.


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.


It sounds to me as though you should create your own custom type and implement the methods in tp_as_sequence (and possibly tp_as_buffer) as appropriate.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜