/
pythonshellplugin.cpp
178 lines (134 loc) · 5.87 KB
/
pythonshellplugin.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*******************************************************************************
Copyright (C) The University of Auckland
OpenCOR is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OpenCOR is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*******************************************************************************/
//==============================================================================
// Python shell plugin
//==============================================================================
#include "pythonqtsupportplugin.h"
#include "pythonshellplugin.h"
//==============================================================================
#include <QCoreApplication>
#include <QFile>
#include <QSettings>
#include <QTimer>
//==============================================================================
#include "pythonbegin.h"
#include "pythonqtsupport.h"
#include "pythonend.h"
//==============================================================================
namespace OpenCOR {
namespace PythonShell {
//==============================================================================
PLUGININFO_FUNC PythonShellPluginInfo()
{
Descriptions descriptions;
descriptions.insert("en", QString::fromUtf8("the Python shell plugin."));
descriptions.insert("fr", QString::fromUtf8("the Python shell plugin."));
return new PluginInfo(PluginInfo::Category::Miscellaneous, true, true,
QStringList() << "Core" << "DataStore"
<< "SimulationSupport"
<< "PythonQtSupport",
descriptions);
}
//==============================================================================
// CLI interface
//==============================================================================
bool PythonShellPlugin::executeCommand(const QString &pCommand,
const QStringList &pArguments,
int &pRes)
{
// Run the given CLI command
if (!pCommand.compare("help")) {
// Display the commands that we support
runHelpCommand();
return true;
} else if (pCommand.isEmpty() || !pCommand.compare("python")) {
// Run a Python shell
return runPython(pArguments, pRes);
} else {
// Not a CLI command that we support
runHelpCommand();
return false;
}
}
//==============================================================================
// Plugin specific
//==============================================================================
void PythonShellPlugin::runHelpCommand()
{
// Output the commands we support
std::cout << "Commands supported by the Python shell plugin:" << std::endl;
std::cout << " * Display the commands supported by the plugin:" << std::endl;
std::cout << " help" << std::endl;
std::cout << " * Run an interactive Python shell in OpenCOR's environment:" << std::endl;
std::cout << " python [option] ... [-c cmd | -m mod | file | -] [arg] ..." << std::endl;
}
//==============================================================================
bool PythonShellPlugin::runPython(const QStringList &pArguments, int &pRes)
{
// The following has been adapted from `Programs/python.c` in the Python sources.
const int argc = pArguments.size() + 1;
wchar_t **argv = reinterpret_cast<wchar_t **>(PyMem_RawMalloc(sizeof(wchar_t*) * (argc+1)));
/* We need a second copy, as Python might modify the first one. */
wchar_t **argv_copy = reinterpret_cast<wchar_t **>(PyMem_RawMalloc(sizeof(wchar_t*) * (argc+1)));
if (!argv || !argv_copy) {
fprintf(stderr, "out of memory\n");
pRes = 1;
return false;
}
/* PEP 754 requires that FP exceptions run in "no stop" mode by default,
* and until C vendors implement C99's ways to control FP exceptions,
* Python requires non-stop mode. Alas, some platforms enable FP
* exceptions by default. Here we disable them.
*/
#ifdef __FreeBSD__
fedisableexcept(FE_OVERFLOW);
#endif
char *oldloc = _PyMem_RawStrdup(setlocale(LC_ALL, nullptr));
if (!oldloc) {
fprintf(stderr, "out of memory\n");
pRes = 1;
return false;
}
setlocale(LC_ALL, "");
argv_copy[0] = argv[0] = Py_DecodeLocale("python", nullptr);
for (int i = 1; i < argc; i++) {
argv[i] = Py_DecodeLocale(pArguments[i-1].toUtf8().constData(), nullptr);
if (!argv[i]) {
PyMem_RawFree(oldloc);
fprintf(stderr, "Fatal Python error: "
"unable to decode the command line argument #%i\n",
i + 1);
pRes = 1;
return false;
}
argv_copy[i] = argv[i];
}
argv_copy[argc] = argv[argc] = nullptr;
setlocale(LC_ALL, oldloc);
PyMem_RawFree(oldloc);
// N.B. PyMain() calls Py_Initialize() and Py_Finalize()
pRes = Py_Main(argc, argv);
for (int i = 0; i < argc; i++) {
PyMem_RawFree(argv_copy[i]);
}
PyMem_RawFree(argv);
PyMem_RawFree(argv_copy);
return true;
}
//==============================================================================
} // namespace PythonShell
} // namespace OpenCOR
//==============================================================================
// End of file
//==============================================================================