- Add preliminary code for Python policy management

git-svn-id: svn://svn.gna.org/svn/sgpemv2/trunk@370 3ecf2c5c-341e-0410-92b4-d18e462d057c
This commit is contained in:
tchernobog 2006-02-21 12:20:14 +00:00
parent 2e1a699b65
commit 633416f340
1 changed files with 77 additions and 4 deletions

View File

@ -20,9 +20,11 @@
#include "python_policy_manager.hh" #include "python_policy_manager.hh"
#include <string>
#include <cassert>
#include <cstdio>
using namespace sgpem; using namespace sgpem;
//static object //static object
PythonPolicyManager* PythonPolicyManager::_instance = NULL; PythonPolicyManager* PythonPolicyManager::_instance = NULL;
@ -64,7 +66,78 @@ PythonPolicyManager::init()
PyRun_SimpleString("import sys\n" PyRun_SimpleString("import sys\n"
"sys.path[:0] = [ '" MODDIR "', '" PYCDIR "' ]\n"); "sys.path[:0] = [ '" MODDIR "', '" PYCDIR "' ]\n");
// Okay, here we go.
// Black magic at work.
// FIXME : Hardcoded policy
char* policy_name = "fcfs_policy";
PyObject* pLoadmeStr = PyString_FromString(policy_name);
PyObject *pModule = PyImport_Import(pLoadmeStr);
Py_DECREF(pLoadmeStr);
if( !pModule )
{
PyErr_Print(); // Error in import
// FIXME : don't exit abruptly, but fall back gracefully
exit(-1);
} }
// Dictionary with defined ``symbols'' for .pyc file
PyObject* pDict = PyModule_GetDict(pModule);
assert(pDict);
// Build up UserPythonScriptAdapter
{
std::string py_adapter = std::string("class UserPythonScriptAdapter(") +
policy_name + "):\n"
" def __init__(self):\n"
" pass;\n"
" def async_configure():\n"
" pass;\n"
" def async_sort_queue():\n"
" pass;\n"
" def async_is_preemptible():\n"
" pass;\n"
" def async_is_timesliced():\n"
" pass;\n"
"\n";
PyRun_SimpleString(py_adapter.c_str());
}
//build SWIG proxy object inside the interpreter
PyRun_SimpleString("py_ad = UserPythonScriptAdapter");
/* const char *buildPyProcess =
"import proto_process\n"
"p = proto_process.Process(19, 'param')\n"
"#set ownership to C++ code\n"
"p.thisown = 0\n";
PyRun_SimpleString(buildPyProcess);
*/
Py_DECREF(pModule);
pModule = PyImport_AddModule("__main__");
pDict = PyModule_GetDict(pModule);
// 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);
}
*/
}