PythonInterface.cpp
Go to the documentation of this file.
1 /*
2  * PythonSingleTon.h
3  *
4  * Created on: Jan. 14, 2021
5  * Author: mathieu
6  */
7 
11 
12 namespace rtabmap {
13 
16 PyThreadState * PythonInterface::mainThreadState_ = 0;
17 unsigned long PythonInterface::mainThreadID_ = 0;
18 
20  threadState_(0)
21 {
22  UScopeMutex lockM(mutex_);
23  if(refCount_ == 0)
24  {
25  UINFO("Py_Initialize() with thread = %d", UThread::currentThreadId());
26  // initialize Python
27  Py_Initialize();
28 
29  // initialize thread support
30  PyEval_InitThreads();
31  Py_DECREF(PyImport_ImportModule("threading"));
32 
33  //release the GIL, store thread state, set the current thread state to NULL
34  mainThreadState_ = PyEval_SaveThread();
37  }
38 
39  ++refCount_;
40 }
41 
43 {
45  if(refCount_>0 && --refCount_==0)
46  {
47  // shut down the interpreter
48  UINFO("Py_Finalize() with thread = %d", UThread::currentThreadId());
49  PyEval_RestoreThread(mainThreadState_);
50  Py_Finalize();
51  }
52 }
53 
55 {
56  mutex_.lock();
57 
58  UDEBUG("Lock: Current thread=%d (main=%d)", UThread::currentThreadId(), mainThreadID_);
60  {
61  PyEval_RestoreThread(mainThreadState_);
62  }
63  else
64  {
65  // create a thread state object for this thread
66  threadState_ = PyThreadState_New(mainThreadState_->interp);
68  PyEval_RestoreThread(threadState_);
69  }
70 }
71 
73 {
75  {
76  mainThreadState_ = PyEval_SaveThread();
77  }
78  else
79  {
80  PyThreadState_Clear(threadState_);
81  PyThreadState_DeleteCurrent();
82  }
83  UDEBUG("Unlock: Current thread=%d (main=%d)", UThread::currentThreadId(), mainThreadID_);
84  mutex_.unlock();
85 }
86 
88 {
89  // Author: https://stackoverflow.com/questions/41268061/c-c-python-exception-traceback-not-being-generated
90 
91  PyObject* type;
92  PyObject* value;
93  PyObject* traceback;
94 
95  PyErr_Fetch(&type, &value, &traceback);
96  PyErr_NormalizeException(&type, &value, &traceback);
97 
98  std::string fcn = "";
99  fcn += "def get_pretty_traceback(exc_type, exc_value, exc_tb):\n";
100  fcn += " import sys, traceback\n";
101  fcn += " lines = []\n";
102  fcn += " lines = traceback.format_exception(exc_type, exc_value, exc_tb)\n";
103  fcn += " output = '\\n'.join(lines)\n";
104  fcn += " return output\n";
105 
106  PyRun_SimpleString(fcn.c_str());
107  PyObject* mod = PyImport_ImportModule("__main__");
108  UASSERT(mod);
109  PyObject* method = PyObject_GetAttrString(mod, "get_pretty_traceback");
110  UASSERT(method);
111  PyObject* outStr = PyObject_CallObject(method, Py_BuildValue("OOO", type, value, traceback));
112  std::string pretty;
113  if(outStr)
114  pretty = PyBytes_AsString(PyUnicode_AsASCIIString(outStr));
115 
116  Py_DECREF(method);
117  Py_DECREF(outStr);
118  Py_DECREF(mod);
119 
120  return pretty;
121 }
122 
123 }
#define PyBytes_AsString
GLM_FUNC_DECL genType mod(genType const &x, genType const &y)
static unsigned long mainThreadID_
#define UASSERT(condition)
int lock() const
Definition: UMutex.h:87
Definition: UMutex.h:54
int unlock() const
Definition: UMutex.h:113
#define UDEBUG(...)
static PyThreadState * mainThreadState_
ULogger class and convenient macros.
PyThreadState * threadState_
static unsigned long currentThreadId()
Definition: UThread.h:96
#define UINFO(...)


rtabmap
Author(s): Mathieu Labbe
autogenerated on Mon Jan 23 2023 03:37:29