English 中文(简体)
C. 返回CCTypes探测器
原标题:Return CTypes pointer from C

I m 撰写一份“灰色延伸”文件,需要把一个CTypes探测器送回一个“<>条码>>的阵容(我需要与另一个希望一个CTypes点人的“灰色”图书馆接口)。

我找不到任何关于C任何类型的CTypes界面的文件。 是否有任何工作环绕,如叫 Python斯底建筑商?

static PyObject *get_pointer(myObject *self)
{
    char *my_pointer = self->internal_pointer;
    return PyCTypes_Pointer(my_pointer); /* how do I turn  my_pointer  into 
              a Python object representing a CTypes pointer? */
}

提前感谢。

问题回答

尽管你在C区重新编制方案,但还是试图利用Talker。 可悲的是,我不了解C类的CTypes接口, could在来源中发现任何 h。 也许你能够用以下文字书写你自己的<代码>PyCTypes_Pointer方法。

我没有做任何清理、错误检查或处理,而是用2.7和3.4代我工作。

#include <Python.h>

static const char *test = "Testing Python ctypes char pointer ..."; 

static PyObject *c_char_p = NULL;
static PyObject *c_void_p = NULL;
static PyObject *cast = NULL;

static PyObject * char_p ( void ) {
    PyObject *p = PyLong_FromVoidPtr((void *) test);
    p = PyObject_CallFunction(c_void_p, "O", p);
    return PyObject_CallFunction(cast, "OO", p, c_char_p);
}

static PyObject *len ( void ) {
    return PyLong_FromSize_t(strlen(test));
}

static PyMethodDef methods[] = {
    {"char_p", (PyCFunction) char_p, METH_NOARGS, ""},
    {"len", (PyCFunction) len, METH_NOARGS, ""},
    { NULL }
};

#if PY_MAJOR_VERSION >= 3
static PyModuleDef module = {PyModuleDef_HEAD_INIT, "pyt", "", -1, methods,
    NULL, NULL, NULL, NULL};
#define INIT_FUNC PyInit_pyp
#else
#define INIT_FUNC initpyp
#endif

PyMODINIT_FUNC INIT_FUNC ( void ) {
    PyObject* m;

    PyObject *ctypes = PyImport_ImportModule("ctypes");
    PyObject *c_char = PyObject_GetAttrString(ctypes, "c_char");
    c_char_p = PyObject_CallMethod(ctypes, "POINTER", "O", c_char);
    c_void_p = PyObject_GetAttrString(ctypes, "c_void_p");
    cast = PyObject_GetAttrString(ctypes, "cast");

#if PY_MAJOR_VERSION >= 3
    m = PyModule_Create(&module);
#else
    m = Py_InitModule("pyp", methods);
#endif

    return (PyMODINIT_FUNC) m;
}

正义的建立

from __future__ import print_function
from distutils.core import Extension, setup
import sys
sys.argv.extend(["build_ext", "-i"])
setup(ext_modules = [Extension( pyp , [ pyp.c ])])
import pyp
c = pyp.char_p()
print(c[:pyp.len()])

并取得类似产出

<class  ctypes.LP_c_char > Testing Python ctypes char pointer ...

如果你只重新处理由零污染的扼杀物,你可能使用<条码>c 类型.c_char_p。 页: 1





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签