Exemple #1
0
int main() {
	Py_Initialize();
	if (!Py_IsInitialized())
		return -1;
	PyRun_SimpleString("import sys");
	PyRun_SimpleString("sys.path.append('./')");
	//导入模块
	PyObject* pModule = PyImport_ImportModule("testpy");
	if (!pModule) {
		printf("Cant open python file!/n");
		return -1;
	}
	//模块的字典列表
	PyObject* pDict = PyModule_GetDict(pModule);
	if (!pDict) {
		printf("Cant find dictionary./n");
		return -1;
	}
	//打印出来看一下
	printDict(pDict);
	//演示函数调用
	PyObject* pFunHi = PyDict_GetItemString(pDict, "sayhi");
	PyObject_CallFunction(pFunHi, "s", "lhb");
	Py_DECREF(pFunHi);
	//演示构造一个Python对象,并调用Class的方法
	//获取Second类
	PyObject* pClassSecond = PyDict_GetItemString(pDict, "Second");
	if (!pClassSecond) {
		printf("Cant find second class./n");
		return -1;
	}
	//获取Person类
	PyObject* pClassPerson = PyDict_GetItemString(pDict, "Person");
	if (!pClassPerson) {
		printf("Cant find person class./n");
		return -1;
	}
	//构造Second的实例
	PyObject* pInstanceSecond = PyInstance_New(pClassSecond, NULL, NULL);
	if (!pInstanceSecond) {
		printf("Cant create second instance./n");
		return -1;
	}
	//构造Person的实例
	PyObject* pInstancePerson = PyInstance_New(pClassPerson, NULL, NULL);
	if (!pInstancePerson) {
		printf("Cant find person instance./n");
		return -1;
	}
	//把person实例传入second的invoke方法
	PyObject_CallMethod(pInstanceSecond, "invoke", "O", pInstancePerson);
	//释放
	Py_DECREF(pInstanceSecond);
	Py_DECREF(pInstancePerson);
	Py_DECREF(pClassSecond);
	Py_DECREF(pClassPerson);
	Py_DECREF(pModule);
	Py_Finalize();
	return 0;
}
Exemple #2
0
int
printDict( dictionary_t *dictionary ) {
    int i;

    printf( "  {\n" );

    for ( i = 0; i < dictionary->len; i++ ) {
        char valueStr[NAME_LEN];
        if ( strcmp( dictionary->value[i].type_PI, Dictionary_MS_T ) == 0 ) {
            printf( "    %s: ",  dictionary->key[i] );
            printDict( ( dictionary_t * ) dictionary->value[i].ptr );
        }
        else if ( strcmp( dictionary->value[i].type_PI, GenArray_MS_T ) == 0 ) {
            printf( "    %s: ",  dictionary->key[i] );
            printGenArray( ( genArray_t * ) dictionary->value[i].ptr );
        }
        else {
            getStrByType_PI( dictionary->value[i].type_PI,
                             dictionary->value[i].ptr, valueStr );
            printf( "    %s: %s\n", dictionary->key[i], valueStr );
        }
    }
    printf( "  }\n" );

    return ( 0 );
}
Exemple #3
0
int main() {  
	Py_Initialize();  
	if (!Py_IsInitialized())  
		return -1;  
	PyRun_SimpleString("import sys");  
	PyRun_SimpleString("sys.path.append('./')");  

	PyObject* pModule = PyImport_ImportModule("testpy");  
	if (!pModule) {  
		printf("Cant open python file!\n");  
		return -1;  
	}  

	PyObject* pDict = PyModule_GetDict(pModule);  
	if (!pDict) {  
		printf("Cant find dictionary.\n");  
		return -1;  
	}  
	
	printDict(pDict);  
	PyObject* pFunHi = PyDict_GetItemString(pDict, "sayhi");  
	PyObject_CallFunction(pFunHi, "s", "lhb");  
	Py_DECREF(pFunHi);  
	PyObject* pClassSecond = PyDict_GetItemString(pDict, "Second");  
	if (!pClassSecond) {  
		printf("Cant find second class.\n");  
		return -1;  
	}  

	PyObject* pClassPerson = PyDict_GetItemString(pDict, "Person");  
	if (!pClassPerson) {  
		printf("Cant find person class.\n");  
		return -1;  
	}  

	PyObject* pInstanceSecond = PyInstance_New(pClassSecond, NULL, NULL);  
	if (!pInstanceSecond) {  
		printf("Cant create second instance.\n");  
		return -1;  
	}  
	
	PyObject* pInstancePerson = PyInstance_New(pClassPerson, NULL, NULL);  
	if (!pInstancePerson) {  
		printf("Cant find person instance.\n");  
		return -1;  
	}  
	PyObject_CallMethod(pInstanceSecond, "invoke", "O", pInstancePerson);  
	
	Py_DECREF(pInstanceSecond);  
	Py_DECREF(pInstancePerson);  
	Py_DECREF(pClassSecond);  
	Py_DECREF(pClassPerson);  
	Py_DECREF(pModule);  
	Py_Finalize();  
	return 0;  
}  
Exemple #4
0
int
printDictArray( dictArray_t *dictArray ) {
    int i;

    if ( dictArray == NULL ) {
        return 0;
    }

    for ( i = 0; i < dictArray->len; i++ ) {
        printDict( &dictArray->dictionary[i] );
    }
    return 0;
}
Exemple #5
0
int
printGenArray( genArray_t *genArray ) {
    int i;

    printf( "  [\n" );

    for ( i = 0; i < genArray->len; i++ ) {
        if ( strcmp( genArray->value[i].type_PI, Dictionary_MS_T ) == 0 ) {
            printDict( ( dictionary_t * ) genArray->value[i].ptr );
        }
        else if ( strcmp( genArray->value[i].type_PI, GenArray_MS_T ) == 0 ) {
            printGenArray( ( genArray_t * ) genArray->value[i].ptr );
        }
        else {
            char valueStr[NAME_LEN];
            getStrByType_PI( genArray->value[i].type_PI,
                             genArray->value[i].ptr, valueStr );
            printf( "  %s\n", valueStr );
        }
    }
    printf( "  ]\n" );

    return ( 0 );
}
Exemple #6
0
int main(void)
{
	Py_Initialize();
	if(!Py_IsInitialized())
		return -1;
	
	PyRun_SimpleString("import sys");
	//where you put your py file
	PyRun_SimpleString("sys.path.append('../')");
	//Import 
	PyObject* pModule = PyImport_ImportModule("pc");//no *.py
	if(!pModule)
	{
		printf("can't open python file!\n");
		return -1;
	}
	//Dict module
	PyObject* pDict = PyModule_GetDict(pModule);
	if(!pDict)
	{
		printf("can't find dictionary.\n");
		return -1;
	}
	printDict(pDict);

	PyObject *pFunHi = PyDict_GetItemString(pDict, "sayHi");
	PyObject_CallFunction(pFunHi, "s", "yourname");// @para1: obj, @para2: name
	//release
	Py_DECREF(pFunHi);
	
	//Contruct a obj , call it's function
	//Second Class
	PyObject *pClassSecond = PyDict_GetItemString(pDict , "Second");
	if(!pClassSecond)
	{
		printf("can't find second class.\n");
		return -1;
	}
	//Person Class
	PyObject *pClassPerson = PyDict_GetItemString(pDict, "Person");
	if(!pClassPerson)
	{
		printf("can't find Person class.\n");
		return -1;
	}
	//Construct Second Instance
	PyObject* pInstanceSecond = PyInstance_New(pClassSecond, NULL, NULL);
	if(!pInstanceSecond)
	{
		printf("can't create second instance.\n");
		return -1;
	}
	//Construct Person Instance
	PyObject *pInstancePerson = PyInstance_New(pClassPerson, NULL, NULL);
	if(!pInstancePerson)
	{
		printf("can't create Person instance.\n");
		return -1;
	}

	//pass Person Instance to Second Instance 
	PyObject_CallMethod(pInstanceSecond, "invoke", "O", pInstancePerson);
	PyObject_CallMethod(pInstanceSecond, "method2", "O", pInstancePerson);

	//call execute a py file
	PyRun_SimpleString("exec(compile(open('./1.py').read(),'1.py', 'exec'),locals(), globals())");
	//release
	Py_DECREF(pInstanceSecond);
	Py_DECREF(pInstancePerson);
	Py_DECREF(pClassSecond);
	Py_DECREF(pClassPerson);
	Py_DECREF(pModule);
	Py_Finalize();

	return 0;
}
Exemple #7
0
int CallPy()
{
	Py_Initialize();
	if (!Py_IsInitialized())
		return -1;

	//c调用python:
	//直接执行python脚本
	PyRun_SimpleString("import sys");
	PyRun_SimpleString("sys.path.append('./')");

	//导入模块
	PyObject* pTestModule = PyImport_ImportModule("testpy");
	if (!pTestModule) {
		printf("Cant open python file!\n");
		return -1;
	}

	//模块的字典列表
	PyObject* pDict = PyModule_GetDict(pTestModule);
	if (!pDict) {
		printf("Cant find dictionary.\n");
		return -1;
	}

	//打印模块的字典
	printDict(pDict);

	//演示函数调用
	PyObject* pFunHi = PyDict_GetItemString(pDict, "HelloWorld");
	PyObject_CallFunction(pFunHi, "s", "Hello World!");
	//Py_DECREF(pFunHi);	//不用减少引用计数
	//Py_DECREF(pDict);
	Py_DECREF(pTestModule);

	/*********************************************************************/
	//python 调用c:
	//手动导入cmodule到python环境
	initcmodule();
	//直接脚本调用,验证python已经导入cmodule模块
	PyRun_SimpleString("import cmodule");
	PyRun_SimpleString("print 'call in python: ', cmodule.print_info(123)");
	//程序调用
	PyObject* pCmodule = PyImport_ImportModule("cmodule");
	PyObject* cprint_info = PyObject_GetAttrString(pCmodule, "print_info");
	if (!PyCallable_Check(cprint_info)) {
		PyErr_SetString(PyExc_TypeError, "parameter must be callable");
		return NULL;
	}
	int iarg = 9;
	PyObject* result = PyObject_CallFunction(cprint_info, "i", iarg) ;
	if (result == NULL) {
		return -1;
	}
	int i = 0, i2 = 0;
	int ok = PyArg_ParseTuple(result, "ii", &i,&i2);		//返回多个参数
	//i = PyInt_AsLong(result);								//返回一个参数
	printf("cmodule return: %d %d %d \n", ok, i, i2);

	Py_DECREF(result);
	Py_DECREF(cprint_info);
	Py_DECREF(pCmodule);

	Py_Finalize();  
	return 0;
}