- 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.h>
#include <glibmm/timer.h>
#include <string>
#include <cassert>
#include <iostream>
#include <unistd.h>
using namespace sgpem;
//static object
@ -31,6 +35,7 @@ PythonPolicyManager* PythonPolicyManager::_instance = NULL;
PythonPolicyManager::PythonPolicyManager()
: _initialized(false)
{
PyEval_InitThreads();
}
PythonPolicyManager* const
@ -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);
PyObject* pUserPolicyDict = PyModule_GetDict(pUserPolicyModule);
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
const char* tryExecute =
"_adapter.async_configure()\n"
"#set ownership to C++ code\n";
//"_adapter.thisown = 0\n";
PyRun_SimpleString(tryExecute);
PyObject_CallMethod(pAdapterObject, "async_configure", NULL);
Py_DECREF(pModule);
PyThreadState *_save;
Py_UNBLOCK_THREADS
sleep(5); // hack'a'ton! magggggiccc nummmbeeerrrrrs!!
// pModule = PyImport_AddModule("__main__");
// pDict = PyModule_GetDict(pModule);
Py_BLOCK_THREADS
// PyObject *pPyProcess = PyMapping_GetItemString(pDict, "p");
// assert(pPyProcess);
// 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);
}
*/
}