17#include "TPyClassGenerator.h"
100 CachedPyString(
const char *
name) : fObj{PyUnicode_FromString(
name)} {}
102 CachedPyString(CachedPyString
const&) =
delete;
103 CachedPyString(CachedPyString &&) =
delete;
104 CachedPyString&
operator=(CachedPyString
const&) =
delete;
105 CachedPyString&
operator=(CachedPyString &&) =
delete;
107 ~CachedPyString() { Py_DECREF(fObj); }
118 static CachedPyString wrapper{
"__bases__"};
119 return wrapper.obj();
123 static CachedPyString wrapper{
"__cpp_name__"};
124 return wrapper.obj();
128 static CachedPyString wrapper{
"__module__"};
129 return wrapper.obj();
133 static CachedPyString wrapper{
"__name__"};
134 return wrapper.obj();
149 if (!Py_IsInitialized()) {
151#if PY_VERSION_HEX < 0x03020000
152 PyEval_InitThreads();
156#if PY_VERSION_HEX < 0x03000000
157 char *argv[] = {
const_cast<char *
>(
"root")};
159 wchar_t *argv[] = {
const_cast<wchar_t *
>(L
"root")};
161 int argc =
sizeof(argv) /
sizeof(argv[0]);
162#if PY_VERSION_HEX < 0x030b0000
168 PyConfig_InitPythonConfig(&config);
170 status = PyConfig_SetArgv(&config, argc, argv);
171 if (PyStatus_Exception(status)) {
172 PyConfig_Clear(&config);
173 std::cerr <<
"Error when setting command line arguments." << std::endl;
177 status = Py_InitializeFromConfig(&config);
178 if (PyStatus_Exception(status)) {
179 PyConfig_Clear(&config);
180 std::cerr <<
"Error when initializing Python." << std::endl;
183 PyConfig_Clear(&config);
185#if PY_VERSION_HEX >= 0x03020000
186#if PY_VERSION_HEX < 0x03090000
187 PyEval_InitThreads();
192 if (!Py_IsInitialized()) {
194 std::cerr <<
"Error: python has not been intialized; returning." << std::endl;
198#if PY_VERSION_HEX < 0x030b0000
199 PySys_SetArgv(argc, argv);
203 const int ret = PyRun_SimpleString(
const_cast<char *
>(
"import ROOT"));
206 std::cerr <<
"Error: import ROOT failed, check your PYTHONPATH environmental variable." << std::endl;
213 gMainDict = PyModule_GetDict(PyImport_AddModule(
const_cast<char *
>(
"__main__")));
221 isInitialized =
kTRUE;
238 PyObject *modNameObj = PyUnicode_FromString(mod_name);
239 PyObject *mod = PyImport_GetModule(modNameObj);
240 PyObject *dct = PyModule_GetDict(mod);
243 PyObject *values = PyDict_Values(dct);
244 for (
int i = 0; i < PyList_GET_SIZE(values); ++i) {
249 if (PyType_Check(
value) || PyObject_HasAttr(
value, PyStrings::basesStr())) {
251 PyObject *pyClName = PyObject_GetAttr(
value, PyStrings::cppNameStr());
253 pyClName = PyObject_GetAttr(
value, PyStrings::nameStr());
256 if (PyErr_Occurred())
260 std::string fullname = mod_name;
262 fullname += PyUnicode_AsUTF8(pyClName);
267 Py_XDECREF(pyClName);
275 Py_DECREF(modNameObj);
277 if (PyErr_Occurred())
297#if PY_VERSION_HEX < 0x03000000
298 Exec((std::string(
"execfile(\"") +
name +
"\")").c_str());
300 Exec((std::string(
"__pyroot_f = open(\"") +
name +
"\"); "
301 "exec(__pyroot_f.read()); "
302 "__pyroot_f.close(); del __pyroot_f")
310 for (
int i = 0; i < PyList_GET_SIZE(current); ++i) {
314 if (!PySequence_Contains(old,
value)) {
316 if (PyType_Check(
value) || PyObject_HasAttr(
value, PyStrings::basesStr())) {
318 PyObject *pyModName = PyObject_GetAttr(
value, PyStrings::moduleStr());
319 PyObject *pyClName = PyObject_GetAttr(
value, PyStrings::nameStr());
321 if (PyErr_Occurred())
326 if ((pyModName && pyClName) &&
327 ((PyUnicode_CheckExact(pyModName) && PyUnicode_CheckExact(pyClName)) ||
328 (PyUnicode_Check(pyModName) && PyUnicode_Check(pyClName)))) {
330 std::string fullname = PyUnicode_AsUTF8(pyModName);
332 fullname += PyUnicode_AsUTF8(pyClName);
338 Py_XDECREF(pyClName);
339 Py_XDECREF(pyModName);
366 std::cerr <<
"Error: no file name specified." << std::endl;
370 std::vector<std::string> args(argc);
371 for (
int i = 0; i < argc; ++i) {
427 PyObject *pyclass = PyObject_GetAttrString(
result,
const_cast<char*
>(
"__class__"));
430 PyObject *
name = PyObject_GetAttr(pyclass, PyStrings::nameStr());
431 PyObject *module = PyObject_GetAttr(pyclass, PyStrings::moduleStr());
434 std::string qname = std::string(PyUnicode_AsUTF8(module)) +
'.' + PyUnicode_AsUTF8(
name);
468 Bool_t bOk = PyDict_SetItemString(
gMainDict,
const_cast<char *
>(label), bound) == 0;
490 PyRun_InteractiveLoop(stdin,
const_cast<char *
>(
"\0"));
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
Binding & operator=(OUT(*fun)(void))
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.
Accessing the Python interpreter from C++.
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 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 Exec(const char *cmd)
Execute a python statement (e.g. "import ROOT").
static Bool_t CPPInstance_Check(PyObject *pyobject)
Test whether the type of the given pyobject is of CPPInstance type or any derived type.
static const TPyReturn Eval(const char *expr)
Evaluate a python expression (e.g.
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.
CPYCPPYY_EXTERN bool Instance_CheckExact(PyObject *pyobject)
CPYCPPYY_EXTERN bool Overload_Check(PyObject *pyobject)
CPYCPPYY_EXTERN bool Overload_CheckExact(PyObject *pyobject)
CPYCPPYY_EXTERN bool Import(const std::string &name)
CPYCPPYY_EXTERN void ExecScript(const std::string &name, const std::vector< std::string > &args)
CPYCPPYY_EXTERN bool Instance_Check(PyObject *pyobject)
CPYCPPYY_EXTERN PyObject * Instance_FromVoidPtr(void *addr, const std::string &classname, bool python_owns=false)
CPYCPPYY_EXTERN void * Instance_AsVoidPtr(PyObject *pyobject)