Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
API.cxx
Go to the documentation of this file.
1// Bindings
2#include "CPyCppyy.h"
3#define CPYCPPYY_INTERNAL 1
4#include "CPyCppyy/API.h"
5#undef CPYCPPYY_INTERNAL
6
7#include "CPPInstance.h"
8#include "CPPOverload.h"
9#include "CPPScope.h"
10#include "ProxyWrappers.h"
11#include "PyStrings.h"
12
13// Standard
14#include <stdio.h>
15#include <iostream>
16#include <string>
17
18//______________________________________________________________________________
19// CPyCppyy API: Interpreter and Proxy Access
20// ==========================================
21//
22// Access to cppyy Python objects from Cling and C++: allows conversion for
23// instances and type checking for scopes, instances, etc.
24// Adds a few convenience functions to call Python from Cling and expose Python
25// classes to Cling for use in inheritance etc.
26
27
28//- data ---------------------------------------------------------------------
29static PyObject* gMainDict = nullptr;
30
31namespace CPyCppyy {
32 extern PyObject* gThisModule;
33}
34
35
36//- private helpers ----------------------------------------------------------
37namespace {
38
39static bool Initialize()
40{
41// Private initialization method: setup the python interpreter and load the
42// cppyy module.
43 static bool isInitialized = false;
44 if (isInitialized)
45 return true;
46
47 if (!Py_IsInitialized()) {
48 // this happens if Cling comes in first
49#if PY_VERSION_HEX < 0x03020000
50 PyEval_InitThreads();
51#endif
52#if PY_VERSION_HEX < 0x03080000
53 Py_Initialize();
54#else
55 PyConfig config;
56 PyConfig_InitPythonConfig(&config);
57 PyConfig_SetString(&config, &config.program_name, L"cppyy");
58 Py_InitializeFromConfig(&config);
59#endif
60#if PY_VERSION_HEX >= 0x03020000
61#if PY_VERSION_HEX < 0x03090000
62 PyEval_InitThreads();
63#endif
64#endif
65
66 // try again to see if the interpreter is initialized
67 if (!Py_IsInitialized()) {
68 // give up ...
69 std::cerr << "Error: python has not been initialized; returning." << std::endl;
70 return false;
71 }
72
73 // set the command line arguments on python's sys.argv
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")};
78#endif
79#if PY_VERSION_HEX < 0x03080000
80 PySys_SetArgv(sizeof(argv)/sizeof(argv[0]), argv);
81#endif
82
83 // force loading of the cppyy module
84 PyRun_SimpleString(const_cast<char*>("import cppyy"));
85 }
86
87 if (!gMainDict) {
88 // retrieve the main dictionary
89 gMainDict = PyModule_GetDict(
90 PyImport_AddModule(const_cast<char*>("__main__")));
91 Py_INCREF(gMainDict);
92 }
93
94// declare success ...
95 isInitialized = true;
96 return true;
97}
98
99} // unnamed namespace
100
101
102//- C++ access to cppyy objects ---------------------------------------------
104{
105// Extract the object pointer held by the CPPInstance pyobject.
106 if (!Initialize())
107 return nullptr;
108
109// check validity of cast
110 if (!CPPInstance_Check(pyobject))
111 return nullptr;
112
113// get held object (may be null)
114 return ((CPPInstance*)pyobject)->GetObject();
115}
116
117//-----------------------------------------------------------------------------
119 void* addr, const std::string& classname, bool python_owns)
120{
121// Bind the addr to a python object of class defined by classname.
122 if (!Initialize())
123 return nullptr;
124
125// perform cast (the call will check TClass and addr, and set python errors)
126 PyObject* pyobject = BindCppObjectNoCast(addr, Cppyy::GetScope(classname), false);
127
128// give ownership, for ref-counting, to the python side, if so requested
129 if (python_owns && CPPInstance_Check(pyobject))
130 ((CPPInstance*)pyobject)->PythonOwns();
131
132 return pyobject;
133}
134
135namespace CPyCppyy {
136// version with C type arguments only for use with Numba
137PyObject* Instance_FromVoidPtr(void* addr, const char* classname, int python_owns) {
138 return Instance_FromVoidPtr(addr, std::string(classname), (bool)python_owns);
139}
140} // namespace CPyCppyy
141
142//-----------------------------------------------------------------------------
144{
145// Test if the given object is of a CPPScope derived type.
146 if (!Initialize())
147 return false;
148
149 return CPPScope_Check(pyobject);
150}
151
152//-----------------------------------------------------------------------------
154{
155// Test if the given object is of a CPPScope type.
156 if (!Initialize())
157 return false;
158
159 return CPPScope_CheckExact(pyobject);
160}
161
162//-----------------------------------------------------------------------------
164{
165// Test if the given pyobject is of CPPInstance derived type.
166 if (!Initialize())
167 return false;
168
169// detailed walk through inheritance hierarchy
170 return CPPInstance_Check(pyobject);
171}
172
173//-----------------------------------------------------------------------------
175{
176// Test if the given pyobject is of CPPInstance type.
177 if (!Initialize())
178 return false;
179
180// direct pointer comparison of type member
181 return CPPInstance_CheckExact(pyobject);
182}
183
184//-----------------------------------------------------------------------------
186{
187// Extends on PySequence_Check() to determine whether an object can be iterated
188// over (technically, all objects can b/c of C++ pointer arithmetic, hence this
189// check isn't 100% accurate, but neither is PySequence_Check()).
190
191// Note: simply having the iterator protocol does not constitute a sequence, bc
192// PySequence_GetItem() would fail.
193
194// default to PySequence_Check() if called with a non-C++ object
195 if (!CPPInstance_Check(pyobject))
196 return (bool)PySequence_Check(pyobject);
197
198// all C++ objects should have sq_item defined, but a user-derived class may
199// have deleted it, in which case this is not a sequence
200 PyTypeObject* t = Py_TYPE(pyobject);
201 if (!t->tp_as_sequence || !t->tp_as_sequence->sq_item)
202 return false;
203
204// if this is the default getitem, it is only a sequence if it's an array type
205 if (t->tp_as_sequence->sq_item == CPPInstance_Type.tp_as_sequence->sq_item) {
206 if (((CPPInstance*)pyobject)->fFlags & CPPInstance::kIsArray)
207 return true;
208 return false;
209 }
210
211// TODO: could additionally verify whether __len__ is supported and/or whether
212// operator()[] takes an int argument type
213
214 return true;
215}
216
217//-----------------------------------------------------------------------------
219{
220// Test whether the given instance can safely return to C++
221 if (!CPPInstance_Check(pyobject))
222 return true; // simply don't know
223
224// the instance fails the lively test if it owns the C++ object while having a
225// reference count of 1 (meaning: it could delete the C++ instance any moment)
226 if (pyobject->ob_refcnt <= 1 && (((CPPInstance*)pyobject)->fFlags & CPPInstance::kIsOwner))
227 return false;
228
229 return true;
230}
231
232//-----------------------------------------------------------------------------
234{
235// Test if the given pyobject is of CPPOverload derived type.
236 if (!Initialize())
237 return false;
238
239// detailed walk through inheritance hierarchy
240 return CPPOverload_Check(pyobject);
241}
242
243//-----------------------------------------------------------------------------
245{
246// Test if the given pyobject is of CPPOverload type.
247 if (!Initialize())
248 return false;
249
250// direct pointer comparison of type member
251 return CPPOverload_CheckExact(pyobject);
252}
253
254
255//- access to the python interpreter ----------------------------------------
256bool CPyCppyy::Import(const std::string& mod_name)
257{
258// Import the named python module and create Cling equivalents for its classes.
259 if (!Initialize())
260 return false;
261
262 PyObject* mod = PyImport_ImportModule(mod_name.c_str());
263 if (!mod) {
264 PyErr_Print();
265 return false;
266 }
267
268// allow finding to prevent creation of a python proxy for the C++ proxy
269 Py_INCREF(mod);
270 PyModule_AddObject(gThisModule, mod_name.c_str(), mod);
271
272// force creation of the module as a namespace
273// TODO: the following is broken (and should live in Cppyy.cxx)
274// TClass::GetClass(mod_name, true);
275
276 PyObject* dct = PyModule_GetDict(mod);
277
278// create Cling classes for all new python classes
279 PyObject* values = PyDict_Values(dct);
280 for (int i = 0; i < PyList_GET_SIZE(values); ++i) {
281 PyObject* value = PyList_GET_ITEM(values, i);
282 Py_INCREF(value);
283
284 // collect classes
285 if (PyClass_Check(value) || PyObject_HasAttr(value, PyStrings::gBases)) {
286 // get full class name (including module)
287 PyObject* pyClName = PyObject_GetAttr(value, PyStrings::gName);
288 if (PyErr_Occurred())
289 PyErr_Clear();
290
291 // build full, qualified name
292 std::string fullname = mod_name;
293 fullname += ".";
294 fullname += CPyCppyy_PyText_AsString(pyClName);
295
296 // force class creation (this will eventually call TPyClassGenerator)
297 // TODO: the following is broken (and should live in Cppyy.cxx) to
298 // TClass::GetClass(fullname.c_str(), true);
299
300 Py_XDECREF(pyClName);
301 }
302
303 Py_DECREF(value);
304 }
305
306 Py_DECREF(values);
307
308// TODO: mod "leaks" here
309 if (PyErr_Occurred())
310 return false;
311 return true;
312}
313
314//-----------------------------------------------------------------------------
315void CPyCppyy::ExecScript(const std::string& name, const std::vector<std::string>& args)
316{
317// Execute a python stand-alone script, with argv CLI arguments.
318//
319// example of use:
320// CPyCppyy::ExecScript("test.py", {"1", "2", "3"});
321
322 if (!Initialize())
323 return;
324
325// verify arguments
326 if (name.empty()) {
327 std::cerr << "Error: no file name specified." << std::endl;
328 return;
329 }
330
331 FILE* fp = fopen(name.c_str(), "r");
332 if (!fp) {
333 std::cerr << "Error: could not open file \"" << name << "\"." << std::endl;
334 return;
335 }
336
337// store a copy of the old cli for restoration
338 PyObject* oldargv = PySys_GetObject(const_cast<char*>("argv")); // borrowed
339 if (!oldargv) // e.g. apache
340 PyErr_Clear();
341 else {
342 PyObject* l = PyList_New(PyList_GET_SIZE(oldargv));
343 for (int i = 0; i < PyList_GET_SIZE(oldargv); ++i) {
344 PyObject* item = PyList_GET_ITEM(oldargv, i);
345 Py_INCREF(item);
346 PyList_SET_ITEM(l, i, item); // steals ref
347 }
348 oldargv = l;
349 }
350
351// create and set (add program name) the new command line
352 int argc = args.size() + 1;
353#if PY_VERSION_HEX < 0x03000000
354// This is a legacy implementation for Python 2
355 const char** argv = new const char*[argc];
356 for (int i = 1; i < argc; ++i) argv[i] = args[i-1].c_str();
357 argv[0] = Py_GetProgramName();
358 PySys_SetArgv(argc, const_cast<char**>(argv));
359 delete [] argv;
360#else
361// This is a common code block for Python 3. We prefer using objects to
362// automatize memory management and not introduce even more preprocessor
363// branching for deletion at the end of the method.
364//
365// FUTURE IMPROVEMENT ONCE OLD PYTHON VERSIONS ARE NOT SUPPORTED BY CPPYY:
366// Right now we use C++ objects to automatize memory management. One could use
367// RAAI and the Python memory allocation API (PEP 445) once some old Python
368// version is deprecated in CPPYY. That new feature is available since version
369// 3.4 and the preprocessor branching to also support that would be so
370// complicated to make the code unreadable.
371 std::vector<std::wstring> argv2;
372 argv2.reserve(argc);
373 argv2.emplace_back(name.c_str(), &name[name.size()]);
374
375 for (int i = 1; i < argc; ++i) {
376 auto iarg = args[i - 1].c_str();
377 argv2.emplace_back(iarg, &iarg[strlen(iarg)]);
378 }
379
380#if PY_VERSION_HEX < 0x03080000
381// Before version 3.8, the code is one simple line
382 wchar_t *argv2_arr[argc];
383 for (int i = 0; i < argc; ++i) {
384 argv2_arr[i] = const_cast<wchar_t *>(argv2[i].c_str());
385 }
386 PySys_SetArgv(argc, argv2_arr);
387
388#else
389// Here we comply to "PEP 587 – Python Initialization Configuration" to avoid
390// deprecation warnings at compile time.
391 class PyConfigHelperRAAI {
392 public:
393 PyConfigHelperRAAI(const std::vector<std::wstring> &argv2)
394 {
395 PyConfig_InitPythonConfig(&fConfig);
396 fConfig.parse_argv = 1;
397 UpdateArgv(argv2);
398 InitFromConfig();
399 }
400 ~PyConfigHelperRAAI() { PyConfig_Clear(&fConfig); }
401
402 private:
403 void InitFromConfig() { Py_InitializeFromConfig(&fConfig); };
404 void UpdateArgv(const std::vector<std::wstring> &argv2)
405 {
406 auto WideStringListAppendHelper = [](PyWideStringList *wslist, const wchar_t *wcstr) {
407 PyStatus append_status = PyWideStringList_Append(wslist, wcstr);
408 if (PyStatus_IsError(append_status)) {
409 std::wcerr << "Error: could not append element " << wcstr << " to arglist - " << append_status.err_msg
410 << std::endl;
411 }
412 };
413 WideStringListAppendHelper(&fConfig.argv, Py_GetProgramName());
414 for (const auto &iarg : argv2) {
415 WideStringListAppendHelper(&fConfig.argv, iarg.c_str());
416 }
417 }
418 PyConfig fConfig;
419 };
420
421 PyConfigHelperRAAI pych(argv2);
422
423#endif // of the else branch of PY_VERSION_HEX < 0x03080000
424#endif // of the else branch of PY_VERSION_HEX < 0x03000000
425
426// actual script execution
427 PyObject* gbl = PyDict_Copy(gMainDict);
428 PyObject* result = // PyRun_FileEx closes fp (b/c of last argument "1")
429 PyRun_FileEx(fp, const_cast<char*>(name.c_str()), Py_file_input, gbl, gbl, 1);
430 if (!result)
431 PyErr_Print();
432 Py_XDECREF(result);
433 Py_DECREF(gbl);
434
435// restore original command line
436 if (oldargv) {
437 PySys_SetObject(const_cast<char*>("argv"), oldargv);
438 Py_DECREF(oldargv);
439 }
440}
441
442//-----------------------------------------------------------------------------
443bool CPyCppyy::Exec(const std::string& cmd)
444{
445// Execute a python statement (e.g. "import noddy").
446 if (!Initialize())
447 return false;
448
449// execute the command
451 PyRun_String(const_cast<char*>(cmd.c_str()), Py_file_input, gMainDict, gMainDict);
452
453// test for error
454 if (result) {
455 Py_DECREF(result);
456 return true;
457 }
458
459 PyErr_Print();
460 return false;
461}
462
463//-----------------------------------------------------------------------------
464const CPyCppyy::PyResult CPyCppyy::Eval(const std::string& expr)
465{
466// Evaluate a python expression.
467//
468// Caution: do not hold on to the return value: either store it in a builtin
469// type (implicit casting will work), or in a pointer to a cppyy object (explicit
470// casting to a void* is required).
471 if (!Initialize())
472 return PyResult();
473
474// evaluate the expression
476 PyRun_String(const_cast<char*>(expr.c_str()), Py_eval_input, gMainDict, gMainDict);
477
478// report errors as appropriate; return void
479 if (!result) {
480 PyErr_Print();
481 return PyResult();
482 }
483
484// results that require no conversion
485 if (result == Py_None || CPPInstance_Check(result) ||
487 PyFloat_Check(result) || PyLong_Check(result) || PyInt_Check(result))
488 return PyResult(result);
489
490// explicit conversion for python type required
491 PyObject* pyclass = (PyObject*)Py_TYPE(result);
492
493// retrieve class name and the module in which it resides
494 PyObject* name = PyObject_GetAttr(pyclass, PyStrings::gName);
495 PyObject* module = PyObject_GetAttr(pyclass, PyStrings::gModule);
496
497 // concat name
498 std::string qname =
499 std::string(CPyCppyy_PyText_AsString(module)) + \
501 Py_DECREF(module);
502 Py_DECREF(name);
503
504// locate cppyy style class with this name
505 // TODO: use Cppyy.cxx ...
506 //TClass* klass = TClass::GetClass(qname.c_str());
507 void* klass = nullptr;
508
509// construct general cppyy python object that pretends to be of class 'klass'
510 if (klass)
511 return PyResult(result);
512
513// no conversion, return null pointer object
514 Py_DECREF(result);
515 return PyResult();
516}
517
518//-----------------------------------------------------------------------------
520// Enter an interactive python session (exit with ^D). State is preserved
521// between successive calls.
522 if (!Initialize())
523 return;
524
525// enter i/o interactive mode
526 PyRun_InteractiveLoop(stdin, const_cast<char*>("\0"));
527}
static PyObject * gMainDict
Definition API.cxx:29
#define Py_TYPE(ob)
Definition CPyCppyy.h:196
#define PyBytes_Check
Definition CPyCppyy.h:61
#define CPyCppyy_PyText_AsString
Definition CPyCppyy.h:76
_object PyObject
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
char name[80]
Definition TGX11.cxx:110
PyTypeObject CPPInstance_Type
CPYCPPYY_EXTERN bool Instance_CheckExact(PyObject *pyobject)
Definition API.cxx:174
CPYCPPYY_EXTERN void Prompt()
Definition API.cxx:519
CPYCPPYY_EXTERN bool Overload_Check(PyObject *pyobject)
Definition API.cxx:233
CPYCPPYY_EXTERN bool Overload_CheckExact(PyObject *pyobject)
Definition API.cxx:244
CPYCPPYY_EXTERN bool Import(const std::string &name)
Definition API.cxx:256
CPYCPPYY_EXTERN void ExecScript(const std::string &name, const std::vector< std::string > &args)
Definition API.cxx:315
CPYCPPYY_EXTERN bool Instance_IsLively(PyObject *pyobject)
Definition API.cxx:218
PyObject * BindCppObjectNoCast(Cppyy::TCppObject_t object, Cppyy::TCppType_t klass, const unsigned flags=0)
bool CPPOverload_Check(T *object)
Definition CPPOverload.h:90
CPYCPPYY_EXTERN bool Sequence_Check(PyObject *pyobject)
Definition API.cxx:185
bool CPPScope_Check(T *object)
Definition CPPScope.h:81
CPYCPPYY_EXTERN bool Instance_Check(PyObject *pyobject)
Definition API.cxx:163
CPYCPPYY_EXTERN PyObject * Instance_FromVoidPtr(void *addr, const std::string &classname, bool python_owns=false)
Definition API.cxx:118
CPYCPPYY_EXTERN bool Scope_CheckExact(PyObject *pyobject)
Definition API.cxx:153
bool CPPInstance_Check(T *object)
bool CPPInstance_CheckExact(T *object)
PyObject * gThisModule
Definition CPPMethod.cxx:30
bool CPPScope_CheckExact(T *object)
Definition CPPScope.h:91
CPYCPPYY_EXTERN void * Instance_AsVoidPtr(PyObject *pyobject)
Definition API.cxx:103
CPYCPPYY_EXTERN bool Scope_Check(PyObject *pyobject)
Definition API.cxx:143
CPYCPPYY_EXTERN bool Exec(const std::string &cmd)
Definition API.cxx:443
bool CPPOverload_CheckExact(T *object)
Definition CPPOverload.h:96
CPYCPPYY_EXTERN const PyResult Eval(const std::string &expr)
Definition API.cxx:464
RPY_EXPORTED TCppScope_t GetScope(const std::string &scope_name)
RooArgList L(Args_t &&... args)
Definition RooArgList.h:156
void Initialize(Bool_t useTMVAStyle=kTRUE)
Definition tmvaglob.cxx:176
TLine l
Definition textangle.C:4