Skip to content

Commit

Permalink
Python: run the set_python_path script at initialisation time and n…
Browse files Browse the repository at this point in the history
…o longer during installation or when running the Python shell (#2205).
  • Loading branch information
dbrnz committed Nov 1, 2019
1 parent 1a9301f commit d3b8020
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 22 deletions.
6 changes: 0 additions & 6 deletions CMakeLists.txt
Expand Up @@ -1380,12 +1380,6 @@ if(WIN32)
\\\${UnregisterExtension} \\\".cellml\\\" \\\"CellML File\\\"
System::Call \\\"Shell32::SHChangeNotify(i 0x08000000, i 0, i 0, i 0)\\\"
")

# Update Python scripts to refer to our installed Python

set(CPACK_NSIS_EXTRA_INSTALL_COMMANDS "${CPACK_NSIS_EXTRA_INSTALL_COMMANDS}
Exec '\\\"\\\$INSTDIR\\\\Python\\\\bin\\\\python.exe\\\" \\\"\\\$INSTDIR\\\\Python\\\\Scripts\\\\set_python_path.py\\\" \\\"\\\$INSTDIR\\\\Python\\\" -s'
")
endif()
elseif(APPLE)
# Select PackageMaker and ZIP as the packagers on macOS
Expand Down
2 changes: 0 additions & 2 deletions distrib/linux/runpython.in
Expand Up @@ -2,6 +2,4 @@

appDir="$(cd "$(dirname "$0")"; pwd)"

${appDir}/setpythonpath

${appDir}/bin/@CMAKE_PROJECT_NAME@ -c PythonShell $*
2 changes: 0 additions & 2 deletions distrib/macos/runpython.in
Expand Up @@ -2,6 +2,4 @@

appDir="$(cd "$(dirname "$0")"; pwd)"

${appDir}/setpythonpath

${appDir}/@CMAKE_PROJECT_NAME@.app/Contents/MacOS/@CMAKE_PROJECT_NAME@ -c PythonShell $*
2 changes: 0 additions & 2 deletions distrib/windows/runpython.bat.in
Expand Up @@ -2,6 +2,4 @@

SET appDir=%~dp0

CALL "%appDir%setpythonpath.bat"

"%appDir%bin\${CMAKE_PROJECT_NAME}" -c PythonShell %*
54 changes: 44 additions & 10 deletions src/plugins/support/PythonQtSupport/src/pythonqtsupportplugin.cpp
Expand Up @@ -22,6 +22,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
//==============================================================================

#include "pythoninterface.h"
#include "pythonqtsupport.h"
#include "pythonqtsupportplugin.h"
#include "solverinterface.h"

Expand Down Expand Up @@ -98,16 +99,6 @@ void PythonQtSupportPlugin::initializePlugin()
signal(SIGINT, SIG_DFL);
#endif

// Set sys.argv
//---ISSUE1255--- DO WE REALLY NEED THIS?...

mArgV = reinterpret_cast<wchar_t **>(PyMem_RawMalloc(2*sizeof(wchar_t*)));

mArgV[0] = const_cast<wchar_t *>(L"");
mArgV[1] = nullptr;

PySys_SetArgvEx(1, mArgV, 0);

// Initialise Qt binding for Python

PythonQt_QtAll::init();
Expand All @@ -121,6 +112,49 @@ void PythonQtSupportPlugin::initializePlugin()
// file name...

mModule.addVariable("__file__", "OpenCOR");

// We already have set PYTHONHOME, so get it

QString pythonHome = qEnvironmentVariable("PYTHONHOME");

// The script to update Python scripts

QString setPythonPathScript = pythonHome
#if defined(Q_OS_WIN)
+ "/Scripts"
#else
+ "/bin"
#endif
+ "/set_python_path.py";

// Create a buffer in which to pass arguments to Python

mArgV = reinterpret_cast<wchar_t **>(PyMem_RawMalloc(4*sizeof(wchar_t*)));

// Set arguments for `set_python_path`
// Note: we need to use an intermediate variable as otherwise the cast
// results in an empty string

auto wSetPythonPathScript = setPythonPathScript.toStdWString();
mArgV[0] = const_cast<wchar_t *>(wSetPythonPathScript.c_str());

auto wPythonHome = pythonHome.toStdWString();
mArgV[1] = const_cast<wchar_t *>(wPythonHome.c_str());

mArgV[2] = const_cast<wchar_t *>(L"-s");
mArgV[3] = nullptr;

PySys_SetArgvEx(3, mArgV, 0);

// Actually update the path to Python in scripts

PythonQtSupport::evaluateFile(setPythonPathScript);

// Clear the argument buffer so that `sys.argv` is empty in the GUI console

mArgV[0] = const_cast<wchar_t *>(L"");
mArgV[1] = nullptr;
PySys_SetArgvEx(1, mArgV, 0);
}

//==============================================================================
Expand Down

0 comments on commit d3b8020

Please sign in to comment.