56 PyObject* pymetabases = PyTuple_New(PyTuple_GET_SIZE(pybases));
57 for (
int i = 0; i < PyTuple_GET_SIZE(pybases); ++i) {
60 PyTuple_SET_ITEM(pymetabases, i, btype);
66 PyObject* args = Py_BuildValue((
char*)
"sO{}", (
name+
"_meta").c_str(), pymetabases);
68 Py_DECREF(pymetabases);
82 args = Py_BuildValue((
char*)
"sO{}",
name.c_str(), pybases);
84 ((PyTypeObject*)pymeta)->tp_new((PyTypeObject*)pymeta, args,
nullptr);
100 PyType_Type.tp_setattro(pyclass, pname, (
PyObject*)property);
116 else PyObject_SetAttr(parent,
pyname, newscope);
127 PyObject* pyclass,
const std::string& mtCppName,
const std::string& mtName)
132 if (!attr) PyErr_Clear();
152 bool hasConstructor =
false;
156 typedef std::vector<PyCallable*> Callables_t;
157 typedef std::map<std::string, Callables_t> CallableCache_t;
158 CallableCache_t cache;
161 getattrofunc oldgetattro =
Py_TYPE(pyclass)->tp_getattro;
162 Py_TYPE(pyclass)->tp_getattro = PyType_Type.tp_getattro;
174 bool setupSetItem =
false;
183 if (mtCppName[0] ==
'~')
192 bool isCall = mtName ==
"__call__";
193 if (isCall || mtName ==
"__getitem__") {
196 if (!cpd.empty() && cpd[cpd.size()- 1] ==
'&' && \
197 qual_return.find(
"const", 0, 5) == std::string::npos) {
198 if (isCall && !potGetItem) potGetItem = method;
214 bool storeOnTemplate =
216 if (storeOnTemplate) {
226 else if (isNamespace)
228 else if (isConstructor) {
230 hasConstructor =
true;
238 if (storeOnTemplate) {
241 PyObject* attr = PyObject_GetAttrString(pyclass,
const_cast<char*
>(mtName.c_str()));
242 if (isTemplate) ((
TemplateProxy*)attr)->AdoptTemplate(pycall);
251 PyObject_SetAttrString(pyclass,
const_cast<char*
>(
"__setitem__"), (
PyObject*)pysi);
260 Callables_t& md = (*(cache.insert(
261 std::make_pair(mtName, Callables_t())).first)).second;
262 md.push_back(pycall);
266 Callables_t& setitem = (*(cache.insert(
267 std::make_pair(std::string(
"__setitem__"), Callables_t())).first)).second;
268 setitem.push_back(
new CPPSetItem(scope, method));
287 if (!isConstructor) {
289 if (mtName0 != mtName1)
295 if (!hasConstructor) {
299 else if (isNamespace)
306 cache[
"__init__"].push_back(defctor);
311 Callables_t& getitem = (*(cache.insert(
312 std::make_pair(std::string(
"__getitem__"), Callables_t())).first)).second;
313 getitem.push_back(
new CPPGetItem(scope, potGetItem));
318 for (CallableCache_t::iterator imd = cache.begin(); imd != cache.end(); ++imd) {
327 for (
auto cit : imd->second)
330 if (!attr) PyErr_Clear();
334 PyType_Type.tp_setattro(pyclass, pymname, (
PyObject*)method);
358 PyObject* eset = PyObject_GetAttrString(pyclass,
380 Py_TYPE(pyclass)->tp_getattro = oldgetattro;
394 std::deque<Cppyy::TCppType_t> bids;
395 for (
size_t ibase = 0; ibase < nbases; ++ibase) {
400 for (
size_t ibase2 = 0; ibase2 < uqb.size(); ++ibase2) {
401 if (uqb[ibase2] ==
name) {
414 uqb.push_front(
name);
416 }
else if (decision == 2) {
427 std::deque<std::string> uqb;
431 size_t nbases = uqb.size();
433 PyObject* pybases = PyTuple_New(nbases ? nbases : 1);
442 for (std::deque<std::string>::size_type ibase = 0; ibase < nbases; ++ibase) {
449 PyTuple_SET_ITEM(pybases, ibase, pyclass);
455 PyObject* newpybases = PyTuple_New(nbases+1);
458 for (
int ibase = 0; ibase < (
int)nbases; ++ibase) {
459 PyObject* pyclass = PyTuple_GET_ITEM(pybases, ibase);
461 PyTuple_SET_ITEM(newpybases, ibase+1, pyclass);
464 pybases = newpybases;
477 PyClassMap_t::iterator pci =
gPyClasses.find(scope);
479 PyObject* pyclass = PyWeakref_GetObject(pci->second);
480 if (pyclass != Py_None) {
505 if (PyErr_Occurred())
517 std::string scName =
"";
522 PyObject* parname = PyObject_GetAttr(parent, PyStrings::gName);
524 PyErr_Format(PyExc_SystemError,
"given scope has no name for %s",
name.c_str());
531 if (PyErr_Occurred())
540 const std::string& lookup = scName.empty() ?
name : (scName+
"::"+
name);
546 PyObject* pytemplate = PyObject_CallFunction(
547 pytcl,
const_cast<char*
>(
"s"),
const_cast<char*
>(lookup.c_str()));
568 if (tc && PyCallable_Check(tc)) {
569 PyObject* nt = PyObject_CallFunction(tc, (
char*)
"ss",
name.c_str(), scName.c_str());
582 PyErr_Format(PyExc_TypeError,
"\'%s\' is not a known C++ class", lookup.c_str());
600 if (actual != lookup) {
602 if (!pyscope) PyErr_Clear();
606 std::string::size_type last = 0;
612 for (std::string::size_type pos = 0; pos <
name.size(); ++pos) {
613 std::string::value_type
c =
name[pos];
622 else if (tpl_open == 0 && \
623 c ==
':' && pos+1 <
name.size() &&
name[ pos+1 ] ==
':') {
625 const std::string& part =
name.substr(last, pos-last);
627 PyObject* next = PyObject_GetAttrString(
628 parent ? parent :
gThisModule,
const_cast<char*
>(part.c_str()));
647 if (!parent &&
name.size() > 0) {
655 std::string unscoped =
name.substr(last, std::string::npos);
656 PyObject* ret = PyObject_GetAttrString(parent, unscoped.c_str());
689 if (pyscope && !(((
CPPScope*)pyscope)->
fFlags & CPPScope::kIsInComplete)) {
690 gPyClasses[klass] = PyWeakref_NewRef(pyscope,
nullptr);
700 PyObject* pyfullname = PyObject_GetAttr(pyscope, PyStrings::gModule);
703 PyObject* modules = PySys_GetObject(
const_cast<char*
>(
"modules"));
704 if (modules && PyDict_Check(modules))
705 PyDict_SetItem(modules, pyfullname, pyscope);
706 Py_DECREF(pyfullname);
712 if (pyscope && !(((
CPPScope*)pyscope)->
fFlags & CPPScope::kIsInComplete))
730 std::deque<std::string> uqb;
732 size_t nbases = uqb.size();
752 for (std::deque<std::string>::size_type ibase = 0; ibase < nbases; ++ibase) {
756 const std::string& parentname = TypeManip::extract_namespace(finalname);
763 PyObject* excbase = PyObject_GetAttrString(base_parent,
764 parentname.empty() ? finalname.c_str() : finalname.substr(parentname.size()+2, std::string::npos).c_str());
765 Py_DECREF(base_parent);
772 Py_XDECREF(best_base);
774 if (finalname !=
"std::exception")
782 PyTuple_SET_ITEM(pybases, 0, best_base);
789 PyObject* dct = PyTuple_GET_ITEM(args, 2);
790 PyDict_SetItem(dct, PyStrings::gUnderlying, pyscope);
791 PyDict_SetItem(dct, PyStrings::gName, PyObject_GetAttr(pyscope, PyStrings::gName));
792 PyDict_SetItem(dct, PyStrings::gCppName, PyObject_GetAttr(pyscope, PyStrings::gCppName));
793 PyDict_SetItem(dct, PyStrings::gModule, PyObject_GetAttr(pyscope, PyStrings::gModule));
796 PyObject* exc_pyscope = PyType_Type.tp_new(&PyType_Type, args,
nullptr);
801 PyType_Type.tp_setattro(parent,
pyname, exc_pyscope);
812 PyErr_SetString(PyExc_TypeError,
"attempt to bind C++ object w/o class");
821 bool isRef = flags & CPPInstance::kIsReference;
822 bool isValue = flags & CPPInstance::kIsValue;
825 if (address && !isValue && !(flags & (CPPInstance::kNoWrapConv|CPPInstance::kNoMemReg))) {
826 PyObject* oldPyObject = MemoryRegulator::RetrievePyObject(
827 isRef ? *(
void**)address : address, pyclass);
830 if (oldPyObject && (!(flags & CPPInstance::kIsPtrPtr) ||
837 PyObject* smart_type = (flags != CPPInstance::kNoWrapConv && (((
CPPClass*)pyclass)->fFlags & CPPScope::kIsSmart)) ? pyclass :
nullptr;
842 pyclass = smart_type;
843 smart_type =
nullptr;
850 (
CPPInstance*)((PyTypeObject*)pyclass)->tp_new((PyTypeObject*)pyclass, args,
nullptr);
856 (isRef ? CPPInstance::kIsReference : 0) | (isValue ? CPPInstance::kIsValue : 0) | (flags & CPPInstance::kIsOwner);
863 if (address && !isRef && !(flags & (CPPInstance::kNoWrapConv|CPPInstance::kNoMemReg)))
864 MemoryRegulator::RegisterPyObject(pyobj, pyobj->
GetObject());
868 if (((
CPPClass*)pyclass)->fFlags & CPPScope::kIsException) {
890 PyErr_SetString(PyExc_TypeError,
"attempt to bind C++ object w/o class");
894 bool isRef = flags & CPPInstance::kIsReference;
900 if (clActual && klass != clActual) {
904 clActual, klass, address, -1 ,
true );
906 address = (
void*)((intptr_t)address + offset);
921 return TupleOfInstances_New(address, klass, dims[0], dims+1);
#define CPyCppyy_PyText_InternFromString
#define CPyCppyy_PyText_AsString
#define CPyCppyy_PyText_AppendAndDel
#define CPyCppyy_PyText_FromString
Cppyy::TCppType_t fUnderlyingType
static PyClassMap_t gPyClasses
std::map< Cppyy::TCppScope_t, PyObject * > PyClassMap_t
void Set(void *address, EFlags flags=kDefault)
void SetSmart(PyObject *smart_type)
const std::string & GetName() const
void AdoptTemplate(PyCallable *pc)
void AdoptMethod(PyCallable *pc)
void MergeOverload(CPPOverload *mp)
R__EXTERN PyObject * gModule
std::string MapOperatorName(const std::string &name, bool bTakesParames)
const std::string Compound(const std::string &name)
Set of helper functions that are invoked from the pythonizors, on the Python side.
CPPOverload * CPPOverload_New(const std::string &name, std::vector< PyCallable * > &methods)
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)
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)
bool CPPScope_Check(T *object)
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)
CPPDataMember * CPPDataMember_New(Cppyy::TCppScope_t scope, Cppyy::TCppIndex_t idata)
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)
R__EXTERN PyObject * gThisModule
PyObject * BindCppObject(Cppyy::TCppObject_t object, Cppyy::TCppType_t klass, const unsigned flags=0)
bool TemplateProxy_Check(T *object)
TemplateProxy * TemplateProxy_New(const std::string &cppname, const std::string &pyname, PyObject *pyclass)
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)
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)
RPY_EXPORTED std::string GetTemplatedMethodName(TCppScope_t scope, TCppIndex_t imeth)
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)
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)
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 TCppScope_t gGlobalScope
RPY_EXPORTED TCppIndex_t GetNumMethods(TCppScope_t scope)
RPY_EXPORTED bool IsAbstract(TCppType_t type)
PyObject_HEAD PyObject * dict