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) {
222 PyObject *value = PyList_GET_ITEM(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) {
287 PyObject *value = PyList_GET_ITEM(current, i);
290 if (!PySequence_Contains(old, value)) {
297 if (PyErr_Occurred())
302 if ((pyModName && pyClName) &&
314 Py_XDECREF(pyClName);
315 Py_XDECREF(pyModName);
334#
if PY_VERSION_HEX < 0x03000000
346 std::cerr <<
"Error: no file name specified." << std::endl;
350 FILE *fp = fopen(
name,
"r");
352 std::cerr <<
"Error: could not open file \"" <<
name <<
"\"." << std::endl;
357 PyObject *oldargv = PySys_GetObject(
const_cast<char *
>(
"argv"));
361 PyObject *
l = PyList_New(PyList_GET_SIZE(oldargv));
362 for (
int i = 0; i < PyList_GET_SIZE(oldargv); ++i) {
363 PyObject *item = PyList_GET_ITEM(oldargv, i);
365 PyList_SET_ITEM(
l, i, item);
372#if PY_VERSION_HEX < 0x03000000
373 const char **argv2 =
new const char *[argc];
374 for (
int i = 1; i < argc; ++i)
375 argv2[i] = argv[i - 1];
376 argv2[0] = Py_GetProgramName();
377 PySys_SetArgv(argc,
const_cast<char **
>(argv2));
386 PyRun_FileEx(fp,
const_cast<char *
>(
name), Py_file_input, gbl, gbl, 1);
394 PySys_SetObject(
const_cast<char *
>(
"argv"), oldargv);
445 PyLong_Check(result) || PyInt_Check(result))
449 PyObject *pyclass = PyObject_GetAttrString(result,
const_cast<char*
>(
"__class__"));
485 TClass *klass =
object->IsA();
490 Bool_t bOk = PyDict_SetItemString(
gMainDict,
const_cast<char *
>(label), bound) == 0;
512 PyRun_InteractiveLoop(stdin,
const_cast<char *
>(
"\0"));
#define CPyCppyy_PyText_AsString
#define CPyCppyy_PyText_CheckExact
#define CPyCppyy_PyText_Check
static PyObject * gMainDict
TClass instances represent classes, structs and namespaces in the ROOT type system.
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.
virtual const char * GetName() const
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 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 void ExecScript(const char *name, int argc=0, const char **argv=0)
Execute a python stand-alone script, with argv CLI arguments.
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)