forked from sofa-framework/SofaPython3
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPythonEnvironment.cpp
More file actions
690 lines (571 loc) · 23.7 KB
/
PythonEnvironment.cpp
File metadata and controls
690 lines (571 loc) · 23.7 KB
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
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
/******************************************************************************
* SofaPython3 plugin *
* (c) 2021 CNRS, University of Lille, INRIA *
* *
* 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/>. *
*******************************************************************************
* Contact information: contact@sofa-framework.org *
******************************************************************************/
#include <fstream>
#if defined(__linux__)
#include <dlfcn.h>
#endif
#include <sofa/helper/system/PluginManager.h>
using sofa::helper::system::PluginManager;
using sofa::helper::system::Plugin;
#include <sofa/helper/system/FileRepository.h>
#include <sofa/helper/system/SetDirectory.h>
#include <sofa/helper/system/FileSystem.h>
using sofa::helper::system::FileSystem;
#include <sofa/helper/Utils.h>
using sofa::helper::Utils;
#include <sofa/helper/StringUtils.h>
using sofa::helper::getAStringCopy ;
#include <SofaPython3/PythonEnvironment.h>
#include <sofa/helper/logging/Messaging.h>
#include <sofa/simulation/SceneLoaderFactory.h>
using sofa::simulation::SceneLoaderFactory;
#include <pybind11/embed.h>
#include <pybind11/eval.h>
/// Makes an alias for the pybind11 namespace to increase readability.
namespace py { using namespace pybind11; }
#include "SceneLoaderPY3.h"
using sofapython3::SceneLoaderPY3;
MSG_REGISTER_CLASS(sofapython3::PythonEnvironment, "SofaPython3::PythonEnvironment")
namespace sofapython3
{
class PythonEnvironmentModule
{
public:
py::module m_sofaModule ;
py::module m_sofaRuntimeModule ;
};
////////////////////////////////////////////////////////////////////////////////////////////////////
/// \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( Py_DecodeLocale(data.c_str(), nullptr) );
}
void reset()
{
for(auto s : m_argv){
PyMem_RawFree(s);
}
m_argv.clear();
addedPath.clear();
}
wchar_t* getDataAt(unsigned int index)
{
return m_argv[index];
}
wchar_t** getDataBuffer()
{
return &m_argv[0];
}
std::set<std::string> addedPath;
private:
std::vector<wchar_t*> m_argv;
};
PythonEnvironmentData* PythonEnvironment::getStaticData()
{
static PythonEnvironmentData* m_staticdata { nullptr } ;
if( !m_staticdata )
{
// TODO: replace new with reference counted pointer (make_unique or make_shared)
m_staticdata = new PythonEnvironmentData();
}
return m_staticdata;
}
PythonEnvironmentModule* PythonEnvironment::getStaticModule()
{
PythonEnvironment::gil lock;
static PythonEnvironmentModule* m_staticmodule { nullptr } ;
if( !m_staticmodule )
{
// TODO: replace new with reference counted pointer (make_unique or make_shared)
m_staticmodule = new PythonEnvironmentModule();
executePython([]{
getStaticModule()->m_sofaModule = py::module::import("Sofa");
getStaticModule()->m_sofaRuntimeModule = py::module::import("SofaRuntime");
});
}
return m_staticmodule;
}
std::string PythonEnvironment::pluginLibraryPath = "";
SOFAPYTHON3_API py::module PythonEnvironment::importFromFile(const std::string& module, const std::string& path, py::object* globals)
{
PythonEnvironment::gil lock;
py::dict locals;
locals["module_name"] = py::cast(module); // have to cast the std::string first
locals["path"] = py::cast(path);
py::object globs = py::globals();
if (globals == nullptr)
globals = &globs;
py::eval<py::eval_statements>( // tell eval we're passing multiple statements
"import importlib.util \n"
"importlib.machinery.SOURCE_SUFFIXES.append('pyscn') \n"
"spec = importlib.util.spec_from_file_location(module_name, path) \n"
"new_module = importlib.util.module_from_spec(spec) \n"
"spec.loader.exec_module(new_module)",
*globals,
locals);
py::module m = py::cast<py::module>(locals["new_module"]);
return m;
}
void PythonEnvironment::Init()
{
std::string pythonVersion = Py_GetVersion();
msg_info("SofaPython3") << "Initializing with python version " << pythonVersion;
if( !SceneLoaderFactory::getInstance()->getEntryFileExtension("py3") )
{
msg_info("SofaPython3") << "Registering a scene loader for [.py, .py3, .pyscn, .py3scn] files." ;
// TODO: replace new with reference counted pointer (make_unique or make_shared)
SceneLoaderFactory::getInstance()->addEntry(new SceneLoaderPY3());
}
/// Prevent the python terminal from being buffered, not to miss or mix up traces.
if( putenv( (char*)"PYTHONUNBUFFERED=1" ) )
msg_warning("SofaPython3") << "failed to set environment variable PYTHONUNBUFFERED";
if ( !Py_IsInitialized() )
{
msg_info("SofaPython3") << "Initializing python";
py::initialize_interpreter();
// the first gil aquisition should happen right after the python interpreter
// is initialized.
static const PyThreadState* init = PyEval_SaveThread(); (void) init;
}
// Required for sys.path, used in addPythonModulePath().
executePython([]{ PyRun_SimpleString("import sys");});
// Force C locale.
executePython([]{ PyRun_SimpleString("import locale");});
executePython([]{ 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.
executePython([]{ 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
executePython([]{ PyRun_SimpleString("try:\n\tfrom scipy import 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.
executePython([]{ 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::string confDir = Utils::getSofaPathPrefix() + "/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 SOFAPYTHON3_PLUGINS_PATH environnement
/// variable to sys.path
const std::string envVarName = "SOFAPYTHON3_PLUGINS_PATH";
const std::string deprecatedEnvVarName = "SOFAPYTHON_PLUGINS_PATH";
std::string usedEnvVarName = envVarName;
char* deprecatedPathVar = getenv(deprecatedEnvVarName.c_str());
char* pathVar = getenv(envVarName.c_str());
// case where only the deprecated env var is set
if (pathVar == nullptr && deprecatedPathVar != nullptr)
{
msg_deprecated("SofaPython3") << deprecatedEnvVarName << " environment variable is deprecated, use " << envVarName << " instead.";
usedEnvVarName = "SOFAPYTHON_PLUGINS_PATH";
}
// case where both env vars are set
else if (pathVar != nullptr && deprecatedPathVar != nullptr)
{
msg_deprecated("SofaPython3") << deprecatedEnvVarName << " and " << envVarName << " environment variables are both set.";
msg_deprecated("SofaPython3") << deprecatedEnvVarName << " is deprecated, and only " << envVarName << " will be used.";
}
sofa::helper::system::FileRepository pluginPathsRepository(envVarName.c_str());
const auto& pluginPaths = pluginPathsRepository.getPaths();
for (auto pluginPath : pluginPaths)
{
std::string cleanPath = FileSystem::cleanPath(pluginPath);
addPythonModulePath(cleanPath);
}
// Add sites-packages wrt the plugin
addPythonModulePathsFromPlugin("SofaPython3");
// Lastly, we (try to) add modules from the root of SOFA
addPythonModulePathsFromDirectory( Utils::getSofaPathPrefix() );
executePython([]{ PyRun_SimpleString("import SofaRuntime");});
// python livecoding related
executePython([]{ PyRun_SimpleString("from Sofa.livecoding import onReimpAFile");});
// general sofa-python stuff
// 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 ( strcmp(p.getModuleName(), sofa_tostring(SOFA_TARGET)) == 0 )
{
pluginLibraryPath = elem.first;
}
}
s_isInitialized = true;
}
// Single implementation for the three different versions
template<class T>
void executePython_(const T& emitter, std::function<void()> cb)
{
sofapython3::PythonEnvironment::gil acquire;
try{
cb();
}catch(py::error_already_set& e)
{
std::stringstream tmp;
tmp << "Unable to execute code." << msgendl
<< "Python exception:" << msgendl
<< " " << e.what()
<< PythonEnvironment::getPythonCallingPointString();
msg_error(emitter) << tmp.str();
}
}
void PythonEnvironment::executePython(const std::string& emitter, std::function<void()> cb)
{
return executePython_(emitter, cb);
}
void PythonEnvironment::executePython(std::function<void()> cb)
{
return executePython_("SofaPython3::executePython", cb);
}
void PythonEnvironment::executePython(const sofa::core::objectmodel::Base* b, std::function<void()> cb)
{
return executePython_(b, cb);
}
void PythonEnvironment::Release()
{
/// Finish the Python Interpreter
/// obviously can't use raii here
static bool isReleased = false;
if( Py_IsInitialized() && !isReleased) {
isReleased = true;
PyGILState_Ensure();
py::finalize_interpreter();
getStaticData()->reset();
}
}
void PythonEnvironment::addPythonModulePath(const std::string& path)
{
if (!(FileSystem::exists(path) && FileSystem::isDirectory(path)))
{
msg_warning("SofaPython3") << "Could not add '" + path + "'" << " to sys.path (it does not exist or is not a directory)";
return;
}
PythonEnvironmentData* data = getStaticData() ;
if ( data->addedPath.find(path)==data->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.
executePython([&]{ PyRun_SimpleString(std::string("sys.path.insert(1,\""+path+"\")").c_str());});
msg_info("SofaPython3")<< "Added '" + path + "' to sys.path";
data->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::addPythonModulePathsFromDirectory(const std::string& directory)
{
if ( ! (FileSystem::exists(directory) && FileSystem::isDirectory(directory)) )
{
return;
}
// Using python<MAJOR>.<MINOR> directory suffix for modules paths is now the recommanded
// standard location: https://docs.python.org/3.11/install/#how-installation-works and
// https://docs.python.org/3.11/library/site.html#module-site
const auto pythonVersionFull = std::string{Py_GetVersion()};
const auto pythonVersion = pythonVersionFull.substr(0, pythonVersionFull.find(" ")); // contains major.minor.patch
const auto pythonVersionMajorMinor = pythonVersion.substr(0, pythonVersion.rfind(".")); // contains only manjor.minor
const auto pythonMajorMinorSuffix = "/python" + pythonVersionMajorMinor;
std::vector<std::string> searchDirs = {
directory,
directory + "/lib",
directory + pythonMajorMinorSuffix,
directory + "/python3" // deprecated
};
// Iterate in the pluginsDirectory and add each sub directory with a 'python' name
// this is mostly for in-tree builds.
std::vector<std::string> files;
FileSystem::listDirectory(directory, files);
for (std::vector<std::string>::iterator i = files.begin(); i != files.end(); ++i)
{
const std::string subdir = directory + "/" + *i;
if (FileSystem::exists(subdir) && FileSystem::isDirectory(subdir))
{
const std::vector<std::string> suffixes = {
pythonMajorMinorSuffix,
"/python3" // deprecated
};
for (const auto& suffix : suffixes)
{
const auto pythonSubdir = subdir + suffix;
if (FileSystem::exists(pythonSubdir) && FileSystem::isDirectory(pythonSubdir))
{
searchDirs.push_back(pythonSubdir);
}
}
}
}
// For each of the directories in pythonDirs, search for a site-packages entry
for(std::string& searchDir : searchDirs)
{
// Search for a subdir "site-packages"
if (FileSystem::exists(searchDir + "/site-packages") && FileSystem::isDirectory(searchDir + "/site-packages"))
{
addPythonModulePath(searchDir + "/site-packages");
}
}
}
void PythonEnvironment::addPythonModulePathsFromPlugin(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 )
{
// 1. Try to find the plugin directory starting from SOFA root
for ( auto path : sofa::helper::system::PluginRepository.getPaths() )
{
std::string pluginRoot = FileSystem::cleanPath( path + "/" + pluginName );
if ( FileSystem::exists(pluginRoot) && FileSystem::isDirectory(pluginRoot) )
{
addPythonModulePathsFromDirectory(pluginRoot);
return;
}
}
// 2. Try to find the plugin directory starting from the plugin library
std::string pluginLibraryPath = elem.first;
// moduleRoot can be 1 or 2 levels above the library directory
// like "plugin_name/lib/plugin_name.so"
// or "sofa_root/bin/Release/plugin_name.dll"
std::string moduleRoot = FileSystem::getParentDirectory(pluginLibraryPath);
int maxDepth = 0;
while(!FileSystem::exists(moduleRoot + "/lib") && maxDepth < 2)
{
moduleRoot = FileSystem::getParentDirectory(moduleRoot);
maxDepth++;
}
addPythonModulePathsFromDirectory(moduleRoot);
return;
}
}
msg_info("SofaPython3") << pluginName << " not found in PluginManager's map.";
}
void PythonEnvironment::addPluginManagerCallback()
{
PluginManager::getInstance().addOnPluginLoadedCallback(pluginLibraryPath,
[](const std::string& pluginLibraryPath, const Plugin& plugin)
{
SOFA_UNUSED(pluginLibraryPath);
// search for plugin with PluginRepository
for ( auto path : sofa::helper::system::PluginRepository.getPaths() )
{
std::string pluginRoot = FileSystem::cleanPath( path + "/" + plugin.getModuleName() );
if ( FileSystem::exists(pluginRoot) && FileSystem::isDirectory(pluginRoot) )
{
addPythonModulePathsFromDirectory(pluginRoot);
return;
}
}
}
);
PluginManager::getInstance().addOnPluginCleanupCallbacks(pluginLibraryPath,
[]()
{
PythonEnvironment::Release();
}
);
}
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;
} // namespace
/// basic script functions
std::string PythonEnvironment::getError()
{
gil lock;
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 = PyBytes_AsString(pvalue);
return error;
}
bool PythonEnvironment::runString(const std::string& script)
{
gil lock;
PyObject* pDict = PyModule_GetDict(PyImport_AddModule("__main__"));
PyObject* result = PyRun_String(script.data(), Py_file_input, pDict, pDict);
if(0 == result)
{
msg_error("SofaPython3") << "Script (string) import error";
PyErr_Print();
return false;
}
Py_DECREF(result);
return true;
}
bool PythonEnvironment::runFile(const std::string& filename,
const std::vector<std::string>& arguments)
{
py::object main = py::module::import(filename.c_str()).attr("__main__");
main(arguments);
return true;
}
std::string PythonEnvironment::getStackAsString()
{
gil lock;
PyObject* pDict = PyModule_GetDict(PyImport_AddModule("SofaRuntime"));
PyObject* pFunc = PyDict_GetItemString(pDict, "getStackForSofa");
if (PyCallable_Check(pFunc))
{
PyObject* res = PyObject_CallFunction(pFunc, nullptr);
std::string tmp=PyBytes_AsString(PyObject_Str(res));
Py_DECREF(res) ;
return tmp;
}
return "Python Stack is empty.";
}
std::string PythonEnvironment::getPythonCallingPointString()
{
gil lock;
return py::cast<std::string>(getStaticModule()->m_sofaModule.attr("getPythonCallingPointAsString")());
}
sofa::helper::logging::FileInfo::SPtr PythonEnvironment::getPythonCallingPointAsFileInfo()
{
gil lock;
py::tuple cp = getStaticModule()->m_sofaRuntimeModule.attr("getPythonCallingPoint")();
return SOFA_FILE_INFO_COPIED_FROM(py::cast<std::string>(cp[0]), py::cast<int>(cp[1]));
}
void PythonEnvironment::setArguments(const std::string& filename, const std::vector<std::string>& arguments)
{
gil lock;
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);
}
void PythonEnvironment::SceneLoaderListerner::rightBeforeLoadingScene(SceneLoader* sceneLoader)
{
SOFA_UNUSED(sceneLoader);
// unload python modules to force importing their eventual modifications
executePython([]{ PyRun_SimpleString("SofaRuntime.unloadModules()");});
}
void PythonEnvironment::setAutomaticModuleReload( bool b )
{
if( b )
SceneLoader::addListener( SceneLoaderListerner::getInstance() );
else
SceneLoader::removeListener( SceneLoaderListerner::getInstance() );
}
void PythonEnvironment::excludeModuleFromReload( const std::string& moduleName )
{
executePython([&]{ PyRun_SimpleString( std::string( "try: SofaRuntime.__SofaPythonEnvironment_modulesExcludedFromReload.append('" + moduleName + "')\nexcept:pass" ).c_str() );});
}
static const bool debug_gil = false;
static PyGILState_STATE lock(const char* trace) {
if(debug_gil) {
auto tid = PyGILState_GetThisThreadState()->thread_id % 10000;
auto id = PyGILState_GetThisThreadState()->id;
if(trace)
std::clog << ">> ["<<id << "(" << tid <<")]:: " << trace<< " wants the gil" << std::endl;
else
std::clog << ">> ["<<id << "(" << tid <<")]:: wants the gil" << std::endl;
}
return PyGILState_Ensure();
}
PythonEnvironment::gil::gil(const char* trace)
: state(lock(trace)),
trace(trace) { }
PythonEnvironment::gil::~gil() {
auto tid = PyGILState_GetThisThreadState()->thread_id % 10000;
auto id = PyGILState_GetThisThreadState()->id;
if(debug_gil) {
if(trace)
std::clog << "<< ["<<id << "(" << tid <<")]: " << trace << " prepare to released the gil" << std::endl;
else
std::clog << "<< ["<<id << "(" << tid <<")]:: prepare to released the gil" << std::endl;
}
PyGILState_Release(state);
if(debug_gil) {
if(trace)
std::clog << "<< ["<<id << "(" << tid <<")]: " << trace << " released the gil" << std::endl;
else
std::clog << "<< ["<<id << "(" << tid <<")]: 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 sofapython