Logo ROOT  
Reference Guide
ProxyWrappers.cxx
Go to the documentation of this file.
1// Bindings
2#include "CPyCppyy.h"
3#include "ProxyWrappers.h"
4#include "CPPClassMethod.h"
5#include "CPPConstructor.h"
6#include "CPPDataMember.h"
7#include "CPPExcInstance.h"
8#include "CPPFunction.h"
9#include "CPPGetSetItem.h"
10#include "CPPInstance.h"
11#include "CPPMethod.h"
12#include "CPPOverload.h"
13#include "CPPScope.h"
14#include "MemoryRegulator.h"
15#include "PyStrings.h"
16#include "Pythonize.h"
17#include "TemplateProxy.h"
18#include "TupleOfInstances.h"
19#include "TypeManip.h"
20#include "Utility.h"
21
22// Standard
23#include <algorithm>
24#include <deque>
25#include <map>
26#include <set>
27#include <string>
28#include <vector>
29
30
31//- data _______________________________________________________________________
32namespace CPyCppyy {
33 extern PyObject* gThisModule;
34 extern PyObject* gPyTypeMap;
35 extern std::set<Cppyy::TCppType_t> gPinnedTypes;
36}
37
38// to prevent having to walk scopes, track python classes by C++ class
39typedef std::map<Cppyy::TCppScope_t, PyObject*> PyClassMap_t;
41
42
43//- helpers --------------------------------------------------------------------
44
45namespace CPyCppyy {
46
47typedef struct {
48 PyObject_HEAD
49 PyObject *dict;
50} proxyobject;
51
52// helper for creating new C++ proxy python types
54{
55// Create a new python shadow class with the required hierarchy and meta-classes.
56 PyObject* pymetabases = PyTuple_New(PyTuple_GET_SIZE(pybases));
57 for (int i = 0; i < PyTuple_GET_SIZE(pybases); ++i) {
58 PyObject* btype = (PyObject*)Py_TYPE(PyTuple_GetItem(pybases, i));
59 Py_INCREF(btype);
60 PyTuple_SET_ITEM(pymetabases, i, btype);
61 }
62
63 std::string name = Cppyy::GetFinalName(klass);
64
65// create meta-class, add a dummy __module__ to pre-empt the default setting
66 PyObject* args = Py_BuildValue((char*)"sO{}", (name+"_meta").c_str(), pymetabases);
67 PyDict_SetItem(PyTuple_GET_ITEM(args, 2), PyStrings::gModule, Py_True);
68 Py_DECREF(pymetabases);
69
70 PyObject* pymeta = (PyObject*)CPPScopeMeta_New(klass, args);
71 Py_DECREF(args);
72 if (!pymeta) {
73 PyErr_Print();
74 return nullptr;
75 }
76
77// alright, and now we really badly want to get rid of the dummy ...
78 PyObject* dictproxy = PyObject_GetAttr(pymeta, PyStrings::gDict);
79 PyDict_DelItem(((proxyobject*)dictproxy)->dict, PyStrings::gModule);
80
81// create actual class
82 args = Py_BuildValue((char*)"sO{}", name.c_str(), pybases);
83 PyObject* pyclass =
84 ((PyTypeObject*)pymeta)->tp_new((PyTypeObject*)pymeta, args, nullptr);
85
86 Py_DECREF(args);
87 Py_DECREF(pymeta);
88
89 return pyclass;
90}
91
92static inline
95{
97 PyObject* pname = CPyCppyy_PyText_InternFromString(const_cast<char*>(property->GetName().c_str()));
98
99// allow access at the instance level
100 PyType_Type.tp_setattro(pyclass, pname, (PyObject*)property);
101
102// allow access at the class level (always add after setting instance level)
103 if (Cppyy::IsStaticData(scope, idata))
104 PyType_Type.tp_setattro((PyObject*)Py_TYPE(pyclass), pname, (PyObject*)property);
105
106// cleanup
107 Py_DECREF(pname);
108 Py_DECREF(property);
109}
110
111static inline
112void AddScopeToParent(PyObject* parent, const std::string& name, PyObject* newscope)
113{
115 if (CPPScope_Check(parent)) PyType_Type.tp_setattro(parent, pyname, newscope);
116 else PyObject_SetAttr(parent, pyname, newscope);
117 Py_DECREF(pyname);
118}
119
120} // namespace CPyCppyy
121
122
123//- public functions ---------------------------------------------------------
124namespace CPyCppyy {
125
126static inline void sync_templates(
127 PyObject* pyclass, const std::string& mtCppName, const std::string& mtName)
128{
129 PyObject* dct = PyObject_GetAttr(pyclass, PyStrings::gDict);
130 PyObject* pyname = CPyCppyy_PyText_InternFromString(const_cast<char*>(mtName.c_str()));
131 PyObject* attr = PyObject_GetItem(dct, pyname);
132 if (!attr) PyErr_Clear();
133 Py_DECREF(dct);
134 if (!TemplateProxy_Check(attr)) {
135 TemplateProxy* pytmpl = TemplateProxy_New(mtCppName, mtName, pyclass);
136 if (CPPOverload_Check(attr)) pytmpl->MergeOverload((CPPOverload*)attr);
137 PyType_Type.tp_setattro(pyclass, pyname, (PyObject*)pytmpl);
138 Py_DECREF(pytmpl);
139 }
140 Py_XDECREF(attr);
141 Py_DECREF(pyname);
142}
143
145{
146// Collect methods and data for the given scope, and add them to the given python
147// proxy object.
148
149// some properties that'll affect building the dictionary
150 bool isNamespace = Cppyy::IsNamespace(scope);
151 bool isAbstract = Cppyy::IsAbstract(scope);
152 bool hasConstructor = false;
154
155// load all public methods and data members
156 typedef std::vector<PyCallable*> Callables_t;
157 typedef std::map<std::string, Callables_t> CallableCache_t;
158 CallableCache_t cache;
159
160// bypass custom __getattr__ for efficiency
161 getattrofunc oldgetattro = Py_TYPE(pyclass)->tp_getattro;
162 Py_TYPE(pyclass)->tp_getattro = PyType_Type.tp_getattro;
163
164// functions in namespaces are properly found through lazy lookup, so do not
165// create them until needed (the same is not true for data members)
166 const Cppyy::TCppIndex_t nMethods = isNamespace ? 0 : Cppyy::GetNumMethods(scope);
167 for (Cppyy::TCppIndex_t imeth = 0; imeth < nMethods; ++imeth) {
168 Cppyy::TCppMethod_t method = Cppyy::GetMethod(scope, imeth);
169
170 // process the method based on its name
171 std::string mtCppName = Cppyy::GetMethodName(method);
172
173 // special case trackers
174 bool setupSetItem = false;
175 bool isConstructor = Cppyy::IsConstructor(method);
176 bool isTemplate = isConstructor ? false : Cppyy::IsMethodTemplate(scope, imeth);
177
178 // filter empty names (happens for namespaces, is bug?)
179 if (mtCppName == "")
180 continue;
181
182 // filter C++ destructors
183 if (mtCppName[0] == '~')
184 continue;
185
186 // translate operators
187 std::string mtName = Utility::MapOperatorName(mtCppName, Cppyy::GetMethodNumArgs(method));
188 if (mtName.empty())
189 continue;
190
191 // operator[]/() returning a reference type will be used for __setitem__
192 bool isCall = mtName == "__call__";
193 if (isCall || mtName == "__getitem__") {
194 const std::string& qual_return = Cppyy::ResolveName(Cppyy::GetMethodResultType(method));
195 const std::string& cpd = Utility::Compound(qual_return);
196 if (!cpd.empty() && cpd[cpd.size()- 1] == '&' && \
197 qual_return.find("const", 0, 5) == std::string::npos) {
198 if (isCall && !potGetItem) potGetItem = method;
199 setupSetItem = true; // will add methods as overloads
200 } else if (isCall) {
201 // not a non-const by-ref return, thus better __getitem__ candidate
202 potGetItem = method;
203 }
204 }
205
206 // do not expose private methods as the Cling wrappers for them won't compile
207 if (!Cppyy::IsPublicMethod(method))
208 continue;
209
210 // template members; handled by adding a dispatcher to the class
211 bool storeOnTemplate =
212 isTemplate ? true : (!isConstructor && Cppyy::ExistsMethodTemplate(scope, mtCppName));
213 if (storeOnTemplate) {
214 sync_templates(pyclass, mtCppName, mtName);
215 // continue processing to actually add the method so that the proxy can find
216 // it on the class when called explicitly
217 }
218
219 // construct the holder
220 PyCallable* pycall = nullptr;
221 if (Cppyy::IsStaticMethod(method)) // class method
222 pycall = new CPPClassMethod(scope, method);
223 else if (isNamespace) // free function
224 pycall = new CPPFunction(scope, method);
225 else if (isConstructor) { // ctor
226 mtName = "__init__";
227 hasConstructor = true;
228 if (!isAbstract)
229 pycall = new CPPConstructor(scope, method);
230 else
231 pycall = new CPPAbstractClassConstructor(scope, method);
232 } else // member function
233 pycall = new CPPMethod(scope, method);
234
235 if (storeOnTemplate) {
236 // template proxy was already created in sync_templates call above, so
237 // add only here, not to the cache of collected methods
238 PyObject* attr = PyObject_GetAttrString(pyclass, const_cast<char*>(mtName.c_str()));
239 if (isTemplate) ((TemplateProxy*)attr)->AdoptTemplate(pycall);
240 else ((TemplateProxy*)attr)->AdoptMethod(pycall);
241 Py_DECREF(attr);
242
243 // for operator[]/() that returns by ref, also add __setitem__
244 if (setupSetItem) {
245 TemplateProxy* pysi = (TemplateProxy*)PyObject_GetAttrString(pyclass, const_cast<char*>("__setitem__"));
246 if (!pysi) {
247 pysi = TemplateProxy_New(mtCppName, "__setitem__", pyclass);
248 PyObject_SetAttrString(pyclass, const_cast<char*>("__setitem__"), (PyObject*)pysi);
249 }
250 if (isTemplate) pysi->AdoptTemplate(new CPPSetItem(scope, method));
251 else pysi->AdoptMethod(new CPPSetItem(scope, method));
252 Py_XDECREF(pysi);
253 }
254
255 } else {
256 // lookup method dispatcher and store method
257 Callables_t& md = (*(cache.insert(
258 std::make_pair(mtName, Callables_t())).first)).second;
259 md.push_back(pycall);
260
261 // special case for operator[]/() that returns by ref, use for getitem/call and setitem
262 if (setupSetItem) {
263 Callables_t& setitem = (*(cache.insert(
264 std::make_pair(std::string("__setitem__"), Callables_t())).first)).second;
265 setitem.push_back(new CPPSetItem(scope, method));
266 }
267 }
268 }
269
270// add proxies for un-instantiated/non-overloaded templated methods
271 const Cppyy::TCppIndex_t nTemplMethods = isNamespace ? 0 : Cppyy::GetNumTemplatedMethods(scope);
272 for (Cppyy::TCppIndex_t imeth = 0; imeth < nTemplMethods; ++imeth) {
273 const std::string mtCppName = Cppyy::GetTemplatedMethodName(scope, imeth);
274 // the number of arguments isn't known until instantiation and as far as C++ is concerned, all
275 // same-named operators are simply overloads; so will pre-emptively add both names if with and
276 // without arguments differ, letting the normal overload mechanism resolve on call
277 bool isConstructor = Cppyy::IsTemplatedConstructor(scope, imeth);
278
279 // first add with no arguments
280 std::string mtName0 = isConstructor ? "__init__" : Utility::MapOperatorName(mtCppName, false);
281 sync_templates(pyclass, mtCppName, mtName0);
282
283 // then add when taking arguments, if this method is different
284 if (!isConstructor) {
285 std::string mtName1 = Utility::MapOperatorName(mtCppName, true);
286 if (mtName0 != mtName1)
287 sync_templates(pyclass, mtCppName, mtName1);
288 }
289 }
290
291// add a pseudo-default ctor, if none defined
292 if (!hasConstructor) {
293 PyCallable* defctor = nullptr;
294 if (isAbstract)
295 defctor = new CPPAbstractClassConstructor(scope, (Cppyy::TCppMethod_t)0);
296 else if (isNamespace)
297 defctor = new CPPNamespaceConstructor(scope, (Cppyy::TCppMethod_t)0);
299 ((CPPScope*)pyclass)->fFlags |= CPPScope::kIsInComplete;
300 defctor = new CPPIncompleteClassConstructor(scope, (Cppyy::TCppMethod_t)0);
301 } else
302 defctor = new CPPConstructor(scope, (Cppyy::TCppMethod_t)0);
303 cache["__init__"].push_back(defctor);
304 }
305
306// map __call__ to __getitem__ if also mapped to __setitem__
307 if (potGetItem) {
308 Callables_t& getitem = (*(cache.insert(
309 std::make_pair(std::string("__getitem__"), Callables_t())).first)).second;
310 getitem.push_back(new CPPGetItem(scope, potGetItem));
311 }
312
313// add the methods to the class dictionary
314 PyObject* dct = PyObject_GetAttr(pyclass, PyStrings::gDict);
315 for (CallableCache_t::iterator imd = cache.begin(); imd != cache.end(); ++imd) {
316 // in order to prevent removing templated editions of this method (which were set earlier,
317 // above, as a different proxy object), we'll check and add this method flagged as a generic
318 // one (to be picked up by the templated one as appropriate) if a template exists
319 PyObject* pyname = CPyCppyy_PyText_FromString(const_cast<char*>(imd->first.c_str()));
320 PyObject* attr = PyObject_GetItem(dct, pyname);
321 Py_DECREF(pyname);
322 if (TemplateProxy_Check(attr)) {
323 // template exists, supply it with the non-templated method overloads
324 for (auto cit : imd->second)
325 ((TemplateProxy*)attr)->AdoptMethod(cit);
326 } else {
327 if (!attr) PyErr_Clear();
328 // normal case, add a new method
329 CPPOverload* method = CPPOverload_New(imd->first, imd->second);
330 PyObject* pymname = CPyCppyy_PyText_InternFromString(const_cast<char*>(method->GetName().c_str()));
331 PyType_Type.tp_setattro(pyclass, pymname, (PyObject*)method);
332 Py_DECREF(pymname);
333 Py_DECREF(method);
334 }
335
336 Py_XDECREF(attr); // could have been found in base class or non-existent
337 }
338 Py_DECREF(dct);
339
340 // collect data members (including enums)
341 const Cppyy::TCppIndex_t nDataMembers = Cppyy::GetNumDatamembers(scope);
342 for (Cppyy::TCppIndex_t idata = 0; idata < nDataMembers; ++idata) {
343 // allow only public members
344 if (!Cppyy::IsPublicData(scope, idata))
345 continue;
346
347 // enum datamembers (this in conjunction with previously collected enums above)
348 if (Cppyy::IsEnumData(scope, idata) && Cppyy::IsStaticData(scope, idata)) {
349 // some implementation-specific data members have no address: ignore them
350 if (!Cppyy::GetDatamemberOffset(scope, idata))
351 continue;
352
353 // two options: this is a static variable, or it is the enum value, the latter
354 // already exists, so check for it and move on if set
355 PyObject* eset = PyObject_GetAttrString(pyclass,
356 const_cast<char*>(Cppyy::GetDatamemberName(scope, idata).c_str()));
357 if (eset) {
358 Py_DECREF(eset);
359 continue;
360 }
361
362 PyErr_Clear();
363
364 // it could still be that this is an anonymous enum, which is not in the list
365 // provided by the class
366 if (strstr(Cppyy::GetDatamemberType(scope, idata).c_str(), "(anonymous)") != 0) {
367 AddPropertyToClass(pyclass, scope, idata);
368 continue;
369 }
370 }
371
372 // properties (aka public (static) data members)
373 AddPropertyToClass(pyclass, scope, idata);
374 }
375
376// restore custom __getattr__
377 Py_TYPE(pyclass)->tp_getattro = oldgetattro;
378
379// all ok, done
380 return 0;
381}
382
383//----------------------------------------------------------------------------
384static void CollectUniqueBases(Cppyy::TCppType_t klass, std::deque<std::string>& uqb)
385{
386// collect bases in acceptable mro order, while removing duplicates (this may
387// break the overload resolution in esoteric cases, but otherwise the class can
388// not be used at all, as CPython will refuse the mro).
389 size_t nbases = Cppyy::GetNumBases(klass);
390
391 std::deque<Cppyy::TCppType_t> bids;
392 for (size_t ibase = 0; ibase < nbases; ++ibase) {
393 const std::string& name = Cppyy::GetBaseName(klass, ibase);
394 int decision = 2;
396 if (!tp) continue; // means this base with not be available Python-side
397 for (size_t ibase2 = 0; ibase2 < uqb.size(); ++ibase2) {
398 if (uqb[ibase2] == name) { // not unique ... skip
399 decision = 0;
400 break;
401 }
402
403 if (Cppyy::IsSubtype(tp, bids[ibase2])) {
404 // mro requirement: sub-type has to follow base
405 decision = 1;
406 break;
407 }
408 }
409
410 if (decision == 1) {
411 uqb.push_front(name);
412 bids.push_front(tp);
413 } else if (decision == 2) {
414 uqb.push_back(name);
415 bids.push_back(tp);
416 }
417 // skipped if decision == 0 (not unique)
418 }
419}
420
422{
423// Build a tuple of python proxy classes of all the bases of the given 'klass'.
424 std::deque<std::string> uqb;
425 CollectUniqueBases(klass, uqb);
426
427// allocate a tuple for the base classes, special case for first base
428 size_t nbases = uqb.size();
429
430 PyObject* pybases = PyTuple_New(nbases ? nbases : 1);
431 if (!pybases)
432 return nullptr;
433
434// build all the bases
435 if (nbases == 0) {
436 Py_INCREF((PyObject*)(void*)&CPPInstance_Type);
437 PyTuple_SET_ITEM(pybases, 0, (PyObject*)(void*)&CPPInstance_Type);
438 } else {
439 for (std::deque<std::string>::size_type ibase = 0; ibase < nbases; ++ibase) {
440 PyObject* pyclass = CreateScopeProxy(uqb[ibase]);
441 if (!pyclass) {
442 Py_DECREF(pybases);
443 return nullptr;
444 }
445
446 PyTuple_SET_ITEM(pybases, ibase, pyclass);
447 }
448
449 // special case, if true python types enter the hierarchy, make sure that
450 // the first base seen is still the CPPInstance_Type
451 if (!PyObject_IsSubclass(PyTuple_GET_ITEM(pybases, 0), (PyObject*)&CPPInstance_Type)) {
452 PyObject* newpybases = PyTuple_New(nbases+1);
453 Py_INCREF((PyObject*)(void*)&CPPInstance_Type);
454 PyTuple_SET_ITEM(newpybases, 0, (PyObject*)(void*)&CPPInstance_Type);
455 for (int ibase = 0; ibase < (int)nbases; ++ibase) {
456 PyObject* pyclass = PyTuple_GET_ITEM(pybases, ibase);
457 Py_INCREF(pyclass);
458 PyTuple_SET_ITEM(newpybases, ibase+1, pyclass);
459 }
460 Py_DECREF(pybases);
461 pybases = newpybases;
462 }
463 }
464
465 return pybases;
466}
467
468} // namespace CPyCppyy
469
470//----------------------------------------------------------------------------
472{
473// Retrieve scope proxy from the known ones.
474 PyClassMap_t::iterator pci = gPyClasses.find(scope);
475 if (pci != gPyClasses.end()) {
476 PyObject* pyclass = PyWeakref_GetObject(pci->second);
477 if (pyclass != Py_None) {
478 Py_INCREF(pyclass);
479 return pyclass;
480 }
481 }
482
483 return nullptr;
484}
485
486//----------------------------------------------------------------------------
488{
489// Convenience function with a lookup first through the known existing proxies.
490 PyObject* pyclass = GetScopeProxy(scope);
491 if (pyclass)
492 return pyclass;
493
495}
496
497//----------------------------------------------------------------------------
499{
500// Build a python shadow class for the named C++ class.
501 std::string cname = CPyCppyy_PyText_AsString(PyTuple_GetItem(args, 0));
502 if (PyErr_Occurred())
503 return nullptr;
504
505 return CreateScopeProxy(cname);
506}
507
508//----------------------------------------------------------------------------
510{
511// Build a python shadow class for the named C++ class or namespace.
512
513// determine complete scope name, if a python parent has been given
514 std::string scName = "";
515 if (parent) {
516 if (CPPScope_Check(parent))
517 scName = Cppyy::GetScopedFinalName(((CPPScope*)parent)->fCppType);
518 else {
519 PyObject* parname = PyObject_GetAttr(parent, PyStrings::gName);
520 if (!parname) {
521 PyErr_Format(PyExc_SystemError, "given scope has no name for %s", name.c_str());
522 return nullptr;
523 }
524
525 // should be a string
526 scName = CPyCppyy_PyText_AsString(parname);
527 Py_DECREF(parname);
528 if (PyErr_Occurred())
529 return nullptr;
530 }
531
532 // accept this parent scope and use it's name for prefixing
533 Py_INCREF(parent);
534 }
535
536// retrieve C++ class (this verifies name, and is therefore done first)
537 const std::string& lookup = scName.empty() ? name : (scName+"::"+name);
538 Cppyy::TCppScope_t klass = Cppyy::GetScope(lookup);
539
540 if (!(bool)klass && Cppyy::IsTemplate(lookup)) {
541 // a "naked" templated class is requested: return callable proxy for instantiations
542 PyObject* pytcl = PyObject_GetAttr(gThisModule, PyStrings::gTemplate);
543 PyObject* pytemplate = PyObject_CallFunction(
544 pytcl, const_cast<char*>("s"), const_cast<char*>(lookup.c_str()));
545 Py_DECREF(pytcl);
546
547 // cache the result
548 AddScopeToParent(parent ? parent : gThisModule, name, pytemplate);
549
550 // done, next step should be a call into this template
551 Py_XDECREF(parent);
552 return pytemplate;
553 }
554
555 if (!(bool)klass) {
556 // could be an enum, which are treated seperately in CPPScope (TODO: maybe they
557 // should be handled here instead anyway??)
558 if (Cppyy::IsEnum(lookup))
559 return nullptr;
560
561 // final possibility is a typedef of a builtin; these are mapped on the python side
562 std::string resolved = Cppyy::ResolveName(lookup);
563 if (gPyTypeMap) {
564 PyObject* tc = PyDict_GetItemString(gPyTypeMap, resolved.c_str()); // borrowed
565 if (tc && PyCallable_Check(tc)) {
566 PyObject* nt = PyObject_CallFunction(tc, (char*)"ss", name.c_str(), scName.c_str());
567 if (nt) {
568 if (parent) {
569 AddScopeToParent(parent, name, nt);
570 Py_DECREF(parent);
571 }
572 return nt;
573 }
574 PyErr_Clear();
575 }
576 }
577
578 // all options have been exhausted: it doesn't exist as such
579 PyErr_Format(PyExc_TypeError, "\'%s\' is not a known C++ class", lookup.c_str());
580 Py_XDECREF(parent);
581 return nullptr;
582 }
583
584// locate class by ID, if possible, to prevent parsing scopes/templates anew
585 PyObject* pyscope = GetScopeProxy(klass);
586 if (pyscope) {
587 if (parent) {
588 AddScopeToParent(parent, name, pyscope);
589 Py_DECREF(parent);
590 }
591 return pyscope;
592 }
593
594// now have a class ... get the actual, fully scoped class name, so that typedef'ed
595// classes are created in the right place
596 const std::string& actual = Cppyy::GetScopedFinalName(klass);
597 if (actual != lookup) {
598 pyscope = CreateScopeProxy(actual);
599 if (!pyscope) PyErr_Clear();
600 }
601
602// locate the parent, if necessary, for memoizing the class if not specified
603 std::string::size_type last = 0;
604 if (!parent) {
605 // TODO: move this to TypeManip, which already has something similar in
606 // the form of 'extract_namespace'
607 // need to deal with template parameters that can have scopes themselves
608 int tpl_open = 0;
609 for (std::string::size_type pos = 0; pos < name.size(); ++pos) {
610 std::string::value_type c = name[pos];
611
612 // count '<' and '>' to be able to skip template contents
613 if (c == '<')
614 ++tpl_open;
615 else if (c == '>')
616 --tpl_open;
617
618 // by only checking for "::" the last part (class name) is dropped
619 else if (tpl_open == 0 && \
620 c == ':' && pos+1 < name.size() && name[ pos+1 ] == ':') {
621 // found a new scope part
622 const std::string& part = name.substr(last, pos-last);
623
624 PyObject* next = PyObject_GetAttrString(
625 parent ? parent : gThisModule, const_cast<char*>(part.c_str()));
626
627 if (!next) { // lookup failed, try to create it
628 PyErr_Clear();
629 next = CreateScopeProxy(part, parent);
630 }
631 Py_XDECREF(parent);
632
633 if (!next) // create failed, give up
634 return nullptr;
635
636 // found scope part
637 parent = next;
638
639 // done with part (note that pos is moved one ahead here)
640 last = pos+2; ++pos;
641 }
642 }
643
644 if (parent && !CPPScope_Check(parent)) {
645 // Special case: parent found is not one of ours (it's e.g. a pure Python module), so
646 // continuing would fail badly. One final lookup, then out of here ...
647 std::string unscoped = name.substr(last, std::string::npos);
648 PyObject* ret = PyObject_GetAttrString(parent, unscoped.c_str());
649 Py_DECREF(parent);
650 return ret;
651 }
652 }
653
654// use the module as a fake scope if no outer scope found
655 if (!parent) {
656 Py_INCREF(gThisModule);
657 parent = gThisModule;
658 }
659
660// if the scope was earlier found as actual, then we're done already, otherwise
661// build a new scope proxy
662 if (!pyscope) {
663 // construct the base classes
664 PyObject* pybases = BuildCppClassBases(klass);
665 if (pybases != 0) {
666 // create a fresh Python class, given bases, name, and empty dictionary
667 pyscope = CreateNewCppProxyClass(klass, pybases);
668 Py_DECREF(pybases);
669 }
670
671 // fill the dictionary, if successful
672 if (pyscope) {
673 if (BuildScopeProxyDict(klass, pyscope)) {
674 // something failed in building the dictionary
675 Py_DECREF(pyscope);
676 pyscope = nullptr;
677 }
678 }
679
680 // store a ref from cppyy scope id to new python class
681 if (pyscope && !(((CPPScope*)pyscope)->fFlags & CPPScope::kIsInComplete)) {
682 gPyClasses[klass] = PyWeakref_NewRef(pyscope, nullptr);
683
684 if (!(((CPPScope*)pyscope)->fFlags & CPPScope::kIsNamespace)) {
685 // add python-style features to classes only
686 if (!Pythonize(pyscope, Cppyy::GetScopedFinalName(klass))) {
687 Py_DECREF(pyscope);
688 pyscope = nullptr;
689 }
690 } else {
691 // add to sys.modules to allow importing from this namespace
692 PyObject* pyfullname = PyObject_GetAttr(pyscope, PyStrings::gModule);
694 CPyCppyy_PyText_AppendAndDel(&pyfullname, PyObject_GetAttr(pyscope, PyStrings::gName));
695 PyObject* modules = PySys_GetObject(const_cast<char*>("modules"));
696 if (modules && PyDict_Check(modules))
697 PyDict_SetItem(modules, pyfullname, pyscope);
698 Py_DECREF(pyfullname);
699 }
700 }
701 }
702
703// store on parent if found/created and complete
704 if (pyscope && !(((CPPScope*)pyscope)->fFlags & CPPScope::kIsInComplete))
705 AddScopeToParent(parent, name, pyscope);
706 Py_DECREF(parent);
707
708// all done
709 return pyscope;
710}
711
712
713//----------------------------------------------------------------------------
715{
716// To allow use of C++ exceptions in lieue of Python exceptions, they need to
717// derive from BaseException, which can not mix with the normal CPPInstance and
718// use of the meta-class. Instead, encapsulate them in a forwarding class that
719// derives from Pythons Exception class
720
721// start with creation of CPPExcInstance type base classes
722 std::deque<std::string> uqb;
723 CollectUniqueBases(((CPPScope*)pyscope)->fCppType, uqb);
724 size_t nbases = uqb.size();
725
726// Support for multiple bases actually can not actually work as-is: the reason
727// for deriving from BaseException is to guarantee the layout needed for storing
728// traces. If some other base is std::exception (as e.g. boost::bad_any_cast) or
729// also derives from std::exception, then there are two trace locations. OTOH,
730// if the other class is a non-exception type, then the exception class does not
731// need to derive from it because it can never be caught as that type forwarding
732// to the proxy will work as expected, through, which is good enough).
733//
734// The code below restricts the hierarchy to a single base class, picking the
735// "best" by filtering std::exception and non-exception bases.
736
737 PyObject* pybases = PyTuple_New(1);
738 if (nbases == 0) {
739 Py_INCREF((PyObject*)(void*)&CPPExcInstance_Type);
740 PyTuple_SET_ITEM(pybases, 0, (PyObject*)(void*)&CPPExcInstance_Type);
741 } else {
742 PyObject* best_base = nullptr;
743
744 for (std::deque<std::string>::size_type ibase = 0; ibase < nbases; ++ibase) {
745 // retrieve bases through their enclosing scope to guarantee treatment as
746 // exception classes and proper caching
747 const std::string& finalname = Cppyy::GetScopedFinalName(Cppyy::GetScope(uqb[ibase]));
748 const std::string& parentname = TypeManip::extract_namespace(finalname);
749 PyObject* base_parent = CreateScopeProxy(parentname);
750 if (!base_parent) {
751 Py_DECREF(pybases);
752 return nullptr;
753 }
754
755 PyObject* excbase = PyObject_GetAttrString(base_parent,
756 parentname.empty() ? finalname.c_str() : finalname.substr(parentname.size()+2, std::string::npos).c_str());
757 Py_DECREF(base_parent);
758 if (!excbase) {
759 Py_DECREF(pybases);
760 return nullptr;
761 }
762
763 if (PyType_IsSubtype((PyTypeObject*)excbase, &CPPExcInstance_Type)) {
764 Py_XDECREF(best_base);
765 best_base = excbase;
766 if (finalname != "std::exception")
767 break;
768 } else {
769 // just skip: there will be at least one exception derived base class
770 Py_DECREF(excbase);
771 }
772 }
773
774 PyTuple_SET_ITEM(pybases, 0, best_base);
775 }
776
777 PyObject* args = Py_BuildValue((char*)"OO{}", pyname, pybases);
778
779// meta-class attributes (__cpp_name__, etc.) can not be resolved lazily so add
780// them directly instead in case they are needed
781 PyObject* dct = PyTuple_GET_ITEM(args, 2);
782 PyDict_SetItem(dct, PyStrings::gUnderlying, pyscope);
783 PyDict_SetItem(dct, PyStrings::gName, PyObject_GetAttr(pyscope, PyStrings::gName));
784 PyDict_SetItem(dct, PyStrings::gCppName, PyObject_GetAttr(pyscope, PyStrings::gCppName));
785 PyDict_SetItem(dct, PyStrings::gModule, PyObject_GetAttr(pyscope, PyStrings::gModule));
786
787// create the actual exception class
788 PyObject* exc_pyscope = PyType_Type.tp_new(&PyType_Type, args, nullptr);
789 Py_DECREF(args);
790 Py_DECREF(pybases);
791
792// cache the result for future lookups and return
793 PyType_Type.tp_setattro(parent, pyname, exc_pyscope);
794 return exc_pyscope;
795}
796
797
798//----------------------------------------------------------------------------
800 Cppyy::TCppType_t klass, const unsigned flags)
801{
802// only known or knowable objects will be bound (null object is ok)
803 if (!klass) {
804 PyErr_SetString(PyExc_TypeError, "attempt to bind C++ object w/o class");
805 return nullptr;
806 }
807
808// retrieve python class
809 PyObject* pyclass = CreateScopeProxy(klass);
810 if (!pyclass)
811 return nullptr; // error has been set in CreateScopeProxy
812
813 bool isRef = flags & CPPInstance::kIsReference;
814 bool isValue = flags & CPPInstance::kIsValue;
815
816// TODO: make sure that a consistent address is used (may have to be done in BindCppObject)
817 if (address && !isValue /* always fresh */ && !(flags & (CPPInstance::kNoWrapConv|CPPInstance::kNoMemReg))) {
818 PyObject* oldPyObject = MemoryRegulator::RetrievePyObject(
819 isRef ? *(void**)address : address, pyclass);
820
821 // ptr-ptr requires old object to be a reference to enable re-use
822 if (oldPyObject && (!(flags & CPPInstance::kIsPtrPtr) ||
823 ((CPPInstance*)oldPyObject)->fFlags & CPPInstance::kIsReference)) {
824 return oldPyObject;
825 }
826 }
827
828// if smart, instantiate a Python-side object of the underlying type, carrying the smartptr
829 PyObject* smart_type = (flags != CPPInstance::kNoWrapConv && (((CPPClass*)pyclass)->fFlags & CPPScope::kIsSmart)) ? pyclass : nullptr;
830 if (smart_type) {
831 pyclass = CreateScopeProxy(((CPPSmartClass*)smart_type)->fUnderlyingType);
832 if (!pyclass) {
833 // simply restore and expose as the actual smart pointer class
834 pyclass = smart_type;
835 smart_type = nullptr;
836 }
837 }
838
839// instantiate an object of this class
840 PyObject* args = PyTuple_New(0);
841 CPPInstance* pyobj =
842 (CPPInstance*)((PyTypeObject*)pyclass)->tp_new((PyTypeObject*)pyclass, args, nullptr);
843 Py_DECREF(args);
844
845// bind, register and return if successful
846 if (pyobj != 0) { // fill proxy value?
847 unsigned objflags =
848 (isRef ? CPPInstance::kIsReference : 0) | (isValue ? CPPInstance::kIsValue : 0) | (flags & CPPInstance::kIsOwner);
849 pyobj->Set(address, (CPPInstance::EFlags)objflags);
850
851 if (smart_type)
852 pyobj->SetSmart(smart_type);
853
854 // do not register null pointers, references (?), or direct usage of smart pointers or iterators
855 if (address && !isRef && !(flags & (CPPInstance::kNoWrapConv|CPPInstance::kNoMemReg)))
856 MemoryRegulator::RegisterPyObject(pyobj, pyobj->GetObject());
857 }
858
859// successful completion; wrap exception options to make them raiseable, normal return otherwise
860 if (((CPPClass*)pyclass)->fFlags & CPPScope::kIsException) {
861 PyObject* exc_obj = CPPExcInstance_Type.tp_new(&CPPExcInstance_Type, nullptr, nullptr);
862 ((CPPExcInstance*)exc_obj)->fCppInstance = (PyObject*)pyobj;
863 Py_DECREF(pyclass);
864 return exc_obj;
865 }
866
867 Py_DECREF(pyclass);
868
869 return (PyObject*)pyobj;
870}
871
872//----------------------------------------------------------------------------
874 Cppyy::TCppType_t klass, const unsigned flags)
875{
876// if the object is a null pointer, return a typed one (as needed for overloading)
877 if (!address)
878 return BindCppObjectNoCast(address, klass, flags);
879
880// only known or knowable objects will be bound
881 if (!klass) {
882 PyErr_SetString(PyExc_TypeError, "attempt to bind C++ object w/o class");
883 return nullptr;
884 }
885
886 bool isRef = flags & CPPInstance::kIsReference;
887
888// get actual class for recycling checking and/or downcasting
889 Cppyy::TCppType_t clActual = isRef ? 0 : Cppyy::GetActualClass(klass, address);
890
891// downcast to real class for object returns, unless pinned
892 if (clActual && klass != clActual) {
893 auto pci = gPinnedTypes.find(klass);
894 if (pci == gPinnedTypes.end()) {
895 intptr_t offset = Cppyy::GetBaseOffset(
896 clActual, klass, address, -1 /* down-cast */, true /* report errors */);
897 if (offset != -1) { // may fail if clActual not fully defined
898 address = (void*)((intptr_t)address + offset);
899 klass = clActual;
900 }
901 }
902 }
903
904// actual binding (returned object may be zero w/ a python exception set)
905 return BindCppObjectNoCast(address, klass, flags);
906}
907
908//----------------------------------------------------------------------------
910 Cppyy::TCppObject_t address, Cppyy::TCppType_t klass, dims_t dims)
911{
912// TODO: this function exists for symmetry; need to figure out if it's useful
913 return TupleOfInstances_New(address, klass, dims[0], dims+1);
914}
#define Py_TYPE(ob)
Definition: CPyCppyy.h:209
#define CPyCppyy_PyText_InternFromString
Definition: CPyCppyy.h:103
#define CPyCppyy_PyText_AsString
Definition: CPyCppyy.h:97
#define CPyCppyy_PyText_AppendAndDel
Definition: CPyCppyy.h:105
#define CPyCppyy_PyText_FromString
Definition: CPyCppyy.h:102
Cppyy::TCppType_t fUnderlyingType
unsigned int fFlags
static PyClassMap_t gPyClasses
std::map< Cppyy::TCppScope_t, PyObject * > PyClassMap_t
_object PyObject
Definition: PyMethodBase.h:41
#define c(i)
Definition: RSha256.hxx:101
@ kIsReference
Definition: TDictionary.h:82
@ kIsNamespace
Definition: TDictionary.h:95
char name[80]
Definition: TGX11.cxx:109
#define pyname
Definition: TMCParticle.cxx:19
void Set(void *address, EFlags flags=kDefault)
Definition: CPPInstance.h:85
void SetSmart(PyObject *smart_type)
const std::string & GetName() const
Definition: CPPOverload.h:62
void AdoptTemplate(PyCallable *pc)
void AdoptMethod(PyCallable *pc)
void MergeOverload(CPPOverload *mp)
PyObject * gDict
Definition: PyStrings.cxx:14
PyObject * gName
Definition: PyStrings.cxx:26
PyObject * gCppName
Definition: PyStrings.cxx:10
PyObject * gTemplate
Definition: PyStrings.cxx:47
PyObject * gModule
Definition: PyStrings.cxx:24
PyObject * gUnderlying
Definition: PyStrings.cxx:31
std::string extract_namespace(const std::string &name)
Definition: TypeManip.cxx:159
std::string MapOperatorName(const std::string &name, bool bTakesParames)
Definition: Utility.cxx:727
const std::string Compound(const std::string &name)
Definition: Utility.cxx:782
CPPOverload * CPPOverload_New(const std::string &name, std::vector< PyCallable * > &methods)
Definition: CPPOverload.h:91
PyTypeObject CPPInstance_Type
PyTypeObject CPPExcInstance_Type
PyObject * GetScopeProxy(Cppyy::TCppScope_t)
static PyObject * CreateNewCppProxyClass(Cppyy::TCppScope_t klass, PyObject *pybases)
bool Pythonize(PyObject *pyclass, const std::string &name)
Definition: Pythonize.cxx:1001
std::set< Cppyy::TCppType_t > gPinnedTypes
PyObject * BindCppObjectNoCast(Cppyy::TCppObject_t object, Cppyy::TCppType_t klass, const unsigned flags=0)
bool CPPOverload_Check(T *object)
Definition: CPPOverload.h:79
PyObject * TupleOfInstances_New(Cppyy::TCppObject_t address, Cppyy::TCppType_t klass, dim_t ndims, dims_t dims)
bool CPPScope_Check(T *object)
Definition: CPPScope.h:76
static int BuildScopeProxyDict(Cppyy::TCppScope_t scope, PyObject *pyclass)
static void sync_templates(PyObject *pyclass, const std::string &mtCppName, const std::string &mtName)
static PyObject * BuildCppClassBases(Cppyy::TCppType_t klass)
static void AddScopeToParent(PyObject *parent, const std::string &name, PyObject *newscope)
CPPScope * CPPScopeMeta_New(Cppyy::TCppScope_t klass, PyObject *args)
Definition: CPPScope.h:88
CPPDataMember * CPPDataMember_New(Cppyy::TCppScope_t scope, Cppyy::TCppIndex_t idata)
Definition: CPPDataMember.h:52
PyObject * CreateExcScopeProxy(PyObject *pyscope, PyObject *pyname, PyObject *parent)
PyObject * CreateScopeProxy(Cppyy::TCppScope_t)
static void AddPropertyToClass(PyObject *pyclass, Cppyy::TCppScope_t scope, Cppyy::TCppIndex_t idata)
PyObject * gThisModule
Definition: API.cxx:32
PyObject * BindCppObject(Cppyy::TCppObject_t object, Cppyy::TCppType_t klass, const unsigned flags=0)
PyObject * gPyTypeMap
bool TemplateProxy_Check(T *object)
Definition: TemplateProxy.h:79
TemplateProxy * TemplateProxy_New(const std::string &cppname, const std::string &pyname, PyObject *pyclass)
Definition: TemplateProxy.h:91
static void CollectUniqueBases(Cppyy::TCppType_t klass, std::deque< std::string > &uqb)
PyObject * BindCppObjectArray(Cppyy::TCppObject_t address, Cppyy::TCppType_t klass, Py_ssize_t *dims)
size_t TCppIndex_t
Definition: cpp_cppyy.h:24
RPY_EXPORTED TCppType_t GetActualClass(TCppType_t klass, TCppObject_t obj)
RPY_EXPORTED ptrdiff_t GetBaseOffset(TCppType_t derived, TCppType_t base, TCppObject_t address, int direction, bool rerror=false)
intptr_t TCppMethod_t
Definition: cpp_cppyy.h:22
RPY_EXPORTED std::string GetTemplatedMethodName(TCppScope_t scope, TCppIndex_t imeth)
void * TCppObject_t
Definition: cpp_cppyy.h:21
RPY_EXPORTED bool IsPublicData(TCppScope_t scope, TCppIndex_t idata)
RPY_EXPORTED intptr_t GetDatamemberOffset(TCppScope_t scope, TCppIndex_t idata)
RPY_EXPORTED bool IsTemplate(const std::string &template_name)
TCppScope_t TCppType_t
Definition: cpp_cppyy.h:19
RPY_EXPORTED bool IsComplete(const std::string &type_name)
RPY_EXPORTED bool IsEnum(const std::string &type_name)
RPY_EXPORTED std::string GetDatamemberName(TCppScope_t scope, TCppIndex_t idata)
RPY_EXPORTED bool IsEnumData(TCppScope_t scope, TCppIndex_t idata)
RPY_EXPORTED bool IsMethodTemplate(TCppScope_t scope, TCppIndex_t imeth)
RPY_EXPORTED TCppIndex_t GetNumTemplatedMethods(TCppScope_t scope)
RPY_EXPORTED bool IsConstructor(TCppMethod_t method)
RPY_EXPORTED bool IsStaticData(TCppScope_t scope, TCppIndex_t idata)
RPY_EXPORTED TCppMethod_t GetMethod(TCppScope_t scope, TCppIndex_t imeth)
RPY_EXPORTED std::string GetFinalName(TCppType_t type)
RPY_EXPORTED bool IsPublicMethod(TCppMethod_t method)
RPY_EXPORTED bool IsStaticMethod(TCppMethod_t method)
RPY_EXPORTED std::string GetMethodName(TCppMethod_t)
RPY_EXPORTED TCppIndex_t GetNumBases(TCppType_t type)
RPY_EXPORTED bool IsTemplatedConstructor(TCppScope_t scope, TCppIndex_t imeth)
RPY_EXPORTED bool IsSubtype(TCppType_t derived, TCppType_t base)
size_t TCppScope_t
Definition: cpp_cppyy.h:18
RPY_EXPORTED std::string ResolveName(const std::string &cppitem_name)
RPY_EXPORTED std::string GetBaseName(TCppType_t type, TCppIndex_t ibase)
RPY_EXPORTED TCppIndex_t GetMethodNumArgs(TCppMethod_t)
RPY_EXPORTED std::string GetScopedFinalName(TCppType_t type)
RPY_EXPORTED bool IsNamespace(TCppScope_t scope)
RPY_EXPORTED TCppIndex_t GetNumDatamembers(TCppScope_t scope)
RPY_EXPORTED TCppScope_t GetScope(const std::string &scope_name)
RPY_EXPORTED std::string GetDatamemberType(TCppScope_t scope, TCppIndex_t idata)
RPY_EXPORTED std::string GetMethodResultType(TCppMethod_t)
RPY_EXPORTED bool ExistsMethodTemplate(TCppScope_t scope, const std::string &name)
RPY_EXPORTED TCppIndex_t GetNumMethods(TCppScope_t scope)
RPY_EXPORTED bool IsAbstract(TCppType_t type)