All pastes #15630 Raw Edit

Untitled

public text v1 · immutable
#15630 ·published 2005-06-23 10:32 UTC
rendered paste body
bool PyEmbed::addPythonPath (string newpath) {
	PyObject *pModuleName, *pModule, *pPythonPath, *pDict, *pNewPath;
   
	pModuleName = PyString_FromString ("sys");
    pModule = PyImport_Import(pModuleName);            //import that module into python
	Py_DECREF (pModuleName);                            //release the string (so the GC can deal with it)
    if (!pModule) {
        PyError::PyShowError ("PyScript Error", "Could not import \"sys\" for adding to PythonPath", "");     
        return false;
    }
	pDict = PyModule_GetDict(pModule);
	Py_DECREF (pModule);
	pPythonPath = PyDict_GetItemString(pDict, "path"); 
    if (!pPythonPath) {
            PyError::PyShowError ("PyScript Error", "Could not get sys.path", "");     
        return false;
    }
	if (PyList_Check(pPythonPath)) {
		pNewPath = PyString_FromString (newpath.c_str());   
		PyList_Append (pPythonPath, pNewPath);
        Py_DECREF(pNewPath);
		return true;
	}

    PyError::PyShowError ("PyScript Error", "sys.path is not a list", "");  
	return false;

}