Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

app_python3: first release #1447

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
86 changes: 49 additions & 37 deletions src/modules/app_python3/app_python_mod.c
Expand Up @@ -194,25 +194,36 @@ static void mod_destroy(void)
destroy_mod_Router();
}


#define PY_GIL_ENSURE gstate = PyGILState_Ensure();
#define PY_GIL_RELEASE PyGILState_Release(gstate);

// #define PY_THREADSTATE_SWAP_IN PyThreadState_Swap(myThreadState);
// #define PY_THREADSTATE_SWAP_NULL PyThreadState_Swap(NULL);
#define PY_THREADSTATE_SWAP_IN
#define PY_THREADSTATE_SWAP_NULL

int apy_load_script(void)
{
PyObject *sys_path, *pDir, *pModule, *pFunc, *pArgs;
PyThreadState *mainThreadState;

PyGILState_STATE gstate;

if (ap_init_modules() != 0) {
return -1;
}

Py_Initialize();
//TODO PyEval_InitThreads();
//TODI mainThreadState = PyThreadState_Get();
PyEval_InitThreads();
myThreadState = PyThreadState_Get();

PY_GIL_ENSURE
format_exc_obj = InitTracebackModule();

if (format_exc_obj == NULL || !PyCallable_Check(format_exc_obj))
{
Py_XDECREF(format_exc_obj);
//TODO PyEval_SaveThread();
PY_GIL_RELEASE
return -1;
}

Expand All @@ -224,7 +235,7 @@ int apy_load_script(void)
"'module' object 'sys' has no attribute 'path'");
python_handle_exception("mod_init");
Py_DECREF(format_exc_obj);
//TODO PyEval_SaveThread();
PY_GIL_RELEASE
return -1;
}

Expand All @@ -235,7 +246,7 @@ int apy_load_script(void)
"PyUnicode_FromString() has failed");
python_handle_exception("mod_init");
Py_DECREF(format_exc_obj);
//TODO PyEval_SaveThread();
PY_GIL_RELEASE
return -1;
}

Expand All @@ -248,7 +259,7 @@ int apy_load_script(void)
"python_msgobj_init() has failed");
python_handle_exception("mod_init");
Py_DECREF(format_exc_obj);
//TODO PyEval_SaveThread();
PY_GIL_RELEASE
return -1;
}

Expand All @@ -259,7 +270,7 @@ int apy_load_script(void)
PyErr_Format(PyExc_ImportError, "No module named '%s'", bname);
python_handle_exception("mod_init");
Py_DECREF(format_exc_obj);
//TODO PyEval_SaveThread();
PY_GIL_RELEASE

return -1;
}
Expand All @@ -277,7 +288,7 @@ int apy_load_script(void)
python_handle_exception("mod_init");
Py_DECREF(format_exc_obj);
Py_XDECREF(pFunc);
//TODO PyEval_SaveThread();
PY_GIL_RELEASE
return -1;
}

Expand All @@ -289,7 +300,7 @@ int apy_load_script(void)
python_handle_exception("mod_init");
Py_DECREF(format_exc_obj);
Py_XDECREF(pFunc);
//TODO PyEval_SaveThread();
PY_GIL_RELEASE
return -1;
}

Expand All @@ -299,7 +310,7 @@ int apy_load_script(void)
python_handle_exception("mod_init");
Py_DECREF(format_exc_obj);
Py_DECREF(pFunc);
//TODO PyEval_SaveThread();
PY_GIL_RELEASE
return -1;
}

Expand All @@ -315,15 +326,15 @@ int apy_load_script(void)
" Should be a class instance.", mod_init_fname.s, bname);
python_handle_exception("mod_init");
Py_DECREF(format_exc_obj);
//TODO PyEval_SaveThread();
PY_GIL_RELEASE
return -1;
}

if (PyErr_Occurred()) {
python_handle_exception("mod_init");
Py_XDECREF(_sr_apy_handler_obj);
Py_DECREF(format_exc_obj);
//TODO PyEval_SaveThread();
PY_GIL_RELEASE
return -1;
}

Expand All @@ -336,13 +347,12 @@ int apy_load_script(void)
mod_init_fname.s, bname);
python_handle_exception("mod_init");
Py_DECREF(format_exc_obj);
//TODO PyEval_SaveThread();
PY_GIL_RELEASE
return -1;
}

