Commit 2a2a530c authored by Dick Hollenbeck's avatar Dick Hollenbeck

improvements to python's GIL acquisition and release, but not done yet, since...

improvements to python's GIL acquisition and release, but not done yet, since I think the GIL needs to be acquired even when not involving wxPython.
parent a88d067a
...@@ -55,7 +55,7 @@ ...@@ -55,7 +55,7 @@
#include <dialog_helpers.h> #include <dialog_helpers.h>
#include <convert_from_iu.h> #include <convert_from_iu.h>
#ifdef KICAD_SCRIPTING #if defined(KICAD_SCRIPTING) || defined(KICAD_SCRIPTING_WXPYTHON)
#include <python_scripting.h> #include <python_scripting.h>
#endif #endif
...@@ -410,7 +410,7 @@ PCB_EDIT_FRAME::PCB_EDIT_FRAME( wxWindow* parent, const wxString& title, ...@@ -410,7 +410,7 @@ PCB_EDIT_FRAME::PCB_EDIT_FRAME( wxWindow* parent, const wxString& title,
wxAuiPaneInfo( mesg ).Name( wxT( "MsgPanel" ) ).Bottom().Layer(10) ); wxAuiPaneInfo( mesg ).Name( wxT( "MsgPanel" ) ).Bottom().Layer(10) );
#ifdef KICAD_SCRIPTING_WXPYTHON #ifdef KICAD_SCRIPTING_WXPYTHON
// Add the scripting panel // Add the scripting panel
EDA_PANEINFO pythonAuiInfo; EDA_PANEINFO pythonAuiInfo;
pythonAuiInfo.ScriptingToolbarPane(); pythonAuiInfo.ScriptingToolbarPane();
...@@ -424,8 +424,7 @@ PCB_EDIT_FRAME::PCB_EDIT_FRAME( wxWindow* parent, const wxString& title, ...@@ -424,8 +424,7 @@ PCB_EDIT_FRAME::PCB_EDIT_FRAME( wxWindow* parent, const wxString& title,
pythonAuiInfo.Name( wxT( "PythonPanel" ) ).Bottom().Layer(9) ); pythonAuiInfo.Name( wxT( "PythonPanel" ) ).Bottom().Layer(9) );
m_pythonPanelHidden = true; m_pythonPanelHidden = true;
#endif #endif
ReFillLayerWidget(); // this is near end because contents establish size ReFillLayerWidget(); // this is near end because contents establish size
......
...@@ -17,14 +17,14 @@ class PYTHON_FOOTPRINT_WIZARD: public FOOTPRINT_WIZARD ...@@ -17,14 +17,14 @@ class PYTHON_FOOTPRINT_WIZARD: public FOOTPRINT_WIZARD
PyObject *m_PyWizard; PyObject *m_PyWizard;
PyObject *CallMethod( const char *aMethod, PyObject *aArglist=NULL ); PyObject *CallMethod( const char *aMethod, PyObject *aArglist=NULL );
wxString CallRetStrMethod( const char *aMethod, PyObject *aArglist=NULL ); wxString CallRetStrMethod( const char *aMethod, PyObject *aArglist=NULL );
wxArrayString CallRetArrayStrMethod( const char *aMethod, wxArrayString CallRetArrayStrMethod( const char *aMethod,
PyObject *aArglist=NULL ); PyObject *aArglist=NULL );
public: public:
PYTHON_FOOTPRINT_WIZARD( PyObject *wizard ); PYTHON_FOOTPRINT_WIZARD( PyObject *wizard );
~PYTHON_FOOTPRINT_WIZARD(); ~PYTHON_FOOTPRINT_WIZARD();
wxString GetName(); wxString GetName();
wxString GetImage(); wxString GetImage();
wxString GetDescription(); wxString GetDescription();
int GetNumParameterPages(); int GetNumParameterPages();
wxString GetParameterPageName( int aPage ); wxString GetParameterPageName( int aPage );
...@@ -33,11 +33,11 @@ public: ...@@ -33,11 +33,11 @@ public:
wxArrayString GetParameterValues( int aPage ); wxArrayString GetParameterValues( int aPage );
wxArrayString GetParameterErrors( int aPage ); wxArrayString GetParameterErrors( int aPage );
wxString SetParameterValues( int aPage, wxArrayString& aValues ); //< must return "OK" or error description wxString SetParameterValues( int aPage, wxArrayString& aValues ); //< must return "OK" or error description
MODULE *GetModule(); MODULE* GetModule();
}; };
class PYTHON_FOOTPRINT_WIZARDS class PYTHON_FOOTPRINT_WIZARDS
{ {
public: public:
static void register_wizard( PyObject *aPyWizard ); static void register_wizard( PyObject *aPyWizard );
......
...@@ -45,8 +45,8 @@ extern "C" void init_kicad( void ); ...@@ -45,8 +45,8 @@ extern "C" void init_kicad( void );
extern "C" void init_pcbnew( void ); extern "C" void init_pcbnew( void );
#define EXTRA_PYTHON_MODULES 10 // this is the number of python #define EXTRA_PYTHON_MODULES 10 // this is the number of python
// modules that we want to add into the list // modules that we want to add into the list
/* python inittab that links module names to module init functions /* python inittab that links module names to module init functions
...@@ -126,11 +126,10 @@ static void swigSwitchPythonBuiltin() ...@@ -126,11 +126,10 @@ static void swigSwitchPythonBuiltin()
* *
*/ */
PyThreadState *g_PythonMainTState; PyThreadState* g_PythonMainTState;
bool pcbnewInitPythonScripting() bool pcbnewInitPythonScripting()
{ {
swigAddBuiltin(); // add builtin functions swigAddBuiltin(); // add builtin functions
swigAddModules(); // add our own modules swigAddModules(); // add our own modules
swigSwitchPythonBuiltin(); // switch the python builtin modules to our new list swigSwitchPythonBuiltin(); // switch the python builtin modules to our new list
...@@ -158,16 +157,18 @@ bool pcbnewInitPythonScripting() ...@@ -158,16 +157,18 @@ bool pcbnewInitPythonScripting()
// load pcbnew inside python, and load all the user plugins, TODO: add system wide plugins // load pcbnew inside python, and load all the user plugins, TODO: add system wide plugins
PY_BLOCK_THREADS( blocked );
#endif #endif
PyRun_SimpleString( "import sys\n" {
"sys.path.append(\".\")\n" PyLOCK lock;
"import pcbnew\n"
"pcbnew.LoadPlugins()" PyRun_SimpleString( "import sys\n"
); "sys.path.append(\".\")\n"
"import pcbnew\n"
"pcbnew.LoadPlugins()"
);
}
PY_UNBLOCK_THREADS( blocked );
return true; return true;
} }
...@@ -180,7 +181,7 @@ void pcbnewFinishPythonScripting() ...@@ -180,7 +181,7 @@ void pcbnewFinishPythonScripting()
} }
#ifdef KICAD_SCRIPTING_WXPYTHON #if defined(KICAD_SCRIPTING_WXPYTHON)
void RedirectStdio() void RedirectStdio()
{ {
...@@ -193,9 +194,8 @@ import wx\n\ ...@@ -193,9 +194,8 @@ import wx\n\
output = wx.PyOnDemandOutputWindow()\n\ output = wx.PyOnDemandOutputWindow()\n\
c sys.stderr = output\n"; c sys.stderr = output\n";
PY_BLOCK_THREADS( blocked ); PyLOCK lock;
PyRun_SimpleString( python_redirect ); PyRun_SimpleString( python_redirect );
PY_UNBLOCK_THREADS( blocked );
} }
...@@ -228,7 +228,7 @@ def makeWindow(parent):\n\ ...@@ -228,7 +228,7 @@ def makeWindow(parent):\n\
PyObject* result; PyObject* result;
// As always, first grab the GIL // As always, first grab the GIL
PY_BLOCK_THREADS( blocked ); PyLOCK lock;
// Now make a dictionary to serve as the global namespace when the code is // Now make a dictionary to serve as the global namespace when the code is
// executed. Put a reference to the builtins module in it. // executed. Put a reference to the builtins module in it.
...@@ -245,7 +245,6 @@ def makeWindow(parent):\n\ ...@@ -245,7 +245,6 @@ def makeWindow(parent):\n\
if( !result ) if( !result )
{ {
PyErr_Print(); PyErr_Print();
PY_UNBLOCK_THREADS( blocked );
return NULL; return NULL;
} }
Py_DECREF(result); Py_DECREF(result);
...@@ -285,10 +284,6 @@ def makeWindow(parent):\n\ ...@@ -285,10 +284,6 @@ def makeWindow(parent):\n\
Py_DECREF( globals ); Py_DECREF( globals );
Py_DECREF( tuple ); Py_DECREF( tuple );
// Finally, after all Python stuff is done, release the GIL
PY_UNBLOCK_THREADS( blocked );
return window; return window;
} }
#endif #endif
...@@ -3,43 +3,57 @@ ...@@ -3,43 +3,57 @@
// undefs explained here: https://bugzilla.redhat.com/show_bug.cgi?id=427617 // undefs explained here: https://bugzilla.redhat.com/show_bug.cgi?id=427617
#ifdef _POSIX_C_SOURCE #ifdef _POSIX_C_SOURCE
#undef _POSIX_C_SOURCE #undef _POSIX_C_SOURCE
#endif #endif
#ifdef _XOPEN_SOURCE #ifdef _XOPEN_SOURCE
#undef _XOPEN_SOURCE #undef _XOPEN_SOURCE
#endif #endif
#include <Python.h>
#ifndef NO_WXPYTHON_EXTENSION_HEADERS
#ifdef KICAD_SCRIPTING_WXPYTHON
#include <wx/wxPython/wxPython.h>
#endif
#endif
#include <Python.h>
#ifndef NO_WXPYTHON_EXTENSION_HEADERS
#ifdef KICAD_SCRIPTING_WXPYTHON
#include <wx/wxPython/wxPython.h>
#endif
#endif
/* Function pcbnewInitPythonScripting /* Function pcbnewInitPythonScripting
* Initializes the Python engine inside pcbnew * Initializes the Python engine inside pcbnew
*/ */
bool pcbnewInitPythonScripting(); bool pcbnewInitPythonScripting();
void pcbnewFinishPythonScripting(); void pcbnewFinishPythonScripting();
#ifdef KICAD_SCRIPTING_WXPYTHON #ifdef KICAD_SCRIPTING_WXPYTHON
void RedirectStdio(); void RedirectStdio();
wxWindow* CreatePythonShellWindow(wxWindow* parent); wxWindow* CreatePythonShellWindow( wxWindow* parent );
class PyLOCK
{
wxPyBlock_t b;
public:
// @todo, find out why these are wxPython specific. We need the GIL regardless.
// Should never assume python will only have one thread calling it.
PyLOCK() { b = wxPyBeginBlockThreads(); }
~PyLOCK() { wxPyEndBlockThreads( b ); }
};
#define PY_BLOCK_THREADS(name) wxPyBlock_t name = wxPyBeginBlockThreads()
#define PY_UNBLOCK_THREADS(name) wxPyEndBlockThreads(name)
#else #else
#define PY_BLOCK_THREADS(name) class PyLOCK
#define PY_UNBLOCK_THREADS(name) {
public:
// @todo: this is wrong, python docs clearly say we need the GIL,
// irrespective of wxPython.
PyLOCK() {}
~PyLOCK() {}
};
#endif #endif
#endif #endif // __PYTHON_SCRIPTING_H
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