コード例 #1
0
ファイル: python_exec.c プロジェクト: AndreiPlesa/opensips
int
python_exec2(struct sip_msg *_msg, char *method_name, char *mystr)
{
    PyObject *pFunc, *pArgs, *pValue, *pResult;
    PyObject *msg;
    int rval;

    PyEval_AcquireLock();
    PyThreadState_Swap(myThreadState);

    pFunc = PyObject_GetAttrString(handler_obj, method_name);
    if (pFunc == NULL || !PyCallable_Check(pFunc)) {
        LM_ERR("%s not found or is not callable\n", method_name);
        Py_XDECREF(pFunc);
        PyThreadState_Swap(NULL);
        PyEval_ReleaseLock();
        return -1;
    }

    msg = newmsgobject(_msg);
    if (msg == NULL) {
        LM_ERR("can't create MSGtype instance\n");
        Py_DECREF(pFunc);
        PyThreadState_Swap(NULL);
        PyEval_ReleaseLock();
        return -1;
    }

    pArgs = PyTuple_New(mystr == NULL ? 1 : 2);
    if (pArgs == NULL) {
        LM_ERR("PyTuple_New() has failed\n");
        msg_invalidate(msg);
        Py_DECREF(msg);
        Py_DECREF(pFunc);
        PyThreadState_Swap(NULL);
        PyEval_ReleaseLock();
        return -1;
    }
    PyTuple_SetItem(pArgs, 0, msg);
    /* Tuple steals msg */

    if (mystr != NULL) {
        pValue = PyString_FromString(mystr);
        if (pValue == NULL) {
            LM_ERR("PyString_FromString(%s) has failed\n", mystr);
            msg_invalidate(msg);
            Py_DECREF(pArgs);
            Py_DECREF(pFunc);
            PyThreadState_Swap(NULL);
            PyEval_ReleaseLock();
            return -1;
        }
        PyTuple_SetItem(pArgs, 1, pValue);
        /* Tuple steals pValue */
    }

    pResult = PyObject_CallObject(pFunc, pArgs);
    msg_invalidate(msg);
    Py_DECREF(pArgs);
    Py_DECREF(pFunc);
    if (PyErr_Occurred()) {
        Py_XDECREF(pResult);
        python_handle_exception("python_exec2");
        PyThreadState_Swap(NULL);
        PyEval_ReleaseLock();
        return -1;
    }

    if (pResult == NULL) {
        LM_ERR("PyObject_CallObject() returned NULL\n");
        PyThreadState_Swap(NULL);
        PyEval_ReleaseLock();
        return -1;
    }

    rval = PyInt_AsLong(pResult);
    Py_DECREF(pResult);
    PyThreadState_Swap(NULL);
    PyEval_ReleaseLock();
    return rval;
}
コード例 #2
0
ファイル: python_mod.c プロジェクト: AlessioCasco/kamailio
static int mod_init(void)
{
    char *dname_src, *bname_src;

    int i;
    PyObject *sys_path, *pDir, *pModule, *pFunc, *pArgs;
    PyThreadState *mainThreadState;

    dname_src = as_asciiz(&script_name);
    bname_src = as_asciiz(&script_name);

    if(dname_src==NULL || bname_src==NULL)
    {
        LM_ERR("no more pkg memory\n");
        return -1;
    }

    dname = strdup(dirname(dname_src));
    if (strlen(dname) == 0)
        dname = ".";
    bname = strdup(basename(bname_src));
    i = strlen(bname);
    if (bname[i - 1] == 'c' || bname[i - 1] == 'o')
        i -= 1;
    if (bname[i - 3] == '.' && bname[i - 2] == 'p' && bname[i - 1] == 'y') {
        bname[i - 3] = '\0';
    } else {
        LM_ERR("%s: script_name doesn't look like a python script\n",
          script_name.s);
        return -1;
    }

    Py_Initialize();
    PyEval_InitThreads();
    mainThreadState = PyThreadState_Get();

    format_exc_obj = InitTracebackModule();

    if (format_exc_obj == NULL || !PyCallable_Check(format_exc_obj))
    {
        Py_XDECREF(format_exc_obj);
        PyEval_ReleaseLock();
        return -1;
    }

    sys_path = PySys_GetObject("path");
    /* PySys_GetObject doesn't pass reference! No need to DEREF */
    if (sys_path == NULL) {
	if (!PyErr_Occurred())
	    PyErr_Format(PyExc_AttributeError, "'module' object 'sys' has no attribute 'path'");
	python_handle_exception("mod_init");
	Py_DECREF(format_exc_obj);
        PyEval_ReleaseLock();
        return -1;
    }

    pDir = PyString_FromString(dname);
    if (pDir == NULL) {
	if (!PyErr_Occurred())
	    PyErr_Format(PyExc_AttributeError, "PyString_FromString() has failed");
	python_handle_exception("mod_init");
	Py_DECREF(format_exc_obj);
        PyEval_ReleaseLock();
        return -1;
    }

    PyList_Insert(sys_path, 0, pDir);
    Py_DECREF(pDir);

    if (ap_init_modules() != 0) {
	if (!PyErr_Occurred())
	    PyErr_SetString(PyExc_AttributeError, "init_modules() has failed");
	python_handle_exception("mod_init");
	Py_DECREF(format_exc_obj);
        PyEval_ReleaseLock();
        return -1;
    }

    if (python_msgobj_init() != 0) {
	if (!PyErr_Occurred())
	    PyErr_SetString(PyExc_AttributeError, "python_msgobj_init() has failed");
	python_handle_exception("mod_init");
	Py_DECREF(format_exc_obj);
        PyEval_ReleaseLock();
        return -1;
    }

    pModule = PyImport_ImportModule(bname);
    if (pModule == NULL) {
	if (!PyErr_Occurred())
	    PyErr_Format(PyExc_ImportError, "No module named '%s'", bname);
	python_handle_exception("mod_init");
	Py_DECREF(format_exc_obj);
        PyEval_ReleaseLock();
        return -1;
    }

    pkg_free(dname_src);
    pkg_free(bname_src);

    pFunc = PyObject_GetAttrString(pModule, mod_init_fname.s);
    Py_DECREF(pModule);

    /* pFunc is a new reference */

    if (pFunc == NULL) {
	if (!PyErr_Occurred())
	    PyErr_Format(PyExc_AttributeError, "'module' object '%s' has no attribute '%s'",  bname, mod_init_fname.s);
	python_handle_exception("mod_init");
	Py_DECREF(format_exc_obj);
        Py_XDECREF(pFunc);
        PyEval_ReleaseLock();
        return -1;
    }

    if (!PyCallable_Check(pFunc)) {
	if (!PyErr_Occurred())
	    PyErr_Format(PyExc_AttributeError, "module object '%s' has is not callable attribute '%s'", bname, mod_init_fname.s);
	python_handle_exception("mod_init");
	Py_DECREF(format_exc_obj);
        Py_XDECREF(pFunc);
        PyEval_ReleaseLock();
        return -1;
    }


    pArgs = PyTuple_New(0);
    if (pArgs == NULL) {
	python_handle_exception("mod_init");
        Py_DECREF(format_exc_obj);
        Py_DECREF(pFunc);
        PyEval_ReleaseLock();
        return -1;
    }

    handler_obj = PyObject_CallObject(pFunc, pArgs);

    Py_XDECREF(pFunc);
    Py_XDECREF(pArgs);

    if (handler_obj == Py_None) {
	if (!PyErr_Occurred())
	    PyErr_Format(PyExc_TypeError, "Function '%s' of module '%s' has returned None. Should be a class instance.", mod_init_fname.s, bname);
	python_handle_exception("mod_init");
        Py_DECREF(format_exc_obj);
        PyEval_ReleaseLock();
        return -1;
    }

    if (PyErr_Occurred()) {
        python_handle_exception("mod_init");
        Py_XDECREF(handler_obj);
        Py_DECREF(format_exc_obj);
        PyEval_ReleaseLock();
        return -1;
    }

    if (handler_obj == NULL) {
        LM_ERR("PyObject_CallObject() returned NULL but no exception!\n");
	if (!PyErr_Occurred())
	    PyErr_Format(PyExc_TypeError, "Function '%s' of module '%s' has returned not returned object. Should be a class instance.", mod_init_fname.s, bname);
	python_handle_exception("mod_init");
        Py_DECREF(format_exc_obj);
        PyEval_ReleaseLock();
        return -1;
    }

    myThreadState = PyThreadState_New(mainThreadState->interp);
    PyEval_ReleaseLock();

    return 0;
}
コード例 #3
0
ファイル: python_mod.c プロジェクト: ZRouter/ZRouter
static int
mod_init(void)
{
    char *dname, *bname;
    int i;
    PyObject *sys_path, *pDir, *pModule, *pFunc, *pArgs;
    PyThreadState *mainThreadState;

    if (script_name.len == 0) {
        script_name.len = strlen(script_name.s);
    }
    if (mod_init_fname.len == 0) {
        mod_init_fname.len = strlen(mod_init_fname.s);
    }
    if (child_init_mname.len == 0) {
        child_init_mname.len = strlen(child_init_mname.s);
    }

    bname = basename(script_name.s);
    i = strlen(bname);
    if (bname[i - 1] == 'c' || bname[i - 1] == 'o')
        i -= 1;
    if (bname[i - 3] == '.' && bname[i - 2] == 'p' && bname[i - 1] == 'y') {
        bname[i - 3] = '\0';
    } else {
        LM_ERR("%s: script_name doesn't look like a python script\n",
          script_name.s);
        return -1;
    }
    dname = dirname(script_name.s);
    if (strlen(dname) == 0)
        dname = ".";

    Py_Initialize();
    PyEval_InitThreads();
    mainThreadState = PyThreadState_Get();

    Py_InitModule("OpenSIPS", OpenSIPSMethods);

    if (python_msgobj_init() != 0) {
        LM_ERR("python_msgobj_init() has failed\n");
        PyEval_ReleaseLock();
        return -1;
    }

    sys_path = PySys_GetObject("path");
    /* PySys_GetObject doesn't pass reference! No need to DEREF */
    if (sys_path == NULL) {
        LM_ERR("cannot import sys.path\n");
        PyEval_ReleaseLock();
        return -1;
    }

    pDir = PyString_FromString(dname);
    if (pDir == NULL) {
        LM_ERR("PyString_FromString() has filed\n");
        PyEval_ReleaseLock();
        return -1;
    }
    PyList_Insert(sys_path, 0, pDir);
    Py_DECREF(pDir);

    pModule = PyImport_ImportModule(bname);
    if (pModule == NULL) {
        LM_ERR("cannot import %s\n", bname);
        PyEval_ReleaseLock();
        return -1;
    }

    pFunc = PyObject_GetAttrString(pModule, mod_init_fname.s);
    Py_DECREF(pModule);
    /* pFunc is a new reference */
    if (pFunc == NULL || !PyCallable_Check(pFunc)) {
        LM_ERR("cannot locate %s function in %s module\n",
          mod_init_fname.s, script_name.s);
        Py_XDECREF(pFunc);
        PyEval_ReleaseLock();
        return -1;
    }

    pModule = PyImport_ImportModule("traceback");
    if (pModule == NULL) {
        LM_ERR("cannot import traceback module\n");
        Py_DECREF(pFunc);
        PyEval_ReleaseLock();
        return -1;
    }

    format_exc_obj = PyObject_GetAttrString(pModule, "format_exception");
    Py_DECREF(pModule);
    if (format_exc_obj == NULL || !PyCallable_Check(format_exc_obj)) {
        LM_ERR("cannot locate format_exception function in" \
          " traceback module\n");
        Py_XDECREF(format_exc_obj);
        Py_DECREF(pFunc);
        PyEval_ReleaseLock();
        return -1;
    }

    pArgs = PyTuple_New(0);
    if (pArgs == NULL) {
        LM_ERR("PyTuple_New() has failed\n");
        Py_DECREF(pFunc);
        Py_DECREF(format_exc_obj);
        PyEval_ReleaseLock();
        return -1;
    }

    handler_obj = PyObject_CallObject(pFunc, pArgs);
    Py_DECREF(pFunc);
    Py_DECREF(pArgs);

    if (PyErr_Occurred()) {
        python_handle_exception("mod_init");
        Py_XDECREF(handler_obj);
        Py_DECREF(format_exc_obj);
        PyEval_ReleaseLock();
        return -1;
    }

    if (handler_obj == NULL) {
        LM_ERR("%s function has not returned object\n",
          mod_init_fname.s);
        Py_DECREF(format_exc_obj);
        PyEval_ReleaseLock();
        return -1;
    }

    myThreadState = PyThreadState_New(mainThreadState->interp);
    PyEval_ReleaseLock();

    return 0;
}
コード例 #4
0
ファイル: python_mod.c プロジェクト: AlessioCasco/kamailio
static int child_init(int rank)
{
    PyObject *pFunc, *pArgs, *pValue, *pResult;
    int rval;
    char *classname;

    PyEval_AcquireLock();
    PyThreadState_Swap(myThreadState);

    // get instance class name
    classname = get_instance_class_name(handler_obj);
    if (classname == NULL)
    {
	if (!PyErr_Occurred())
	    PyErr_Format(PyExc_AttributeError, "'module' instance has no class name");
	python_handle_exception("child_init");
	Py_DECREF(format_exc_obj);
	PyThreadState_Swap(NULL);
	PyEval_ReleaseLock();
	return -1;
    }

    pFunc = PyObject_GetAttrString(handler_obj, child_init_mname.s);

    if (pFunc == NULL) {
	python_handle_exception("child_init");
	Py_XDECREF(pFunc);
	Py_DECREF(format_exc_obj);
        PyThreadState_Swap(NULL);
        PyEval_ReleaseLock();
        return -1;
    }

    if (!PyCallable_Check(pFunc)) {
	if (!PyErr_Occurred())
	    PyErr_Format(PyExc_AttributeError, "class object '%s' has is not callable attribute '%s'", !classname ? "None" : classname, mod_init_fname.s);
	python_handle_exception("child_init");
	Py_DECREF(format_exc_obj);
	Py_XDECREF(pFunc);
        PyThreadState_Swap(NULL);
        PyEval_ReleaseLock();
        return -1;
    }

    pArgs = PyTuple_New(1);
    if (pArgs == NULL) {
	python_handle_exception("child_init");
	Py_DECREF(format_exc_obj);
        Py_DECREF(pFunc);
        PyThreadState_Swap(NULL);
        PyEval_ReleaseLock();
        return -1;
    }

    pValue = PyInt_FromLong((long)rank);
    if (pValue == NULL) {
	python_handle_exception("child_init");
	Py_DECREF(format_exc_obj);
        Py_DECREF(pArgs);
        Py_DECREF(pFunc);
        PyThreadState_Swap(NULL);
        PyEval_ReleaseLock();
        return -1;
    }
    PyTuple_SetItem(pArgs, 0, pValue);
    /* pValue has been stolen */

    pResult = PyObject_CallObject(pFunc, pArgs);
    Py_DECREF(pFunc);
    Py_DECREF(pArgs);

    


    if (PyErr_Occurred()) {
        python_handle_exception("child_init");
	Py_DECREF(format_exc_obj);
        Py_XDECREF(pResult);
        PyThreadState_Swap(NULL);
        PyEval_ReleaseLock();
        return -1;
    }

    if (pResult == NULL) {
        LM_ERR("PyObject_CallObject() returned NULL but no exception!\n");
        PyThreadState_Swap(NULL);
        PyEval_ReleaseLock();
        return -1;
    }

    if (!PyInt_Check(pResult))
    {
	if (!PyErr_Occurred())
	    PyErr_Format(PyExc_TypeError, "method '%s' of class '%s' should return 'int' type", child_init_mname.s, !classname ? "None" : classname);
	python_handle_exception("child_init");
	Py_DECREF(format_exc_obj);
	Py_XDECREF(pResult);
	PyThreadState_Swap(NULL);
	PyEval_ReleaseLock();
	return -1;
    }


    rval = PyInt_AsLong(pResult);
    Py_DECREF(pResult);
    PyThreadState_Swap(NULL);
    PyEval_ReleaseLock();

    return rval;
}
コード例 #5
0
ファイル: python_mod.c プロジェクト: ZRouter/ZRouter
static int
child_init(int rank)
{
    PyObject *pFunc, *pArgs, *pValue, *pResult;
    int rval;

    PyEval_AcquireLock();
    PyThreadState_Swap(myThreadState);

    pFunc = PyObject_GetAttrString(handler_obj, child_init_mname.s);
    if (pFunc == NULL || !PyCallable_Check(pFunc)) {
        LM_ERR("cannot locate %s function\n", child_init_mname.s);
        if (pFunc != NULL) {
            Py_DECREF(pFunc);
        }
        PyThreadState_Swap(NULL);
        PyEval_ReleaseLock();
        return -1;
    }

    pArgs = PyTuple_New(1);
    if (pArgs == NULL) {
        LM_ERR("PyTuple_New() has failed\n");
        Py_DECREF(pFunc);
        PyThreadState_Swap(NULL);
        PyEval_ReleaseLock();
        return -1;
    }

    pValue = PyInt_FromLong(rank);
    if (pValue == NULL) {
        LM_ERR("PyInt_FromLong() has failed\n");
        Py_DECREF(pArgs);
        Py_DECREF(pFunc);
        PyThreadState_Swap(NULL);
        PyEval_ReleaseLock();
        return -1;
    }
    PyTuple_SetItem(pArgs, 0, pValue);
    /* pValue has been stolen */

    pResult = PyObject_CallObject(pFunc, pArgs);
    Py_DECREF(pFunc);
    Py_DECREF(pArgs);

    if (PyErr_Occurred()) {
        python_handle_exception("child_init");
        Py_XDECREF(pResult);
        PyThreadState_Swap(NULL);
        PyEval_ReleaseLock();
        return -1;
    }

    if (pResult == NULL) {
        LM_ERR("PyObject_CallObject() returned NULL but no exception!\n");
        PyThreadState_Swap(NULL);
        PyEval_ReleaseLock();
        return -1;
    }

    rval = PyInt_AsLong(pResult);
    Py_DECREF(pResult);
    PyThreadState_Swap(NULL);
    PyEval_ReleaseLock();
    return rval;
}