20#include "TPyClassGenerator.h"
101 namespace PyStrings {
118 if (!Py_IsInitialized()) {
120#if PY_VERSION_HEX < 0x03020000
121 PyEval_InitThreads();
125#if PY_VERSION_HEX < 0x03000000
126 char *argv[] = {
const_cast<char *
>(
"root")};
128 wchar_t *argv[] = {
const_cast<wchar_t *
>(L
"root")};
130 int argc =
sizeof(argv) /
sizeof(argv[0]);
131#if PY_VERSION_HEX < 0x030b0000
137 PyConfig_InitPythonConfig(&config);
139 status = PyConfig_SetArgv(&config, argc, argv);
140 if (PyStatus_Exception(status)) {
141 PyConfig_Clear(&config);
142 std::cerr <<
"Error when setting command line arguments." << std::endl;
146 status = Py_InitializeFromConfig(&config);
147 if (PyStatus_Exception(status)) {
148 PyConfig_Clear(&config);
149 std::cerr <<
"Error when initializing Python." << std::endl;
152 PyConfig_Clear(&config);
154#if PY_VERSION_HEX >= 0x03020000
155#if PY_VERSION_HEX < 0x03090000
156 PyEval_InitThreads();
161 if (!Py_IsInitialized()) {
163 std::cerr <<
"Error: python has not been intialized; returning." << std::endl;
167#if PY_VERSION_HEX < 0x030b0000
168 PySys_SetArgv(argc, argv);
172 const int ret = PyRun_SimpleString(
const_cast<char *
>(
"import ROOT"));
175 std::cerr <<
"Error: import ROOT failed, check your PYTHONPATH environmental variable." << std::endl;
182 gMainDict = PyModule_GetDict(PyImport_AddModule(
const_cast<char *
>(
"__main__")));
190 isInitialized =
kTRUE;
204 PyObject *mod = PyImport_ImportModule(mod_name);
217 PyObject *dct = PyModule_GetDict(mod);
220 PyObject *values = PyDict_Values(dct);
221 for (
int i = 0; i < PyList_GET_SIZE(values); ++i) {
233 if (PyErr_Occurred())
237 std::string fullname = mod_name;
244 Py_XDECREF(pyClName);
253 if (PyErr_Occurred())
273#if PY_VERSION_HEX < 0x03000000
274 Exec((std::string(
"execfile(\"") +
name +
"\")").c_str());
276 Exec((std::string(
"__pyroot_f = open(\"") +
name +
"\"); "
277 "exec(__pyroot_f.read()); "
278 "__pyroot_f.close(); del __pyroot_f")
286 for (
int i = 0; i < PyList_GET_SIZE(current); ++i) {
290 if (!PySequence_Contains(old,
value)) {
297 if (PyErr_Occurred())
302 if ((pyModName && pyClName) &&
314 Py_XDECREF(pyClName);
315 Py_XDECREF(pyModName);
342 std::cerr <<
"Error: no file name specified." << std::endl;
346 FILE *fp = fopen(
name,
"r");
348 std::cerr <<
"Error: could not open file \"" <<
name <<
"\"." << std::endl;
353 PyObject *oldargv = PySys_GetObject(
const_cast<char *
>(
"argv"));
357 PyObject *
l = PyList_New(PyList_GET_SIZE(oldargv));
358 for (
int i = 0; i < PyList_GET_SIZE(oldargv); ++i) {
359 PyObject *item = PyList_GET_ITEM(oldargv, i);
361 PyList_SET_ITEM(
l, i, item);
368#if PY_VERSION_HEX < 0x03000000
370 const char **argv2 =
new const char *[argc];
371 for (
int i = 1; i < argc; ++i)
372 argv2[i] = argv[i - 1];
373 argv2[0] = Py_GetProgramName();
374 PySys_SetArgv(argc,
const_cast<char **
>(argv2));
383 std::vector<std::wstring> argv2;
387 for (
int i = 1; i < argc; ++i) {
388 auto iarg = argv[i - 1];
389 argv2.emplace_back(iarg, &iarg[strlen(iarg)]);
392#if PY_VERSION_HEX < 0x03080000
394 wchar_t *argv2_arr[argc];
395 for (
int i = 0; i < argc; ++i) {
396 argv2_arr[i] =
const_cast<wchar_t *
>(argv2[i].c_str());
398 PySys_SetArgv(argc, argv2_arr);
402 class PyConfigHelperRAAI {
404 PyConfigHelperRAAI(
const std::vector<std::wstring> &argv2)
406 PyConfig_InitPythonConfig(&fConfig);
407 fConfig.parse_argv = 1;
411 ~PyConfigHelperRAAI() { PyConfig_Clear(&fConfig); }
414 void InitFromConfig() { Py_InitializeFromConfig(&fConfig); };
415 void UpdateArgv(
const std::vector<std::wstring> &argv2)
417 auto WideStringListAppendHelper = [](PyWideStringList *wslist,
const wchar_t *wcstr) {
418 PyStatus append_status = PyWideStringList_Append(wslist, wcstr);
419 if (PyStatus_IsError(append_status)) {
420 std::wcerr <<
"Error: could not append element " << wcstr <<
" to arglist - " << append_status.err_msg
424 WideStringListAppendHelper(&fConfig.argv, Py_GetProgramName());
425 for (
const auto &iarg : argv2) {
426 WideStringListAppendHelper(&fConfig.argv, iarg.c_str());
432 PyConfigHelperRAAI pych(argv2);
440 PyRun_FileEx(fp,
const_cast<char *
>(
name), Py_file_input, gbl, gbl, 1);
442 std::cerr <<
"An error occurred executing file " <<
name << std::endl;
451 PySys_SetObject(
const_cast<char *
>(
"argv"), oldargv);
506 PyObject *pyclass = PyObject_GetAttrString(
result,
const_cast<char*
>(
"__class__"));
547 Bool_t bOk = PyDict_SetItemString(
gMainDict,
const_cast<char *
>(label), bound) == 0;
569 PyRun_InteractiveLoop(stdin,
const_cast<char *
>(
"\0"));
#define CPyCppyy_PyText_AsString
#define CPyCppyy_PyText_CheckExact
#define CPyCppyy_PyText_Check
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.
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.
R__EXTERN PyObject * gName
R__EXTERN PyObject * gCppName
R__EXTERN PyObject * gBases
R__EXTERN PyObject * gModule
Set of helper functions that are invoked from the pythonizors, on the Python side.
PyObject * BindCppObjectNoCast(Cppyy::TCppObject_t object, Cppyy::TCppType_t klass, const unsigned flags=0)
bool CPPOverload_Check(T *object)
bool CPPInstance_Check(T *object)
bool CPPInstance_CheckExact(T *object)
R__EXTERN PyObject * gThisModule
PyObject * BindCppObject(Cppyy::TCppObject_t object, Cppyy::TCppType_t klass, const unsigned flags=0)
bool CPPOverload_CheckExact(T *object)
RPY_EXPORTED TCppScope_t GetScope(const std::string &scope_name)