概述
#include#include#includechar*GDALPythonObjectToCStr(PyObject*pyObject);intmain(intargc,char*argv[])
{
Py_Initialize();if(!Py_IsInitialized())
{return-1;
}
PyRun_SimpleString("import sys");
PyRun_SimpleString("sys.path.append('./script')");
PyObject*pModule;
PyObject*pDict;
PyObject*pFunc;
pModule=PyImport_ImportModule("list");if(!pModule)
{
printf("can't find list.py");
system("PAUSE");
getchar();return-1;
}
pDict=PyModule_GetDict(pModule);if(!pDict)
{return-1;
}
pFunc=PyDict_GetItemString(pDict,"list_get");if(!pFunc||!PyCallable_Check(pFunc))
{
printf("can't find function [list_get]");
getchar();return-1;
}
PyObject*args=PyTuple_New(1);
PyTuple_SetItem(args,0,Py_BuildValue("s","List.htm"));
PyObject*value=PyObject_CallObject(pFunc,args);intret=PySequence_Check(value);
printf("check:%dn",ret);intlength=PySequence_Size(value);
printf("length:%dn",length);inti=0;for(;i
{
PyObject*obj=PySequence_GetItem(value,i);//char* str = PyBytes_AS_STRING(obj);char*str=GDALPythonObjectToCStr(obj);
printf("link:%sn",str);
free(str);
}
Py_DECREF(args);
Py_DECREF(pModule);
Py_Finalize();
system("PAUSE");return0;
}/*Return a NULL terminated c String from a PyObject*//*Result must be freed with GDALPythonFreeCStr*/char*GDALPythonObjectToCStr(PyObject*pyObject)
{#ifPY_VERSION_HEX >= 0x03000000if(PyUnicode_Check(pyObject))
{char*pszStr;char*pszNewStr;
Py_ssize_t nLen;
PyObject*pyUTF8Str=PyUnicode_AsUTF8String(pyObject);
PyBytes_AsStringAndSize(pyUTF8Str,&pszStr,&nLen);
pszNewStr=(char*)malloc(nLen+1);
memcpy(pszNewStr,pszStr,nLen+1);
Py_XDECREF(pyUTF8Str);returnpszNewStr;
}elseif(PyBytes_Check(pyObject))
{char*pszStr;char*pszNewStr;
Py_ssize_t nLen;
PyBytes_AsStringAndSize(pyObject,&pszStr,&nLen);
pszNewStr=(char*)malloc(nLen+1);
memcpy(pszNewStr,pszStr,nLen+1);returnpszNewStr;
}else{char*pszStr=(char*)malloc(1);
pszStr[0]='