This repository has been archived by the owner on Jul 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PythonEnvironment.cpp
615 lines (496 loc) · 19.5 KB
/
PythonEnvironment.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
/******************************************************************************
* SOFA, Simulation Open-Framework Architecture *
* (c) 2006 INRIA, USTL, UJF, CNRS, MGH *
* *
* This program is free software; you can redistribute it and/or modify it *
* under the terms of the GNU Lesser General Public License as published by *
* the Free Software Foundation; either version 2.1 of the License, or (at *
* your option) any later version. *
* *
* This program 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 Lesser General Public License *
* for more details. *
* *
* You should have received a copy of the GNU Lesser General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
*******************************************************************************
* Authors: The SOFA Team and external contributors (see Authors.txt) *
* *
* Contact information: contact@sofa-framework.org *
******************************************************************************/
#include <fstream>
#include "PythonMacros.h"
#include "PythonEnvironment.h"
#include "PythonScriptController.h"
#include <sofa/helper/system/FileRepository.h>
#include <sofa/helper/system/FileSystem.h>
#include <sofa/helper/system/SetDirectory.h>
#include <sofa/simulation/Node.h>
#include <sofa/helper/Utils.h>
#include <sofa/helper/StringUtils.h>
using sofa::helper::getAStringCopy ;
#include <sofa/helper/system/PluginManager.h>
using sofa::helper::system::PluginManager;
using sofa::helper::system::Plugin;
#if defined(__linux__)
# include <dlfcn.h> // for dlopen(), see workaround in Init()
#endif
using sofa::helper::system::FileSystem;
using sofa::helper::Utils;
namespace sofa
{
namespace simulation
{
////////////////////////////////////////////////////////////////////////////////////////////////////
/// \brief The PythonEnvironmentData class which hold "static" data as long as python is running
///
/// The class currently hold the argv that are exposed in the python 'sys.argv'.
/// The elements added are copied and the object hold the pointer to the memory allocated.
/// The memory is release when the object is destructed or the reset function called.
///
/// Other elements than sys.argv may be added depending on future needs.
///
////////////////////////////////////////////////////////////////////////////////////////////////////
class PythonEnvironmentData
{
public:
~PythonEnvironmentData() { reset(); }
int size() { return m_argv.size(); }
void add(const std::string& data)
{
m_argv.push_back( getAStringCopy(data.c_str()) );
}
void reset()
{
for(auto s : m_argv)
delete[] s;
m_argv.clear();
}
char* getDataAt(unsigned int index)
{
return m_argv[index];
}
char** getDataBuffer()
{
return &m_argv[0];
}
private:
std::vector<char*> m_argv;
};
PythonEnvironmentData* PythonEnvironment::getStaticData()
{
static PythonEnvironmentData* m_staticdata { nullptr } ;
if( !m_staticdata )
m_staticdata = new PythonEnvironmentData();
return m_staticdata;
}
std::string PythonEnvironment::pluginLibraryPath = "";
PyMODINIT_FUNC initModulesHelper(const std::string& name, PyMethodDef* methodDef)
{
PythonEnvironment::gil lock(__func__);
Py_InitModule(name.c_str(), methodDef);
}
void PythonEnvironment::addModule(const std::string& name, PyMethodDef* methodDef)
{
initModulesHelper(name, methodDef);
}
void PythonEnvironment::Init()
{
std::string pythonVersion = Py_GetVersion();
#ifndef NDEBUG
SP_MESSAGE_INFO("Python version: " + pythonVersion)
#endif
#if defined(__linux__)
// WARNING: workaround to be able to import python libraries on linux (like
// numpy), at least on Ubuntu (see http://bugs.python.org/issue4434). It is
// not fixing the real problem, but at least it is working for now.
std::string pythonLibraryName = "libpython" + std::string(pythonVersion,0,3) + ".so";
dlopen( pythonLibraryName.c_str(), RTLD_LAZY|RTLD_GLOBAL );
#endif
// Prevent the python terminal from being buffered, not to miss or mix up traces.
if( putenv( (char*)"PYTHONUNBUFFERED=1" ) )
SP_MESSAGE_WARNING("failed to set environment variable PYTHONUNBUFFERED");
if ( !Py_IsInitialized() )
{
Py_Initialize();
}
PyEval_InitThreads();
// the first gil lock is here
gil lock(__func__);
// Append sofa modules to the embedded python environment.
bindSofaPythonModule();
// Required for sys.path, used in addPythonModulePath().
PyRun_SimpleString("import sys");
// Force C locale.
PyRun_SimpleString("import locale");
PyRun_SimpleString("locale.setlocale(locale.LC_ALL, 'C')");
// Workaround: try to import numpy and to launch numpy.finfo to cache data;
// this prevents a deadlock when calling numpy.finfo from a worker thread.
// ocarre: may crash on some configurations, we have to find a fix
PyRun_SimpleString("try:\n\timport numpy;numpy.finfo(float)\nexcept:\n\tpass");
// Workaround: try to import scipy from the main thread this prevents a deadlock when importing scipy from a worker thread when we use the SofaScene asynchronous loading
PyRun_SimpleString("try:\n\tfrom scipy import misc, optimize\nexcept:\n\tpass\n");
// If the script directory is not available (e.g. if the interpreter is invoked interactively
// or if the script is read from standard input), path[0] is the empty string,
// which directs Python to search modules in the current directory first.
PyRun_SimpleString(std::string("sys.path.insert(0,\"\")").c_str());
// Add the paths to the plugins' python modules to sys.path. Those paths
// are read from all the files in 'etc/sofa/python.d'
std::vector< std::string > paths = sofa::helper::system::DataRepository.getPaths();
paths.push_back(Utils::getSofaPathPrefix());
for (auto path : paths)
{
std::string confDir = path + "/etc/sofa/python.d";
if (FileSystem::exists(confDir))
{
std::vector<std::string> files;
FileSystem::listDirectory(confDir, files);
for (size_t i = 0; i < files.size(); i++)
{
addPythonModulePathsFromConfigFile(confDir + "/" + files[i]);
}
}
}
// Add the directories listed in the SOFAPYTHON_PLUGINS_PATH environnement
// variable (colon-separated) to sys.path
char * pathVar = getenv("SOFAPYTHON_PLUGINS_PATH");
if (pathVar != nullptr)
{
std::istringstream ss(pathVar);
std::string path;
while(std::getline(ss, path, ':'))
{
if (FileSystem::exists(path))
addPythonModulePathsForPlugins(path);
else
SP_MESSAGE_WARNING("no such directory: '" + path + "'");
}
}
// Add the directories listed in SofaPython.so/../../python
addPythonModulePathsForPluginsByName("SofaPython");
// python livecoding related
PyRun_SimpleString("from SofaPython.livecoding import onReimpAFile");
// general sofa-python stuff
PyRun_SimpleString("import SofaPython");
// python modules are automatically reloaded at each scene loading
setAutomaticModuleReload( true );
// Initialize pluginLibraryPath by reading PluginManager's map
std::map<std::string, Plugin>& map = PluginManager::getInstance().getPluginMap();
for( const auto& elem : map)
{
Plugin p = elem.second;
if ( p.getModuleName() == sofa_tostring(SOFA_TARGET) )
{
pluginLibraryPath = elem.first;
}
}
}
void PythonEnvironment::Release()
{
removePluginManagerCallback();
// Finish the Python Interpreter
// obviously can't use raii here
if( Py_IsInitialized() ) {
PyGILState_Ensure();
Py_Finalize();
}
}
void PythonEnvironment::addPythonModulePath(const std::string& path)
{
static std::set<std::string> addedPath;
if (addedPath.find(path)==addedPath.end()) {
// note not to insert at first 0 place
// an empty string must be at first so modules can be found in the current directory first.
{
gil lock(__func__);
PyRun_SimpleString(std::string("sys.path.insert(1,\""+path+"\")").c_str());
}
SP_MESSAGE_INFO("Added '" + path + "' to sys.path");
addedPath.insert(path);
}
}
void PythonEnvironment::addPythonModulePathsFromConfigFile(const std::string& path)
{
std::ifstream configFile(path.c_str());
std::string line;
while(std::getline(configFile, line))
{
if (!FileSystem::isAbsolute(line))
{
line = Utils::getSofaPathPrefix() + "/" + line;
}
addPythonModulePath(line);
}
}
void PythonEnvironment::addPythonModulePathsForPlugins(const std::string& pluginsDirectory)
{
bool added = false;
std::vector<std::string> pythonDirs = {
pluginsDirectory + "/python",
pluginsDirectory + "/python2.7",
};
std::vector<std::string> files;
FileSystem::listDirectory(pluginsDirectory, files);
for (std::vector<std::string>::iterator i = files.begin(); i != files.end(); ++i)
{
const std::string pluginSubdir = pluginsDirectory + "/" + *i;
if (FileSystem::exists(pluginSubdir) && FileSystem::isDirectory(pluginSubdir))
{
pythonDirs.push_back(pluginSubdir + "/python");
pythonDirs.push_back(pluginSubdir + "/python2.7");
}
}
for(std::string pythonDir : pythonDirs)
{
// Search for a subdir "site-packages"
if (FileSystem::exists(pythonDir+"/site-packages") && FileSystem::isDirectory(pythonDir+"/site-packages"))
{
addPythonModulePath(pythonDir+"/site-packages");
added = true;
}
// Or fallback to "python"
else if (FileSystem::exists(pythonDir) && FileSystem::isDirectory(pythonDir))
{
addPythonModulePath(pythonDir);
added = true;
}
}
if(!added)
{
SP_MESSAGE_INFO("No python dir found in " + pluginsDirectory);
}
}
void PythonEnvironment::addPythonModulePathsForPluginsByName(const std::string& pluginName)
{
std::map<std::string, Plugin>& map = PluginManager::getInstance().getPluginMap();
for( const auto& elem : map)
{
Plugin p = elem.second;
if ( p.getModuleName() == pluginName )
{
std::string pluginLibraryPath = elem.first;
// moduleRoot should be 2 levels above the library (plugin_name/lib/plugin_name.so)
std::string moduleRoot = FileSystem::getParentDirectory(FileSystem::getParentDirectory(pluginLibraryPath));
addPythonModulePathsForPlugins(moduleRoot);
return;
}
}
SP_MESSAGE_INFO(pluginName + " not found in PluginManager's map.");
}
void PythonEnvironment::addPluginManagerCallback()
{
PluginManager::getInstance().addOnPluginLoadedCallback(pluginLibraryPath,
[](const std::string& pluginLibraryPath, const Plugin& plugin) {
// WARNING: loaded plugin must be organized like plugin_name/lib/plugin_name.so
for ( auto path : sofa::helper::system::PluginRepository.getPaths() )
{
std::string pluginRoot = FileSystem::cleanPath( path + "/" + plugin.getModuleName() );
if ( FileSystem::exists(pluginRoot) && FileSystem::isDirectory(pluginRoot) )
{
addPythonModulePathsForPlugins(pluginRoot);
return;
}
}
}
);
}
void PythonEnvironment::removePluginManagerCallback()
{
PluginManager::getInstance().removeOnPluginLoadedCallback(pluginLibraryPath);
}
// some basic RAII stuff to handle init/termination cleanly
namespace {
struct raii {
raii() {
// initialization is done when loading the plugin
// otherwise it can be executed too soon
// when an application is directly linking with the SofaPython library
}
~raii() {
PythonEnvironment::Release();
}
};
static raii singleton;
}
// basic script functions
std::string PythonEnvironment::getError()
{
gil lock(__func__);
std::string error;
PyObject *ptype, *pvalue /* error msg */, *ptraceback /*stack snapshot and many other informations (see python traceback structure)*/;
PyErr_Fetch(&ptype, &pvalue, &ptraceback);
if(pvalue)
error = PyString_AsString(pvalue);
return error;
}
bool PythonEnvironment::runString(const std::string& script)
{
gil lock(__func__);
PyObject* pDict = PyModule_GetDict(PyImport_AddModule("__main__"));
PyObject* result = PyRun_String(script.data(), Py_file_input, pDict, pDict);
if(nullptr == result)
{
SP_MESSAGE_ERROR("Script (string) import error")
PyErr_Print();
return false;
}
Py_DECREF(result);
return true;
}
std::string PythonEnvironment::getStackAsString()
{
gil lock(__func__);
PyObject* pDict = PyModule_GetDict(PyImport_AddModule("SofaPython"));
PyObject* pFunc = PyDict_GetItemString(pDict, "getStackForSofa");
if (PyCallable_Check(pFunc))
{
PyObject* res = PyObject_CallFunction(pFunc, nullptr);
std::string tmp=PyString_AsString(PyObject_Str(res));
Py_DECREF(res) ;
return tmp;
}
return "Python Stack is empty.";
}
std::string PythonEnvironment::getPythonCallingPointString()
{
PyObject* pDict = PyModule_GetDict(PyImport_AddModule("SofaPython"));
PyObject* pFunc = PyDict_GetItemString(pDict, "getPythonCallingPointAsString");
if (PyCallable_Check(pFunc))
{
PyObject* res = PyObject_CallFunction(pFunc, nullptr);
std::string tmp=PyString_AsString(PyObject_Str(res));
Py_DECREF(res) ;
return tmp;
}
return "Python Stack is empty.";
}
helper::logging::FileInfo::SPtr PythonEnvironment::getPythonCallingPointAsFileInfo()
{
PyObject* pDict = PyModule_GetDict(PyImport_AddModule("SofaPython"));
PyObject* pFunc = PyDict_GetItemString(pDict, "getPythonCallingPoint");
if (pFunc && PyCallable_Check(pFunc))
{
PyObject* res = PyObject_CallFunction(pFunc, nullptr);
if(res && PySequence_Check(res) ){
PyObject* filename = PySequence_GetItem(res, 0) ;
PyObject* number = PySequence_GetItem(res, 1) ;
std::string tmp=PyString_AsString(filename);
auto lineno = PyInt_AsLong(number);
Py_DECREF(res) ;
return SOFA_FILE_INFO_COPIED_FROM(tmp, lineno);
}
}
return SOFA_FILE_INFO_COPIED_FROM("undefined", -1);
}
void PythonEnvironment::setArguments(const std::string& filename, const std::vector<std::string>& arguments)
{
const std::string basename = sofa::helper::system::SetDirectory::GetFileNameWithoutExtension(filename.c_str());
PythonEnvironmentData* data = getStaticData() ;
data->reset() ;
data->add( basename );
if(!arguments.empty()) {
for(const std::string& arg : arguments) {
data->add(arg);
}
}
PySys_SetArgvEx( data->size(), data->getDataBuffer(), 0);
}
bool PythonEnvironment::runFile(const std::string& filename, const std::vector<std::string>& arguments) {
const gil lock(__func__);
const std::string dir = sofa::helper::system::SetDirectory::GetParentDir(filename.c_str());
const std::string basename = sofa::helper::system::SetDirectory::GetFileNameWithoutExtension(filename.c_str());
// Load the scene script
PyObject* script = PyFile_FromString((char*)filename.c_str(), (char*)("r"));
if(!arguments.empty())
setArguments(filename, arguments);
if( !script ) {
SP_MESSAGE_ERROR("cannot open file:" << filename)
PyErr_Print();
return false;
}
PyObject* __main__ = PyModule_GetDict(PyImport_AddModule("__main__"));
// save/restore __main__.__file__
PyObject* __file__ = PyDict_GetItemString(__main__, "__file__");
Py_XINCREF(__file__);
// temporarily set __main__.__file__ = filename during file loading
{
PyObject* __tmpfile__ = PyString_FromString(filename.c_str());
PyDict_SetItemString(__main__, "__file__", __tmpfile__);
Py_XDECREF(__tmpfile__);
}
const int error = PyRun_SimpleFileEx(PyFile_AsFile(script), filename.c_str(), 0);
// don't wait for gc to close the file
PyObject_CallMethod(script, (char*) "close", nullptr);
Py_XDECREF(script);
// restore backup if needed
if(__file__) {
PyDict_SetItemString(__main__, "__file__", __file__);
} else {
const int err = PyDict_DelItemString(__main__, "__file__");
assert(!err); (void) err;
}
Py_XDECREF(__file__);
if(error) {
SP_MESSAGE_ERROR("Script (file:" << basename << ") import error")
PyErr_Print();
return false;
}
return true;
}
void PythonEnvironment::SceneLoaderListerner::rightBeforeLoadingScene()
{
gil lock(__func__);
// unload python modules to force importing their eventual modifications
PyRun_SimpleString("SofaPython.unloadModules()");
}
void PythonEnvironment::setAutomaticModuleReload( bool b )
{
if( b )
SceneLoader::addListener( SceneLoaderListerner::getInstance() );
else
SceneLoader::removeListener( SceneLoaderListerner::getInstance() );
}
void PythonEnvironment::excludeModuleFromReload( const std::string& moduleName )
{
gil lock(__func__);
PyRun_SimpleString( std::string( "try: SofaPython.__SofaPythonEnvironment_modulesExcludedFromReload.append('" + moduleName + "')\nexcept:pass" ).c_str() );
}
static const bool debug_gil = false;
static PyGILState_STATE lock(const char* trace) {
if(debug_gil && trace) {
std::clog << ">> " << trace << " wants the gil" << std::endl;
}
// this ensures that we start with no active thread before first locking the
// gil: this way the last gil unlock lets python threads to run (otherwise
// the main thread still holds the gil, preventing python threads to run
// until the main thread exits).
// the first gil aquisition should happen right after the python interpreter
// is initialized.
static const PyThreadState* init = PyEval_SaveThread(); (void) init;
return PyGILState_Ensure();
}
PythonEnvironment::gil::gil(const char* trace)
: state(lock(trace)),
trace(trace) { }
PythonEnvironment::gil::~gil() {
PyGILState_Release(state);
if(debug_gil && trace) {
std::clog << "<< " << trace << " released the gil" << std::endl;
}
}
PythonEnvironment::no_gil::no_gil(const char* trace)
: state(PyEval_SaveThread()),
trace(trace) {
if(debug_gil && trace) {
std::clog << ">> " << trace << " temporarily released the gil" << std::endl;
}
}
PythonEnvironment::no_gil::~no_gil() {
if(debug_gil && trace) {
std::clog << "<< " << trace << " wants to reacquire the gil" << std::endl;
}
PyEval_RestoreThread(state);
}
} // namespace simulation
} // namespace sofa