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;
211 bool storeOnTemplate =
213 if (storeOnTemplate) {
223 else if (isNamespace)
225 else if (isConstructor) {
227 hasConstructor =
true;
235 if (storeOnTemplate) {
238 PyObject* attr = PyObject_GetAttrString(pyclass,
const_cast<char*
>(mtName.c_str()));
239 if (isTemplate) ((
TemplateProxy*)attr)->AdoptTemplate(pycall);
248 PyObject_SetAttrString(pyclass,
const_cast<char*
>(
"__setitem__"), (
PyObject*)pysi);
257 Callables_t& md = (*(cache.insert(
258 std::make_pair(mtName, Callables_t())).first)).second;
259 md.push_back(pycall);
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));
284 if (!isConstructor) {
286 if (mtName0 != mtName1)
292 if (!hasConstructor) {
296 else if (isNamespace)
303 cache[
"__init__"].push_back(defctor);
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));
315 for (CallableCache_t::iterator imd = cache.begin(); imd != cache.end(); ++imd) {
324 for (
auto cit : imd->second)
327 if (!attr) PyErr_Clear();
331 PyType_Type.tp_setattro(pyclass, pymname, (
PyObject*)method);
355 PyObject* eset = PyObject_GetAttrString(pyclass,
377 Py_TYPE(pyclass)->tp_getattro = oldgetattro;
391 std::deque<Cppyy::TCppType_t> bids;
392 for (
size_t ibase = 0; ibase < nbases; ++ibase) {
397 for (
size_t ibase2 = 0; ibase2 < uqb.size(); ++ibase2) {
398 if (uqb[ibase2] ==
name) {
411 uqb.push_front(
name);
413 }
else if (decision == 2) {
424 std::deque<std::string> uqb;
428 size_t nbases = uqb.size();
430 PyObject* pybases = PyTuple_New(nbases ? nbases : 1);
439 for (std::deque<std::string>::size_type ibase = 0; ibase < nbases; ++ibase) {
446 PyTuple_SET_ITEM(pybases, ibase, pyclass);
452 PyObject* newpybases = PyTuple_New(nbases+1);
455 for (
int ibase = 0; ibase < (
int)nbases; ++ibase) {
456 PyObject* pyclass = PyTuple_GET_ITEM(pybases, ibase);
458 PyTuple_SET_ITEM(newpybases, ibase+1, pyclass);
461 pybases = newpybases;
474 PyClassMap_t::iterator pci =
gPyClasses.find(scope);
476 PyObject* pyclass = PyWeakref_GetObject(pci->second);
477 if (pyclass != Py_None) {
502 if (PyErr_Occurred())
514 std::string scName =
"";
521 PyErr_Format(PyExc_SystemError,
"given scope has no name for %s",
name.c_str());
528 if (PyErr_Occurred())
537 const std::string& lookup = scName.empty() ?
name : (scName+
"::"+
name);
543 PyObject* pytemplate = PyObject_CallFunction(
544 pytcl,
const_cast<char*
>(
"s"),
const_cast<char*
>(lookup.c_str()));
565 if (tc && PyCallable_Check(tc)) {
566 PyObject* nt = PyObject_CallFunction(tc, (
char*)
"ss",
name.c_str(), scName.c_str());
579 PyErr_Format(PyExc_TypeError,
"\'%s\' is not a known C++ class", lookup.c_str());
597 if (actual != lookup) {
599 if (!pyscope) PyErr_Clear();
603 std::string::size_type last = 0;
609 for (std::string::size_type pos = 0; pos <
name.size(); ++pos) {
610 std::string::value_type
c =
name[pos];
619 else if (tpl_open == 0 && \
620 c ==
':' && pos+1 <
name.size() &&
name[ pos+1 ] ==
':') {
622 const std::string& part =
name.substr(last, pos-last);
624 PyObject* next = PyObject_GetAttrString(
625 parent ? parent :
gThisModule,
const_cast<char*
>(part.c_str()));
647 std::string unscoped =
name.substr(last, std::string::npos);
648 PyObject* ret = PyObject_GetAttrString(parent, unscoped.c_str());
681 if (pyscope && !(((
CPPScope*)pyscope)->
fFlags & CPPScope::kIsInComplete)) {
682 gPyClasses[klass] = PyWeakref_NewRef(pyscope,
nullptr);
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);
704 if (pyscope && !(((
CPPScope*)pyscope)->
fFlags & CPPScope::kIsInComplete))
722 std::deque<std::string> uqb;
724 size_t nbases = uqb.size();
744 for (std::deque<std::string>::size_type ibase = 0; ibase < nbases; ++ibase) {
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);
764 Py_XDECREF(best_base);
766 if (finalname !=
"std::exception")
774 PyTuple_SET_ITEM(pybases, 0, best_base);
781 PyObject* dct = PyTuple_GET_ITEM(args, 2);
788 PyObject* exc_pyscope = PyType_Type.tp_new(&PyType_Type, args,
nullptr);
793 PyType_Type.tp_setattro(parent,
pyname, exc_pyscope);
804 PyErr_SetString(PyExc_TypeError,
"attempt to bind C++ object w/o class");
814 bool isValue = flags & CPPInstance::kIsValue;
817 if (address && !isValue && !(flags & (CPPInstance::kNoWrapConv|CPPInstance::kNoMemReg))) {
818 PyObject* oldPyObject = MemoryRegulator::RetrievePyObject(
819 isRef ? *(
void**)address : address, pyclass);
822 if (oldPyObject && (!(flags & CPPInstance::kIsPtrPtr) ||
829 PyObject* smart_type = (flags != CPPInstance::kNoWrapConv && (((
CPPClass*)pyclass)->fFlags & CPPScope::kIsSmart)) ? pyclass :
nullptr;
834 pyclass = smart_type;
835 smart_type =
nullptr;
842 (
CPPInstance*)((PyTypeObject*)pyclass)->tp_new((PyTypeObject*)pyclass, args,
nullptr);
855 if (address && !isRef && !(flags & (CPPInstance::kNoWrapConv|CPPInstance::kNoMemReg)))
856 MemoryRegulator::RegisterPyObject(pyobj, pyobj->
GetObject());
882 PyErr_SetString(PyExc_TypeError,
"attempt to bind C++ object w/o class");
892 if (clActual && klass != clActual) {
896 clActual, klass, address, -1 ,
true );
898 address = (
void*)((intptr_t)address + offset);
#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)
std::string extract_namespace(const std::string &name)
std::string MapOperatorName(const std::string &name, bool bTakesParames)
const std::string Compound(const std::string &name)
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)
PyObject * TupleOfInstances_New(Cppyy::TCppObject_t address, Cppyy::TCppType_t klass, dim_t ndims, dims_t dims)
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)
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 TCppIndex_t GetNumMethods(TCppScope_t scope)
RPY_EXPORTED bool IsAbstract(TCppType_t type)