Commit 16e12f18 by Baruch Sterin

pyabc: fix callbacks into python to work correctly by moving to PyGILEState_Ensure/Release APIs

parent ef0fbf03
...@@ -262,8 +262,6 @@ void pyabc_internal_set_command_callback( PyObject* callback ) ...@@ -262,8 +262,6 @@ void pyabc_internal_set_command_callback( PyObject* callback )
pyabc_internal_python_command_callback = callback; pyabc_internal_python_command_callback = callback;
} }
PyThreadState *_save;
static int pyabc_internal_abc_command_callback(Abc_Frame_t * pAbc, int argc, char ** argv) static int pyabc_internal_abc_command_callback(Abc_Frame_t * pAbc, int argc, char ** argv)
{ {
int i; int i;
...@@ -271,13 +269,15 @@ static int pyabc_internal_abc_command_callback(Abc_Frame_t * pAbc, int argc, cha ...@@ -271,13 +269,15 @@ static int pyabc_internal_abc_command_callback(Abc_Frame_t * pAbc, int argc, cha
PyObject* args; PyObject* args;
PyObject* arglist; PyObject* arglist;
PyObject* res; PyObject* res;
PyGILState_STATE gstate;
long lres; long lres;
if ( !pyabc_internal_python_command_callback ) if ( !pyabc_internal_python_command_callback )
return 0; return 0;
Py_BLOCK_THREADS gstate = PyGILState_Ensure();
args = PyList_New(argc); args = PyList_New(argc);
...@@ -292,14 +292,14 @@ static int pyabc_internal_abc_command_callback(Abc_Frame_t * pAbc, int argc, cha ...@@ -292,14 +292,14 @@ static int pyabc_internal_abc_command_callback(Abc_Frame_t * pAbc, int argc, cha
if ( !res ) if ( !res )
{ {
Py_UNBLOCK_THREADS PyGILState_Release(gstate);
return -1; return -1;
} }
lres = PyInt_AsLong(res); lres = PyInt_AsLong(res);
Py_DECREF(res); Py_DECREF(res);
Py_UNBLOCK_THREADS PyGILState_Release(gstate);
return lres; return lres;
} }
...@@ -309,11 +309,11 @@ int run_command(char* cmd) ...@@ -309,11 +309,11 @@ int run_command(char* cmd)
Abc_Frame_t* pAbc = Abc_FrameGetGlobalFrame(); Abc_Frame_t* pAbc = Abc_FrameGetGlobalFrame();
int rc; int rc;
Py_UNBLOCK_THREADS Py_BEGIN_ALLOW_THREADS
rc = Cmd_CommandExecute(pAbc, cmd); rc = Cmd_CommandExecute(pAbc, cmd);
Py_BLOCK_THREADS Py_END_ALLOW_THREADS
return rc; return rc;
} }
...@@ -401,13 +401,15 @@ int Util_SignalSystem(const char* cmd) ...@@ -401,13 +401,15 @@ int Util_SignalSystem(const char* cmd)
{ {
PyObject* arglist; PyObject* arglist;
PyObject* res; PyObject* res;
PyGILState_STATE gstate;
long lres; long lres;
if ( !pyabc_internal_system_callback ) if ( !pyabc_internal_system_callback )
return -1; return -1;
Py_BLOCK_THREADS gstate = PyGILState_Ensure();
arglist = Py_BuildValue("(O)", PyString_FromString(cmd)); arglist = Py_BuildValue("(O)", PyString_FromString(cmd));
Py_INCREF(arglist); Py_INCREF(arglist);
...@@ -417,14 +419,14 @@ int Util_SignalSystem(const char* cmd) ...@@ -417,14 +419,14 @@ int Util_SignalSystem(const char* cmd)
if ( !res ) if ( !res )
{ {
Py_UNBLOCK_THREADS PyGILState_Release(gstate);
return -1; return -1;
} }
lres = PyInt_AsLong(res); lres = PyInt_AsLong(res);
Py_DECREF(res); Py_DECREF(res);
Py_UNBLOCK_THREADS PyGILState_Release(gstate);
return lres; return lres;
} }
...@@ -437,12 +439,14 @@ int Util_SignalTmpFile(const char* prefix, const char* suffix, char** out_name) ...@@ -437,12 +439,14 @@ int Util_SignalTmpFile(const char* prefix, const char* suffix, char** out_name)
PyObject* arglist; PyObject* arglist;
PyObject* res; PyObject* res;
PyGILState_STATE gstate;
*out_name = NULL; *out_name = NULL;
if ( !pyabc_internal_tmpfile_callback ) if ( !pyabc_internal_tmpfile_callback )
return 0; return 0;
Py_BLOCK_THREADS gstate = PyGILState_Ensure();
arglist = Py_BuildValue("(ss)", prefix, suffix); arglist = Py_BuildValue("(ss)", prefix, suffix);
Py_INCREF(arglist); Py_INCREF(arglist);
...@@ -452,7 +456,7 @@ int Util_SignalTmpFile(const char* prefix, const char* suffix, char** out_name) ...@@ -452,7 +456,7 @@ int Util_SignalTmpFile(const char* prefix, const char* suffix, char** out_name)
if ( !res ) if ( !res )
{ {
Py_UNBLOCK_THREADS PyGILState_Release(gstate);
return -1; return -1;
} }
...@@ -463,7 +467,7 @@ int Util_SignalTmpFile(const char* prefix, const char* suffix, char** out_name) ...@@ -463,7 +467,7 @@ int Util_SignalTmpFile(const char* prefix, const char* suffix, char** out_name)
Py_DECREF(res); Py_DECREF(res);
Py_UNBLOCK_THREADS PyGILState_Release(gstate);
return open(*out_name, O_WRONLY); return open(*out_name, O_WRONLY);
} }
...@@ -472,11 +476,13 @@ void Util_SignalTmpFileRemove(const char* fname, int fLeave) ...@@ -472,11 +476,13 @@ void Util_SignalTmpFileRemove(const char* fname, int fLeave)
{ {
PyObject* arglist; PyObject* arglist;
PyObject* res; PyObject* res;
PyGILState_STATE gstate;
if ( !pyabc_internal_tmpfile_remove_callback ) if ( !pyabc_internal_tmpfile_remove_callback )
return; return;
Py_BLOCK_THREADS gstate = PyGILState_Ensure();
arglist = Py_BuildValue("(si)", fname, fLeave); arglist = Py_BuildValue("(si)", fname, fLeave);
Py_INCREF(arglist); Py_INCREF(arglist);
...@@ -485,7 +491,7 @@ void Util_SignalTmpFileRemove(const char* fname, int fLeave) ...@@ -485,7 +491,7 @@ void Util_SignalTmpFileRemove(const char* fname, int fLeave)
Py_DECREF(arglist); Py_DECREF(arglist);
Py_XDECREF(res); Py_XDECREF(res);
Py_UNBLOCK_THREADS PyGILState_Release(gstate);
} }
void pyabc_internal_set_util_callbacks( PyObject* system_callback, PyObject* tmpfile_callback, PyObject* tmpfile_remove_callback ) void pyabc_internal_set_util_callbacks( PyObject* system_callback, PyObject* tmpfile_callback, PyObject* tmpfile_remove_callback )
...@@ -1045,7 +1051,7 @@ def cmd_python(cmd_args): ...@@ -1045,7 +1051,7 @@ def cmd_python(cmd_args):
usage = "usage: %prog [options] <Python files>" usage = "usage: %prog [options] <Python files>"
parser = optparse.OptionParser(usage) parser = optparse.OptionParser(usage, prog="python")
parser.add_option("-c", "--cmd", dest="cmd", help="Execute Python command directly") parser.add_option("-c", "--cmd", dest="cmd", help="Execute Python command directly")
parser.add_option("-v", "--version", action="store_true", dest="version", help="Display Python Version") parser.add_option("-v", "--version", action="store_true", dest="version", help="Display Python Version")
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment