- 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:
parent
e4f6b8d719
commit
4e65ca6cd0
|
@ -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
|
// Loads ScriptAdapter module and get its dictionary
|
||||||
{
|
pLoadmeStr = PyString_FromString("ScriptAdapter");
|
||||||
// FIXME : Warning!! "import fcfs" shouldn't be here!!
|
PyObject* pScriptAdapterModule = PyImport_Import(pLoadmeStr);
|
||||||
std::string s = std::string("import ") + policy_name + "\n"
|
Py_DECREF(pLoadmeStr);
|
||||||
"import ScriptAdapter\n"
|
assert(pScriptAdapterModule);
|
||||||
"_adapter = ScriptAdapter.ScriptAdapter(" +
|
PyObject* pScriptAdapterDict = PyModule_GetDict(pScriptAdapterModule);
|
||||||
policy_name + "." + policy_name + ")";
|
assert(pScriptAdapterDict);
|
||||||
PyRun_SimpleString(s.c_str());
|
|
||||||
}
|
// 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";
|
|
||||||
//"_adapter.thisown = 0\n";
|
|
||||||
PyRun_SimpleString(tryExecute);
|
|
||||||
|
|
||||||
Py_DECREF(pModule);
|
PyThreadState *_save;
|
||||||
|
Py_UNBLOCK_THREADS
|
||||||
|
sleep(5); // hack'a'ton! magggggiccc nummmbeeerrrrrs!!
|
||||||
|
|
||||||
// pModule = PyImport_AddModule("__main__");
|
Py_BLOCK_THREADS
|
||||||
// pDict = PyModule_GetDict(pModule);
|
|
||||||
|
|
||||||
// PyObject *pPyProcess = PyMapping_GetItemString(pDict, "p");
|
// And now, who's your daddy, huh?
|
||||||
// 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);
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue