python導出函數方法

1、導出函數範例

#include <python/Python.h>
#include <iostream>

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:\n";
	while (trace && trace->tb_frame)
	{
		/*PyFrameObject *frame = (PyFrameObject*)trace->tb_frame;
		stack += base::strfmt("  File \"%s\", line %i, in %s\n",
			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+"\n";
}

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!'\n");

	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...") 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章