3#define CPYCPPYY_INTERNAL 1
5#undef CPYCPPYY_INTERNAL
43 static bool isInitialized =
false;
47 if (!Py_IsInitialized()) {
49#if PY_VERSION_HEX < 0x03020000
52#if PY_VERSION_HEX < 0x03080000
56 PyConfig_InitPythonConfig(&config);
57 PyConfig_SetString(&config, &config.program_name, L
"cppyy");
58 Py_InitializeFromConfig(&config);
60#if PY_VERSION_HEX >= 0x03020000
61#if PY_VERSION_HEX < 0x03090000
67 if (!Py_IsInitialized()) {
69 std::cerr <<
"Error: python has not been initialized; returning." << std::endl;
74#if PY_VERSION_HEX < 0x03000000
75 char* argv[] = {
const_cast<char*
>(
"cppyy")};
76#elif PY_VERSION_HEX < 0x03080000
77 wchar_t* argv[] = {
const_cast<wchar_t*
>(
L"cppyy")};
79#if PY_VERSION_HEX < 0x03080000
80 PySys_SetArgv(
sizeof(argv)/
sizeof(argv[0]), argv);
84 PyRun_SimpleString(
const_cast<char*
>(
"import cppyy"));
90 PyImport_AddModule(
const_cast<char*
>(
"__main__")));
122 void* addr,
const std::string& classname,
bool python_owns)
199 return (
bool)PySequence_Check(pyobject);
203 PyTypeObject* t =
Py_TYPE(pyobject);
204 if (!t->tp_as_sequence || !t->tp_as_sequence->sq_item)
208 if (t->tp_as_sequence->sq_item ==
CPPInstance_Type.tp_as_sequence->sq_item) {
229 if (pyobject->ob_refcnt <= 1 && (((
CPPInstance*)pyobject)->fFlags & CPPInstance::kIsOwner))
265 PyObject* mod = PyImport_ImportModule(mod_name.c_str());
273 PyModule_AddObject(
gThisModule, mod_name.c_str(), mod);
279 PyObject* dct = PyModule_GetDict(mod);
282 PyObject* values = PyDict_Values(dct);
283 for (
int i = 0; i < PyList_GET_SIZE(values); ++i) {
288 if (PyClass_Check(
value) || PyObject_HasAttr(
value, PyStrings::gBases)) {
290 PyObject* pyClName = PyObject_GetAttr(
value, PyStrings::gName);
291 if (PyErr_Occurred())
295 std::string fullname = mod_name;
303 Py_XDECREF(pyClName);
312 if (PyErr_Occurred())
330 std::cerr <<
"Error: no file name specified." << std::endl;
334 FILE* fp = fopen(
name.c_str(),
"r");
336 std::cerr <<
"Error: could not open file \"" <<
name <<
"\"." << std::endl;
341 PyObject* oldargv = PySys_GetObject(
const_cast<char*
>(
"argv"));
345 PyObject*
l = PyList_New(PyList_GET_SIZE(oldargv));
346 for (
int i = 0; i < PyList_GET_SIZE(oldargv); ++i) {
347 PyObject* item = PyList_GET_ITEM(oldargv, i);
349 PyList_SET_ITEM(
l, i, item);
355 int argc = args.size() + 1;
356#if PY_VERSION_HEX < 0x03000000
358 const char** argv =
new const char*[argc];
359 for (
int i = 1; i < argc; ++i) argv[i] = args[i-1].c_str();
360 argv[0] = Py_GetProgramName();
361 PySys_SetArgv(argc,
const_cast<char**
>(argv));
374 std::vector<std::wstring> argv2;
378 for (
int i = 1; i < argc; ++i) {
379 auto iarg = args[i - 1].c_str();
380 argv2.emplace_back(iarg, &iarg[strlen(iarg)]);
383#if PY_VERSION_HEX < 0x03080000
385 wchar_t *argv2_arr[argc];
386 for (
int i = 0; i < argc; ++i) {
387 argv2_arr[i] =
const_cast<wchar_t *
>(argv2[i].c_str());
389 PySys_SetArgv(argc, argv2_arr);
394 class PyConfigHelperRAAI {
396 PyConfigHelperRAAI(
const std::vector<std::wstring> &argv2)
398 PyConfig_InitPythonConfig(&fConfig);
399 fConfig.parse_argv = 1;
403 ~PyConfigHelperRAAI() { PyConfig_Clear(&fConfig); }
406 void InitFromConfig() { Py_InitializeFromConfig(&fConfig); };
407 void UpdateArgv(
const std::vector<std::wstring> &argv2)
409 auto WideStringListAppendHelper = [](PyWideStringList *wslist,
const wchar_t *wcstr) {
410 PyStatus append_status = PyWideStringList_Append(wslist, wcstr);
411 if (PyStatus_IsError(append_status)) {
412 std::wcerr <<
"Error: could not append element " << wcstr <<
" to arglist - " << append_status.err_msg
416 WideStringListAppendHelper(&fConfig.argv, Py_GetProgramName());
417 for (
const auto &iarg : argv2) {
418 WideStringListAppendHelper(&fConfig.argv, iarg.c_str());
424 PyConfigHelperRAAI pych(argv2);
432 PyRun_FileEx(fp,
const_cast<char*
>(
name.c_str()), Py_file_input, gbl, gbl, 1);
440 PySys_SetObject(
const_cast<char*
>(
"argv"), oldargv);
497 PyObject*
name = PyObject_GetAttr(pyclass, PyStrings::gName);
498 PyObject* module = PyObject_GetAttr(pyclass, PyStrings::gModule);
510 void* klass =
nullptr;
529 PyRun_InteractiveLoop(stdin,
const_cast<char*
>(
"\0"));
static PyObject * gMainDict
#define CPyCppyy_PyText_AsString
std::ios_base::fmtflags fFlags
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
PyTypeObject CPPInstance_Type
CPYCPPYY_EXTERN bool Instance_CheckExact(PyObject *pyobject)
CPYCPPYY_EXTERN void Prompt()
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_IsLively(PyObject *pyobject)
PyObject * BindCppObjectNoCast(Cppyy::TCppObject_t object, Cppyy::TCppType_t klass, const unsigned flags=0)
bool CPPOverload_Check(T *object)
CPYCPPYY_EXTERN bool Sequence_Check(PyObject *pyobject)
bool CPPScope_Check(T *object)
CPYCPPYY_EXTERN bool Instance_Check(PyObject *pyobject)
CPYCPPYY_EXTERN PyObject * Instance_FromVoidPtr(void *addr, const std::string &classname, bool python_owns=false)
CPYCPPYY_EXTERN bool Scope_CheckExact(PyObject *pyobject)
bool CPPInstance_Check(T *object)
bool CPPInstance_CheckExact(T *object)
bool CPPScope_CheckExact(T *object)
CPYCPPYY_EXTERN void * Instance_AsVoidPtr(PyObject *pyobject)
CPYCPPYY_EXTERN bool Scope_Check(PyObject *pyobject)
CPYCPPYY_EXTERN bool Exec(const std::string &cmd)
bool CPPOverload_CheckExact(T *object)
CPYCPPYY_EXTERN const PyResult Eval(const std::string &expr)
RPY_EXPORTED TCppScope_t GetScope(const std::string &scope_name)
RooArgList L(Args_t &&... args)
void Initialize(Bool_t useTMVAStyle=kTRUE)