modify a python list from C++ -


i got python list can obtain pointer , pass pointer address c++ work on

mypointer = thelist.as_pointer() 

now pass address c++ ctypes

in c++ can following:

*(float*) mypointer = 2.0f; //for example 

and python values update immediatly,now problem is: how extend or delete values (like modify list directly c++) sense these data std::vector how push_back , on adjust size fast way (as iterating in python pretty slow)

given pointer, cannot extend list. passing c++ address of array internally used implement python list. cannot std::vector, python lists not implemented stl vectors, c-level arrays of python object references. these arrays not contain backpointer python list object.

to mutate list, take @ the documentation , link c++ code python library. if must call c++ function ctypes, call id of list of list pointer list, opposed element array. can operate on list this:

#include <python.h>  extern "c" void append_to_list(uintptr_t lst_id) {     pyobject* lst = reinterpret_cast<pyobject*>(ptr);     pyobject* new_num = pyfloat_fromdouble(2.0);     if (!new_num) {         ... pyfloat creation failed ...     }     int ret = pylist_append(lst, new_num);     py_decref(new_num);     if (ret) {         ... pylist_append failed ...     } } 

Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -