【问题标题】:Accessing the c pointer to structure in python在python中访问指向结构的c指针
【发布时间】:2014-04-29 17:19:46
【问题描述】:

是否可以将int 转换为类类型?

我在 C 中有以下代码:

#include "Python.h"

#define PYTHON_FILENAME "modelparam"

void getmodelparam(long pModelParam) ;

typedef struct {
 int seconds;
 int nanoseconds;
} someTime;


int main ()
{
someTime *pSome ;
long a ;
printf ("Testing the python interfaces\n") ;

pSome = (someTime *) calloc(1, sizeof(someTime)) ;

pSome->seconds = 10 ;
pSome->nanoseconds = 20 ;

a = (long) pSome ;
printf ("a is %d, pSome is %d\n", a, pSome) ;

getmodelparam(a) ;  

printf ("After the python call values are : %d, %d\n",pSome->seconds, pSome->nanoseconds) ; 

return 0 ;

}
void getmodelparam(long pModelParam)
{
    PyObject    *pName ;
PyObject    *pModule ; 
PyObject    *pDict ;
PyObject    *pFunc ;
int         iSize = 0 ;
char        pcFunctionName[] = "modifymodelparam" ;

double      dTemp1, dTemp2 ; 

/* Initialize the python interpreter */
Py_Initialize() ;

/* Get Python code/module */
pName = PyUnicode_FromString(PYTHON_FILENAME);
if (NULL != pName)
{
    /* Import the module equivalent to doing 'import calresidual' in python */
    pModule = PyImport_Import(pName);
    Py_DECREF(pName) ;
    if (NULL != pModule)
    {
        /* Get the function and check if its callable function */   
        pFunc = PyObject_GetAttrString(pModule, pcFunctionName);
        if (pFunc && PyCallable_Check(pFunc))
        {
            /* Build the input arguments */
            PyObject *pResult = PyObject_CallFunction(pFunc,"i", pModelParam) ;
        }
        else
        {
            printf ("Some error with the function\n") ;
        }
    }
    else
    {
        printf ("Couldnt load the module %s\n", PYTHON_FILENAME) ;
    }
}
else
{
    printf ("Couldnt convert the name of the module to python name\n") ;
}
/* Release the resources. */
Py_DECREF(pModule) ;
Py_DECREF(pFunc) ;
Py_DECREF(pName) ;

/*Release the interpreter */
Py_Finalize() ;
}

在 Python 代码中:

import ctypes

class someTime(ctypes.Structure):
     _fields_ = [("seconds", ctypes.c_uint),
                 ("nanoseconds", ctypes.c_uint)]  

def modifymodelparam(m):
# Not sure how to access internal elements using m ?? 
    # How to typecast m ?? 
    n = someTime(m)
print ('Seconds', n.seconds)

如何将从 C 传递的地址类型转换为 Python 中的类类型,以便我可以访问这些类参数或间接说访问结构参数?

【问题讨论】:

    标签: python embedding python-c-api cpython c-api


    【解决方案1】:

    上面的代码有一些错误。

    需要将指针地址作为 unsigned long/int 传递给 python 模块。所以

    void getmodelparam(long pModelParam)
    
    //has to become
    
    void getmodelparam(unsigned long pModelParam)
    
    //and 
    
    PyObject *pResult = PyObject_CallFunction(pFunc,"i", pModelParam) ;
    
    // has to become 
    
    PyObject *pResult = PyObject_CallFunction(pFunc,"k", pModelParam) ;
    

    然后在python文件中:

    def modifymodelparam(m):
        n = ctypes.cast(m, ctypes.POINTER(someTime))
        print (n.contents.seconds)
        print (n.contents.nanoseconds)
    

    【讨论】:

      【解决方案2】:

      要进行类型转换,可以使用强制转换函数

      n = ctypes.cast(m, someTime)

      [谷歌]http://docs.python.org/2/library/ctypes.html

      【讨论】:

      • 这个我已经试过了。但我无法通过执行同一文档中列出的 n.contents 或 n[0] 来获取内容。
      猜你喜欢
      • 2017-01-04
      • 1970-01-01
      • 2021-07-14
      • 2011-08-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-25
      相关资源
      最近更新 更多