diff --git a/src/backend/pyloader/python_policy_manager.cc b/src/backend/pyloader/python_policy_manager.cc index b542b2d..5fb5e0f 100644 --- a/src/backend/pyloader/python_policy_manager.cc +++ b/src/backend/pyloader/python_policy_manager.cc @@ -20,9 +20,13 @@ #include "python_policy_manager.hh" +#include +#include + #include #include #include +#include using namespace sgpem; //static object @@ -31,21 +35,22 @@ PythonPolicyManager* PythonPolicyManager::_instance = NULL; PythonPolicyManager::PythonPolicyManager() : _initialized(false) { + PyEval_InitThreads(); } PythonPolicyManager* const PythonPolicyManager::get_instance() { - if(!_instance) - _instance = new PythonPolicyManager(); - return _instance; + if(!_instance) + _instance = new PythonPolicyManager(); + return _instance; } Policy& PythonPolicyManager::get_policy() { - // FIXME : assumes that _python_policy is always != NULL! - return *_python_policy; + // FIXME : assumes that _python_policy is always != NULL! + return *_python_policy; } void @@ -69,13 +74,13 @@ PythonPolicyManager::init() // Okay, here we go. // Black magic at work. - // FIXME : Hardcoded policy + // FIXME : Hardcoded policy name char* policy_name = "fcfs"; PyObject* pLoadmeStr = PyString_FromString(policy_name); - PyObject* pModule = PyImport_Import(pLoadmeStr); + PyObject* pUserPolicyModule = PyImport_Import(pLoadmeStr); Py_DECREF(pLoadmeStr); - if( !pModule ) + if( !pUserPolicyModule ) { PyErr_Print(); // Error in import // FIXME : don't exit abruptly, but fall back gracefully @@ -83,47 +88,38 @@ PythonPolicyManager::init() } // Dictionary with defined ``symbols'' for .pyc file - PyObject* pDict = PyModule_GetDict(pModule); - assert(pDict); - - // Build up UserPythonScriptAdapter - { - // FIXME : Warning!! "import fcfs" shouldn't be here!! - std::string s = std::string("import ") + policy_name + "\n" - "import ScriptAdapter\n" - "_adapter = ScriptAdapter.ScriptAdapter(" + - policy_name + "." + policy_name + ")"; - PyRun_SimpleString(s.c_str()); - } + PyObject* pUserPolicyDict = PyModule_GetDict(pUserPolicyModule); + assert(pUserPolicyDict); + // Loads ScriptAdapter module and get its dictionary + pLoadmeStr = PyString_FromString("ScriptAdapter"); + PyObject* pScriptAdapterModule = PyImport_Import(pLoadmeStr); + Py_DECREF(pLoadmeStr); + assert(pScriptAdapterModule); + PyObject* pScriptAdapterDict = PyModule_GetDict(pScriptAdapterModule); + assert(pScriptAdapterDict); + + // Now takes the user-defined policy class from pUserPolicyDict + PyObject* pPolicyClass = PyDict_GetItemString(pUserPolicyDict, policy_name); + assert(pPolicyClass); // FIXME needs stricter checking and exception throwing + + // Creates a new object of type ScriptAdapter : + // takes init function from ScriptAdapter class + PyObject* pAdapterClass = PyDict_GetItemString(pScriptAdapterDict, "ScriptAdapter"); + PyObject* pAdapterCtorParam = PyTuple_New(1); + Py_INCREF(pPolicyClass); // PyTuple_SetItem steals a reference + PyTuple_SetItem(pAdapterCtorParam, 0, pPolicyClass); + PyObject* pAdapterObject = PyInstance_New(pAdapterClass, pAdapterCtorParam, NULL); + // try code - const char* tryExecute = - "_adapter.async_configure()\n" - "#set ownership to C++ code\n"; - //"_adapter.thisown = 0\n"; - PyRun_SimpleString(tryExecute); - - Py_DECREF(pModule); + PyObject_CallMethod(pAdapterObject, "async_configure", NULL); + + PyThreadState *_save; + Py_UNBLOCK_THREADS + sleep(5); // hack'a'ton! magggggiccc nummmbeeerrrrrs!! - // pModule = PyImport_AddModule("__main__"); - // pDict = PyModule_GetDict(pModule); - - // PyObject *pPyProcess = PyMapping_GetItemString(pDict, "p"); - // assert(pPyProcess); + Py_BLOCK_THREADS + + // And now, who's your daddy, huh? - // Matteo: PyTuple_SetItem steals the reference, so - // we don't DECREF pPyProcess. - // See http://www.python.org/doc/2.4.2/api/refcountDetails.html#l2h-13 - // PyTuple_SetItem(pArgs, 0, pPyProcess); - - // assert(pFunc); - - /* if(PyCallable_Check(pFunc)) - { - PyObject* ret = PyObject_CallObject(pFunc, pArgs); - Py_DECREF(pArgs); - if(!ret) PyErr_Print(); - else Py_DECREF(ret); - } - */ }