//TODO myThreadState = PyThreadState_New(mainThreadState->interp);
//TODO PyEval_SaveThread();

//myThreadState = PyThreadState_New(mainThreadState->interp);
PY_GIL_RELEASE
return 0;
}

Expand All @@ -351,9 +361,11 @@ int apy_init_script(int rank)
PyObject *pFunc, *pArgs, *pValue, *pResult;
int rval;
char *classname;
PyGILState_STATE gstate;


//TODO PyEval_AcquireLock();
//TODO PyThreadState_Swap(myThreadState);
PY_GIL_ENSURE
PY_THREADSTATE_SWAP_IN

// get instance class name
classname = get_instance_class_name(_sr_apy_handler_obj);
Expand All @@ -364,8 +376,8 @@ int apy_init_script(int rank)
"'module' instance has no class name");
python_handle_exception("child_init");
Py_DECREF(format_exc_obj);
//TODO PyThreadState_Swap(NULL);
//TODO PyEval_SaveThread();
PY_THREADSTATE_SWAP_NULL
PY_GIL_RELEASE
return -1;
}

Expand All @@ -375,8 +387,8 @@ int apy_init_script(int rank)
python_handle_exception("child_init");
Py_XDECREF(pFunc);
Py_DECREF(format_exc_obj);
//TODO PyThreadState_Swap(NULL);
//TODO PyEval_SaveThread();
PY_THREADSTATE_SWAP_NULL
PY_GIL_RELEASE
return -1;
}

Expand All @@ -388,8 +400,8 @@ int apy_init_script(int rank)
python_handle_exception("child_init");
Py_DECREF(format_exc_obj);
Py_XDECREF(pFunc);
//TODO PyThreadState_Swap(NULL);
//TODO PyEval_SaveThread();
PY_THREADSTATE_SWAP_NULL
PY_GIL_RELEASE
return -1;
}

Expand All @@ -398,8 +410,8 @@ int apy_init_script(int rank)
python_handle_exception("child_init");
Py_DECREF(format_exc_obj);
Py_DECREF(pFunc);
//TODO PyThreadState_Swap(NULL);
//TODO PyEval_SaveThread();
PY_THREADSTATE_SWAP_NULL
PY_GIL_RELEASE
return -1;
}

Expand All @@ -409,8 +421,8 @@ int apy_init_script(int rank)
Py_DECREF(format_exc_obj);
Py_DECREF(pArgs);
Py_DECREF(pFunc);
//TODO PyThreadState_Swap(NULL);
//TODO PyEval_SaveThread();
PY_THREADSTATE_SWAP_NULL
PY_GIL_RELEASE
return -1;
}
PyTuple_SetItem(pArgs, 0, pValue);
Expand All @@ -424,15 +436,15 @@ int apy_init_script(int rank)
python_handle_exception("child_init");
Py_DECREF(format_exc_obj);
Py_XDECREF(pResult);
//TODO PyThreadState_Swap(NULL);
//TODO PyEval_SaveThread();
PY_THREADSTATE_SWAP_NULL
PY_GIL_RELEASE
return -1;
}

if (pResult == NULL) {
LM_ERR("PyObject_CallObject() returned NULL but no exception!\n");
//TODO PyThreadState_Swap(NULL);
//TODO PyEval_SaveThread();
PY_THREADSTATE_SWAP_NULL
PY_GIL_RELEASE
return -1;
}

Expand All @@ -445,15 +457,15 @@ int apy_init_script(int rank)
python_handle_exception("child_init");
Py_DECREF(format_exc_obj);
Py_XDECREF(pResult);
//TODO PyThreadState_Swap(NULL);
//TODO PyEval_SaveThread();
PY_THREADSTATE_SWAP_NULL
PY_GIL_RELEASE
return -1;
}

rval = PyLong_AsLong(pResult);
Py_DECREF(pResult);
//TODO PyThreadState_Swap(NULL);
//TODO PyEval_SaveThread();
PY_THREADSTATE_SWAP_NULL
PY_GIL_RELEASE

return rval;
}
Expand Down
43 changes: 26 additions & 17 deletions src/modules/app_python3/python_exec.c
Expand Up @@ -48,6 +48,14 @@ sr_apy_env_t *sr_apy_env_get()

static int _sr_apy_exec_pid = 0;

#define PY_GIL_ENSURE gstate = PyGILState_Ensure();
#define PY_GIL_RELEASE PyGILState_Release(gstate);

