From 633416f34069e919ed17c943ae68e726b4514d11 Mon Sep 17 00:00:00 2001 From: tchernobog Date: Tue, 21 Feb 2006 12:20:14 +0000 Subject: [PATCH] - Add preliminary code for Python policy management git-svn-id: svn://svn.gna.org/svn/sgpemv2/trunk@370 3ecf2c5c-341e-0410-92b4-d18e462d057c --- src/backend/pyloader/python_policy_manager.cc | 81 ++++++++++++++++++- 1 file changed, 77 insertions(+), 4 deletions(-) diff --git a/src/backend/pyloader/python_policy_manager.cc b/src/backend/pyloader/python_policy_manager.cc index 0a026a7..1ff7cd3 100644 --- a/src/backend/pyloader/python_policy_manager.cc +++ b/src/backend/pyloader/python_policy_manager.cc @@ -20,9 +20,11 @@ #include "python_policy_manager.hh" +#include +#include +#include using namespace sgpem; - //static object PythonPolicyManager* PythonPolicyManager::_instance = NULL; @@ -63,8 +65,79 @@ PythonPolicyManager::init() PyRun_SimpleString("import sys\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); + } + */ + } - - -