37,718
社区成员
发帖
与我相关
我的任务
分享
#include "windows.h"
#include "stdio.h"
#include "python.h"
class test
{
public :
test()
: levl(0)
{
(*name) = 0;
}
void setName(const char _name[])
{
memcpy(name, _name, sizeof(name));
}
const char* getName() const
{
return name;
}
void setLevel(int _levl)
{
levl = _levl;
}
int getLevel() const
{
return levl;
}
static void _pyInit(test* self, PyObject* args)
{
(*self->name) = 0;
self->levl = 0;
}
static void _pyDest(test* self)
{
Py_TYPE(self)->tp_free((PyObject*)self);
}
static PyObject* _py_setName(test* self, PyObject* args)
{
Py_INCREF(Py_None);
const char* name = 0;
if (PyArg_ParseTuple(args, "s", &name))
{
strncpy(self->name, name, sizeof(self->name));
}
return Py_None;
}
public :
PyObject_HEAD
char name[128];
int levl;
};
static PyMethodDef _py_test_method[] =
{
{ "setName", (PyCFunction)test::_py_setName, METH_VARARGS, "get name" },
{ NULL, NULL, NULL, NULL }
};
static PyTypeObject _py_test_class =
{
PyVarObject_HEAD_INIT(NULL, 0)
"test",
sizeof(test),
0,
(destructor)test::_pyDest,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
"test class",
0,
0,
0,
0,
0,
0,
_py_test_method,
0,
0,
0,
0,
0,
0,
0,
(initproc)test::_pyInit,
0,
};
static PyModuleDef _py_test_module =
{
PyModuleDef_HEAD_INIT,
"test_module",
"test_module",
-1,
NULL,
NULL,
NULL,
NULL,
NULL
};
int main()
{
Py_Initialize();
if (!Py_IsInitialized())
return 1;
PyObject* module = 0;
_py_test_class.tp_new = PyType_GenericNew;
if (PyType_Ready(&_py_test_class) < 0)
return 1;
module = PyModule_Create(&_py_test_module);
if (!module)
return 1;
Py_INCREF(&_py_test_module);
PyModule_AddObject(module, "test", (PyObject*)&_py_test_class);
PyRun_SimpleString("exec(open('py.py').read())");
Py_Finalize();
return 0;
}
import test_module
print(dir())