Author: juhliarik Date: Wed Oct 3 13:26:15 2007 New Revision: 41225 URL: http://svn.opensuse.org/viewcvs/yast?rev=41225&view=rev Log: reimplementation of import module (import python module into ycp) Modified: trunk/python-bindings/src/YPython.cc Modified: trunk/python-bindings/src/YPython.cc URL: http://svn.opensuse.org/viewcvs/yast/trunk/python-bindings/src/YPython.cc?rev=41225&r1=41224&r2=41225&view=diff ============================================================================== --- trunk/python-bindings/src/YPython.cc (original) +++ trunk/python-bindings/src/YPython.cc Wed Oct 3 13:26:15 2007 @@ -56,6 +56,8 @@ YPython * YPython::_yPython = 0; +PyObject * YPython::_pMain = NULL; + YPython::YPython(){} @@ -72,6 +74,13 @@ } +PyObject* +YPython::pMain() +{ + + return _pMain; +} + YCPValue YPython::destroy() @@ -92,9 +101,28 @@ * Loads a module. */ YCPValue -YPython::loadModule( YCPList argList ) +YPython::loadModule(string module) { - Py_Initialize(); + string path; + string module_name; + size_t found; + + //found last "/" in path + found = module.find_last_of("/"); + //extract directory from path module + path = module.substr(0,found+1); + //extract module name from path + module_name = module.substr(found+1); + //delete last 3 chars from module name ".py" + module_name.erase(module_name.size()-3); //delete ".py" + //initialize python and set the path where are python modules + if (!Py_IsInitialized()) { + setenv("PYTHONPATH", path.c_str(), 1); + Py_Initialize(); + + } + + YPython::_pMain = PyImport_ImportModule(module_name.c_str()); return YCPVoid(); } @@ -119,9 +147,8 @@ YCPValue result = YCPNull (); - pMain = PyImport_AddModule("__main__"); - //PyRun_SimpleString("import __main__"); - pMainDict = PyModule_GetDict(pMain); + //pMain = PyImport_AddModule("__main__"); + pMainDict = PyModule_GetDict(YPython::_pMain); pFunc = PyDict_GetItemString(pMainDict, function.c_str()); pArgs = PyTuple_New(argList->size()-1); @@ -137,9 +164,9 @@ Py_CLEAR(pArgs); - if (pReturn){ + if (pReturn) result = PythonTypeToYCPType(pReturn); // create YCP value - }else + else y2error("pReturn == 0"); Py_CLEAR(pReturn); -- To unsubscribe, e-mail: yast-commit+unsubscribe@opensuse.org For additional commands, e-mail: yast-commit+help@opensuse.org