- Super mega hacked implementation of python_policy_manager.

Haven't tried running it.


git-svn-id: svn://svn.gna.org/svn/sgpemv2/trunk@380 3ecf2c5c-341e-0410-92b4-d18e462d057c
This commit is contained in:
tchernobog 2006-02-22 12:39:30 +00:00
parent e4f6b8d719
commit 4e65ca6cd0
1 changed files with 43 additions and 47 deletions

View File

@ -20,9 +20,13 @@
#include "python_policy_manager.hh" #include "python_policy_manager.hh"
#include <Python.h>
#include <glibmm/timer.h>
#include <string> #include <string>
#include <cassert> #include <cassert>
#include <iostream> #include <iostream>
#include <unistd.h>
using namespace sgpem; using namespace sgpem;
//static object //static object
@ -31,21 +35,22 @@ PythonPolicyManager* PythonPolicyManager::_instance = NULL;
PythonPolicyManager::PythonPolicyManager() PythonPolicyManager::PythonPolicyManager()
: _initialized(false) : _initialized(false)
{ {
PyEval_InitThreads();
} }
PythonPolicyManager* const PythonPolicyManager* const
PythonPolicyManager::get_instance() PythonPolicyManager::get_instance()
{ {
if(!_instance) if(!_instance)
_instance = new PythonPolicyManager(); _instance = new PythonPolicyManager();
return _instance; return _instance;
} }
Policy& Policy&
PythonPolicyManager::get_policy() PythonPolicyManager::get_policy()
{ {
// FIXME : assumes that _python_policy is always != NULL! // FIXME : assumes that _python_policy is always != NULL!
return *_python_policy; return *_python_policy;
} }
void void
@ -69,13 +74,13 @@ PythonPolicyManager::init()
// Okay, here we go. // Okay, here we go.
// Black magic at work. // Black magic at work.
// FIXME : Hardcoded policy // FIXME : Hardcoded policy name
char* policy_name = "fcfs"; char* policy_name = "fcfs";
PyObject* pLoadmeStr = PyString_FromString(policy_name); PyObject* pLoadmeStr = PyString_FromString(policy_name);
PyObject* pModule = PyImport_Import(pLoadmeStr); PyObject* pUserPolicyModule = PyImport_Import(pLoadmeStr);
Py_DECREF(pLoadmeStr); Py_DECREF(pLoadmeStr);
if( !pModule ) if( !pUserPolicyModule )
{ {
PyErr_Print(); // Error in import PyErr_Print(); // Error in import
// FIXME : don't exit abruptly, but fall back gracefully // FIXME : don't exit abruptly, but fall back gracefully
@ -83,47 +88,38 @@ PythonPolicyManager::init()
} }
// Dictionary with defined ``symbols'' for .pyc file // Dictionary with defined ``symbols'' for .pyc file
PyObject* pDict = PyModule_GetDict(pModule); PyObject* pUserPolicyDict = PyModule_GetDict(pUserPolicyModule);
assert(pDict); assert(pUserPolicyDict);
// 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());
}
// 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 // try code
const char* tryExecute = PyObject_CallMethod(pAdapterObject, "async_configure", NULL);
"_adapter.async_configure()\n"
"#set ownership to C++ code\n"; PyThreadState *_save;
//"_adapter.thisown = 0\n"; Py_UNBLOCK_THREADS
PyRun_SimpleString(tryExecute); sleep(5); // hack'a'ton! magggggiccc nummmbeeerrrrrs!!
Py_DECREF(pModule);
// pModule = PyImport_AddModule("__main__"); Py_BLOCK_THREADS
// pDict = PyModule_GetDict(pModule);
// And now, who's your daddy, huh?
// PyObject *pPyProcess = PyMapping_GetItemString(pDict, "p");
// assert(pPyProcess);
// 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);
}
*/
} }