Python Forum
How to create ctypes.c_void_p object from C++ and set it as claas attribute
Thread Rating:
  • 1 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to create ctypes.c_void_p object from C++ and set it as claas attribute
#1
Hi,

I'm embedding the Python interpreter into a C++ application. The target is to be able to prototype with arrayfire.

What I'm trying to do is pass an af::array from C++ to Python, do some computation and return something.

The arrayfire python bindings define an Array class which holds an attribute being the af::array reference. This attribute type is ctypes.c_void_p.

The Python code for this class base class is:
from .library import *
from .util import *

class BaseArray(object):
    """
    Base array class for arrayfire. For internal use only.
    """
    def __init__(self):
        self.arr = c_void_ptr_t(0)

[/icode]


I made a test script which looks like this:
[icode]import arrayfire as af
def test_Array(array):
    return af.min(array)
[/icode]

My C++ code looks like this:
[icode]int Interpreter::testArray()
{
    af::array myArray = af::seq(5, 10, 1);

    // Create arrayfire array and set 
    PyObject* afArray = createObject("arrayfire", "Array");

    // The 'arr' attribute is holding the af_array reference
    PyObject* attr = PyObject_GetAttrString(afArray, "arr");

    // Build an attribute object with our AF array reference
    PyObject* capsule = PyCapsule_New(myArray.get(), "c_void_p", nullptr);
    PyObject* attrObj = createObject("ctypes", "c_void_p", Py_BuildValue("(O)", capsule));

    // Set 'arr'
    int results = PyObject_SetAttr(afArray, PyUnicode_FromString("arr"), attrObj);

    // Call test function
    PyObject *pFunc = PyObject_GetAttrString(m_module, "test_Array");
    PyObject *pArgs = Py_BuildValue("(O)", afArray);

    if (pFunc)
    {
        PyObject *pResult = PyObject_CallObject(pFunc, pArgs);

        if (pResult)
        {
            float res = PyFloat_AsDouble(pResult);
            qDebug() << "Array min: " << res;
        }
        else
        {
            getError();
        }
        Py_XDECREF(pResult);
    }

    //...
    return 0;
}

PyObject* Interpreter::createObject(const QString& module, const QString& name, PyObject* args /*= nullptr*/)
{
    PyObject* pName = nullptr;
    PyObject* pModule = nullptr;
    PyObject* pDict = nullptr;
    PyObject* pClass = nullptr;
    PyObject* pInstance = nullptr;

    // Build the name object
    pName = PyUnicode_FromString(module.toLatin1());

    // Load the module object
    pModule = PyImport_Import(pName);

    if (!pModule)
    {
        qDebug() << "Unknown module name";
        return pInstance;
    }

    // pDict is a borrowed reference
    pDict = PyModule_GetDict(pModule);

    // Build the name of a callable class
    pClass = PyDict_GetItemString(pDict, name.toLatin1());

    // Create an instance of the class
    if (PyCallable_Check(pClass))
    {
        pInstance = PyObject_CallObject(pClass, args);
    }
    else
    {
        qDebug() << "Cannot instantiate the Python object";
    }

    return pInstance;
}
What is the proper way to set this arr attribute with a void*?

Thanks
Reply
#2
You need to place :'[code]'


at the start of code blocks you finish ok
but left out the start tag
Reply
#3
In response to my original question here's the code
//...

// The 'arr' attribute is holding the af_array reference
PyObject* attr = PyObject_GetAttrString(afArray, "arr");

// Build an attribute object with our AF array reference
PyObject* ctypes_mod = PyImport_ImportModule("ctypes");
PyObject* c_void_p = PyObject_GetAttrString(ctypes_mod, "c_void_p");
PyObject* p = PyObject_CallFunction(c_void_p, "O", PyLong_FromVoidPtr(my_void*));

// Set 'arr'
int results = PyObject_SetAttr(afArray, PyUnicode_FromString("arr"), p);
// ...
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  using ctypes to use a dll in a python module dauriac 3 379 Mar-06-2024, 04:38 PM
Last Post: dauriac
  Trying to debug segfault in ctypes binding to Fortran bthomas 1 603 Sep-01-2023, 11:26 AM
Last Post: bthomas
  AttributeError: '_tkinter.tkapp' object has no attribute 'username' Konstantin23 4 1,742 Aug-04-2023, 12:41 PM
Last Post: Konstantin23
  Python: AttributeError: 'PageObject' object has no attribute 'extract_images' Melcu54 2 3,917 Jun-18-2023, 07:47 PM
Last Post: Melcu54
  Object attribute behavior different in 2 scripts db042190 1 750 Jun-14-2023, 12:37 PM
Last Post: deanhystad
  Possible to create an object inside another object that is visible outside that objec MeghansUncle2 17 2,257 May-25-2023, 02:08 PM
Last Post: MeghansUncle2
  ctypes juliolop 7 1,401 Apr-20-2023, 03:33 PM
Last Post: Larz60+
  cx_oracle Error - AttributeError: 'function' object has no attribute 'cursor' birajdarmm 1 2,390 Apr-15-2023, 05:17 PM
Last Post: deanhystad
  Pandas AttributeError: 'DataFrame' object has no attribute 'concat' Sameer33 5 5,674 Feb-17-2023, 06:01 PM
Last Post: Sameer33
  WebDriver' object has no attribute 'find_element_by_css_selector rickadams 3 5,937 Sep-19-2022, 06:11 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020