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

I have a library written using cython that wraps a C library, and I expose a few lines of c in python code. These lines are large and static (cannot free them), so just making a python line from them (which makes a copy) is not an option - I get OOM errors.

I have code working for python 2.x currently using the old buffer API that looks something like this:

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

This just works (tm) for python 2.x, but the old buffer API is missing in 3.x, and I can't figure out how to get this with the new one.

I see that there PyMemoryView_FromBuffer and PyBuffer_FillInfo , which are supposed to do the same, but PyBuffer_FillInfo wants an object that does not exist for me (this is just a module level function), creating a dummy object and passing it just gives me segfault. so I guess this object should somehow support the buffer ... but where is it documented?

further from the experiments with memory, they do not look at all or act like strings (or bytes), so I will either have to rewrite all my Python code, or somehow recreate this functionality.

Am I missing something? is there an easy way to replace PyBuffer_FromMemory in py3k?

Note. I use cython, but this is the source material of c-api, so you can respond without participating in cython.

+7
source share
2 answers

According to this thread, the second argument to PyBuffer_FillInfo is optional. Could you pass NULL to your place? If not, you can simply create an instance of PyBuffer yourself and fill in the appropriate fields.

+1
source

It seems to me that you should create your own custom type and implement methods in tp_as_sequence (and possibly tp_as_buffer ).

0
source

All Articles