KiCad PCB EDA Suite
python_scripting.h
Go to the documentation of this file.
1 /*
2  * This program source code file is part of KiCad, a free EDA CAD application.
3  *
4  * Copyright (C) 1992-2019 KiCad Developers, see AUTHORS.txt for contributors.
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, you may find one here:
18  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19  * or you may search the http://www.gnu.org website for the version 2 license,
20  * or you may write to the Free Software Foundation, Inc.,
21  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22  */
23 
24 #ifndef __PYTHON_SCRIPTING_H
25 #define __PYTHON_SCRIPTING_H
26 
27 // undefs explained here: https://bugzilla.redhat.com/show_bug.cgi?id=427617
28 
29 #ifdef _POSIX_C_SOURCE
30  #undef _POSIX_C_SOURCE
31 #endif
32 #ifdef _XOPEN_SOURCE
33  #undef _XOPEN_SOURCE
34 #endif
35 
36 #if defined(WIN32)
37  #undef pid_t // wxWidgets defines this, python typedefs, result is a conflict
38 #endif
39 
40 #undef HAVE_CLOCK_GETTIME // macro is defined in Python.h and causes redefine warning
41 #include <Python.h>
42 #undef HAVE_CLOCK_GETTIME
43 
44 #ifndef NO_WXPYTHON_EXTENSION_HEADERS
45 #ifdef KICAD_SCRIPTING_WXPYTHON
46  #ifdef KICAD_SCRIPTING_WXPYTHON_PHOENIX
47  #include <wx/window.h>
48  #else
49  #include <wx/wxPython/wxPython.h>
50  #endif
51 #endif
52 #endif
53 
54 #include <wx/string.h>
55 #include <wx/arrstr.h>
56 
57 
61 bool pcbnewInitPythonScripting( const char* aStockScriptingPath, const char* aUserScriptingPath );
63 
69 void pcbnewGetUnloadableScriptNames( wxString& aNames );
70 
76 void pcbnewGetScriptsSearchPaths( wxString& aNames );
77 
83 void pcbnewGetWizardsBackTrace( wxString& aNames );
84 
91 void pcbnewUpdatePythonEnvVar( const wxString& aVar, const wxString& aValue );
92 
93 
94 #ifdef KICAD_SCRIPTING_WXPYTHON
95 void RedirectStdio();
96 wxWindow* CreatePythonShellWindow( wxWindow* parent, const wxString& aFramenameId );
97 #endif
98 
99 bool IsWxPythonLoaded();
100 
101 #if 0 && defined (KICAD_SCRIPTING_WXPYTHON)
102 // This definition of PyLOCK crashed Pcbnew under some conditions (JPC),
103 // especially reloading plugins
104 class PyLOCK
105 {
106  wxPyBlock_t b;
107 public:
108 
109  // @todo, find out why these are wxPython specific. We need the GIL regardless.
110  // Should never assume python will only have one thread calling it.
111  PyLOCK() { b = wxPyBeginBlockThreads(); }
112  ~PyLOCK() { wxPyEndBlockThreads( b ); }
113 };
114 #else
115 class PyLOCK
116 {
117  PyGILState_STATE gil_state;
118 public:
119  PyLOCK() { gil_state = PyGILState_Ensure(); }
120  ~PyLOCK() { PyGILState_Release( gil_state ); }
121 };
122 #endif
123 
124 wxString PyStringToWx( PyObject* str );
125 wxArrayString PyArrayStringToWx( PyObject* arr );
126 wxString PyErrStringWithTraceback();
127 
128 wxString PyScriptingPath( bool aUserPath = false );
129 wxString PyPluginsPath( bool aUserPath = false );
130 
131 #endif // __PYTHON_SCRIPTING_H
void pcbnewGetWizardsBackTrace(wxString &aNames)
Return the backtrace of errors (if any) when wizard python scripts are loaded.
bool pcbnewInitPythonScripting(const char *aStockScriptingPath, const char *aUserScriptingPath)
Initialize the Python engine inside pcbnew.
PyGILState_STATE gil_state
wxString PyScriptingPath(bool aUserPath=false)
Find the Python scripting path.
wxArrayString PyArrayStringToWx(PyObject *arr)
wxString PyStringToWx(PyObject *str)
void pcbnewFinishPythonScripting()
wxString PyErrStringWithTraceback()
bool IsWxPythonLoaded()
void pcbnewUpdatePythonEnvVar(const wxString &aVar, const wxString &aValue)
Set an environment variable in the current Python interpreter.
void pcbnewGetUnloadableScriptNames(wxString &aNames)
Collect the list of python scripts which could not be loaded.
wxString PyPluginsPath(bool aUserPath=false)
void pcbnewGetScriptsSearchPaths(wxString &aNames)
Collect the list of paths where python scripts are searched.