7

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.

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Sorry somewhat forgot about this question... After discussing it on cython-users i went in a different direction - Wrapping the data in my own object that uses the new buffer protocol and emulating some of the old-style buffer objects functionality with it. I will mark this answers as accept regardless because it was the most useful one :)
@bdew can you share your end result? I am trying to do the same thing and I find the documentation unhelpful, the new buffer API is more heavyweight and all I've gained so far is memory leaks and crashes.. The mailing list link is dead btw.
0

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.

1 Comment

Your links are to old-style buffer API docs (for python 2.7), the new API seems to use completely different slots. but thanks for the pointer, those are probably the methods i need to implement if i wrap this up in an object.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.