102PyThreadState *mainThreadState;
107 PyGILState_STATE m_GILState;
110 PyGILRAII() : m_GILState(PyGILState_Ensure()) {}
111 ~PyGILRAII() { PyGILState_Release(m_GILState); }
115 void operator()(
PyObject* obj)
const {
120using PyObjectRef = std::unique_ptr<PyObject, PyObjDeleter>;
130 static std::mutex initMutex;
131 const std::lock_guard<std::mutex> lock(initMutex);
133 static Bool_t isInitialized =
false;
137 if (!Py_IsInitialized()) {
141 mainThreadState = PyEval_SaveThread();
149 PyObject* rootModule = PyImport_ImportModule(
"ROOT");
156 PyObject* interpreterAttr = PyObject_GetAttrString(rootModule,
"gInterpreter");
157 if (!interpreterAttr) {
159 Py_DecRef(rootModule);
163 Py_DecRef(interpreterAttr);
168 gMainDict = PyModule_GetDict(PyImport_AddModule(
"__main__"));
176 if (PyDict_SetItemString(
gMainDict,
"ROOT", rootModule) != 0) {
178 Py_DecRef(rootModule);
182 Py_DecRef(rootModule);
189 isInitialized =
true;
212 PyObjectRef modNameObj{PyUnicode_FromString(mod_name)};
213 PyObjectRef mod{PyImport_GetModule(modNameObj.get())};
214 PyObject *dct = PyModule_GetDict(mod.get());
216 PyObjectRef basesStr{PyUnicode_FromString(
"__bases__")};
217 PyObjectRef cppNameStr{PyUnicode_FromString(
"__cpp_name__")};
218 PyObjectRef nameStr{PyUnicode_FromString(
"__name__")};
221 PyObjectRef values{PyDict_Values(dct)};
222 for (
int i = 0; i < PyList_Size(values.get()); ++i) {
223 PyObjectRef
value{PyList_GetItem(values.get(), i)};
224 Py_IncRef(
value.get());
227 if (PyType_Check(
value.get()) || PyObject_HasAttr(
value.get(), basesStr.get())) {
229 PyObjectRef pyClName{PyObject_GetAttr(
value.get(), cppNameStr.get())};
231 if (PyErr_Occurred())
233 pyClName = PyObjectRef{PyObject_GetAttr(
value.get(), nameStr.get())};
236 if (PyErr_Occurred())
240 std::string fullname = mod_name;
242 fullname += PyUnicode_AsUTF8AndSize(pyClName.get(),
nullptr);
249 return !PyErr_Occurred();
266 PyObjectRef old{PyDict_Values(
gMainDict)};
270 std::string escapedName;
271 escapedName.reserve(std::char_traits<char>::length(
name));
272 for (
const char *
p =
name; *
p; ++
p) {
274 case '\\': escapedName +=
"\\\\";
break;
275 case '"': escapedName +=
"\\\"";
break;
276 case '\n': escapedName +=
"\\n";
break;
277 case '\r': escapedName +=
"\\r";
break;
278 default: escapedName += *
p;
break;
283 Exec((std::string(
"__pyroot_f = open(\"") + escapedName +
285 "exec(__pyroot_f.read()); "
286 "__pyroot_f.close(); del __pyroot_f")
290 PyObjectRef current{PyDict_Values(
gMainDict)};
292 PyObjectRef basesStr{PyUnicode_FromString(
"__bases__")};
293 PyObjectRef moduleStr{PyUnicode_FromString(
"__module__")};
294 PyObjectRef nameStr{PyUnicode_FromString(
"__name__")};
297 for (
int i = 0; i < PyList_Size(current.get()); ++i) {
298 PyObjectRef
value{PyList_GetItem(current.get(), i)};
299 Py_IncRef(
value.get());
301 if (!PySequence_Contains(old.get(),
value.get())) {
303 if (PyType_Check(
value.get()) || PyObject_HasAttr(
value.get(), basesStr.get())) {
305 PyObjectRef pyModName{PyObject_GetAttr(
value.get(), moduleStr.get())};
306 PyObjectRef pyClName{PyObject_GetAttr(
value.get(), nameStr.get())};
308 if (PyErr_Occurred())
313 if ((pyModName && pyClName) && ((PyUnicode_CheckExact(pyModName.get()) && PyUnicode_CheckExact(pyClName.get())) ||
314 (PyUnicode_Check(pyModName.get()) && PyUnicode_Check(pyClName.get())))) {
316 std::string fullname = PyUnicode_AsUTF8AndSize(pyModName.get(),
nullptr);
318 fullname += PyUnicode_AsUTF8AndSize(pyClName.get(),
nullptr);
346 std::cerr <<
"Error: no file name specified." << std::endl;
350 std::vector<std::string> args(argc);
351 for (
int i = 0; i < argc; ++i) {
387 std::stringstream command;
392 command <<
"; ROOT.Internal.SwapWithObjAtAddr['std::any'](" << resultName <<
", "
393 <<
reinterpret_cast<std::intptr_t
>(
result) <<
")";
winID h TVirtualViewer3D TVirtualGLPainter p
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t result
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void value
static PyObject * gMainDict
TClass instances represent classes, structs and namespaces in the ROOT type system.
TClass * IsA() const override
static TClass * GetClass(const char *name, Bool_t load=kTRUE, Bool_t silent=kFALSE)
Static method returning pointer to TClass of the specified class name.
const char * GetName() const override
Returns name of object.
Mother of all ROOT objects.
static void Prompt()
Enter an interactive python session (exit with ^D).
static Bool_t CPPOverload_Check(PyObject *pyobject)
Test whether the type of the given pyobject is of CPPOverload type or any derived type.
static void * CPPInstance_AsVoidPtr(PyObject *pyobject)
Extract the object pointer held by the CPPInstance pyobject.
static void ExecScript(const char *name, int argc=0, const char **argv=nullptr)
Execute a python stand-alone script, with argv CLI arguments.
static Bool_t Import(const char *name)
Import the named python module and create Cling equivalents for its classes and methods.
static Bool_t CPPInstance_CheckExact(PyObject *pyobject)
Test whether the type of the given pyobject is CPPinstance type.
static Bool_t Bind(TObject *object, const char *label)
Bind a ROOT object with, at the python side, the name "label".
static void LoadMacro(const char *name)
Execute the give python script as if it were a macro (effectively an execfile in main),...
static Bool_t Exec(const char *cmd, std::any *result=nullptr, std::string const &resultName="_anyresult")
Executes a Python command within the current Python environment.
static Bool_t CPPOverload_CheckExact(PyObject *pyobject)
Test whether the type of the given pyobject is CPPOverload type.
static Bool_t Initialize()
Initialization method: setup the python interpreter and load the ROOT module.
static Bool_t CPPInstance_Check(PyObject *pyobject)
Test whether the type of the given pyobject is of CPPInstance type or any derived type.
static PyObject * CPPInstance_FromVoidPtr(void *addr, const char *classname, Bool_t python_owns=kFALSE)
Bind the addr to a python object of class defined by classname.
CPYRT_EXTERN bool Exec(const std::string &cmd)
CPYRT_EXTERN bool Scope_Check(PyObject *pyobject)
CPYRT_EXTERN bool Overload_Check(PyObject *pyobject)
CPYRT_EXTERN bool Instance_Check(PyObject *pyobject)
CPYRT_EXTERN void ExecScript(const std::string &name, const std::vector< std::string > &args)
CPYRT_EXTERN void Prompt()
CPYRT_EXTERN void * Instance_AsVoidPtr(PyObject *pyobject)
CPYRT_EXTERN bool Overload_CheckExact(PyObject *pyobject)
PyObject * Instance_FromVoidPtr(void *addr, const char *classname, int python_owns)
CPYRT_EXTERN bool Instance_CheckExact(PyObject *pyobject)
CPYRT_EXTERN bool Import(const std::string &name)