尽管你在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