pythonエクスポート関数メソッド

7556 ワード

1、エクスポート関数の例
#include 
#include 

using namespace std;
int add(int arg1, int arg2)
{
	return arg1 + arg2;
}

int sub(int arg1, int arg2)
{
	return arg1 - arg2;
}

static PyObject* math_add(PyObject* self, PyObject* args)
{
	int arg1, arg2;
	if (!PyArg_ParseTuple(args, "ii", &arg1, &arg2))
	{
		return NULL;
	}
	int result = add(arg1, arg2);
	return (PyObject*)Py_BuildValue("i", result);
}

static PyObject* math_sub(PyObject* self, PyObject* args)
{
	int arg1, arg2;
	if (!PyArg_ParseTuple(args, "ii", &arg1, &arg2))
	{
		return NULL;
	}
	int result = sub(arg1, arg2);
	return (PyObject*)Py_BuildValue("i", result);
}

static PyMethodDef MathMethods[] =
{
	{ "add", math_add, METH_VARARGS, "Execute math command:add." },
	{ "sub", math_sub, METH_VARARGS, "Execute math command:sub." },
	{ NULL, NULL }
};

static struct PyModuleDef MathModule =
{
	PyModuleDef_HEAD_INIT,
	"math_capi",
	"example module doc string",
	-1,
	MathMethods,
	NULL,
	NULL,
	NULL,
	NULL
};

static PyObject* __mathError;
PyMODINIT_FUNC
PyInit_Math()
{
	PyObject* module = PyModule_Create(&MathModule);
	if (NULL == module)
	{
		return NULL;
	}
	__mathError = PyErr_NewException("math.error", NULL, NULL);
	Py_INCREF(__mathError);
	PyModule_AddObject(module, "error", __mathError);
	return module;
}

std::string 
format_python_traceback(PyObject *tb)
{
	PyTracebackObject *trace = (PyTracebackObject*)tb;
	std::string stack;

	stack = "Traceback:
"; while (trace && trace->tb_frame) { /*PyFrameObject *frame = (PyFrameObject*)trace->tb_frame; stack += base::strfmt(" File \"%s\", line %i, in %s
", PyString_AsString(frame->f_code->co_filename), trace->tb_lineno, PyString_AsString(frame->f_code->co_name)); PyObject *code = PyErr_ProgramText(PyString_AsString(frame->f_code->co_filename), trace->tb_lineno); if (code) { stack += base::strfmt(" %s", PyString_AsString(code)); Py_DECREF(code); }*/ trace = trace->tb_next; } return stack; } std::string format_python_exception(std::string &summary) { std::string reason, stack; PyObject *exc, *val, *tb; PyErr_Fetch(&exc, &val, &tb); PyErr_NormalizeException(&exc, &val, &tb); if (val) { PyObject *tmp = PyObject_Str(val); if (tmp) { reason = PyBytes_AsString(tmp); Py_DECREF(tmp); } } if (tb) stack = format_python_traceback(tb); else stack = "No stack information. "; PyErr_Restore(exc, val, tb); summary = reason; return stack + reason+"
"; } static std::string exception_detail() { PyObject *exc_class = NULL, *exc = NULL, *exc_tb = NULL; PyErr_Fetch(&exc_class, &exc, &exc_tb); if (exc) { PyObject *str = PyObject_Str(exc); if (str) { //char *s = PyByteArray_AsString(str); char *s = PyBytes_AsString(str); if (s) return s; Py_DECREF(str); } Py_DECREF(exc); } Py_XDECREF(exc_class); Py_XDECREF(exc_tb); // PyErr_Restore(exc, val, tb); return ""; } // "Hello World" void HelloWorld() { PyImport_AppendInittab("math_capi", PyInit_Math); Py_Initialize(); PyImport_ImportModule("math_capi"); PyRun_SimpleString("import sys"); PyRun_SimpleString("sys.path.append('.')"); PyRun_SimpleString("sys.path.append('./')"); PyRun_SimpleString("print 'Hello Python!'
"); PyObject* pModule = PyImport_ImportModule("testpython"); PyErr_Print(); std::string strError = exception_detail(); if (!pModule) { return; } PyErr_Print(); PyObject* pFunc = PyObject_GetAttrString(pModule, "HelloWorld"); PyErr_Print(); if (pFunc && PyCallable_Check(pFunc)) { PyObject_CallObject(pFunc, NULL); } Py_Finalize(); } // Add , int void Add() { Py_Initialize(); PyObject* pModule = NULL; PyObject* pFunc = NULL; pModule = PyImport_ImportModule("testpython"); // Test:Python pFunc = PyObject_GetAttrString(pModule, "add"); // Add:Python // PyObject* pArgs = PyTuple_New(2); // ,2 PyTuple_SetItem(pArgs, 0, Py_BuildValue("i", 5)); // 0--- i int PyTuple_SetItem(pArgs, 1, Py_BuildValue("i", 7)); // 1--- // PyObject* pReturn = NULL; pReturn = PyEval_CallObject(pFunc, pArgs); // // int . int result; PyArg_Parse(pReturn, "i", &result); // i int cout << "5+7 = " << result << endl; Py_Finalize(); } // void TestTransferDict() { Py_Initialize(); PyObject* pModule = NULL; PyObject* pFunc = NULL; pModule = PyImport_ImportModule("testpython"); // Test:Python pFunc = PyObject_GetAttrString(pModule, "TestDict"); // Add:Python // PyObject* pArgs = PyTuple_New(1); PyObject* pDict = PyDict_New(); // PyDict_SetItemString(pDict, "Name", Py_BuildValue("s", "scott")); // PyDict_SetItemString(pDict, "Age", Py_BuildValue("i", 18)); // PyTuple_SetItem(pArgs, 0, pDict); // 0--- // PyObject* pReturn = NULL; pReturn = PyEval_CallObject(pFunc, pArgs); // // int size = PyDict_Size(pReturn); cout << " : " << size << endl; PyObject *pNewAge = PyDict_GetItemString(pReturn, "Age"); int newAge; PyArg_Parse(pNewAge, "i", &newAge); cout << "True Age: " << newAge << endl; Py_Finalize(); } // void TestClass() { Py_Initialize(); PyObject* pModule = NULL; PyObject* pFunc = NULL; pModule = PyImport_ImportModule("testpython"); // Test:Python pFunc = PyObject_GetAttrString(pModule, "TestDict"); // Add:Python // Person PyObject* pClassPerson = PyObject_GetAttrString(pModule, "Person"); // Person PyInstance_New PyObject* pInstancePerson = PyObject_CallObject(pClassPerson,NULL); // PyObject_CallMethod(pInstancePerson, "greet", "s", "Hello Kitty"); // s , "Hello Kitty" Py_Finalize(); } int main(int argc, char *argv[]) { /* argv[0] Python */ //Py_SetProgramName(argv[0]); cout << "Starting Test..." << endl; cout << "HelloWorld()-------------" << endl; HelloWorld(); cout << "Add()--------------------" << endl; Add(); cout << "TestDict-----------------" << endl; TestTransferDict(); cout << "TestClass----------------" << endl; TestClass(); system("pause"); return 0; }

2、スクリプトの作成
#testpython.py import math_capi def HelloWorld():     print("Hello World!")     print(math_capi.add(3,5))     print(math_capi.sub(5,3)) def add(a, b):     return a+b def TestDict(dict):     print(dict)        dict["Age"] = 17     return dict class Person:     def greet(self, greetStr):         print(greetStr) #print add(5,7)  #a = raw_input("Enter To Continue...")