// #define PY_THREADSTATE_SWAP_IN PyThreadState_Swap(myThreadState);
// #define PY_THREADSTATE_SWAP_NULL PyThreadState_Swap(NULL);
#define PY_THREADSTATE_SWAP_IN
#define PY_THREADSTATE_SWAP_NULL

/**
*
*/
Expand All @@ -59,15 +67,16 @@ int apy_exec(sip_msg_t *_msg, char *fname, char *fparam, int emode)
sip_msg_t *bmsg;
int mpid;
int locked = 0;

PyGILState_STATE gstate;

bmsg = _sr_apy_env.msg;
_sr_apy_env.msg = _msg;
mpid = getpid();

if(_sr_apy_exec_pid!=mpid) {
//TODO PyEval_AcquireLock();
PY_GIL_ENSURE
_sr_apy_exec_pid = mpid;
//TODO PyThreadState_Swap(myThreadState);
PY_THREADSTATE_SWAP_IN
locked = 1;
}

Expand All @@ -81,8 +90,8 @@ int apy_exec(sip_msg_t *_msg, char *fname, char *fparam, int emode)
Py_XDECREF(pFunc);
if(locked) {
_sr_apy_exec_pid = 0;
//TODO PyThreadState_Swap(NULL);
//TODO PyEvalReleaseLock();
PY_THREADSTATE_SWAP_NULL
PY_GIL_RELEASE
}
_sr_apy_env.msg = bmsg;
if(emode==1) {
Expand All @@ -98,8 +107,8 @@ int apy_exec(sip_msg_t *_msg, char *fname, char *fparam, int emode)
Py_DECREF(pFunc);
if(locked) {
_sr_apy_exec_pid = 0;
//TODO PyThreadState_Swap(NULL);
//TODO PyEvalReleaseLock();
PY_THREADSTATE_SWAP_NULL
PY_GIL_RELEASE
}
_sr_apy_env.msg = bmsg;
return -1;
Expand All @@ -113,8 +122,8 @@ int apy_exec(sip_msg_t *_msg, char *fname, char *fparam, int emode)
Py_DECREF(pFunc);
if(locked) {
_sr_apy_exec_pid = 0;
//TODO PyThreadState_Swap(NULL);
//TODO PyEvalReleaseLock();
PY_THREADSTATE_SWAP_NULL
PY_GIL_RELEASE
}
_sr_apy_env.msg = bmsg;
return -1;
Expand All @@ -131,8 +140,8 @@ int apy_exec(sip_msg_t *_msg, char *fname, char *fparam, int emode)
Py_DECREF(pFunc);
if(locked) {
_sr_apy_exec_pid = 0;
//TODO PyThreadState_Swap(NULL);
//TODO PyEvalReleaseLock();
PY_THREADSTATE_SWAP_NULL
PY_GIL_RELEASE
}
_sr_apy_env.msg = bmsg;
return -1;
Expand All @@ -150,8 +159,8 @@ int apy_exec(sip_msg_t *_msg, char *fname, char *fparam, int emode)
python_handle_exception("python_exec2");
if(locked) {
_sr_apy_exec_pid = 0;
//TODO PyThreadState_Swap(NULL);
//TODO PyEvalReleaseLock();
PY_THREADSTATE_SWAP_NULL
PY_GIL_RELEASE
}
_sr_apy_env.msg = bmsg;
return -1;
Expand All @@ -161,8 +170,8 @@ int apy_exec(sip_msg_t *_msg, char *fname, char *fparam, int emode)
LM_ERR("PyObject_CallObject() returned NULL\n");
if(locked) {
_sr_apy_exec_pid = 0;
//TODO PyThreadState_Swap(NULL);
//TODO PyEvalReleaseLock();
PY_THREADSTATE_SWAP_NULL
PY_GIL_RELEASE
}
_sr_apy_env.msg = bmsg;
return -1;
Expand All @@ -172,8 +181,8 @@ int apy_exec(sip_msg_t *_msg, char *fname, char *fparam, int emode)
Py_DECREF(pResult);
if(locked) {
_sr_apy_exec_pid = 0;
//TODO PyThreadState_Swap(NULL);
//TODO PyEval_ReleaseLock();
PY_THREADSTATE_SWAP_NULL
PY_GIL_RELEASE
}
_sr_apy_env.msg = bmsg;
return rval;
Expand Down