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
51#endif
52#if PY_VERSION_HEX < 0x03080000
54#else
55 PyConfig config;
57 PyConfig_SetString(&config, &config.program_name, L"cppyy");
59#endif
60#if PY_VERSION_HEX >= 0x03020000
61#if PY_VERSION_HEX < 0x03090000
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
90 PyImport_AddModule(const_cast<char*>("__main__")));
91 // The gMainDict is borrowed, i.e. we are not calling Py_INCREF(gMainDict).
92 // Like this, we avoid unexpectedly affecting how long __main__ is kept
93 // alive. The gMainDict is only used in Exec(), ExecScript(), and Eval(),
94 // which should not be called after __main__ is garbage collected anyway.
95 }
96
97// declare success ...
98 isInitialized = true;
99 return true;
100}
101
102} // unnamed namespace
103
104
105//- C++ access to cppyy objects ---------------------------------------------
107{
108 if (!Instance_Check(pyobject)) {
109 PyErr_SetString(PyExc_TypeError, "Instance_GetScopedFinalName : object is not a C++ instance");
110 return "";
111 }
112
115}
116
117//-----------------------------------------------------------------------------
119{
120// Extract the object pointer held by the CPPInstance pyobject.
121 if (!Initialize())
122 return nullptr;
123
124// check validity of cast
126 return nullptr;
127
128// get held object (may be null)
129 return ((CPPInstance*)pyobject)->GetObject();
130}
131
132//-----------------------------------------------------------------------------
134 void* addr, const std::string& classname, bool python_owns)
135{
136// Bind the addr to a python object of class defined by classname.
137 if (!Initialize())
138 return nullptr;
139
140// perform cast (the call will check TClass and addr, and set python errors)
142
143// give ownership, for ref-counting, to the python side, if so requested
145 ((CPPInstance*)pyobject)->PythonOwns();
146
147 return pyobject;
148}
149
150namespace CPyCppyy {
151// version with C type arguments only for use with Numba
152PyObject* Instance_FromVoidPtr(void* addr, const char* classname, int python_owns) {
153 return Instance_FromVoidPtr(addr, std::string(classname), (bool)python_owns);
154}
155} // namespace CPyCppyy
156
157//-----------------------------------------------------------------------------
159{
160// Test if the given object is of a CPPScope derived type.
161 if (!Initialize())
162 return false;
163
164 return CPPScope_Check(pyobject);
165}
166
167//-----------------------------------------------------------------------------
169{
170// Test if the given object is of a CPPScope type.
171 if (!Initialize())
172 return false;
173
175}
176
177//-----------------------------------------------------------------------------
179{
180// Test if the given pyobject is of CPPInstance derived type.
181 if (!Initialize())
182 return false;
183
184// detailed walk through inheritance hierarchy
186}
187
188//-----------------------------------------------------------------------------
190{
191// Test if the given pyobject is of CPPInstance type.
192 if (!Initialize())
193 return false;
194
195// direct pointer comparison of type member
197}
198
199//-----------------------------------------------------------------------------
201{
202 if (!Initialize())
203 return;
204
205// check validity of cast
207 return;
208
209 ((CPPInstance *)pyobject)->PythonOwns();
210}
211
212//-----------------------------------------------------------------------------
214{
215 if (!Initialize())
216 return;
217
218// check validity of cast
220 return;
221
222 ((CPPInstance *)pyobject)->CppOwns();
223}
224
225//-----------------------------------------------------------------------------
227{
228// Extends on PySequence_Check() to determine whether an object can be iterated
229// over (technically, all objects can b/c of C++ pointer arithmetic, hence this
230// check isn't 100% accurate, but neither is PySequence_Check()).
231
232// Note: simply having the iterator protocol does not constitute a sequence, bc
233// PySequence_GetItem() would fail.
234
235// default to PySequence_Check() if called with a non-C++ object
237 return (bool)PySequence_Check(pyobject);
238
239// all C++ objects should have sq_item defined, but a user-derived class may
240// have deleted it, in which case this is not a sequence
242 if (!t->tp_as_sequence || !t->tp_as_sequence->sq_item)
243 return false;
244
245// if this is the default getitem, it is only a sequence if it's an array type
246 if (t->tp_as_sequence->sq_item == CPPInstance_Type.tp_as_sequence->sq_item) {
247 if (((CPPInstance*)pyobject)->fFlags & CPPInstance::kIsArray)
248 return true;
249 return false;
250 }
251
252// TODO: could additionally verify whether __len__ is supported and/or whether
253// operator()[] takes an int argument type
254
255 return true;
256}
257
258//-----------------------------------------------------------------------------
260{
261// Test whether the given instance can safely return to C++
263 return true; // simply don't know
264
265// the instance fails the lively test if it owns the C++ object while having a
266// reference count of 1 (meaning: it could delete the C++ instance any moment)
267 if (Py_REFCNT(pyobject) <= 1 && (((CPPInstance*)pyobject)->fFlags & CPPInstance::kIsOwner))
268 return false;
269
270 return true;
271}
272
273//-----------------------------------------------------------------------------
275{
276// Test if the given pyobject is of CPPOverload derived type.
277 if (!Initialize())
278 return false;
279
280// detailed walk through inheritance hierarchy
282}
283
284//-----------------------------------------------------------------------------
286{
287// Test if the given pyobject is of CPPOverload type.
288 if (!Initialize())
289 return false;
290
291// direct pointer comparison of type member
293}
294
295//-----------------------------------------------------------------------------
297{
298 CPPInstance::ReduceMethod() = reduceMethod;
299}
300
301//- access to the python interpreter ----------------------------------------
302bool CPyCppyy::Import(const std::string& mod_name)
303{
304// Import the named python module and create Cling equivalents for its classes.
305 if (!Initialize())
306 return false;
307
309 if (!mod) {
310 PyErr_Print();
311 return false;
312 }
313
314// allow finding to prevent creation of a python proxy for the C++ proxy
315 Py_INCREF(mod);
317
318// force creation of the module as a namespace
319// TODO: the following is broken (and should live in Cppyy.cxx)
320// TClass::GetClass(mod_name, true);
321
323
324// create Cling classes for all new python classes
325 PyObject* values = PyDict_Values(dct);
326 for (int i = 0; i < PyList_GET_SIZE(values); ++i) {
327 PyObject* value = PyList_GET_ITEM(values, i);
329
330 // collect classes
331 if (PyClass_Check(value) || PyObject_HasAttr(value, PyStrings::gBases)) {
332 // get full class name (including module)
333 PyObject* pyClName = PyObject_GetAttr(value, PyStrings::gName);
334 if (PyErr_Occurred())
335 PyErr_Clear();
336
337 // build full, qualified name
338 std::string fullname = mod_name;
339 fullname += ".";
341
343 }
344
346 }
347
348 Py_DECREF(values);
349
350// TODO: mod "leaks" here
351 if (PyErr_Occurred())
352 return false;
353 return true;
354}
355
356//-----------------------------------------------------------------------------
357void CPyCppyy::ExecScript(const std::string& name, const std::vector<std::string>& args)
358{
359// Execute a python stand-alone script, with argv CLI arguments.
360//
361// example of use:
362// CPyCppyy::ExecScript("test.py", {"1", "2", "3"});
363
364 if (!Initialize())
365 return;
366
367// verify arguments
368 if (name.empty()) {
369 std::cerr << "Error: no file name specified." << std::endl;
370 return;
371 }
372
373 FILE* fp = fopen(name.c_str(), "r");
374 if (!fp) {
375 std::cerr << "Error: could not open file \"" << name << "\"." << std::endl;
376 return;
377 }
378
379// store a copy of the old cli for restoration
380 PyObject* oldargv = PySys_GetObject("argv"); // borrowed
381 if (oldargv) {
383 oldargv = copy; // now owned
384 } else {
385 PyErr_Clear();
386 }
387
388// build new argv
389 const int argc = (int)args.size() + 1;
390 std::vector<wchar_t*> wargv(argc);
391
392 wargv[0] = Py_DecodeLocale(name.c_str(), nullptr);
393
394 for (int i = 1; i < argc; ++i) {
395 wargv[i] = Py_DecodeLocale(args[i - 1].c_str(), nullptr);
396 }
397
398// set sys.argv
399 PyObject* sysmod = PyImport_ImportModule("sys"); // new reference
400 if (sysmod) {
402 for (int i = 0; i < argc; ++i) {
404 }
408 } else {
409 PyErr_Print();
410 }
411
412// actual script execution
414 PyObject* result = // PyRun_FileEx closes fp (b/c of last argument "1")
415 PyRun_FileEx(fp, const_cast<char*>(name.c_str()), Py_file_input, gbl, gbl, 1);
416
417 if (!result)
418 PyErr_Print();
419
421 Py_DECREF(gbl);
422
423// restore original command line
424 if (oldargv) {
425 PySys_SetObject("argv", oldargv);
427 }
428
429// free memory from Py_DecodeLocale
430 for (auto ptr : wargv)
431 PyMem_RawFree(ptr);
432}
433
434//-----------------------------------------------------------------------------
435bool CPyCppyy::Exec(const std::string& cmd)
436{
437// Execute a python statement (e.g. "import noddy").
438 if (!Initialize())
439 return false;
440
441// execute the command
443 PyRun_String(const_cast<char*>(cmd.c_str()), Py_file_input, gMainDict, gMainDict);
444
445// test for error
446 if (result) {
448 return true;
449 }
450
451 PyErr_Print();
452 return false;
453}
454
455//-----------------------------------------------------------------------------
457// Enter an interactive python session (exit with ^D). State is preserved
458// between successive calls.
459 if (!Initialize())
460 return;
461
462// enter i/o interactive mode
463 PyRun_InteractiveLoop(stdin, const_cast<char*>("\0"));
464}
static PyObject * gMainDict
Definition API.cxx:29
#define Py_TYPE(ob)
Definition CPyCppyy.h:196
#define CPyCppyy_PyText_AsString
Definition CPyCppyy.h:76
uint32_t fFlags
_object PyObject
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
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:148
PyTypeObject CPPInstance_Type
CPYCPPYY_EXTERN bool Instance_CheckExact(PyObject *pyobject)
Definition API.cxx:189
CPYCPPYY_EXTERN void Prompt()
Definition API.cxx:456
CPYCPPYY_EXTERN bool Overload_Check(PyObject *pyobject)
Definition API.cxx:274
CPYCPPYY_EXTERN bool Overload_CheckExact(PyObject *pyobject)
Definition API.cxx:285
CPYCPPYY_EXTERN bool Import(const std::string &name)
Definition API.cxx:302
CPYCPPYY_EXTERN void ExecScript(const std::string &name, const std::vector< std::string > &args)
Definition API.cxx:357
CPYCPPYY_EXTERN bool Instance_IsLively(PyObject *pyobject)
Definition API.cxx:259
PyObject * BindCppObjectNoCast(Cppyy::TCppObject_t object, Cppyy::TCppType_t klass, const unsigned flags=0)
bool CPPOverload_Check(T *object)
Definition CPPOverload.h:94
CPYCPPYY_EXTERN bool Sequence_Check(PyObject *pyobject)
Definition API.cxx:226
bool CPPScope_Check(T *object)
Definition CPPScope.h:81
CPYCPPYY_EXTERN bool Instance_Check(PyObject *pyobject)
Definition API.cxx:178
CPYCPPYY_EXTERN PyObject * Instance_FromVoidPtr(void *addr, const std::string &classname, bool python_owns=false)
Definition API.cxx:133
CPYCPPYY_EXTERN bool Scope_CheckExact(PyObject *pyobject)
Definition API.cxx:168
bool CPPInstance_Check(T *object)
bool CPPInstance_CheckExact(T *object)
CPYCPPYY_EXTERN void Instance_SetCppOwns(PyObject *pyobject)
Definition API.cxx:213
CPYCPPYY_EXTERN void Instance_SetPythonOwns(PyObject *pyobject)
Definition API.cxx:200
PyObject * gThisModule
Definition CPPMethod.cxx:30
bool CPPScope_CheckExact(T *object)
Definition CPPScope.h:91
CPYCPPYY_EXTERN void Instance_SetReduceMethod(PyCFunction reduceMethod)
Definition API.cxx:296
CPYCPPYY_EXTERN std::string Instance_GetScopedFinalName(PyObject *pyobject)
Definition API.cxx:106
CPYCPPYY_EXTERN void * Instance_AsVoidPtr(PyObject *pyobject)
Definition API.cxx:118
CPYCPPYY_EXTERN bool Scope_Check(PyObject *pyobject)
Definition API.cxx:158
CPYCPPYY_EXTERN bool Exec(const std::string &cmd)
Definition API.cxx:435
bool CPPOverload_CheckExact(T *object)
TCppScope_t TCppType_t
Definition cpp_cppyy.h:35
RPY_EXPORTED std::string GetScopedFinalName(TCppType_t type)
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